OpenCV Tutorial

Image Processing

Feature Detection and Description

Drawing Functions

Video Processing

Applications and Projects

Intensity Transformation Operations on Images in OpenCV

Intensity transformation operations in image processing aim to change the pixel intensities in an image. These operations can enhance the image or extract certain features from the image. OpenCV (Open Source Computer Vision Library) provides various functions to achieve these transformations.

Here's a basic tutorial on some of the intensity transformation operations using OpenCV in Python:

1. Setup:

Make sure you have OpenCV installed. If not, install it using pip:

pip install opencv-python

Import necessary libraries:

import cv2
import numpy as np
import matplotlib.pyplot as plt

2. Image Negative:

To obtain the negative of an image, subtract every pixel value from the maximum intensity value.

def image_negative(img):
    return 255 - img

image = cv2.imread('path_to_image.jpg', cv2.IMREAD_GRAYSCALE)
negative_image = image_negative(image)

plt.subplot(121), plt.imshow(image, cmap='gray'), plt.title('Original Image')
plt.subplot(122), plt.imshow(negative_image, cmap='gray'), plt.title('Negative Image')
plt.show()

3. Log Transformation:

Log transformation is used to expand the values of dark pixels and compress the higher values. It's commonly used in Fourier transformation.

def log_transformation(img, c=1):
    return c * np.log(1 + np.float32(img))

log_image = log_transformation(image)

plt.subplot(121), plt.imshow(image, cmap='gray'), plt.title('Original Image')
plt.subplot(122), plt.imshow(log_image, cmap='gray'), plt.title('Log Transformed Image')
plt.show()

4. Gamma (Power Law) Transformation:

Gamma transformations are used for either dark or bright image enhancements.

def gamma_transformation(img, gamma=1.0):
    return np.power(img, gamma)

gamma_image = gamma_transformation(image, 2.2)

plt.subplot(121), plt.imshow(image, cmap='gray'), plt.title('Original Image')
plt.subplot(122), plt.imshow(gamma_image, cmap='gray'), plt.title('Gamma Transformed Image')
plt.show()

5. Contrast Stretching:

This helps in enhancing the contrast of the image.

def contrast_stretching(img):
    min_val = np.min(img)
    max_val = np.max(img)
    return (img - min_val) / (max_val - min_val) * 255

contrast_image = contrast_stretching(image)

plt.subplot(121), plt.imshow(image, cmap='gray'), plt.title('Original Image')
plt.subplot(122), plt.imshow(contrast_image, cmap='gray'), plt.title('Contrast Stretched Image')
plt.show()

Note: Ensure to adjust the path_to_image.jpg to the path of your image. Also, remember to properly handle any potential issues like data type mismatches or value overflows when implementing these transformations.

This tutorial is just a brief introduction. OpenCV offers a vast array of image processing functions and tools.

  1. Intensity Scaling in OpenCV Python:

    • Description: Introduction to basic intensity scaling in OpenCV.
    • Code:
      import cv2
      import numpy as np
      
      # Read the image
      img = cv2.imread('image.jpg')
      
      # Intensity scaling (multiply by a scalar)
      scaled_img = cv2.multiply(img, 2)  # Scaling by a factor of 2
      
      # Display the original and scaled images
      cv2.imshow('Original Image', img)
      cv2.imshow('Intensity Scaled Image', scaled_img)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
  2. Histogram Equalization in OpenCV for Intensity Transformation:

    • Description: Demonstrates histogram equalization for enhancing image contrast.
    • Code:
      import cv2
      import numpy as np
      
      # Read the grayscale image
      gray_img = cv2.imread('grayscale_image.jpg', cv2.IMREAD_GRAYSCALE)
      
      # Apply histogram equalization
      equalized_img = cv2.equalizeHist(gray_img)
      
      # Display the original and equalized images
      cv2.imshow('Original Grayscale Image', gray_img)
      cv2.imshow('Histogram Equalized Image', equalized_img)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
  3. Contrast Stretching and Normalization in OpenCV:

    • Description: Illustrates contrast stretching and normalization techniques.
    • Code:
      import cv2
      import numpy as np
      
      # Read the grayscale image
      gray_img = cv2.imread('grayscale_image.jpg', cv2.IMREAD_GRAYSCALE)
      
      # Contrast stretching
      contrast_stretched = cv2.normalize(gray_img, None, 0, 255, cv2.NORM_MINMAX)
      
      # Display the original and contrast-stretched images
      cv2.imshow('Original Grayscale Image', gray_img)
      cv2.imshow('Contrast Stretched Image', contrast_stretched)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
  4. Gamma Correction for Intensity Transformation in OpenCV:

    • Description: Applies gamma correction to adjust image brightness.
    • Code:
      import cv2
      import numpy as np
      
      # Read the image
      img = cv2.imread('image.jpg')
      
      # Apply gamma correction
      gamma = 1.5
      gamma_corrected = np.power(img / 255.0, gamma) * 255.0
      
      # Convert to uint8
      gamma_corrected = np.uint8(gamma_corrected)
      
      # Display the original and gamma-corrected images
      cv2.imshow('Original Image', img)
      cv2.imshow('Gamma Corrected Image', gamma_corrected)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
  5. Logarithmic Transformation in OpenCV:

    • Description: Implements logarithmic transformation for enhancing details.
    • Code:
      import cv2
      import numpy as np
      
      # Read the grayscale image
      gray_img = cv2.imread('grayscale_image.jpg', cv2.IMREAD_GRAYSCALE)
      
      # Apply logarithmic transformation
      log_transformed = np.log1p(gray_img)
      
      # Normalize to 0-255
      log_transformed = cv2.normalize(log_transformed, None, 0, 255, cv2.NORM_MINMAX)
      
      # Convert to uint8
      log_transformed = np.uint8(log_transformed)
      
      # Display the original and log-transformed images
      cv2.imshow('Original Grayscale Image', gray_img)
      cv2.imshow('Log-Transformed Image', log_transformed)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
  6. Power-law Transformation in OpenCV:

    • Description: Implements power-law (gamma) transformation for image enhancement.
    • Code:
      import cv2
      import numpy as np
      
      # Read the grayscale image
      gray_img = cv2.imread('grayscale_image.jpg', cv2.IMREAD_GRAYSCALE)
      
      # Apply power-law transformation
      gamma = 0.5
      power_law_transformed = np.power(gray_img / 255.0, gamma) * 255.0
      
      # Convert to uint8
      power_law_transformed = np.uint8(power_law_transformed)
      
      # Display the original and power-law transformed images
      cv2.imshow('Original Grayscale Image', gray_img)
      cv2.imshow('Power-law Transformed Image', power_law_transformed)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
  7. Piecewise Linear Transformation in OpenCV:

    • Description: Applies piecewise linear transformation for custom intensity mapping.
    • Code:
      import cv2
      import numpy as np
      
      # Read the grayscale image
      gray_img = cv2.imread('grayscale_image.jpg', cv2.IMREAD_GRAYSCALE)
      
      # Piecewise linear transformation
      piecewise_linear_transformed = cv2.LUT(gray_img, np.piecewise(gray_img, [gray_img < 128, gray_img >= 128], [lambda x: 2*x, lambda x: 2*x - 255]))
      
      # Display the original and piecewise linear transformed images
      cv2.imshow('Original Grayscale Image', gray_img)
      cv2.imshow('Piecewise Linear Transformed Image', piecewise_linear_transformed)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
  8. Histogram Specification in OpenCV:

    • Description: Implements histogram specification for adjusting the intensity distribution.
    • Code:
      import cv2
      import numpy as np
      
      # Read the reference and target images
      reference_img = cv2.imread('reference_image.jpg', cv2.IMREAD_GRAYSCALE)
      target_img = cv2.imread('target_image.jpg', cv2.IMREAD_GRAYSCALE)
      
      # Apply histogram specification
      matched_img = cv2.xphoto.createSimpleWB().createReference_1C(reference_img).apply(target_img)
      
      # Display the reference, target, and matched images
      cv2.imshow('Reference Image', reference_img)
      cv2.imshow('Target Image', target_img)
      cv2.imshow('Matched Image', matched_img)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
  9. Adaptive Histogram Equalization in OpenCV:

    • Description: Demonstrates adaptive histogram equalization for localized contrast enhancement.
    • Code:
      import cv2
      import numpy as np
      
      # Read the grayscale image
      gray_img = cv2.imread('grayscale_image.jpg', cv2.IMREAD_GRAYSCALE)
      
      # Apply adaptive histogram equalization
      adaptive_equalized = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)).apply(gray_img)
      
      # Display the original and adaptive equalized images
      cv2.imshow('Original Grayscale Image', gray_img)
      cv2.imshow('Adaptive Equalized Image', adaptive_equalized)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
  10. Real-Time Intensity Transformation with OpenCV:

    • Description: Demonstrates real-time intensity transformation using OpenCV.
    • Code:
      import cv2
      
      # Initialize camera
      cap = cv2.VideoCapture(0)
      
      while True:
          # Capture frame-by-frame
          ret, frame = cap.read()
      
          # Apply intensity transformation in real-time (e.g., gamma correction)
      
          # Display the result
          cv2.imshow('Real-Time Intensity Transformation', frame)
      
          # Break the loop on 'q' key press
          if cv2.waitKey(1) & 0xFF == ord('q'):
              break
      
      # Release the camera and close windows
      cap.release()
      cv2.destroyAllWindows()