OpenCV exploration road (thirteen): detailed mask mask

In OpenCV we often encounter a name: Mask (mask). Many functions use it, so what exactly is this Mask? When I first came into contact with the Mask, I was really confused, and I couldn't understand what the Mask was for. After consulting a lot of information, I also have a little understanding of Mask. Let me talk about my understanding below.

For example, if I want to cut out a picture, I need to use Mask, so I will use cutout as an example to explain the role of Mask in it. Go to the program first, and then analyze it sentence by sentence. The function of this program is to cut out the specified area.

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

int main()
{
	Mat image, mask;
	Rect r1(100, 100, 250, 300);
	Mat img1, img2, img3, img4;
	image = imread("lol17.jpg");
	mask = Mat::zeros(image.size(), CV_8UC1);
	mask(r1).setTo(255);
	img1 = image(r1);
	image.copyTo(img2, mask);

	image.copyTo(img3);
	img3.setTo(0, mask);


	imshow("Image sequence", image);
	imshow("img1", img1);
	imshow("img2", img2);
	imshow("img3", img3);
	imshow("mask", mask);

	waitKey();
	return 0;
}

The original image:

Pay attention to the two sentences in the program about the operation of Mask.

mask = Mat::zeros(image.size(), CV_8UC1); 
mask(r1).setTo(255);  //r1是设置好的感兴趣区域

Explain the operation of the above two sentences.

  • The first step is to create a mask image of the same size as the original image, and initialize all pixels to 0, so the whole image becomes a completely black image.
  • In the second step, all pixel values ​​in the r1 area in the mask map are set to 255, that is, the entire r1 area becomes white.

In this way, the Mask image can be obtained.

Pay attention to this sentence, which image is copied to which image?

image.copyTo(img2, mask);

Of course, the original image image is copied to the destination image img2.
In fact, the full version of the copy action is as follows:

The original image (image) and the mask (mask) are ANDed to obtain the result image (img2).

What is the AND operation of a graph and a mask?

In fact, each pixel in the original image and each corresponding pixel in the mask are ANDed. For example 1 & 1 = 1; 1 & 0 = 0;

For example, a 3 * 3 image is calculated with a 3 * 3 mask, and the resulting image is:

To put it bluntly, the mask is a bitmap, to choose which pixels are allowed to be copied and which pixels are not allowed to be copied. If the value of the mask pixel is non-zero, I copy it, otherwise I don't copy it.

Because in the mask we got above, the region of interest is white, indicating that the pixels in the region of interest are all non-zero, and the non-interest regions are all black, indicating that the pixels in those regions are all 0. Once the original image and the mask image are ANDed, the resulting image only leaves the image of the region of interest in the original image. Also as shown below.

image.copyTo(img2, mask);

The following two lines of code do the same thing as above. First, copy the original image image to img3, and then img3 sets the white area of ​​the mask to 0 (black). For example, if the pixels in the mask are not 0, I will set my The pixel value of the point corresponding to the image is set to 0, otherwise nothing is done. The pseudocode is if mask(i,j)>0 then img3(i,j)=0.

image.copyTo(img3);
img3.setTo(0, mask);

If you want to directly extract the target area, just write it like this:

img1 = image(r1);

Guess you like

Origin blog.csdn.net/weixin_58045467/article/details/130747211