Negative transformation of an image using Python and OpenCV


An image with reversed brightness is referred a negative image, in which the light parts of the image appear dark and the darker parts appear lighter. The negative transformation of a colored image (RGB image) will be reversed, with red areas appearing cyan, greens appearing magenta, and blues appearing yellow.

In Medical Imaging, Remote Sensing, and some other applications negative transformation is widely used. It also helps to find the details from the darker regions of the image.

The negative transformation function is −

As we know that an 8-bit image has a max intensity value is 255, therefore we need to subtract each pixel from 255(max intensity value) to produce the negative image.

s = T(r) = L – 1 – r

In this article, we will see different ways to get the negative transformation of an image using OpenCV Python.

Using cv2.bitwise_not() function:

The function calculates the bit-wise inversion of the input array. Following is the syntax of the not() function –

cv2.bitwise_not(src[, dst[, mask]])

Parameters

  • src: input image array.

  • dst: output array that has the same size and type as the input array (optional).

  • mask: optional parameter.

Example

In this example, we will invert every value of the image array to get the negative image.

import cv2
 
# Load the image
img = cv2.imread('Images/Fruits.jpg')
 
# Invert the image using cv2.bitwise_not
img_neg = cv2.bitwise_not(img)
 
# Show the image
cv2.imshow('negative',img_neg)
cv2.waitKey(0)

Input image

Output image

Using Subtraction

In this approach, we loop through the pixels using two for loops (based on the image dimensions), and the red, green, and blue values of each pixel are subtracted from the maximum pixel value (255).

Example

let’s implement a Negative Transformation of a Colored image without using any functions.

import cv2
 
# Load the image
img = cv2.imread('Images/Fruits.jpg', cv2.COLOR_BGR2RGB)

height, width, _ = img.shape
  
for i in range(0, height - 1):
   for j in range(0, width - 1):
      # each pixel has RGB channle data 
      pixel = img[i, j]
      
      # red
      pixel[0] = 255 - pixel[0]
      
      # green
      pixel[1] = 255 - pixel[1]
      
      # blue
      pixel[2] = 255 - pixel[2]
      
      # Store new values in the pixel
      img[i, j] = pixel
        
# Display the negative transformed image
cv2.imshow('negative image', img)
cv2.waitKey(0)

Output Image

We have successfully transformed the color image to negative. Initially, we have iterated all values (pixel data) of the image array using the for loops and then each pixel value is subtracted from the maximum pixel value (255) to get the negative transformed image.

Example

Instead of looping each pixel here, we will directly apply the subtraction operation between the max intensity value and the image array.

import numpy as np
import cv2

img = cv2.imread('Images/Fruits.jpg')

# Subtract the img array values from max value(calculated from dtype)
img_neg = 255 - img

# Show the negative image
cv2.imshow('negative',img_neg)
cv2.waitKey(0)

Output

For grayscale images

For a grayscale images, the light parts of the image appear dark and the darker parts appear lighter in the negative transformation.

Example

In this example, we will read the image in grayscale mode to generate its negative.

import cv2
 
# Load the image
gray = cv2.imread('Images/Fruits.jpg', 0)
 
cv2.imshow('Gray image:', gray)

# Invert the image using cv2.bitwise_not
gray_neg = cv2.bitwise_not(gray)
 
# Show the image
cv2.imshow('negative',gray_neg)
cv2.waitKey(0)

Input image

Output image

We have seen different ways to get the negative transformation of an image using OpenCV Python.

Updated on: 31-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements