Masking Images

Learn to mask images in OpenCV.

Masking is another essential tool used to edit images in OpenCV. Masking is an image editing technique used to highlight a specific part of an image. It’s used to combine different images, as well as highlight the required portion in the combined images.

We can use bitwise operators to mask images. This lesson will explain how the process is carried out.

To start off, we create a blank image using the Mat::zeros() function of the OpenCV library. We’ll create the mask in a blank image and later use it to mask an image.

cv::Mat circleA = cv::Mat::zeros(cv::Size(600,600), CV_8UC3);

Note: While masking, the size of the mask image should be the same as the image being masked. We need to read an image as img before creating the blank image.

Create the mask

First, we’ll create a crescent-shaped mask for our image. We create two different blank images, one with a circle and one with a rectangle. Next, we use the AND bitwise operator to combine them.

Get hands-on with 1400+ tech skills courses.