c++ - OpenCV - 带面具的 absdiff

标签 c++ opencv mask mat

我正在尝试使用蒙版计算两个图像的绝对差异,因此在计算差异时只考虑图像的一个区域。但是 OpenCV 在 its function 中没有掩码部分.我看到了this question但对我不起作用。我试图将结果乘以掩码,以便只保留指定的区域。
代码:

Mat region = //a grayscale image containing a region of 255 and the rest is zero
Mat img1, img2 = //two images of the same size as the region image and of type CV_8UC1
Mat mask = region / 255; //to make a binary mask
Mat difference = Mat::zeros(region .rows, region .cols, CV_8UC1);

cv::absdiff(img1, img2, difference);
difference = difference * mask;
if (!difference.empty()) imshow("difference", difference);
当我尝试这个时,我得到一个错误。
错误:

Error: Assertion failed (a_size.width == len) in cv::gemm


这发生在这里:
inline
Mat& Mat::operator = (const MatExpr& e)
{
    e.op->assign(e, *this);
    return *this;
}

最佳答案

difference * mask表示您正在执行Matrix multiplication ,在这种情况下,如果要执行 Element wise multiplication,则差异的高度必须与掩码的宽度相同。您应该调用 difference.mul(mask)

关于c++ - OpenCV - 带面具的 absdiff,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60156131/

相关文章:

jQuery 输入掩码长度

CSS 掩码在 Angular 5 中无法正常工作

c++ - 这是一个有效的 C 语句吗?

c++ - 在模板参数推导过程中会发生什么?

c++ - 如何在 C++ 中的变量中存储任何类型的函数?

java - 如何使用 OpenCV 和 Java 从(视频)图像中删除背景?

python - 问题 : Glib-Gobject-warning **: cannot register existing type 'GDKDisplayManager'

c++ - 限制 Tesseract 的预处理

c++ - 总结然后平均每 10 行从一个 txt 文件

python - 如何使用来自 Tensorflow 对象检测 API 的 Mask-RCNN 模型创建自己的数据集?