Image negatives, most of you might have heard this term, in good old days were used to produce images. Film Photography has not yet become obsolete as some wedding photographers are still shooting film. Because one has to pay for the film rolls and processing fees, most people have now switched to digital.
I recently heard of Foveon X3 direct image sensor which claims to combine the power of digital sensor with the essence of the film. (Check here)
Image negative is produced by subtracting each pixel from the maximum intensity value. e.g. for an 8-bit image, the max intensity value is 28– 1 = 255, thus each pixel is subtracted from 255 to produce the output image.
Thus, the transformation function used in image negative is
s = T(r) = L – 1 – r
Where L-1 is the max intensity value and s, and r
For grayscale images, light areas appear dark and vice versa. For
1 2 3 4 5 6 7 8 9 10 11 |
import cv2 import numpy as np # Load the image img = cv2.imread('D:/downloads/forest.jpg') # Check the datatype of the image print(img.dtype) # Subtract the img from max value(calculated from dtype) img_neg = 255 - img # Show the image cv2.imshow('negative',img_neg) cv2.waitKey(0) |
The output looks like this
Method 2
OpenCV provides a built-in function cv2.bitwise_not() that inverts every bit of an array. This takes as input the original image and outputs the inverted image. Below is the code for this.
1 2 3 4 5 6 7 8 9 10 11 |
import cv2 # Load the image img = cv2.imread('D:/downloads/forest.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) |
There is a long debate going on whether black on white or white on black is better. To my knowledge, Image negative favors black on white thus it is suited for enhancing the white or gray information embedded in the dark regions of the image especially when the black areas are dominant in size.
Application: In grayscale images, when the background is black, the foreground gray levels are not clearly visible. So, converting background to white, the gray levels now become more visible.
In the next blog, we will discuss Log transformations in detail. Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.