如何根据opencv中的某些条件修改Mat的值?

sam*_*ran 5 opencv mat

在matlab中,a(a> 50)= 0可以替换大于50到0的所有元素.我想在openCV中使用Mat做同样的事情.怎么做?

小智 12

纳阿.要做到这一点,只需一行:

cv::Mat img = imread('your image path');
img.setTo(0,img>50);
Run Code Online (Sandbox Code Playgroud)

就如此容易.


Gil*_*lad 6

您想要的是使用cv :: threshold截断图像。

以下应满足您的要求:

cv::threshold(dst, dst, 50, 0, CV_THRESH_TOZERO_INV);
Run Code Online (Sandbox Code Playgroud)

这是功能定义

double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
Run Code Online (Sandbox Code Playgroud)

http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html?highlight=threshold#threshold