投稿日:2025年2月12日

Basic and applied practical course on image processing using Python

Introduction to Image Processing

Image processing is an essential technique used in various fields like computer vision, machine learning, and artificial intelligence.
It involves the manipulation of images to improve their quality or to extract important information.
Python, with its rich libraries and ease of use, is a popular programming language for image processing tasks.

In this article, we will explore the basics and applied concepts of image processing using Python.
We will also discuss some practical applications to help you understand how image processing can be a valuable tool in solving real-world problems.

Understanding Image Processing

Image processing refers to a set of techniques used to enhance, analyze, and manipulate images.
It involves operations that transform an image into another image to achieve desired results.

Some common image processing tasks include noise removal, image enhancement, edge detection, and image segmentation.
These tasks are performed to improve the visual quality of images or to prepare data for further analysis.

Types of Image Processing

There are two main types of image processing: analog and digital.
Analog image processing is used for hard copies, such as printed photographs.
Digital image processing involves manipulating images with a computer and is the focus of this article.
With digital image processing, images are converted into a set of numbers that correspond to pixels.

Python Libraries for Image Processing

Python offers a variety of libraries to perform image processing tasks effectively.
These libraries provide functions that make it easy to manipulate and analyze images.

OpenCV

OpenCV (Open Source Computer Vision Library) is an open-source library with a comprehensive set of tools for image and video processing.
It offers various algorithms for image manipulation, such as resizing, cropping, and rotating images.
OpenCV is frequently used in real-time applications and has support for a wide range of image formats.

PIL/Pillow

Pillow, the friendly fork of the Python Imaging Library (PIL), is a simple and easy-to-use library for image manipulation.
It supports opening, cropping, saving, and displaying various image formats.
Pillow is ideal for small tasks, such as image resizing, format conversion, and applying basic image effects.

scikit-image

scikit-image is a collection of algorithms for image processing in Python.
Built on top of SciPy, it provides tools for basic and advanced image processing tasks.
This library is designed for use in scientific computing and offers capabilities for tasks such as image filtering, segmentation, and transformation.

Basic Image Processing Operations

In this section, we will cover some fundamental image processing operations and how to perform them using Python libraries.

Reading and Displaying Images

The first step in image processing is reading an image file and displaying it.
Using OpenCV, you can read an image with the `cv2.imread()` function and display it using `cv2.imshow()`.

“`python
import cv2

# Read the image
image = cv2.imread(‘image.jpg’)

# Display the image
cv2.imshow(‘Displayed Image’, image)

# Wait for a key event, then close the window
cv2.waitKey(0)
cv2.destroyAllWindows()
“`

Image Resizing

Resizing is a common operation in image processing when you want to scale the image to a specific size.
In OpenCV, the `cv2.resize()` function lets you resize an image.

“`python
# Resize the image to 200×200 pixels
resized_image = cv2.resize(image, (200, 200))

# Display the resized image
cv2.imshow(‘Resized Image’, resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
“`

Image Rotation

Rotating an image is useful for aligning images or changing their orientation.
You can use the `cv2.getRotationMatrix2D()` and `cv2.warpAffine()` functions to rotate an image.

“`python
# Get the rotation matrix for a 45-degree rotation
rotation_matrix = cv2.getRotationMatrix2D((image.shape[1] / 2, image.shape[0] / 2), 45, 1)

# Rotate the image
rotated_image = cv2.warpAffine(image, rotation_matrix, (image.shape[1], image.shape[0]))

# Display the rotated image
cv2.imshow(‘Rotated Image’, rotated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
“`

Image Filtering

Image filtering is used to enhance images, such as sharpening or blurring.
This can be done using OpenCV’s filtering functions.

“`python
# Apply a Gaussian blur filter
blurred_image = cv2.GaussianBlur(image, (5, 5), 0)

# Display the blurred image
cv2.imshow(‘Blurred Image’, blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
“`

Advanced Image Processing Concepts

Once you are comfortable with basic image processing techniques, you can explore more advanced topics.

Edge Detection

Edge detection is an important image processing technique used to identify the boundaries within an image.
The Canny edge detection algorithm is a popular method implemented in the `cv2.Canny()` function.

“`python
# Perform Canny edge detection
edges = cv2.Canny(image, 100, 200)

# Display the edge-detected image
cv2.imshow(‘Edges’, edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
“`

Image Segmentation

Image segmentation is the process of dividing an image into different regions for easier analysis.
OpenCV provides several algorithms for segmentation, such as thresholding and contour detection.

“`python
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply thresholding
_, thresholded = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

# Display the segmented image
cv2.imshow(‘Segmented Image’, thresholded)
cv2.waitKey(0)
cv2.destroyAllWindows()
“`

Practical Applications of Image Processing

Image processing has countless practical applications in our daily lives.
Some of the common applications include:

– **Medical Imaging:** Enhancing X-rays, MRIs, and other medical scans for better diagnosis.
– **Facial Recognition:** Identifying and verifying individuals based on facial features.
– **Autonomous Vehicles:** Analyzing the surrounding environment to navigate safely.
– **Augmented Reality:** Overlaying computer-generated images on the real-world view.

Conclusion

Image processing is a powerful tool in the hands of developers and scientists.
Mastering its basics and practical applications can dramatically enhance your problem-solving capabilities.
With Python’s extensive libraries and resources, you can easily start experimenting and implementing image processing techniques in your projects.
Whether in academia or industry, the skills you gain will prove invaluable in a world increasingly reliant on digital imagery and machine vision.

You cannot copy content of this page