Title: Image Masking with OpenCV - PyImageSearch

URL Source: https://pyimagesearch.com/2021/01/19/image-masking-with-opencv/

Published Time: 2021-01-20T04:36:00+00:00

Markdown Content:
In this tutorial, you will learn how to mask images using OpenCV.

![Image 1](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20700%20450'%3E%3C/svg%3E)

My previous guide discussed [bitwise operations](https://pyimagesearch.com/2021/01/19/opencv-bitwise-and-or-xor-and-not/), a very common set of techniques used heavily in image processing.

And as I hinted previously, we can use both bitwise operations and masks to construct ROIs that are _non-rectangular_. This allows us to extract regions from images that are of completely _arbitrary shape._

**Put simply; a mask allows us to focus only on the portions of the image that interests us.**

For example, let’s say that we were building a computer vision system to recognize faces. The only part of the image we are interested in finding and describing is the parts of the image that contain faces — we simply don’t care about the rest of the image’s content. **Provided that we could find the faces in the image, we may construct a mask to show only the faces in the image.**

Another image masking application you’ll encounter is alpha blending and transparency (e.g., in this guide on [_Creating GIFs with OpenCV_](https://pyimagesearch.com/2018/11/05/creating-gifs-with-opencv/)). When applying transparency to images with [OpenCV](https://opencv.org/), we need to tell OpenCV what parts of the image transparency should be applied to versus not — _masks allow us to make that distinction._

**To learn how to perform image masking with OpenCV, _just keep reading._**

![Image 2](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20128%20128'%3E%3C/svg%3E)

#### Looking for the source code to this post?

[Jump Right To The Downloads Section](#download-the-code)

In the first part of this tutorial, we’ll configure our development environment and review our project structure.

We’ll then implement a Python script to mask images with OpenCV.

### **Configuring your development environment**

To follow this guide, you need to have the OpenCV library installed on your system.

Luckily, OpenCV is pip-installable:

$ pip install opencv-contrib-python

**If you need help configuring your development environment for OpenCV, I _highly recommend_ that you read my** [**_pip install OpenCV_ guide**](https://pyimagesearch.com/2018/09/19/pip-install-opencv/) — it will have you up and running in a matter of minutes.

### **Having problems configuring your development environment?**

[![Image 3](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20500%20500'%3E%3C/svg%3E)](https://pyimagesearch.com/pyimagesearch-plus/)

**Figure 1:** Having trouble configuring your development environment? Want access to pre-configured Jupyter Notebooks running on Google Colab? Be sure to join [PyImageSearch Plus](https://pyimagesearch.com/pyimagesearch-plus/) — you will be up and running with this tutorial in a matter of minutes.

All that said, are you:

*   Short on time?
*   Learning on your employer’s administratively locked system?
*   Wanting to skip the hassle of fighting with the command line, package managers, and virtual environments?
*   **Ready to run the code _right now_ on your Windows, macOS, or Linux systems?**

Then join [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/) today!

**Gain access to Jupyter Notebooks for this tutorial and other PyImageSearch guides that are _pre-configured_ to run on Google Colab’s ecosystem right in your web browser!** No installation required.

And best of all, these Jupyter Notebooks will run on Windows, macOS, and Linux!

### Project structure

Performing image masking with OpenCV is easier than you think. But before we write any code, let’s first review our project directory structure.

Start by using the **_“Downloads”_** section of this guide to access the source code and example image.

Your project folder should look like the following:

$ tree . --dirsfirst
.
├── adrian.png
└── opencv\_masking.py

0 directories, 2 files

Our `opencv_masking.py` script will load the input `adrian.png` image from disk. We’ll then use masking to extract both the body and face from the image using rectangular and circular masks, respectively.

### **Implementing image masking with OpenCV**

Let’s learn how to apply image masking using OpenCV!

Open the `opencv_masking.py` file in your project directory structure, and let’s get to work:

\# import the necessary packages
import numpy as np
import argparse
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add\_argument("-i", "--image", type=str, default="adrian.png",
	help="path to the input image")
args = vars(ap.parse\_args())

**Lines 2-4** import our required Python packages. We then parse our command line arguments on **Lines 7-10**.

We only need a single switch here, `--image`, which is the path to the image we want to mask. We go ahead and default the `--image` argument to the `adrian.png` file in our project directory.

Let’s now load this image from disk and perform masking:

\# load the original input image and display it to our screen
image = cv2.imread(args\["image"\])
cv2.imshow("Original", image)

# a mask is the same size as our image, but has only two pixel
# values, 0 and 255 -- pixels with a value of 0 (background) are
# ignored in the original image while mask pixels with a value of
# 255 (foreground) are allowed to be kept
mask = np.zeros(image.shape\[:2\], dtype="uint8")
cv2.rectangle(mask, (0, 90), (290, 450), 255, -1)
cv2.imshow("Rectangular Mask", mask)

# apply our mask -- notice how only the person in the image is
# cropped out
masked = cv2.bitwise\_and(image, image, mask=mask)
cv2.imshow("Mask Applied to Image", masked)
cv2.waitKey(0)

**Lines 13 and 14** load the original `image` from disk and display it to our screen:

![Image 4](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20615%20500'%3E%3C/svg%3E)

**Figure 2:** Loading our input image from disk.

We then construct a NumPy array, filled with zeros, with the same width and height as our original image on **Line 20**.

As I mentioned in our previous tutorial on _[Image cropping with OpenCV](https://pyimagesearch.com/2021/01/19/crop-image-with-opencv/)_, we can use [object detection methods](https://pyimagesearch.com/category/object-detection/) to detect objects/people in images automatically. Still, we’ll be using our _a priori_ knowledge of our example image for the time being.

We _know_ that the region we want to extract is in the image’s _bottom-left_ corner. **Line 21** draws a white rectangle on our mask, which corresponds to the region we want to extract from our original image.

Remember reviewing the `cv2.bitwise_and` function in our [bitwise operations tutorial](https://pyimagesearch.com/2021/01/19/opencv-bitwise-and-or-xor-and-not/)? It turns out that this function is used extensively when applying masks to images.

We apply our mask on **Line 26** using the `cv2.bitwise_and` function.

The first two parameters are the `image` itself (i.e., the image where we want to apply the bitwise operation).

However, the important part of this function is the `mask` keyword. When supplied, the `bitwise_and` function is `True` when the pixel values of the input images are equal, and the mask is non-zero at each _(x, y)_\-coordinate (in this case, only pixels that are part of the white rectangle).

After applying our mask, we display the output on **Lines 27 and 28**, which you can see in **Figure 3**:

![Image 5](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20700%20284'%3E%3C/svg%3E)

**Figure 3:** _Left:_ Constructing a rectangular mask. _Right:_ Applying the rectangular mask to the image with OpenCV.

Using our rectangular mask, we could extract only the region of the image that contains the person and ignore the rest.

Let’s look at another example, but this time using a _non-rectangular_ mask:

\# now, let's make a circular mask with a radius of 100 pixels and
# apply the mask again
mask = np.zeros(image.shape\[:2\], dtype="uint8")
cv2.circle(mask, (145, 200), 100, 255, -1)
masked = cv2.bitwise\_and(image, image, mask=mask)

# show the output images
cv2.imshow("Circular Mask", mask)
cv2.imshow("Mask Applied to Image", masked)
cv2.waitKey(0)

On **Line 32,** we re-initialize our `mask` to be filled with zeros and the same dimensions as our original image.

Then, we draw a white circle on our mask image, starting at the center of my face with a radius of `100` pixels.

Applying the circular mask is then performed on **Line 34**, again using the `cv2.bitwise_and` function.

The results of our circular mask can be seen in **Figure 4**:

![Image 6](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20700%20284'%3E%3C/svg%3E)

**Figure 4:** _Left:_ Creating a circular mask. _Right:_ Extracting the face from the input image using a circular mask instead of a rectangular one.

Here, we can see that our circle mask is shown on the _left_ and the application of the mask on the _right_. Unlike the output from **Figure 3**, when we extracted a rectangular region, this time, we have extracted a circular region that corresponds to only my face in the image.

Furthermore, we can use this approach to extract regions from an image of arbitrary shape (rectangles, circles, lines, polygons, etc.).

### **OpenCV image masking results**

To perform image masking with OpenCV, be sure to access the **_“Downloads”_** section of this tutorial to retrieve the source code and example image.

From there, open a shell and execute the following command:

$ python opencv\_masking.py

Your masking output should match mine from the previous section.

OpenCV Image Masking is a powerful for manipulating images. It allows you to apply effects to a single image and create an entirely new look.

With OpenCV Image Masking, you can selectively modify colors, contrast, lighten or darken, add or remove noise, and even erase parts or objects from an image. It is also possible to add text, and special effects, and even convert images to a different file format.

OpenCV Image Masking is a great way to easily create stunning visuals and might help you with:

*   graphic design
*   application development
*   robotics
*   autonomy
*   computer vision research topics

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

**Summary**
-----------

You came to [learn Computer Vision](https://pyimagesearch.com/) and basic masking, super! In this tutorial, you learned the basics of masking using OpenCV.

The key point of masks is that they allow us to focus our computation only on regions of the image that interest us. Focusing our computations on regions that interest us dramatically impacts when we explore topics such as machine learning, image classification, and object detection.

For example, let’s assume that we wanted to build a system to classify the species of the flower.

In reality, we are probably only interested in the flower petals’ color and texture to perform the classification. But since we are capturing the photo in a natural environment, we’ll also have many other regions in our image, including dirt from the ground, insects, and other flowers crowding the view. How will we quantify and classify just the flower we are interested in? **As we’ll see, the answer is masks.**

Updated: December 30th, 2022 with updated links and content.

**To download the source code to this post (and be notified when future tutorials are published here on PyImageSearch), _simply enter your email address in the form below!_**

![Image 7](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Download the Source Code and FREE 17-page Resource Guide

Enter your email address below to get a .zip of the code and a **FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning.** Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL!