🤖
Robotics Handbook
HomeConnect
  • Welcome
    • Authors Note
  • Computer Aided Designs and Simulations
    • Computer Aided Design and Simulations
    • 3D Modelling CAD
      • SolidWorks
    • Simulations
    • PCB Design
  • ROS (Advanced)
    • ROS
    • ROS
      • Concepts and Packages
      • Manual and Quick Setup
    • Some Important packages
  • Hardware
    • Design Processes
      • Materials Selection
      • Build and Prototyping
      • 3D Printing and Machining
    • Fabrication Parts
  • Common Mechanisms
    • Wheels and Drives
    • Power Transmission
  • Career Paths & Research Opportunities
    • Career in Robotics
    • Job Roles In Robotics
    • Conferences and Journals
  • Companies Hiring for Robotics
  • Leading Institutes
  • Mathematical and Programming Foundations
    • Linear Algebra for Robotics
    • Calculus
  • Programming for Robotics
    • Common Languages
    • Algorithms
    • Digital Twin
  • Embedded Systems for Robotics
    • Embedded Systems
    • Microcontrollers
      • Microcontrollers (Advanced Theory)
      • Choosing a Microcontroller
    • Sensors and Actuators
      • Sensors for Robotics
      • Actuators for Robotics
    • Communication
      • Communication Protocols
    • RTOS
    • Power Systems
      • Battery Charging and Storage Best Practices
  • ML and Perception
    • ML and Perception
    • Reinforcement Learning
    • Cameras, Depth Sensors and LiDAR
    • Image Processing Basics (OpenCV)
    • Object Detection and Tracking
    • Example of a Vision Pipeline
  • Mobile Robotics
    • Mobile Robotics
    • SLAM and Navigation
    • Robot Kinematics and Dynamics
      • Some Kinematic Models
    • Trajectory Planning
    • AMR's and AGV's
    • MH633 : Mobile Robotics
      • Geometric Foundations
      • Kinematics
  • Frontiers and Emerging Fields
    • Frontiers and Emerging Fields
    • Humanoids
    • Autonomous Navigation
    • Bio-inspired and Soft Robotics
    • Space Robotics
    • Cobots
    • Edge Robotics
    • Medical Robotics
  • Drones, Rocketry and Aviation
    • Drones
      • Drone Anatomy
    • Rocketry
Powered by GitBook
On this page
  • Image Processing Basics with OpenCV
  • 1. Reading, Displaying, and Writing Images
  • 2. Color Space Conversion
  • 3. Geometric Transformations
  • 4. Image Filtering and Smoothing
  • 5. Thresholding and Binarization
  • 6. Edge Detection
  • 7. Contour Detection and Object Counting
  • 8. Feature Detection and Description
  • 9. Video Processing Basics
  • Summary
  • Further Learning Resources
  1. ML and Perception

Image Processing Basics (OpenCV)

Image Processing Basics with OpenCV

OpenCV (Open Source Computer Vision Library) is a powerful, open-source library widely used for image and video processing, computer vision, and machine learning. It provides a rich set of functions for manipulating images, detecting features, performing transformations, and much more. Below is an overview of essential image processing concepts in OpenCV, along with Python code snippets and useful references to get started.

1. Reading, Displaying, and Writing Images

Before processing, you need to load images into your program, display them for visualization, and save results.

pythonimport cv2

# Read an image from file
image = cv2.imread('path_to_image.jpg')

# Display the image in a window
cv2.imshow('Original Image', image)
cv2.waitKey(0)  # Wait for a key press to close the window
cv2.destroyAllWindows()

# Save the image to disk
cv2.imwrite('output_image.jpg', image)
  • cv2.imread() loads the image into a NumPy array.

  • cv2.imshow() creates a window to display the image.

  • cv2.waitKey(0) waits indefinitely for a key press.

  • cv2.imwrite() saves the image to a file.

2. Color Space Conversion

Many image processing tasks require converting images between color spaces, e.g., from BGR (OpenCV default) to grayscale.

python# Convert BGR image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

cv2.imshow('Grayscale Image', gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Grayscale images simplify processing by reducing channels to one intensity channel.

3. Geometric Transformations

You can perform image rotation, translation, scaling, and affine transformations.

pythonimport numpy as np

# Get image dimensions
(h, w) = image.shape[:2]

# Define rotation matrix to rotate image by 45 degrees around center
M = cv2.getRotationMatrix2D((w//2, h//2), 45, 1.0)

# Apply affine warp
rotated = cv2.warpAffine(image, M, (w, h))

cv2.imshow('Rotated Image', rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()

4. Image Filtering and Smoothing

Filters reduce noise or extract features. Common filters include blurring and sharpening.

python# Gaussian Blur
blurred = cv2.GaussianBlur(image, (7, 7), 0)

cv2.imshow('Blurred Image', blurred)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Sharpening using kernel
kernel = np.array([[0, -1, 0],
                   [-1, 5,-1],
                   [0, -1, 0]])
sharpened = cv2.filter2D(image, -1, kernel)

cv2.imshow('Sharpened Image', sharpened)
cv2.waitKey(0)
cv2.destroyAllWindows()

5. Thresholding and Binarization

Convert grayscale images into binary images to segment objects.

python# Global thresholding
_, thresh = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)

cv2.imshow('Thresholded Image', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Adaptive thresholding for varying lighting
adaptive_thresh = cv2.adaptiveThreshold(gray_image, 255,
                                        cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                        cv2.THRESH_BINARY, 11, 2)

cv2.imshow('Adaptive Threshold', adaptive_thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

6. Edge Detection

Detect edges using algorithms like Canny edge detector.

pythonedges = cv2.Canny(gray_image, 100, 200)

cv2.imshow('Edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

7. Contour Detection and Object Counting

Contours are curves joining continuous points with the same intensity. Useful for shape analysis and object counting.

python# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Draw contours
output = image.copy()
cv2.drawContours(output, contours, -1, (0, 255, 0), 2)

print(f"Number of objects detected: {len(contours)}")

cv2.imshow('Contours', output)
cv2.waitKey(0)
cv2.destroyAllWindows()

8. Feature Detection and Description

OpenCV supports feature detectors like SIFT, SURF, ORB for matching and recognition.

python# Initialize ORB detector
orb = cv2.ORB_create()

# Detect keypoints and descriptors
keypoints, descriptors = orb.detectAndCompute(gray_image, None)

# Draw keypoints
output = cv2.drawKeypoints(image, keypoints, None, color=(0,255,0))

cv2.imshow('ORB Keypoints', output)
cv2.waitKey(0)
cv2.destroyAllWindows()

9. Video Processing Basics

OpenCV can capture and process video streams from cameras.

pythoncap = cv2.VideoCapture(0)  # Open default camera

while True:
    ret, frame = cap.read()
    if not ret:
        break

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('Video Feed - Grayscale', gray)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Summary

OpenCV provides a comprehensive set of tools to perform image processing, from basic operations like reading and displaying images to advanced techniques like feature detection, segmentation, and video analysis. The library’s Python bindings make it accessible for rapid prototyping and deployment.

Further Learning Resources

PreviousCameras, Depth Sensors and LiDARNextObject Detection and Tracking

Last updated 1 day ago

Reference:

Reference:

Reference:

Reference:

Reference:

Reference:

Reference:

Reference:

Reference:

Official OpenCV Python Tutorials:

PyImageSearch OpenCV Guide:

OpenCV Documentation:

OpenCV Video Tutorials by Rob Mulla:

DataCamp OpenCV Tutorial:

OpenCV Tutorial: Read, Display and Write an Image
OpenCV-Python Tutorials: Color Space Conversion
OpenCV Image Processing Tutorial
Image Processing in OpenCV
OpenCV Thresholding Tutorial
OpenCV Canny Edge Detection
PyImageSearch: Counting Objects with Contours
OpenCV Feature Detection
OpenCV Video I/O Tutorial
https://docs.opencv.org/4.x/d6/d00/tutorial_py_root.html
https://pyimagesearch.com/2018/07/19/opencv-tutorial-a-guide-to-learn-opencv/
https://docs.opencv.org/4.x/
https://www.youtube.com/watch?v=kSqxn6zGE0c
https://www.datacamp.com/tutorial/opencv-tutorial