OpenCV and Image Processing Basics

Chapter 15: OpenCV and Image Processing Basics

OpenCV (Open Source Computer Vision Library) is a library of programming functions mainly aimed at real-time computer vision. While computer graphics is about generating images from models, computer vision (and image processing) is about extracting data or modifying existing images.

What is OpenCV?

Originally developed by Intel, OpenCV is now the industry-standard library for image processing. It is written in C++ but has popular wrappers for Python, Java, and other languages.

  • Capabilities: Object detection, face recognition, motion tracking, image stitching, and 3D reconstruction.
  • Image Representation: In OpenCV, images are represented as NumPy arrays (in Python) or Matrices (in C++).

Basic Operations

1. Reading and Displaying Images

In Python, reading an image is simple:

import cv2

# Load an image (default: BGR color space)
img = cv2.imread('my_image.jpg')

# Display the image in a window
cv2.imshow('Image Window', img)
cv2.waitKey(0) # Wait for a key press
cv2.destroyAllWindows()
  • Important: OpenCV uses BGR (Blue, Green, Red) order by default, while most other libraries (like Matplotlib) use RGB.

2. Drawing on Images

OpenCV provides functions for drawing directly onto image arrays.

  • cv2.line(img, start, end, color, thickness)
  • cv2.rectangle(img, pt1, pt2, color, thickness)
  • cv2.circle(img, center, radius, color, thickness)
  • cv2.putText(img, text, org, fontFace, fontScale, color, thickness)
OpenCV Drawing primitives

Image Processing Techniques

1. Grayscale Conversion

Reducing a 3-channel (BGR) image to a 1-channel grayscale image is a common first step in computer vision.

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

2. Gaussian Blur

Smoothing an image to reduce noise before further processing. It uses a kernel (a small matrix) to average the pixel values.

blurred_img = cv2.GaussianBlur(img, (5, 5), 0)

3. Edge Detection (Canny)

The Canny edge detector is a multi-stage algorithm used to detect a wide range of edges in images.

edges = cv2.Canny(gray_img, 100, 200)
  1. Noise Reduction: Uses Gaussian blur.
  2. Gradient Calculation: Finds intensity gradients.
  3. Non-maximum Suppression: Thins out the edges.
  4. Hysteresis Thresholding: Filters out weak edges.
Edges Simplified Canny Representation

Summary

OpenCV is an essential companion to computer graphics. While graphics focus on creation, OpenCV focuses on understanding and modification. From simple filtering and edge detection to advanced machine learning-based object detection, OpenCV provides the tools needed to bridge the gap between digital images and real-world data.