C++ 将蒙版应用于 OpenCV 中的图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7479265/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:01:30  来源:igfitidea点击:

Apply Mask to image in OpenCV?

c++imageimage-processingopencvmask

提问by fdh

I have a binary mask that I want to be permanently applied to a colour image how would I do this? The binary mask should be preferably permanent- as in I don't want to reapply the mask to the image every time I call a function.

我有一个想要永久应用于彩色图像的二进制蒙版,我该怎么做?二进制掩码最好是永久性的 - 因为我不想每次调用函数时都将掩码重新应用于图像。

Basic Code Examples would be appreciated. If you use code, please explain the code instead of just posting it.

基本代码示例将不胜感激。如果您使用代码,请解释代码而不是仅仅发布它。

Thank You

谢谢你

采纳答案by SSteve

You don't apply a binary mask to an image. You (optionally) use a binary mask in a processing function call to tell the function which pixels of the image you want to process. If I'm completely misinterpreting your question, you should add more detail to clarify.

您不会对图像应用二进制掩码。您(可选)在处理函数调用中使用二进制掩码来告诉函数您要处理图像的哪些像素。如果我完全误解了你的问题,你应该添加更多细节来澄清。

回答by Chaos_99

While @perrejba s answer is correct, it uses the legacy C-style functions. As the question is tagged C++, you may want to use a method instead:

虽然@perrejba 的答案是正确的,但它使用了遗留的 C 风格函数。由于问题被标记为 C++,因此您可能希望改用一种方法:

inputMat.copyTo(outputMat, maskMat);

inputMat.copyTo(outputMat, maskMat);

All objects are of type cv::Mat.

所有对象都是 类型cv::Mat

Please be aware that the masking is binary. Any non-zero value in the mask is interpreted as 'do copy'. Even if the mask is a greyscale image.

请注意,屏蔽是二进制的。掩码中的任何非零值都被解释为“执行复制”。即使蒙版是灰度图像。

Also be aware that the .copyTo() function does not clear the output before copying.

还要注意 .copyTo() 函数在复制之前不会清除输出。

If you want to permanently alter the original Image, you have to do an additional copy/clone/assignment. The copyTo() function is not defined for overlapping input/output images. So you can't use the same image as both input and output.

如果要永久更改原始图像,则必须进行额外的复制/克隆/分配。没有为重叠输入/输出图像定义 copyTo() 函数。所以你不能使用相同的图像作为输入和输出。

回答by volpato

You can use the mask to copy only the region of interest of an original image to a destination one:

您可以使用掩码仅将原始图像的感兴趣区域复制到目标区域:

cvCopy(origImage,destImage,mask);

where maskshould be an 8-bit single channel array.

其中mask应该是一个 8 位单通道阵列。

See more at the OpenCV docs

OpenCV 文档中查看更多信息

回答by rudyryk

Well, this question appears on top of search results, so I believe we need code example here. Here's the Python code:

嗯,这个问题出现在搜索结果的顶部,所以我相信我们在这里需要代码示例。这是 Python 代码:

import cv2

def apply_mask(frame, mask):
    """Apply binary mask to frame, return masked image.
    """
    return cv2.bitwise_and(frame, frame, mask=mask)

Mask and frame must be the same size, so pixels remain as-is where mask is 1and are set to zero where mask pixel is 0.

掩码和帧的大小必须相同,因此像素在掩码所在的位置保持原样,1在掩码像素为 的位置设置为零0

And for C++it's a little bit different:

因为C++它有点不同:

cv::Mat inFrame; // Original (non-empty) image
cv::Mat mask; // Original (non-empty) mask

// ...

cv::Mat outFrame;  // Result output
inFrame.copyTo(outFrame, mask);

回答by Ahmed

Here is some code to apply binary mask on a video frame sequence acquired from a webcam. comment and uncomment the "bitwise_not(Mon_mask,Mon_mask);"line and see the effect.

下面是一些代码,用于在从网络摄像头获取的视频帧序列上应用二进制掩码。注释并取消注释“bitwise_not(Mon_mask,Mon_mask);”行并查看效果。

bests, Ahmed.

最好的,艾哈迈德。

#include "cv.h"      // include it to used Main OpenCV functions.
#include "highgui.h" //include it to use GUI functions.

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    int c;

int radius=100;
      CvPoint2D32f center;
    //IplImage* color_img;
      Mat image, image0,image1; 
        IplImage *tmp;
    CvCapture* cv_cap = cvCaptureFromCAM(0);

    while(1)  {
        tmp = cvQueryFrame(cv_cap); // get frame
          // IplImage to Mat
            Mat imgMat(tmp);
            image =tmp; 



    center.x = tmp->width/2;
    center.y = tmp->height/2;

         Mat Mon_mask(image.size(), CV_8UC1, Scalar(0,0,0));


        circle(Mon_mask, center, radius, Scalar(255,255,255), -1, 8, 0 ); //-1 means filled

        bitwise_not(Mon_mask,Mon_mask);// commenté ou pas = RP ou DMLA 





        if(tmp != 0)

           imshow("Glaucom", image); // show frame

     c = cvWaitKey(10); // wait 10 ms or for key stroke
    if(c == 27)
        break; // if ESC, break and quit
    }
    /* clean up */
    cvReleaseCapture( &cv_cap );
    cvDestroyWindow("Glaucom");

}