Last modified: Jan 17, 2025 By Alexander Williams

Python OpenCV cv2.bitwise_not() Guide

Python OpenCV is a powerful library for image processing. One of its useful functions is cv2.bitwise_not(). This function inverts the pixel values of an image. It is often used in image masking and thresholding.

In this guide, we will explore how to use cv2.bitwise_not(). We will also provide examples to help you understand its practical applications.

What is cv2.bitwise_not()?

The cv2.bitwise_not() function performs a bitwise NOT operation on the input image. This means it inverts the binary representation of the pixel values. For example, a white pixel (255) becomes black (0), and vice versa.

This function is useful in various image processing tasks. It can be used to create masks, invert colors, and more. It works well with other OpenCV functions like cv2.bitwise_and() and cv2.bitwise_or().

Syntax of cv2.bitwise_not()

The syntax for cv2.bitwise_not() is simple. It takes two main arguments: the source image and the destination image. The destination image is optional.


cv2.bitwise_not(src, dst=None, mask=None)

src: The input image. It can be a single-channel or multi-channel image.

dst: The output image. If not provided, the function creates a new image.

mask: An optional mask. If provided, the operation is applied only to the masked region.

Example: Inverting an Image

Let's start with a simple example. We will load an image and invert its colors using cv2.bitwise_not().


import cv2

# Load an image
image = cv2.imread('image.jpg')

# Invert the image
inverted_image = cv2.bitwise_not(image)

# Display the original and inverted images
cv2.imshow('Original Image', image)
cv2.imshow('Inverted Image', inverted_image)

cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, the original image is loaded using cv2.imread(). The cv2.bitwise_not() function is then used to invert the image. Finally, both the original and inverted images are displayed.

Example: Using a Mask

You can also use a mask with cv2.bitwise_not(). This allows you to invert only specific parts of the image. Here's how you can do it:


import cv2
import numpy as np

# Load an image
image = cv2.imread('image.jpg')

# Create a mask
mask = np.zeros(image.shape[:2], dtype="uint8")
cv2.rectangle(mask, (50, 50), (200, 200), 255, -1)

# Invert the image using the mask
masked_image = cv2.bitwise_not(image, mask=mask)

# Display the original, mask, and masked images
cv2.imshow('Original Image', image)
cv2.imshow('Mask', mask)
cv2.imshow('Masked Image', masked_image)

cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, a mask is created using cv2.rectangle(). The cv2.bitwise_not() function is then applied only to the masked region. The result is an image where only the specified area is inverted.

Practical Applications

The cv2.bitwise_not() function is widely used in image processing. Here are some practical applications:

1. Image Masking: You can use cv2.bitwise_not() to create masks. This is useful for isolating specific parts of an image.

2. Thresholding: Inverting an image can help in thresholding. It makes it easier to detect objects in certain scenarios.

3. Image Enhancement: Inverting colors can enhance certain features in an image. This is useful in medical imaging and other fields.

For more advanced image processing techniques, check out our guide on cv2.morphologyEx().

Conclusion

The cv2.bitwise_not() function is a versatile tool in OpenCV. It allows you to invert pixel values, create masks, and enhance images. With the examples provided, you should be able to start using this function in your own projects.

Remember, cv2.bitwise_not() works well with other OpenCV functions. Experiment with different combinations to achieve the desired results. Happy coding!