Hey coders! I am sure you have implemented multiple image processing libraries. But were you aware that a python always has some tricks in its pocket?! It has another computer vision and image processing library known as
The library is implemented in
C++ and operates on NumPy arrays, which makes it much quicker. Currently, it contains
around 100 computer vision and image processing functionalities.
Also Read: 5 Ways To Display Images in Python
In this tutorial, we will start off with some simple image operations using the Mahotas library. Before we move ahead we will start off by installing the library using the
pip command. The same can be done using the command shown below.
pip install mahotas
After the installation of the libraries, we will go ahead and import the libraries into the program. We will import
pylab for image display-related functionalities.
import mahotas from pylab import imshow, show
You can use any image for image processing. For this tutorial, we will be using a random image from the internet. We will use
mahotas.imread to load the image and
imshow function to display the image.
img = mahotas.imread('sample.jpg') imshow(img) show()
mean of an image is used for a number of purposes, including noise reduction in image processing. Mahotas is able to compute the mean of an image but is limited to one channel at a time, as our image is colored we will reduce it to one channel at a time.
img0 = img[:,:,0] img1 = img[:,:,1] img2 = img[:,:,2] mean0 = img0.mean() mean1 = img1.mean() mean2 = img2.mean() print("Mean Value for the channel 1 is ", mean0) print("Mean Value for the channel 2 is ", mean1) print("Mean Value for the channel 3 is ", mean2)
The image that I am using has a total of 3 channels. So I am computing 3 mean values for the 3 channels, respectively.
Mean Value for the channel 1 is 195.63318904447684 Mean Value for the channel 2 is 172.86992779952305 Mean Value for the channel 3 is 172.8701535539508
Using the library, we can also crop the image using the code snippet below. You can mention the starting and ending pixels to any pixel values that you want.
I will first try to get the bulb and the hand in focus, which kind of lies between the 1000 and 3000-pixel values on the x-axis.
img2 = img[:, 1000:3000] imshow(img2) show()
Let’s not try to crop the image even further with cropping along the y axis as well to focus just on the bulb. Look at the code snippet below. We have chosen the values just like before from the x and y-axis in the plot of the full image.
img2 = img[1000:2500, 1300:2300] imshow(img2) show()
roundness is the measure of how similar/close the object in the image is to forming a perfect round circle. Let’s look at two different values for the same, one for the full image and the other for the image which has a bulb focused on it. The differences in the values are quite evident.
img1 = img[::,1] r1 = mahotas.features.roundness(img1) r2 = mahotas.features.roundness(img2) print("Roundness of the full image : ", r1) print("Roundness of the bulb focused image : ", r2)
The output that came out of the code is as follows:
Roundness of the full image : 0.0 Roundness of the bulb focused image : 0.0009273648133338048
local maxima of the image are the areas that are recognized as the
local peaks in the image. They can be displayed using the following code lines.
img2 = img.max(1) lmaxim = mahotas.locmax(img) imshow(lmaxim) show()
We can have the grayscale version of the image with the help of the
Overlay image like in the code snippet below.
img = img[:, :, ] ol = mahotas.overlay(img) imshow(ol) show()
In this tutorial, we understood and learned how developers can use mahotas for image processing with the help of some basic functions. There are many more functions available in the library. Stay tuned for more!