Image Processing with Python
Image Processing
Image processing defines a method that relates to the manipulation of an image. The process determines a set of operations on an image that result in image enhancement or extraction of useful information. Performing image processing proves helpful in finding out various aspects and patterns present in the image. Pattern recognition systems lead to handwriting analysis, image recognition, computer-aided medical diagnosis, robotics, self-driving cars, object detection, and many others. A 2D function F(x,y) usually represents an image. Here x and y are mapping coordinates, whereas F determines the intensity of an image at that point.
Image Processing and Python
The developer community mainly uses Python for image processing. Python possesses a range of useful libraries and tools to help in effective image manipulation and extract helpful insight. There are numerous classical algorithms, tools, and techniques which help in efficient image processing. Python helps in the processing of raw data to generate classifiers and acquire the most accurate output.
Python Libraries For Image Processing
Python helps in providing numerous valuable libraries for the sake of image processing. Following are some of the Python libraries for this purpose:
- OpenCV: It is an image processing library that emphasizes real-time computer vision. It is an open-source library that deals with applications like 2D and 3D feature toolkits, gesture recognition, facial recognition system, robotics, human-computer interaction, object identification, and many more. OpenCV also supports multiple languages, which include Python, Java, and C++.
- Numpy and Scipy Library: These are two of the most important libraries in Python which help in numerical and scientific operations. Image manipulation and processing become more manageable with these libraries.
- Scikit: This library provides various algorithms like support vector machine, k neighbors, random forest, and many others.
- Python Imaging Library (PIL): PIL or Pillow is an open-source python library that helps in image processing tasks and provides functionalities such as filtering, opening, saving, manipulating images, creating thumbnails, resizing, rotation, conversion, and many others.
Example
Here is an example of using Python for image processing. In this example, the main goal is to count the number of points in the given image, similar to counting stars in the sky. A simple example to make the system count stars in the images of the sky using step by step guide is the following:
Importing Libraries
It is crucial to import all the relevant libraries to perform complex tasks. The skimage package enables developers to perform image processing using Python. Following are the essential libraries to import for such purpose:
from matplotlib import pyplot as plt from skimage import data from skimage.feature import blob_dog, blob_log, blob_doh from math import sqrt from skimage.color import rgb2gray import glob from skimage.io import imread
Importing the Image
The next step is to import the image file in Python. In this example, the algorithm imports the image in grayscale, which means that each pixel is a shade of grey. Moreover, each pixel later becomes a cell in the matrix. For this example, there is a matrix of 480*581 pixels. Following is the code to import the image file in Python:
example_file = glob.glob(r"C:Desktop_sky.gif")[0] img = imread(example_file, as_grey=True) plt.imshow(img, cmap=cm.gray) plt.show()
Finding the Number of Points
Following is an example of a code that helps in searching for the objects in the picture. Here the variable blobs_log provides three outputs for every object the algorithm finds. The first two outputs are the object’s coordinates, and the third output is the area of the object. The area column helps in determining the radius of the object. The output shows a counter for 308 blobs in the picture.
blobs_log = blob_log(img, max_sigma=30, num_sigma=10, threshold=.1) blobs_log[:, 2] = blobs_log[:, 2] * sqrt(2) numrows = len(blobs_log) print("Number of blobs : " ,numrows)
Validation of the Output
The final step is to verify the output and validate if the algorithm successfully found all the points in the image. For this purpose, the algorithm circles each estimated star position and searches if any stars are missing. Following is the code to achieve this goal:
fig, ax = plt.subplots(1, 1) plt.imshow(img, cmap=cm.gray) for blob in blobs_log: y, x, r = blob c = plt.Circle((x, y), r+5, color='lime', linewidth=2, fill=False) ax.add_patch(c)
Other useful articles:
- OOP in Python
- Python v2 vs Python v3
- Variables, Data Types, and Syntaxes in Python
- Operators, Booleans, and Tuples
- Loops and Statements in Python
- Python Functions and Modules
- Regular Expressions in Python
- Python Interfaces
- JSON Data and Python
- Pip and its Uses in Python
- File Handling in Python
- Searching and Sorting Algorithms in Python
- System Programming (Pipes &Threads etc.)
- Database Programming in Python
- Debugging with Assertion in Python
- Sockets in Python
- InterOp in Python
- Exception Handling in Python
- Environments in Python
- Foundation of Data Science
- Reinforcement Learning
- Python for AI
- Applied Text Mining in Python
- Python Iterations using Libraries
- NumPy vs SciPy
- Python Array Indexing and Slicing
- PyGame
- PyTorch
- Python & Libraries
- Python with MySQL
- Python with MongoDB
- Path Planning Algorithm in Python
- Image Processing with Python