Basics of Image Processing
Setting Up Your Environment
Before diving into the image processing concepts, ensure you have Python installed on your system. You’ll also need to install OpenCV and Pillow, which are widely used libraries for image processing tasks. You can install these using pip:
pip install opencv-python-headless pillow
Image Representation
Images are represented as arrays in computer vision. In color images, each pixel is typically represented by three values corresponding to the Red, Green, and Blue (RGB) color channels.
Reading and Displaying Images
Let’s start by reading and displaying an image using OpenCV.
import cv2
# Load an image
image = cv2.imread('path_to_your_image.jpg')
# Display the image in a window
cv2.imshow('Image', image)
cv2.waitKey(0) # Wait for a key press to close the window
cv2.destroyAllWindows()
Converting Color Spaces
Converting Color SpacesChanging the color space of an image is a common operation. For example, converting an image from RGB to grayscale is often used in various image processing tasks to simplify the analysis.
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Display the grayscale image
cv2.imshow('Grayscale Image', gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Basic Image Manipulations
Resizing Images
Resizing is crucial for many applications, especially when dealing with images of varying dimensions.
# Resize the image to 200x200 pixels
resized_image = cv2.resize(image, (200, 200))
# Display the resized image
cv2.imshow('Resized Image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Cropping Images
Cropping is used to cut out a portion of an image.
# Crop the image using array slicing
cropped_image = image[50:150, 100:200] # Crop from y=50:150, x=100:200
# Display the cropped image
cv2.imshow('Cropped Image', cropped_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Image Fitering
Applying a filter to an image is a common way to enhance or modify the image. For example, a Gaussian blur is often used to reduce noise.
# Apply a Gaussian blur with a 5x5 kernel
blurred_image = cv2.GaussianBlur(image, (5, 5), 0)
# Display the blurred image
cv2.imshow('Blurred Image', blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Edge Detection
Edge detection is a technique used to identify the boundaries of objects within images.
# Detect edges using the Canny algorithm
edges = cv2.Canny(gray_image, 100, 200)
# Display the edges
cv2.imshow('Edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()