collect! 10 Python libraries for image processing

This is the 888th technical sharing of “Attack Coder”

Source: Python Programming Time

This article should take approximately 7 minutes to read.

In this article, we will sort out the Python libraries commonly used in computer vision projects. If you want to enter the field of computer vision, you can first understand the libraries introduced in this article, which will be very helpful for your work.

1. PIL/Pillow

Pillow is a versatile and user-friendly Python library that provides a rich set of functions and support for various image formats, making it an essential tool for developers working with images in their projects.

It supports opening, manipulating, and saving many different image file formats, and users can also perform basic operations on images such as cropping, resizing, rotating, and changing image color.

Pillow also lets you add text and shapes to images, providing an easy way to annotate your visuals.

This library is also the image processing library used by torchvison. It is powerful and easy to use. It is recommended to use.



2. OpenCV (Open Source Computer Vision Library)

OpenCV is undoubtedly one of the most popular image processing libraries. It was originally developed by Intel Corporation and has been widely used in the field of computer vision. It supports countless algorithms related to computer vision and machine learning, which help understand visual data and make insightful decisions. OpenCV is also highly optimized for real-time applications, making it an excellent choice for video surveillance, self-driving cars, and advanced robotics.

OpenCV has the most functions and is faster than Pillow in terms of processing speed, so it is recommended to use it when speed is required.

Another point is that the channel read by OpenCV is BGR, while other libraries are all RGB, so if they are mixed, they need to be converted. Remember this code:

cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

3. Mahotas

Mahotas includes a set of functions for image processing and computer vision, which are mostly done in high-performance C++ and use multi-threading, making it very fast.

It also includes various morphological operations such as erosion, dilation and connected component analysis. These operations are the basis for tasks such as image binarization, denoising, and shape analysis. OpenCV has these functions, but Mahotas focuses more on image processing, and does not have everything like OpenCV. Therefore, Mahotas’s API is simpler and more friendly. And it is easier to learn than OpenCV, but the speed is almost the same.

example

Here is a simple example (using the example files included with mahotas) calling the watershed using the above threshold area as a seed (we used Otsu to define the threshold).

# import using ``mh`` abbreviation which is common:
import mahotas as mh

# Load one of the demo images
im = mh.demos.load('nuclear')

# Automatically compute a threshold
T_otsu = mh.thresholding.otsu(im)

# Label the thresholded image (thresholding is done with numpy operations
seeds,nr_regions = mh.label(im > T_otsu)

# Call seeded watershed to expand the threshold
labeled = mh.cwatershed(im.max() - im, seeds)

Here is a very simple example using mahotas.distance (compute distance map):

import pylab as p
import numpy as np
import mahotas as mh

f = np.ones((256,256), bool)
f[200:,240:] = False
f[128:144,32:48] = False
# f is basically True with the exception of two islands: one in the lower-right
# corner, another, middle-left

dmap = mh.distance(f)
p.imshow(dmap)
p.show()

4. Scikit-Image

Scikit-Image builds on the Scikit-Learn machine learning library with extended functionality, including more advanced image processing capabilities. So if you are already using Scikit for ML, consider using this library.

It provides a complete set of image processing algorithms. It supports image segmentation, geometric transformations, color space operations and filtering.

Unlike many other libraries, Scikit-Image supports multidimensional images, which is helpful for tasks involving video or medical imaging. Scikit-Image integrates seamlessly with other Python scientific libraries such as NumPy and SciPy.

from skimage import data, io, filters

image = data.coins()
# ... or any other NumPy array!
edges = filters.sobel(image)
io.imshow(edges)
io.show()

0a5500683f43ae356177b1467fdd8325.png

5. TensorFlow Image

TensorFlow Image is a module of TensorFlow that supports image decoding, encoding, cropping, resizing, and conversion. You can also take advantage of TensorFlow’s GPU support to provide faster image processing for larger data sets.

This means that if you use TF, you can use it as part of a training pipeline.

Load data using Keras utility functions: tf.keras.utils.image_dataset_from_directory utility functions load images from disk.

Create a dataset and define some parameters for the loader:

batch_size = 32
img_height = 180
img_width = 180

When developing a model, it is best to use validation splits. You will use 80% of the images for training and 20% for validation.

train_ds = tf.keras.utils.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

6. PyTorch Vision

Similar to TensorFlow Image, PyTorch Vision is part of the PyTorch ecosystem and is primarily used for machine learning tasks related to image processing.

import torchvision
video_path = "path to a test video"
reader = torchvision.io.VideoReader(video_path, "video")
reader_md = reader.get_metadata()
print(reader_md["video"]["fps"])
video.set_current_stream("video:0")

7. SimpleCV

SimpleCV is built on OpenCV, PIL (Python Imaging Library) and NumPy, providing users with a simple yet powerful set of functions and tools for loading, processing and analyzing images.

SimpleCV is designed to make computer vision technology more reliable and accessible to beginners and non-experts. It provides a simple API that hides the underlying complexity, allowing users to quickly implement common computer vision tasks.

However, there is currently little official maintenance, so this project is likely to die.

import SimpleCV
camera = SimpleCV.Camera()
image = camera.getImage()
image.show()

8、Imageio

Imageio is a Python library for reading and writing various image formats. It provides a simple yet powerful API that enables users to easily process image and video data. Imageio provides a common data model that enables users to store image data in a variety of ways. It can represent image data using NumPy arrays, PIL image objects, or simple Python byte strings. And it provides the function of reading and writing video files frame by frame, which is very useful for processing video streams or extracting frames from videos.



import imageio.v3 as iio
im = iio.imread('imageio:chelsea.png') # read a standard image
im.shape # im is a NumPy array of shape (300, 451, 3)
iio.imwrite('chelsea.jpg', im) # convert to jpg

9.albumentations

Albumentations is a Python library for image enhancement and data augmentation. It focuses on providing efficient, flexible and easy-to-use data augmentation methods in machine learning and computer vision tasks.

I have always regarded this library as an alternative to torchvision, because it not only has many data augmentation methods, but also can directly handle the enhancement of masked bboxes.



import albumentations as A
import cv2

# Declare an augmentation pipeline
transform = A.Compose([
    A.RandomCrop(width=256, height=256),
    A.HorizontalFlip(p=0.5),
    A.RandomBrightnessContrast(p=0.2),
])

# Read an image with OpenCV and convert it to the RGB colorspace
image = cv2.imread("image.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Augment an image
transformed = transform(image=image)
transformed_image = transformed["image"]

10, timm

timm is a PyTorch model library. Although it may not have anything to do with image processing, it provides a wide range of pre-trained models and computer vision models, which is very helpful when we perform deep learning. Now it is a sub-project of huggingface, which means that the project has financial support, so there is no need to worry about development issues.



import timm
import torch

model = timm.create_model('resnet34')
x = torch.randn(1, 3, 224, 224)
model(x).shape

Summary

Whether you are just getting started with basic image processing or exploring advanced machine learning models, these libraries provide the necessary tools for a wide range of image processing tasks.

f71629623f27ea46a15ab8ada7ed46ae.png

End

Welcome everyone to join [ChatGPT & AI Monetization Circle] and master AI artifacts with zero threshold! We will take you from novice to master, unlocking the infinite possibilities of intelligent Q&A, automated creation, and technology monetization. Grow with us and start a new journey in AI! Act now, the future is already here! (For details, please click: Knowledge Planet: ChatGPT & AI Monetization Circle, officially launched!)

Scan the QR code to join:

c57cbb2d979487853fb1acddd32ccf47.jpeg

589f2ad2e703b8f7d3a78024c0597461.png

Click to see you are the best

outside_default.png

The knowledge points of the article match the official knowledge archive, and you can further learn relevant knowledge. Python entry skill treeArtificial intelligenceMachine learning toolkit Scikit-learn389189 people are learning the system