简体   繁体   English

根据另一个 Mat 的非零像素选择 cv::Mat 的一部分?

[英]Select part of a cv::Mat based on non-zero pixels of another Mat?

I am trying to update part of a Mat based on another Mat.我正在尝试根据另一个垫子更新垫子的一部分。 For example, I want to select a part of img that is not zero in mask and add a constant value to it.例如,我想选择mask不为零的img部分并为其添加一个常量值。 When I try this:当我尝试这个时:

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;

I get this error:我收到此错误:

Error: Assertion failed ((int)ranges.size() == d)错误:断言失败 ((int)ranges.size() == d)

img and mask have the same size. imgmask具有相同的大小。

How can I select an area of an image based on another image (mask)?如何根据另一个图像(蒙版)选择图像区域?

Many of the OpenCV functions will support mask in default, in other word you don't need to find non zero values and based on that doing sum operation, you just need to use cv::add function that in default support using mask as an argument,许多 OpenCV 函数在默认情况下将支持掩码,换句话说,您不需要找到非零值,并且基于进行求和运算,您只需要使用cv::add函数,该函数在默认情况下支持使用掩码作为争论,

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

And about your code关于你的代码

img(locations) += 5;

As far as I know we don't have any like this overloaded operator+ in OpenCV to use.据我所知,我们在 OpenCV 中没有任何类似重载的operator+可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM