Intensity Transformation Operations on Images in Python OpenCV

FeaImg Intensity Transformation Images

Hey Folks! In this tutorial, we will have a look into changing the intensity of images using the Python programming language.


Introduction to Image Intensity Transformation

Images are subjected to intensity modifications for contrast manipulation or picture thresholding. These are in the spatial domain, which means they are done directly on the pixels of the picture at hand rather than on the Fourier transform of the image.


Implementing Intensity Transformation Operations on Images – Python OpenCV

The first step involves loading the necessary modules/libraries and loading the image we would like to work on in the program using the cv2.imread function of the OpenCV library.

import cv2
import numpy as np
from google.colab.patches import cv2_imshow

img = cv2.imread('sample.jpg')
cv2_imshow(img)
Intensity Transformations Sample Image
Intensity Transformations Sample Image

Next, we set a gamma value which will define the intensity of the image, and gamma correction is done along the gamma value to get the image of the correct intensity.

As the last step, we save the image using the cv2.imwrite method of the OpenCV library.

for gamma in [0.1, 0.5, 1.2, 2.2]:
	gamma_corrected = np.array(255*(img / 255) ** gamma, dtype = 'uint8')
	cv2.imwrite('gamma_transformed_'+str(gamma)+'.jpg', gamma_corrected)

All the output images for the four gamma values are displayed below.

Gamma Transformed0 1
Gamma Transformed0 1
Gamma Transformed0 5
Gamma Transformed0 5
Gamma Transformed1 2
Gamma Transformed1 2
Gamma Transformed2 2
Gamma Transformed2 2

Conclusion

Congratulations! You just learned how to build a Python program to modify the intensity of images using the OpenCV Library in Python. Hope you enjoyed it! 😇

Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:

  1. Visualizing Colors In Images Using Histograms – Python OpenCV
  2. Draw Shapes Using OpenCV – A Complete How-To Guide
  3. Live Sketch Using Webcam with Python OpenCV [Easy Guide]
  4. Python OpenCV filter2D() function – A Complete Guide

Thank you for taking your time out! Hope you learned something new!! 😄