Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Negative transformation of an image using Python and OpenCV
An image with reversed brightness is referred to as a negative image, where the light parts appear dark and the darker parts appear lighter. In a colored image (RGB), the negative transformation reverses colors red areas appear cyan, greens appear magenta, and blues appear yellow.
Negative transformation is widely used in Medical Imaging, Remote Sensing, and other applications. It helps reveal details from darker regions of the image.
Negative Transformation Formula
For an 8-bit image with maximum intensity value 255, we subtract each pixel from 255 to produce the negative image ?
s = T(r) = L - 1 - r
Where L is the maximum intensity level (256 for 8-bit), r is the input pixel value, and s is the output pixel value.
Method 1: Using cv2.bitwise_not() Function
The bitwise_not() function calculates the bit-wise inversion of the input array ?
Syntax
cv2.bitwise_not(src[, dst[, mask]])
Parameters
src: Input image array
dst: Output array with same size and type as input (optional)
mask: Optional operation mask
Example
This example inverts every pixel value in the image array to create the negative image ?
import cv2
import numpy as np
# Create a sample image
img = np.array([[[100, 150, 200], [50, 75, 125]],
[[200, 100, 50], [175, 225, 75]]], dtype=np.uint8)
print("Original image:")
print(img)
# Invert the image using cv2.bitwise_not
img_neg = cv2.bitwise_not(img)
print("\nNegative image:")
print(img_neg)
Original image: [[[100 150 200] [ 50 75 125]] [[200 100 50] [175 225 75]]] Negative image: [[[155 105 55] [205 180 130]] [[ 55 155 205] [ 80 30 180]]]
Method 2: Using Direct Subtraction
We can directly subtract the image array from the maximum pixel value (255) using NumPy operations ?
Example
import numpy as np
# Create a sample image
img = np.array([[[100, 150, 200], [50, 75, 125]],
[[200, 100, 50], [175, 225, 75]]], dtype=np.uint8)
print("Original image:")
print(img)
# Subtract from max value (255)
img_neg = 255 - img
print("\nNegative image using subtraction:")
print(img_neg)
Original image: [[[100 150 200] [ 50 75 125]] [[200 100 50] [175 225 75]]] Negative image using subtraction: [[[155 105 55] [205 180 130]] [[ 55 155 205] [ 80 30 180]]]
Method 3: Manual Pixel-by-Pixel Transformation
This approach loops through each pixel and subtracts RGB values from 255 individually ?
Example
import numpy as np
# Create a sample image
img = np.array([[[100, 150, 200], [50, 75, 125]],
[[200, 100, 50], [175, 225, 75]]], dtype=np.uint8)
print("Original image:")
print(img)
height, width, channels = img.shape
# Create a copy to avoid modifying original
img_copy = img.copy()
for i in range(height):
for j in range(width):
# Get pixel values
pixel = img_copy[i, j]
# Transform each channel
pixel[0] = 255 - pixel[0] # Red
pixel[1] = 255 - pixel[1] # Green
pixel[2] = 255 - pixel[2] # Blue
# Store transformed values
img_copy[i, j] = pixel
print("\nNegative image (manual method):")
print(img_copy)
Original image: [[[100 150 200] [ 50 75 125]] [[200 100 50] [175 225 75]]] Negative image (manual method): [[[155 105 55] [205 180 130]] [[ 55 155 205] [ 80 30 180]]]
Grayscale Image Negative Transformation
For grayscale images, the transformation simply inverts intensity values light areas become dark and vice versa ?
Example
import cv2
import numpy as np
# Create a sample grayscale image
gray = np.array([[50, 100, 150],
[200, 75, 225],
[25, 175, 125]], dtype=np.uint8)
print("Original grayscale image:")
print(gray)
# Method 1: Using bitwise_not
gray_neg1 = cv2.bitwise_not(gray)
# Method 2: Using subtraction
gray_neg2 = 255 - gray
print("\nNegative using bitwise_not:")
print(gray_neg1)
print("\nNegative using subtraction:")
print(gray_neg2)
Original grayscale image: [[ 50 100 150] [200 75 225] [ 25 175 125]] Negative using bitwise_not: [[205 155 105] [ 55 180 30] [230 80 130]] Negative using subtraction: [[205 155 105] [ 55 180 30] [230 80 130]]
Comparison of Methods
| Method | Performance | Best For | Code Simplicity |
|---|---|---|---|
cv2.bitwise_not() |
Fast | General use | High |
| Direct subtraction | Very Fast | NumPy arrays | High |
| Manual loops | Slow | Understanding concept | Low |
Conclusion
Negative transformation inverts image brightness by subtracting pixel values from the maximum intensity (255). Use cv2.bitwise_not() or direct subtraction 255 - img for efficient implementation, while manual loops help understand the underlying concept.
