英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论