What is Image Processing?
As the name suggests, Image Processing is a method used to process the images to extract useful information from images or get enhanced images. It has applications in almost every field. For example, we can identify criminals from their images much faster than just using their textual descriptions with face identification. It is helpful in object detection, self-driving cars, robotics, pattern recognition, in the medical field to identify diseases using images, etc.
Also See, Intersection in Python and Convert String to List Python.
Popular Python Libraries for Image Processing
Python is a widely used language for Image Processing. It contains multiple libraries and frameworks for quick implementation of image processing methods.
Some examples of the tools are OpenCV and Pillow().
OpenCV
OpenCV - Open Source Computer Vision is a crucial library for image processing containing 2000+ optimized codes for machine learning and computer vision.
It can be used for multiple tasks like
- Reading an image
- Extract RGB value
- Resizing, rotating of image
- Changing the color space of an image
- Smoothing of images
- Extracting foreground
- Image segmentation
1. Reading an Image
Reading an image involves loading the image data from a file into a program for processing. In Python, the OpenCV library is commonly used for image-processing tasks. The image can be read in grayscale, color, or unchanged modes.
Code Implementation:
import cv2
# Read an image
image = cv2.imread('example.jpg') # Reads in BGR format by default
gray_image = cv2.imread('example.jpg', cv2.IMREAD_GRAYSCALE) # Read in grayscale
# Display the image
cv2.imshow('Image', image)
cv2.imshow('Grayscale Image', gray_image)
cv2.waitKey(0) # Wait for a key press to close the window
cv2.destroyAllWindows()
2. Extracting RGB Values
An image is a matrix of pixels, where each pixel has three values (Red, Green, Blue) in color images. To extract RGB values, we access pixel values from the image matrix.
Code Implementation:
# Accessing RGB values at a specific pixel
(x, y) = (50, 100) # Specify coordinates
(b, g, r) = image[y, x] # OpenCV uses BGR instead of RGB
print(f"Pixel at ({x}, {y}) -> R: {r}, G: {g}, B: {b}")
3. Resizing and Rotating an Image
- Resizing changes the dimensions of the image. This can be useful for scaling an image to a desired size.
- Rotation involves rotating the image by a specified angle around its center.
Code Implementation:
# Resizing the image
resized_image = cv2.resize(image, (200, 200)) # Resize to 200x200 pixels
# Rotating the image by 45 degrees
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
rotation_matrix = cv2.getRotationMatrix2D(center, 45, 1.0) # Angle and scale
rotated_image = cv2.warpAffine(image, rotation_matrix, (w, h))
# Display resized and rotated images
cv2.imshow('Resized Image', resized_image)
cv2.imshow('Rotated Image', rotated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
4. Changing the Color Space of an Image
Changing the color space involves converting an image from one format (e.g., BGR) to another (e.g., HSV, Grayscale). It is useful in tasks like object detection and image filtering.
Code Implementation:
# Convert BGR to HSV
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Convert BGR to Grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('HSV Image', hsv_image)
cv2.imshow('Grayscale Image', gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
5. Smoothing of Images
Smoothing reduces noise and sharp variations in the image. Techniques include Gaussian blur, median blur, and bilateral filtering.
Code Implementation:
# Apply Gaussian Blur
gaussian_blur = cv2.GaussianBlur(image, (5, 5), 0)
# Apply Median Blur
median_blur = cv2.medianBlur(image, 5)
# Display smoothed images
cv2.imshow('Gaussian Blur', gaussian_blur)
cv2.imshow('Median Blur', median_blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
6. Extracting Foreground
Foreground extraction involves isolating the main object(s) in an image from the background. This can be achieved using methods like grabCut.
Code Implementation:
import numpy as np
mask = np.zeros(image.shape[:2], np.uint8)
bgd_model = np.zeros((1, 65), np.float64)
fgd_model = np.zeros((1, 65), np.float64)
# Define a rectangle containing the foreground
rect = (50, 50, 200, 200)
cv2.grabCut(image, mask, rect, bgd_model, fgd_model, 5, cv2.GC_INIT_WITH_RECT)
# Modify mask to extract the foreground
mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')
foreground = image * mask2[:, :, np.newaxis]
cv2.imshow('Foreground', foreground)
cv2.waitKey(0)
cv2.destroyAllWindows()
7. Image Segmentation
Image segmentation divides an image into regions based on color, texture, or other features. Techniques include thresholding, k-means clustering, and watershed.
Code Implementation:
# Convert to grayscale and apply thresholding
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)
# Apply Watershed Algorithm
markers = cv2.connectedComponents(thresh)[1]
markers = cv2.watershed(image, markers)
# Display segmentation result
segmented_image = cv2.applyColorMap((markers * 10).astype('uint8'), cv2.COLORMAP_JET)
cv2.imshow('Segmented Image', segmented_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Follow this link to know more about OpenCV.
Pillow()
Pillow(), also known as Python Image Library (PIL), is one of the most potent image processing libraries.
In PIL, we have a library called Image, which can carry out various operations like resizing, rotating the image, etc.
Like OpenCV, Pillow() can be used for various functions like
- Load an image - using the open() function.
- Display an image - using the show() function.
- Know the format of an image - using the format() function.
- Save the image after processing in png format - using the save() function.
- Resize the image - using the resize() function.
- Crop the image into desired dimensions - using the crop() function.
- Know the size of the image - using the size attribute.
- Know about the pixel - using the mode attribute.
- Flip the image - using the transform() function.
Follow this link to know more about the Pillow() library.
You can also practice with the help of Online Python Compiler
Frequently Asked Questions
What is image processing?
Image Processing is the method in which we perform operations on an image to extract useful information from the image or get an enhanced image.
What is the best image processing library in Python?
OpenCV, Pillow(), and Scikit-image are among Python’s best image processing libraries.
Why do we need image processing?
We need image processing as it helps us extract useful information from images. For example, image processing can be used to identify thieves quickly compared to identifying thieves using just the textual descriptions of the person.
Which is better for image processing, Python or MATLAB?
Python is faster than MATLAB, which is a great advantage. For example, OpenCV libraries are much faster in Python than MATLAB as MATLAB has a lot of wrappers that consume time.
What is DN in pixel?
Every pixel consists of a numeric value known as Digital Number, which stores the electromagnetic energy measured for the ground resolution cell.
Conclusion
This article discussed Image Processing with Python, OpenCV, and Pillow.
We hope this blog has helped you enhance your knowledge regarding image Processing with Python and if you would like to learn more, check out our free content on NLP and more unique courses.