OpenCV C++ 中与 np.ma.masked_where 等价的函数是什么?

huangapple go评论90阅读模式
英文:

What is the opencv C++ equivalent funciton for np.ma.masked_where

问题

我试图对我的张量进行阈值处理并创建掩码,如下所示:

cv::Mat prediction; // 假设prediction是一个2D的opencv Mat,范围在[0, 1]之间
cv::threshold(prediction, prediction, 0.5, 1.0, cv::THRESH_BINARY); // 阈值处理

prediction.convertTo(prediction, CV_8U, 255.0); // 转换为8位无符号整数,范围为[0, 255]

// 现在prediction是一个黑白图像,0表示黑色,255表示白色

这是在C++和OpenCV中实现类似Python代码的方式。希望对你有所帮助。

英文:

I was trying to threshold and create mask for my tensor as follow

prediction = prediction>0.5   ---  where prediciton is a 2D tensor normalized to [0,1], I am thresholiding the value as 0.5 

prediction = np.ma.masked_where(prediction == 0, prediction) -- this turns the entries of prediction into boolean values for subsequent processing 



predicted_mask = ((np.array(prediction))*255.0).astype(np.uint8) --finally here , change /generate image as a black and white 

This was in python and it worked fine , I am looking to turn this into c++ / opencv ..Any help is apprecaited

答案1

得分: 1

Masked_where是一个NumPy函数,而不是OpenCV函数。
与Python不同,循环在C++中通常会更快,你通常会编写一个函数来遍历数组并直接应用所需的条件。

然而,如果你确实想要使用掩码,这个答案可能会有帮助:https://stackoverflow.com/questions/23573311/opencv-cvmat-set-if

英文:

Masked_where is a numpy function, not an opencv function.
Unlike python where looping is relatively slow, for c++ you would often just write a function to loop through the array and apply the conditions you want directly.

However, if you do want to use masks, this answer may be useful https://stackoverflow.com/questions/23573311/opencv-cvmat-set-if

huangapple
  • 本文由 发表于 2023年1月9日 11:17:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定