How do you create a binary mask of an image in python?

The other answers did not work for me. Back then, I spent so much time to find a good masking function. Here are two simple answers with numpy only.

import numpy as np

arr = np.arange(27).reshape(3,3,3) #3 channel image
mask = np.zeros(shape=(3,3))
mask[1,1] = 1 # binary mask
mask_3d = np.stack((mask,mask,mask),axis=0) #3 channel mask

## Answer 1
# Simply multiply the image array with the mask

masked_arr = arr*mask_3d

## Answer 2
# Use the where function in numpy

masked_arr = np.where(mask_3d==1,arr,mask_3d)

#Both answer gives
print(masked_arr)

array([[[ 0.,  0.,  0.],
        [ 0.,  4.,  0.],
        [ 0.,  0.,  0.]],

       [[ 0.,  0.,  0.],
        [ 0., 13.,  0.],
        [ 0.,  0.,  0.]],

       [[ 0.,  0.,  0.],
        [ 0., 22.,  0.],
        [ 0.,  0.,  0.]]])

Hello,

I'm try to create binary mask for medical images, im new to OpenCV and any advice will help on how, or if it possible, to do so with OpenCV. Below is the type of pictures I try to create mask for:

How do you create a binary mask of an image in python?

I need to color the vocal cords area in white(Marked with triangle) and the rest in black, so it will look something like that:

How do you create a binary mask of an image in python?

i tried to find the contours using that code:

import cv2
import numpy as np
img = cv2.imread("LPR.png", cv2.IMREAD_GRAYSCALE)
_, threshold = cv2.threshold(img, 50, 255, cv2. THRESH_BINARY_INV)
_, contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
for cnt in contours:
    cv2.drawContours(img, [cnt], 0, (0))
cv2.imshow("LPR", img)
cv2.imshow("Threshold", threshold)
cv2.waitKey(0)

this is the result:

How do you create a binary mask of an image in python?

i tried to clean the rest of the image using some adaptiveThreshold but the result was worse. i also try to detect the triangle shape that created by the Vocal cords, also without success.

Does anyone have any suggestions on how I can get the desired result or what method I can use?

Any advice will help, Thanks.

The original image for example:

How do you create a binary mask of an image in python?

The problem had been solved. That is all I can't go any furthers. For opencv 3.x add this `_, contours, _ = . New python code has been changed. For opencv 4.x, see below:

#!/usr/bin/python37
#Raspberry pi 3B/3B+, 4B, Buster, OpenCV 4.2.0
#Date: 5th April, 2020

import cv2 as cv
import numpy as np

img = cv.imread('organ1.jpg')
image_contours = np.zeros((img.shape[1],
                           img.shape[0], 1),
                          np.uint8)

image_binary = np.zeros((img.shape[1],
                         img.shape[0], 1),
                        np.uint8)

for channel in range(img.shape[2]):
    ret, image_thresh = cv.threshold(img[:, :, channel],
                                     38, 255,
                                     cv.THRESH_BINARY)

    contours = cv.findContours(image_thresh, 1, 1)[0]   
    cv.drawContours(image_contours,
                    contours, -1,
                    (255,255,255), 3)

contours = cv.findContours(image_contours, cv.RETR_LIST,
                           cv.CHAIN_APPROX_SIMPLE)[0]

cv.drawContours(image_binary, [max(contours, key = cv.contourArea)],
                -1, (255, 255, 255), -1)

cv.imwrite('LPR.jpg', image_binary)
cv.imshow('LPR', image_binary)
cv.waitKey(0) & 0xFF is 27
cv.destroyAllWindows()

Output:

How do you create a binary mask of an image in python?

In this 5th part of the image processing series, we discuss more on the Arithmetic and bitwise operations, and masking of images in Python.

It is recommended that the previous articles be run through, before starting off on your masked learning adventure here.

Setting up the environment

The following lines of code are used in all of the applications given below. We’ll include those here instead so you don’t have to read through a huge block of code.

Helps reduce clutter :)

# importing numpy to work with pixels
import numpy as np

# importing argument parsers
import argparse

# importing the OpenCV module
import cv2


# initializing an argument parser object
ap = argparse.ArgumentParser()

# adding the argument, providing the user an option
# to input the path of the image
ap.add_argument("-i", "--image", required=True, help="Path to the image")

# parsing the argument
args = vars(ap.parse_args())

# reading the image location through args
# and reading the image using cv2.imread
image = cv2.imread(args["image"])

Arithmetic Operations on Images using Python

Arithmetic Operations allow us to enhance a lot of aspects of an image.

We can work with lighting, shadows, the red, blue, and green color enhancement.

A lot of image filters on applications use the same method to alter and beautify photographs as well.

So, let’s get started with all of the code!

First, in order to understand whether the limit can go over 255 or 0, we can conduct a simple test, which provides us with 255 and 0.

# printing out details of image min, max and the wrap around
print("max of 255 :", str(cv2.add(np.uint8([200]), np.uint8([100]))))
print("min of 0 :", str(cv2.subtract(np.uint8([50]), np.uint8([100]))))

print("wrap around :", str(np.uint8([200]) + np.uint8([100])))
print("wrap around :", str(np.uint8([50]) - np.uint8([100])))

In this example, we are increasing the intensity of all the pixels in the image by 100.

# adding pixels of value 255 (white) to the image
M = np.ones(image.shape, dtype="uint8") * 100
added = cv2.add(image, M)
cv2.imshow("Added", added)
cv2.waitKey(0)

This is done by constructing a matrix with the same size as our images using the NumPy module, and adding it with our image.

In case we wish to darken an image, we subtract from the pixel values of the image, as shown below,

# adding pixels of value 0 (black) to the image
M = np.ones(image.shape, dtype="uint8") * 50
subtracted = cv2.subtract(image, M)
cv2.imshow("Subtracted", subtracted)
cv2.waitKey(0)

This should provide you with two different variations of the original image, one lighter, and the other darker.

Bitwise Operations

We use Bitwise operations a lot of the times while attempting to mask images.

This feature of OpenCV allows us to filter out the part of the image that is relevant to us.

Setting up

To work on Bitwise operations, we’ll first need two variables or images that we can conduct the operations on.

So, let’s create a bitwise square and a bitwise circle through which we can use the bitwise operations.

Note that bitwise operations require the images to be black and white.

# creating a square of zeros using a variable
rectangle = np.zeros((300, 300), dtype="uint8")
cv2.rectangle(rectangle, (25, 25), (275, 275), 255, -1)
cv2.imshow("Rectangle : ", rectangle)

# creating a circle of zeros using a variable
circle = np.zeros((300, 300), dtype="uint8")
cv2.circle(circle, (150, 150), 150, 255, -1)
cv2.imshow("Circle : ", circle)

The output images that you receive should look like this,

How do you create a binary mask of an image in python?
Bit Square

Combine with the AND operation

Bitwise addition refers to the addition of two different images, and decide which is to be displayed using an AND operation on each pixel of the images.

# the bitwise_and function executes the AND operation
# on both the images
bitwiseAnd = cv2.bitwise_and(rectangle, circle)
cv2.imshow("AND", bitwiseAnd)
cv2.waitKey(0)

Bitwise addition of both the circle and the square gives us an output which should look like this,

How do you create a binary mask of an image in python?
AND Bit Square

Given a choice with the OR operation

Bitwise OR provides us with a product of the two images with an OR operation performed on each pixel of the images.

# the bitwise_or function executes the OR operation
# on both the images
bitwiseOr = cv2.bitwise_or(rectangle, circle)
cv2.imshow("OR", bitwiseOr)
cv2.waitKey(0)

Upon performing the operation Bitwise OR, you should receive something like this,

How do you create a binary mask of an image in python?
OR Bit Square

Exclusivity with the XOR operation

Another operation that is provided by the cv2 module is the XOR operation, which we can use through the bitwise_xor function.

# the bitwise_xor function executes the XOR operation
# on both the images
bitwiseXor = cv2.bitwise_xor(rectangle, circle)
cv2.imshow("XOR", bitwiseXor)
cv2.waitKey(0)
How do you create a binary mask of an image in python?
XOR Bit Square

Negation using the NOT operation

Lastly, we have the negation operation, which is performed using the bitwise_not function.

The NOT operation only requires a single image as we’re not adding or subtracting anything here.

We still use it on both here however, that’s also an option.

# the bitwise_not function executes the NOT operation
# on both the images
bitwiseNot = cv2.bitwise_not(rectangle, circle)
cv2.imshow("NOT", bitwiseNot)
cv2.waitKey(0)

The circle is inside the square in this case, and as such is not visible,

How do you create a binary mask of an image in python?
Not Bit Square

Masking of images using Python OpenCV

Masking is used in Image Processing to output the Region of Interest, or simply the part of the image that we are interested in.

We tend to use bitwise operations for masking as it allows us to discard the parts of the image that we do not need.

So, let’s get started with masking!

The process of masking images

We have three steps in masking.

  1. Creating a black canvas with the same dimensions as the image, and naming it as mask.
  2. Changing the values of the mask by drawing any figure in the image and providing it with a white color.
  3. Performing the bitwise ADD operation on the image with the mask.

Following the same process, let’s create a few masks and use them on our image.

First, let’s work with a rectangle mask.

# creating a mask of that has the same dimensions of the image
# where each pixel is valued at 0
mask = np.zeros(image.shape[:2], dtype="uint8")

# creating a rectangle on the mask
# where the pixels are valued at 255
cv2.rectangle(mask, (0, 90), (290, 450), 255, -1)
cv2.imshow("Mask", mask)

# performing a bitwise_and with the image and the mask
masked = cv2.bitwise_and(image, image, mask=mask)
cv2.imshow("Mask applied to Image", masked)
cv2.waitKey(0)

Now, let’s try it out with a circle mask.

# creating a mask of that has the same dimensions of the image
# where each pixel is valued at 0
mask = np.zeros(image.shape[:2], dtype="uint8")

# creating a rectangle on the mask
# where the pixels are valued at 255
cv2.circle(mask, (145, 200), 100, 255, -1)
cv2.imshow("Mask", mask)

# performing a bitwise_and with the image and the mask
masked = cv2.bitwise_and(image, image, mask=mask)
cv2.imshow("Mask applied to Image", masked)
cv2.waitKey(0)

If everything works out just fine, we should receive outputs which look something like this,

How do you create a binary mask of an image in python?
Rectangular Mask

Conclusion

We’re finally getting started with the core of Image Processing, and understanding bitwise operations and masking in it is important.

It helps us to block out parts or only take in parts of the image that we are interested in, so, quite a useful concept.

We’re proceeding at a decent pace, but, in case you wish to time skip and get to the end, be my guest!

Here’s articles which let you look into OpenCV and Facial Recognition, and a Java implementation of Android and CameraX OpenCV.

References

  • Official OpenCV Website
  • Introduction to starting out with OpenCV
  • My GitHub Repository for Image Processing
  • Arithmetic Operations Code
  • Bitwise Operations Code
  • Masking Code

How do you make a binary mask in Python?

import numpy as np arr = np. arange(27). reshape(3,3,3) #3 channel image mask = np. zeros(shape=(3,3)) mask[1,1] = 1 # binary mask mask_3d = np.

How do you make an image mask in Python?

Masking of images using Python OpenCV.
Creating a black canvas with the same dimensions as the image, and naming it as mask ..
Changing the values of the mask by drawing any figure in the image and providing it with a white color..
Performing the bitwise ADD operation on the image with the mask..

How do you make a binary mask?

You can create this binary mask by specifying the vertices of the polygon using the using the roipoly function, or by specifying the vertices and the target size of the mask using the poly2mask function. poly2mask does not require an input image.

How do I crop an image using binary mask?

use findContours or extract all mask points (manually) and use the minBoundingRect function. Afterwards use subimage to get the cropped image.