【问题标题】:Select part of a cv::Mat based on non-zero pixels of another Mat?根据另一个 Mat 的非零像素选择 cv::Mat 的一部分?
【发布时间】:2020-01-27 16:04:31
【问题描述】:

我正在尝试根据另一个 Mat 更新 Mat 的一部分。例如,我想选择img 中mask 中不为零的部分,并为其添加一个常量值。当我尝试这个时:

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)

img 和 mask 大小相同。

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

【问题讨论】:

  • 在imread中,如果你想在openCV中使用灰度图像作为1通道,你必须使用标志cv.:IMREAD_GRAYSCALE或cv::IMREAD_UNCHANGED。
  • 好的,我已将其添加到 imread 中。

标签: c++ opencv mat


【解决方案1】:

许多 OpenCV 函数默认支持掩码,换句话说,您不需要找到非零值,并且基于此进行求和运算,您只需要使用默认支持使用掩码的 cv::add 函数作为论据,

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

关于你的代码

img(locations) += 5;

据我所知,我们在 OpenCV 中没有类似的重载 operator+ 可供使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-08
    • 2012-05-15
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    相关资源
    最近更新 更多