Image processing in Python can be an incredibly useful skill, ranging from simple tasks like converting image formats, cropping, and filtering, to more complex applications including machine learning. This article serves as a basic introduction to working with image data using two popular Python packages: OpenCV and Matplotlib. By the end, you'll have a solid understanding of how image data is structured and some of the possibilities that come with using Python for image manipulation.
To kick things off, make sure you have a Kaggle account, as this allows you to easily follow along with the code in the provided notebook. For our demonstration, we will be using a dataset containing images of cats and dogs.
First, we need to import the necessary libraries. For data handling, we will use Pandas, and NumPy. To list the files of our images, we’ll import the Glob library. Additionally, we will import the image processing libraries OpenCV (cv2
) and Matplotlib:
import pandas as pd
import numpy as np
import glob
import cv2
import matplotlib.pyplot as plt
Now, we can use Glob to list all the .jpg files in our cat and dog directories:
dog_files = glob.glob("path_to_dogs/*.jpg")
cat_files = glob.glob("path_to_cats/*.jpg")
Let’s read an example cat image using both Matplotlib and OpenCV:
image_mpl = plt.imread(cat_files[1856])
image_cv2 = cv2.imread(cat_files[20])
In both cases, the image file is read and stored as a NumPy array. The shape of this array is typically three-dimensional, representing height, width, and color channels.
Understanding the structure of the image data is critical. The array comprises three dimensions:
The values in this array correspond to the intensity of each pixel, which are generally in the range of 0 to 255. As an example, you could flatten this array into a one-dimensional array and visualize these pixel values using a histogram.
We can display the image using Matplotlib’s imshow
, and adjust our plot to remove the axis for a cleaner view:
plt.imshow(image_mpl)
plt.axis('off')
plt.show()
Each pixel's colors are a combination of the three channels. The difference between Matplotlib and OpenCV lies in how these channels are ordered; OpenCV uses BGR (Blue, Green, Red) instead of RGB.
Now, we can explore some basic manipulations using OpenCV.
Grayscale Conversion: We can convert a colored image into grayscale:
image_gray = cv2.cvtColor(image_cv2, cv2.COLOR_BGR2GRAY)
Resizing Images: OpenCV offers functionality to resize images. For instance, to shrink an image to 25% of its original size:
image_resized = cv2.resize(image_cv2, None, fx=0.25, fy=0.25)
Filtering: We can apply filters to enhance or blur images. For sharpening:
sharpen_kernel = np.array([[0, -1, 0], [-1, 5,-1], [0, -1, 0]])
image_sharpened = cv2.filter2D(image_cv2, -1, sharpen_kernel)
Blurring: To blur the image:
image_blurred = cv2.GaussianBlur(image_cv2, (9, 9), 0)
Once you’ve manipulated your image, you can save it using either:
OpenCV:
cv2.imwrite('processed_image.png', image_blurred)
Matplotlib:
plt.imsave('processed_image.png', image_mpl)
Success! You now have the basics of image processing down using Python's OpenCV and Matplotlib.
Image Processing, OpenCV, Python, Matplotlib, Image Manipulation, Grayscale Conversion, Resizing, Filtering, Histogram, RGB
1. What are the key libraries used for image processing in Python?
The primary libraries discussed are OpenCV (cv2) and Matplotlib.
2. How do RGB and BGR differ in image processing?
RGB (Red, Green, Blue) is the color order used by Matplotlib, while OpenCV uses BGR (Blue, Green, Red). This affects how images are displayed when read via these two libraries.
3. How can I convert an image to grayscale using OpenCV?
Use the cv2.cvtColor()
function with the cv2.COLOR_BGR2GRAY
parameter.
4. What methods can I use to resize images?
You can use cv2.resize()
, providing either percentage adjustments (fx and fy) or specific dimensions.
5. How do I save a processed image?
You can save images using cv2.imwrite()
for OpenCV or plt.imsave()
for Matplotlib.
In addition to the incredible tools mentioned above, for those looking to elevate their video creation process even further, Topview.ai stands out as a revolutionary online AI video editor.
TopView.ai provides two powerful tools to help you make ads video in one click.
Materials to Video: you can upload your raw footage or pictures, TopView.ai will edit video based on media you uploaded for you.
Link to Video: you can paste an E-Commerce product link, TopView.ai will generate a video for you.