Python Image Processing: OpenCV and scikit-image Libraries
Python is a versatile programming language that can be used for various tasks, including image processing. When it comes to image processing in Python, two popular libraries that come to mind are OpenCV and scikit-image. In this blog post, we will explore these two libraries and how they can be used for image processing in Python.
What is OpenCV?
OpenCV, short for Open Source Computer Vision Library, is an open-source computer vision and machine learning software library. It was developed by Intel and is now maintained by a community of developers. OpenCV has over 2,500 optimized algorithms, including a wide range of image and video processing functions.
OpenCV is written in C++ and has a Python interface. It can be used on various platforms, including Windows, Linux, macOS, Android, and iOS. OpenCV provides functions for image and video capture, image processing, object detection, and machine learning.
What is scikit-image?
scikit-image, also known as skimage, is an open-source image processing library for Python. It is built on top of NumPy, SciPy, and matplotlib, and provides a range of image processing functions. scikit-image has a simple and easy-to-use interface, making it suitable for beginners and advanced users.
scikit-image is written in Python and is compatible with various platforms, including Windows, Linux, and macOS. It provides functions for image filtering, segmentation, feature extraction, and morphological operations.
Installing OpenCV and scikit-image
To use OpenCV and scikit-image, you need to install them. You can install them using pip, a package manager for Python. To install OpenCV, run the following command:
pip install opencv-python
To install scikit-image, run the following command:
pip install scikit-image
Once you have installed these libraries, you can start using them for image processing in Python.
Image Processing with OpenCV
OpenCV provides a wide range of functions for image processing. Let's look at some of the basic functions for reading, displaying, and saving images.
Reading and Displaying Images
To read an image using OpenCV, use the cv2.imread()
function. This function takes the path of the image file as a parameter and returns a NumPy array representing the image.
import cv2
# read an image
img = cv2.imread('image.jpg')
# display the image
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
In the above example, we read an image file named 'image.jpg' using the cv2.imread()
function. We then displayed the image using the cv2.imshow()
function. The cv2.waitKey()
function waits for a key event, and the cv2.destroyAllWindows()
function destroys all windows created by the program.
Saving Images
To save an image using OpenCV, use the cv2.imwrite()
function. This function takes the path of the image file and the NumPy array representing the image as parameters.
import cv2
# read an image
img = cv2.imread('image.jpg')
# save the image
cv2.imwrite('new_image.jpg', img)
In the above example, we read an image file named 'image.jpg' using the cv2.imread()
function. We then saved the image using the cv2.imwrite()
function.
Image Filtering
OpenCV provides functions for image filtering, which can be used to remove noise and enhance the image. The cv2.filter2D()
function can be used to apply a kernel to an image.
import cv2
import numpy as np
# read an image
img = cv2.imread('image.jpg')
# define the kernel
kernel = np.ones((5,5),np.float32)/25
# apply the kernel to the image
dst = cv2.filter2D(img,-1,kernel)
# display the original and filtered images
cv2.imshow('Original', img)
cv2.imshow('Filtered', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
In the above example, we read an image file named 'image.jpg' using the cv2.imread()
function. We then defined a kernel of size 5x5 with all values set to 1, which is then divided by 25 to normalize the values. We applied the kernel to the image using the cv2.filter2D()
function and displayed the original and filtered images using the cv2.imshow()
function.
Image Thresholding
Image thresholding is a technique used to separate objects from the background in an image. OpenCV provides functions for image thresholding, which can be used to create binary images.
import cv2
# read an image
img = cv2.imread('image.jpg', 0)
# apply thresholding
ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
# display the original and thresholded images
cv2.imshow('Original', img)
cv2.imshow('Thresholded', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
In the above example, we read an image file named 'image.jpg' using the cv2.imread()
function and converted it to grayscale using the 0
argument. We then applied thresholding using the cv2.threshold()
function, which takes the image, threshold value, maximum value, and thresholding type as parameters. We displayed the original and thresholded images using the cv2.imshow()
function.
Image Processing with scikit-image
scikit-image provides a range of functions for image processing. Let's look at some of the basic functions for reading, displaying, and saving images.
Reading and Displaying Images
To read an image using scikit-image, use the skimage.io.imread()
function. This function takes the path of the image file as a parameter and returns a NumPy array representing the image.
import skimage.io
# read an image
img = skimage.io.imread('image.jpg')
# display the image
skimage.io.imshow(img)
skimage.io.show()
In the above example, we read an image file named 'image.jpg' using the skimage.io.imread()
function. We then displayed the image using the skimage.io.imshow()
and skimage.io.show()
functions.
Saving Images
To save an image using scikit-image, use the skimage.io.imsave()
function. This function takes the path of the image file and the NumPy array representing the image as parameters.
import skimage.io
# read an image
img = skimage.io.imread('image.jpg')
# save the image
skimage.io.imsave('new_image.jpg', img)
In the above example, we read an image file named 'image.jpg' using the skimage.io.imread()
function. We then saved the image using the skimage.io.imsave()
function.
Image Filtering
scikit-image provides functions for image filtering, which can be used to remove noise and enhance the image. The skimage filters
module contains various functions for filtering an image. Let's take a look at how to apply a Gaussian filter to an image using the gaussian()
function.
import skimage.io
from skimage import filters
# read an image
img = skimage.io.imread('image.jpg')
# apply Gaussian filter
gaussian_img = filters.gaussian(img, sigma=2)
# display the original and filtered images
skimage.io.imshow(img)
skimage.io.show()
skimage.io.imshow(gaussian_img)
skimage.io.show()
In the above example, we read an image file named 'image.jpg' using the skimage.io.imread()
function. We then applied a Gaussian filter to the image using the filters.gaussian()
function and displayed the original and filtered images using the skimage.io.imshow()
and skimage.io.show()
functions.
Image Thresholding
scikit-image provides functions for image thresholding, similar to OpenCV. Let's take a look at how to apply a binary threshold to an image using the threshold_otsu()
function.
import skimage.io
from skimage.filters import threshold_otsu
# read an image
img = skimage.io.imread('image.jpg', as_gray=True)
# apply binary thresholding
thresh = threshold_otsu(img)
binary = img > thresh
# display the original and thresholded images
skimage.io.imshow(img)
skimage.io.show()
skimage.io.imshow(binary)
skimage.io.show()
In the above example, we read an image file named 'image.jpg' using the skimage.io.imread()
function and converted it to grayscale using the as_gray=True
argument. We then applied binary thresholding to the image using the threshold_otsu()
function and displayed the original and thresholded images using the skimage.io.imshow()
and skimage.io.show()
functions.
Edge Detection
scikit-image provides functions for edge detection, which can be used to detect edges in an image. Let's take a look at how to apply the Canny edge detection algorithm to an image using the feature.canny()
function.
import skimage.io
from skimage import feature
# read an image
img = skimage.io.imread('image.jpg', as_gray=True)
# apply Canny edge detection
edges = feature.canny(img, sigma=2)
# display the original and edge detected images
skimage.io.imshow(img)
skimage.io.show()
skimage.io.imshow(edges)
skimage.io.show()
In the above example, we read an image file named 'image.jpg' using the skimage.io.imread()
function and converted it to grayscale using the as_gray=True
argument. We then applied the Canny edge detection algorithm to the image using the feature.canny()
function and displayed the original and edge detected images using the skimage.io.imshow()
and skimage.io.show()
functions.
Conclusion
Python has a vast number of image processing libraries, but OpenCV and scikit-image are two of the most popular ones. OpenCV is a powerful library that provides functions for image processing, computer vision, and machine learning. It is widely used in industry and academia for a wide range of applications, including image and video processing, face recognition, and object detection. On the other hand, scikit-image is a lightweight library that provides a range of functions for image processing. It is particularly useful for scientific computing and research applications. In this blog, we have looked at some of the basic functions for image processing using OpenCV and scikit-image, including image filtering, thresholding, and edge detection.
You may also like
Python Image Processing with Opencv and Libraries
Python Image Processing - Get a popular programming language for ima...
Continue readingBuilding a Basic Image Processing Tool with Python and OpenCV
In this blog, we explore how to build a basic image processing tool ...
Continue readingPython for Image Processing: Manipulating and Analyzing Images
This blog post explores the use of Python for image processing, focu...
Continue reading