如何在CV_8UC1中使用蒙版absdiff()灰度图像 [英] How to absdiff() a grayscale image with a mask in CV_8UC1
问题描述
invert_mask
是CV_8UC1,具有填充的轮廓(像素值255),src
是灰度
invert_mask
is CV_8UC1 with some filled contours (pixel value 255) and src
is grayscale
我想反转src
除了遮罩下的区域外,输出图像几乎看起来正确.它们只是黑色的.看起来只有白色变成黑色,但黑色保持黑色
The ouput image almost looks right except for the areas under the mask.. They are just black. It looks like only the white color is turned to black but the black color stays black
我不想对src
图片进行二值化,因为在此之后我必须对其进行一些处理
I don't want to binarize the src
image because I have to do some processing on it after this
cv::absdiff(invert_mask, 255 - src, src);
src = 255 - src;
如果执行此操作,白色会转换为黑色,但黑色/灰色不会被更改
If I do this the white color is converted to black but the black/gray color is untouched
cv::absdiff(invert_mask, src, src);
src
推荐答案
要实现所需的颜色替换,使用简单阈值处理的以下简短代码应该可以解决问题:
To achieve the desired color replacement, the following short snippet using simple thresholding should do the trick:
cv::Mat src = cv::imread("src.png", cv::IMREAD_GRAYSCALE);
cv::Mat invert_mask = cv::imread("invert_mask.png", cv::IMREAD_GRAYSCALE);
cv::Mat textInMask = src.clone().mul(invert_mask / 255);
cv::threshold(textInMask, textInMask, 200, 255, cv::THRESH_BINARY);
cv::Mat output = src.clone() + invert_mask - textInMask;
输出图像:
质量可能会提高,这只是基本概念.
The quality may be improved, this is just the basic concept.
这篇关于如何在CV_8UC1中使用蒙版absdiff()灰度图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!