C++ 如何在 OpenCV 中将图像蒙版部分中的所有内容“归零”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11416032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to "zero" everything within a masked part of an image in OpenCV
提问by Hymanson Dean Goodwin
If I have an image (IplImage 8-bit) and a binary mask (which is also an 8-bit IplImage of the same size, where every pixel has a value of either 0 or 255), how can I make every pixel in the image that corresponds with a pixel in the mask with a value of zero have a value of zero, and every pixel in the image that corresponds with a pixel in the mask with any other value (namely 255) have the same value as in the original image?
如果我有一个图像(IplImage 8 位)和一个二进制掩码(它也是一个相同大小的 8 位 IplImage,其中每个像素的值都是 0 或 255),我怎样才能使每个像素在与掩码中值为零的像素对应的图像的值为零,并且与掩码中具有任何其他值(即255)的像素对应的图像中的每个像素都具有与原始值相同的值图片?
In other words, anything that is "in the mask area" will keep its original value, and anything outside the mask area will become zero.
换句话说,任何“在遮罩区域内”的东西都将保持其原始值,而在遮罩区域之外的任何东西都将为零。
采纳答案by Abid Rahman K
回答by Antonio Sesto
Simplest way, with 'Mat img' (image to be masked, input) and 'Mat masked' (masked image, output):
最简单的方法,使用“Mat img”(要屏蔽的图像,输入)和“Mat masked”(屏蔽图像,输出):
img.copyTo(masked, mask)
where 'Mat mask' is a matrix not necessarily binary (copyTo considers elements with zero value). Masked can be of any size and type; it is reallocated if needed.
其中“Mat mask”是一个不一定是二进制的矩阵(copyTo 考虑具有零值的元素)。蒙面可以是任何大小和类型;如果需要,它会重新分配。
See the doc.
请参阅文档。
回答by Sam
Multiply or bit-and the mask with the image. There are some OpenCV functions for that, but I do not know their names for the C interface.
与图像相乘或位和掩码。有一些 OpenCV 函数,但我不知道 C 接口的名称。
in C++:
在 C++ 中:
Mat image, mask;
image = image * mask;
// or
image = image & mask;