选择基于另一个垫子的非零像素的cv::Mat的一部分?



我正在尝试基于另一个垫子更新垫子的一部分。例如,我想选择imgmask不为零的部分,并向其添加一个常量值。当我尝试这个时:

Mat mask = imread("some grayscale image with a white area in a black background", IMREAD_GRAYSCALE);
Mat img = Mat::zeros(mask.rows, mask.cols, CV_8UC1);
Mat bnry, locations;
threshold(mask, bnry, 100, 255, THRESH_BINARY);
findNonZero(bnry, locations);
img(locations) += 5;

我收到此错误:

错误:断言失败 ((int(ranges.size(( == d(

imgmask具有相同的大小。

如何根据另一个图像(蒙版(选择图像区域?

许多 OpenCV 函数默认支持 mask,换句话说,您不需要找到非零值,并且基于该 sum 运算,您只需要使用默认支持使用 mask 作为参数cv::add函数,

cv::add(img,10,img,mask);  // 10 is an arbitrary constant value

关于你的代码

img(locations) += 5;

据我所知,我们在OpenCV中没有这样的重载operator+可以使用。

最新更新