Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Image Processing Algorithms in Computer Vision
Next article icon

Image Processing Algorithms in Computer Vision

Last Updated : 04 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In the field of computer vision, image preprocessing is a crucial step that involves transforming raw image data into a format that can be effectively utilized by machine learning algorithms. Proper preprocessing can significantly enhance the accuracy and efficiency of image recognition tasks. This article explores various image preprocessing algorithms commonly used in computer vision.

What is image processing in computer vision?

Image processing in computer vision refers to a set of techniques and algorithms used to manipulate and analyze digital images to extract meaningful information. It involves transforming raw image data into a format that is easier to understand and interpret by both humans and machine learning algorithms. The goal of image processing is to improve the quality of images, enhance specific features, and prepare the data for further analysis, recognition, and decision-making tasks.

Key Objectives of Image Processing

  1. Enhancement: Improving the visual appearance of an image or accentuating certain features to make them more prominent.
  2. Restoration: Correcting defects or distortions in an image to recover the original scene.
  3. Segmentation: Dividing an image into meaningful regions or objects for easier analysis.
  4. Compression: Reducing the size of image files for storage and transmission efficiency.
  5. Analysis: Extracting quantitative or qualitative information from images.

Now we will discuss all the different preprocessing algorithms used in computer vision;

1. Image Resizing

Image resizing changes the dimensions of an image. The challenge is to maintain image quality while altering its size. Here are the main interpolation methods:

a) Nearest Neighbor Interpolation:

  • Simplest and fastest method
  • Selects the pixel value of the nearest neighbor
  • Results in a blocky image when enlarging

b) Bilinear Interpolation:

  • Uses a weighted average of the 4 nearest pixel values
  • Smoother results than nearest neighbor, but can cause some blurring

c) Bicubic Interpolation:

  • Uses a weighted average of the 16 nearest pixel values
  • Produces the smoothest results, especially for photographic images

Code Implementation

Python
import cv2 import numpy as np import matplotlib.pyplot as plt  def resize_image(image, target_size):     print(f"Original image shape: {image.shape}")     print(f"Target size: {target_size}")          resized_nn = cv2.resize(image, target_size, interpolation=cv2.INTER_NEAREST)     resized_bilinear = cv2.resize(image, target_size, interpolation=cv2.INTER_LINEAR)     resized_bicubic = cv2.resize(image, target_size, interpolation=cv2.INTER_CUBIC)          print(f"Resized image shape: {resized_nn.shape}")          return resized_nn, resized_bilinear, resized_bicubic  # Example usage image_path = '/kaggle/input/sample-image/tint1.jpg' image = cv2.imread(image_path)  if image is None:     print(f"Failed to load image from {image_path}") else:     print(f"Successfully loaded image from {image_path}")          target_size = (800, 600)     nn, bilinear, bicubic = resize_image(image, target_size)          # Display the results     plt.figure(figsize=(15, 5))          plt.subplot(131)     plt.imshow(cv2.cvtColor(nn, cv2.COLOR_BGR2RGB))     plt.title('Nearest Neighbor')     plt.axis('off')          plt.subplot(132)     plt.imshow(cv2.cvtColor(bilinear, cv2.COLOR_BGR2RGB))     plt.title('Bilinear')     plt.axis('off')          plt.subplot(133)     plt.imshow(cv2.cvtColor(bicubic, cv2.COLOR_BGR2RGB))     plt.title('Bicubic')     plt.axis('off')          plt.tight_layout()     plt.show()  print("Script completed") 

Output:

op1
Output


Code Explanation:

  • Import Libraries: Imports cv2 for image processing, numpy for calculations, and matplotlib.pyplot for plotting images.
  • Define resize_image Function: Resizes the image using three methods: Nearest Neighbor, Bilinear, and Bicubic interpolation.
  • Load Image: Reads the image from the specified path and checks if the image was loaded successfully.
  • Perform Resizing: Applies Nearest Neighbor, Bilinear, and Bicubic resizing methods to the image.
  • Display Results: Shows the original image resized using different methods side by side using matplotlib.

2. Image Normalization

Normalization adjusts pixel intensity values to a standard scale. Common techniques include:

a) Min-Max Scaling:

  • Scales values to a fixed range, typically [0, 1].
Formula: (x - min) / (max - min)

b) Z-score Normalization:

  • Transforms data to have a mean of 0 and standard deviation of 1.
Formula: (x - mean) / standard_deviation

c) Histogram Equalization:

Enhances contrast by spreading out the most frequent intensity values.

Implementation

Python
import cv2 import numpy as np import matplotlib.pyplot as plt  def normalize_image(image):     print("Performing image normalization...")          # Min-Max Scaling     min_max = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX)          # Z-score Normalization     z_score = np.zeros_like(image, dtype=np.float32)     for i in range(3):  # for each channel         channel = image[:,:,i]         mean = np.mean(channel)         std = np.std(channel)         z_score[:,:,i] = (channel - mean) / (std + 1e-8)  # adding small value to avoid division by zero     z_score = cv2.normalize(z_score, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)          # Histogram Equalization     gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)     hist_eq = cv2.equalizeHist(gray)          print("Normalization complete.")     return min_max, z_score, hist_eq  # Example usage image_path = '/kaggle/input/sample-image/input_image.jpg' image = cv2.imread(image_path)  if image is None:     print(f"Failed to load image from {image_path}") else:     print(f"Successfully loaded image from {image_path}")          min_max, z_score, hist_eq = normalize_image(image)          # Display the results     plt.figure(figsize=(20, 5))          plt.subplot(141)     plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))     plt.title('Original')     plt.axis('off')          plt.subplot(142)     plt.imshow(cv2.cvtColor(min_max, cv2.COLOR_BGR2RGB))     plt.title('Min-Max Scaling')     plt.axis('off')          plt.subplot(143)     plt.imshow(cv2.cvtColor(z_score, cv2.COLOR_BGR2RGB))     plt.title('Z-score Normalization')     plt.axis('off')          plt.subplot(144)     plt.imshow(hist_eq, cmap='gray')     plt.title('Histogram Equalization')     plt.axis('off')          plt.tight_layout()     plt.show()  print("Script completed") 

Output:

Norm
Normalization


Code Explanation:

  • Import Libraries: Imports cv2, numpy, and matplotlib.pyplot for image processing and visualization.
  • Define normalize_image Function: Applies Min-Max Scaling, Z-Score Normalization, and Histogram Equalization to the image.
  • Load Image: Reads an image from a specified path and checks if it loaded successfully.
  • Perform Normalization: Executes Min-Max Scaling, Z-Score Normalization, and Histogram Equalization to adjust the image.
  • Display Results: Shows the original and processed images side by side using matplotlib.

3. Image Augmentation

Image augmentation creates modified versions of images to expand the training dataset. Key techniques include:

a.) Geometric Transformations:

  • Rotation: Turning the image around a center point.
  • Flipping: Mirroring the image horizontally or vertically.
  • Scaling: Changing the size of the image.
  • Cropping: Cutting out a part of the image to use.

b) Color Adjustments:

  • Brightness: Making the image lighter or darker.
  • Contrast: Changing the difference between light and dark areas.
  • Saturation: Adjusting the intensity of colors.
  • Hue: Shifting the overall color tone of the image.

c) Noise Addition:

  • Adding Random Noise: Introducing random variations to pixel values to simulate imperfections.
Python
import cv2 import numpy as np import matplotlib.pyplot as plt  def augment_image(image):     print("Performing image augmentation...")          # Rotation     rows, cols = image.shape[:2]     M = cv2.getRotationMatrix2D((cols/2, rows/2), 45, 1)     rotated = cv2.warpAffine(image, M, (cols, rows))          # Flipping     flipped = cv2.flip(image, 1)  # 1 for horizontal flip          # Brightness adjustment     bright = cv2.convertScaleAbs(image, alpha=1.5, beta=0)          # Add noise     noise = np.random.normal(0, 25, image.shape).astype(np.uint8)     noisy = cv2.add(image, noise)          print("Augmentation complete.")     return rotated, flipped, bright, noisy  # Example usage image_path = '/kaggle/input/sample-image/input_image.jpg' image = cv2.imread(image_path)  if image is None:     print(f"Failed to load image from {image_path}") else:     print(f"Successfully loaded image from {image_path}")          rotated, flipped, bright, noisy = augment_image(image)          # Display the results     plt.figure(figsize=(20, 5))          plt.subplot(151)     plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))     plt.title('Original')     plt.axis('off')          plt.subplot(152)     plt.imshow(cv2.cvtColor(rotated, cv2.COLOR_BGR2RGB))     plt.title('Rotated')     plt.axis('off')          plt.subplot(153)     plt.imshow(cv2.cvtColor(flipped, cv2.COLOR_BGR2RGB))     plt.title('Flipped')     plt.axis('off')          plt.subplot(154)     plt.imshow(cv2.cvtColor(bright, cv2.COLOR_BGR2RGB))     plt.title('Brightness Adjusted')     plt.axis('off')          plt.subplot(155)     plt.imshow(cv2.cvtColor(noisy, cv2.COLOR_BGR2RGB))     plt.title('Noisy')     plt.axis('off')          plt.tight_layout()     plt.show()  print("Script completed") 

Output:

Aug-(1)
Augmentation

Code Explanation:

  • Import Libraries: Imports cv2 for image processing, numpy for numerical operations, and matplotlib.pyplot for displaying images.
  • Define augment_image Function: Performs four types of image augmentations: rotation, flipping, brightness adjustment, and noise addition.
  • Load Image: Reads the image from the specified path and confirms successful loading.
  • Perform Augmentation: Applies rotation, horizontal flipping, brightness adjustment, and adds Gaussian noise to the image.
  • Display Results: Shows the original and augmented images (rotated, flipped, brightness adjusted, noisy) side by side using matplotlib.

4. Image Denoising

Denoising removes noise from images, enhancing quality and clarity. Common methods include:

a) Gaussian Blur:

  • Applies a Gaussian function to smooth the image.
  • Effective for reducing Gaussian noise.

b) Median Filtering:

  • Replaces each pixel with the median of neighboring pixels.
  • Effective for salt-and-pepper noise.

c) Bilateral Filtering:

Preserves edges while reducing noise.

Code Implementation

Python
import cv2 import matplotlib.pyplot as plt  def denoise_image(image):     print("Performing image denoising...")          gaussian = cv2.GaussianBlur(image, (5, 5), 0)     median = cv2.medianBlur(image, 5)     bilateral = cv2.bilateralFilter(image, 9, 75, 75)          print("Denoising complete.")     return gaussian, median, bilateral  # Example usage image_path = '/kaggle/input/sample-image/noisy_image.jpg' image = cv2.imread(image_path)  if image is None:     print(f"Failed to load image from {image_path}") else:     print(f"Successfully loaded image from {image_path}")          gaussian, median, bilateral = denoise_image(image)          # Display the results     plt.figure(figsize=(20, 5))          plt.subplot(141)     plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))     plt.title('Original (Noisy)')     plt.axis('off')          plt.subplot(142)     plt.imshow(cv2.cvtColor(gaussian, cv2.COLOR_BGR2RGB))     plt.title('Gaussian Blur')     plt.axis('off')          plt.subplot(143)     plt.imshow(cv2.cvtColor(median, cv2.COLOR_BGR2RGB))     plt.title('Median Blur')     plt.axis('off')          plt.subplot(144)     plt.imshow(cv2.cvtColor(bilateral, cv2.COLOR_BGR2RGB))     plt.title('Bilateral Filter')     plt.axis('off')          plt.tight_layout()     plt.show()  print("Script completed") 

Output:

Denoising
Denoising

Code Explanation:

  • Import Libraries: Imports cv2 for image processing and matplotlib.pyplot for displaying images.
  • Define denoise_image Function: Applies three different denoising techniques to the noisy image: Gaussian Blur, Median Blur, and Bilateral Filter.
  • Load Image: Reads the noisy image from the specified path and checks if the image was successfully loaded.
  • Perform Denoising: Applies Gaussian Blur, Median Blur, and Bilateral Filter to the noisy image to reduce noise.
  • Display Results: Uses matplotlib to display the original noisy image and the results of the three denoising methods side by side.

5. Edge Detection

Edge detection identifies boundaries of objects within images. Key algorithms include:

a) Sobel Operator

  • Gradient Calculation: Measures changes in image intensity.
  • Edge Emphasis: Highlights horizontal and vertical edges using convolution kernels.

b) Canny Edge Detector

  • Multi-stage Algorithm: Involves noise reduction, gradient calculation, and edge tracking.
  • Intensity Gradients and Non-maximum Suppression: Detects edges by suppressing non-maximal pixels.

c) Laplacian of Gaussian (LoG)

  • Combines Gaussian Smoothing with Laplacian Operator: Smooths image to reduce noise, then applies Laplacian to detect edges.

Code Example:

Python
import cv2 import numpy as np import matplotlib.pyplot as plt  def detect_edges(image):     print("Performing edge detection...")          gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)          # Sobel     sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)     sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)     sobel = np.sqrt(sobelx**2 + sobely**2)     sobel = np.uint8(sobel / sobel.max() * 255)          # Canny     canny = cv2.Canny(gray, 100, 200)          # Laplacian of Gaussian     blur = cv2.GaussianBlur(gray, (3, 3), 0)     log = cv2.Laplacian(blur, cv2.CV_64F)     log = np.uint8(np.absolute(log))          print("Edge detection complete.")     return sobel, canny, log  # Example usage image_path = '/kaggle/input/sample-image/tint1.jpg' image = cv2.imread(image_path)  if image is None:     print(f"Failed to load image from {image_path}") else:     print(f"Successfully loaded image from {image_path}")          sobel, canny, log = detect_edges(image)          # Display the results     plt.figure(figsize=(20, 5))          plt.subplot(141)     plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))     plt.title('Original')     plt.axis('off')          plt.subplot(142)     plt.imshow(sobel, cmap='gray')     plt.title('Sobel')     plt.axis('off')          plt.subplot(143)     plt.imshow(canny, cmap='gray')     plt.title('Canny')     plt.axis('off')          plt.subplot(144)     plt.imshow(log, cmap='gray')     plt.title('Laplacian of Gaussian')     plt.axis('off')          plt.tight_layout()     plt.show()  print("Script completed") 

Output:

Edge
Edge Detection

Explanation:

  • Import Libraries: Imports cv2 for image processing, numpy for calculations, and matplotlib.pyplot for plotting images.
  • Define detect_edges Function: Converts the image to grayscale and applies Sobel, Canny, and Laplacian of Gaussian methods to detect edges.
  • Load Image: Reads an image from a specified path and verifies if it was loaded successfully.
  • Perform Edge Detection: Calls detect_edges to get edge-detected versions of the image using Sobel, Canny, and LoG techniques.
  • Display Results: Uses matplotlib to show the original image and the edge-detected results side by side.

6. Image Binarization

Binarization converts an image to black and white based on a threshold. Methods include:

a) Global Thresholding:

  • Applies a single threshold value for the entire image.
  • Otsu's method automatically determines the optimal threshold.

b) Adaptive Thresholding:

  • Uses different thresholds for different regions of the image.
  • Better for images with varying illumination.
Python
import cv2 import matplotlib.pyplot as plt  def binarize_image(image):     print("Performing image binarization...")          gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)          # Global thresholding (Otsu's method)     _, global_thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)          # Adaptive thresholding     adaptive_thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,                                              cv2.THRESH_BINARY, 11, 2)          print("Binarization complete.")     return global_thresh, adaptive_thresh  # Example usage image_path = '/kaggle/input/sample-image/tint1.jpg' image = cv2.imread(image_path)  if image is None:     print(f"Failed to load image from {image_path}") else:     print(f"Successfully loaded image from {image_path}")          global_thresh, adaptive_thresh = binarize_image(image)          # Display the results     plt.figure(figsize=(15, 5))          plt.subplot(131)     plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))     plt.title('Original')     plt.axis('off')          plt.subplot(132)     plt.imshow(global_thresh, cmap='gray')     plt.title('Global Thresholding')     plt.axis('off')          plt.subplot(133)     plt.imshow(adaptive_thresh, cmap='gray')     plt.title('Adaptive Thresholding')     plt.axis('off')          plt.tight_layout()     plt.show()  print("Script completed") 

Output:

Binarization-(2)
Binarization

Explanation:

  • Import Libraries: Imports cv2 for image processing and matplotlib.pyplot for visualizing images.
  • Define binarize_image Function: Converts the image to grayscale and applies global thresholding (Otsu’s method) and adaptive thresholding for binary image creation.
  • Load Image: Reads an image from the specified path and checks if it was loaded correctly.
  • Perform Binarization: Calls binarize_image to apply global and adaptive thresholding methods on the grayscale image to create binary images.
  • Display Results: Uses matplotlib to show the original image, global thresholded image, and adaptive thresholded image in a 1x3 grid layout.

Conclusion

Image preprocessing is a vital step in computer vision that significantly influences the performance of machine learning models. Techniques such as resizing, normalization, augmentation, denoising, histogram equalization, edge detection, and binarization help in transforming raw image data into a more usable form. Understanding and effectively applying these preprocessing algorithms can lead to more accurate and efficient image recognition systems.


Next Article
Image Processing Algorithms in Computer Vision

P

poojashu00qn
Improve
Article Tags :
  • Blogathon
  • Computer Vision
  • AI-ML-DS
  • Interview-Questions
  • Data Science Blogathon 2024

Similar Reads

    Computer Vision Algorithms
    Computer vision seeks to mimic the human visual system, enabling computers to see, observe, and understand the world through digital images and videos. This capability is not just about capturing visual data. Still, it involves interpreting and making decisions based on that data, opening up myriad
    14 min read
    Image Thresholding Techniques in Computer Vision
    Image thresholding is a technique in computer vision that converts a grayscale image into a binary image by setting each pixel to either black or white based on a specific threshold value. The article provides a comprehensive overview of various image thresholding techniques used in computer vision,
    7 min read
    AI in Image Processing
    Imagine you're looking at a photo. It's true that you can see objects, colors and shapes, but did you realize that computers can also "see" and comprehend images? This incredible capability is made possible by the field of image processing, which gains even more strength when artificial intelligence
    8 min read
    Feature Descriptor in Image Processing
    In image processing, a feature descriptor is a representation of an image region or key point that captures relevant information about the image content. In this article, we are going to discuss one of the image processing algorithms i.e. Feature Descriptor Image processingImage processing is a comp
    5 min read
    Image Segmentation Approaches and Techniques in Computer Vision
    Image segmentation partitions an image into multiple segments that simplify the image's representation, making it more meaningful and easier to work with. This technique is essential for various applications, from medical imaging and autonomous driving to object detection and image editing. Effectiv
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences