Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 28, 2022 01:25 am

Image Processing UsingPython


In a 1911 newspaper article, the editor Tess Flanders was quoted as saying:


Use a picture. It's worth a thousand words.

It is thus no doubt that pictures play an important part in our communications—not just general photographs, but also specialized images like MRIs or ultrasounds.


We can obtain photos through different acquisition devices. For instance, melanoma (skin cancer) images are retrieved using a dermatoscope. We take photos of ourselves or friends using a digital camera or a smartphone. Sometimes, however, we notice some issues in our pictures, like blurring for instance, which may be due to the acquisition device used.


But, what to do in this case? You were sent some medical images to analyze, and you don't have the choice of retaking such images. Even if you retook an image, the resolution you see will not change, nor any other issues you face. Image processing comes into play in such situations.



image processing: The analysis and manipulation of a digitized image, especially in order to improve its quality. — Oxford Dictionaries

"Digitized image" here refers to the fact that the image is processed by a computer. Getting the computer in this game means using a programming language.


In this tutorial I will show you how we can use the Python programming language to perform image processing tasks on an image.


scikit-image


The library we are going to use in order to carry out our image processing tasks is scikit-image. According to the paper scikit-image: image processing in Python:


scikit-image is an image processing library that implements algorithms and utilities for use in research, education and industry applications. It is released under the liberal Modified BSD open source license, provides a well-documented API in the Python programming language, and is developed by an active, international team of collaborators.

The first thing we need to do is install scikit-image. Instructions for installing the library can be found on the download page, and in this tutorial I will show you how to install the library on a Mac OS X machine, as this is what I'm currently using in writing this tutorial.


As scikit-image is an external library, the first thing we have to do is install that library. For that, I will be using pip, which is (based on Wikipedia):


A package management system used to install and manage software packages written in Python. Many packages can be found in the Python Package Index (PyPI).

You can follow the steps mentioned in the Python Packaging User Guide for installing pip, but if you have Python 2.7.9 and higher, or Python 3.4 and higher, you already have pip!


scikit-image now can be simply installed by typing the following command:


pip install scikit-image


We now have the library installed and ready for some image processing fun!


The test image we will be using in this tutorial is a pizzeria illustration. Go ahead and download it, or simply use the image of your choice. The image looks as follows:


Original ImageOriginal ImageOriginal Image

Dimensions of an Image


Sometimes we need to know the dimensions of an image (more on that in the filtering section). Once we have loaded the image into our memory from a file using the imread() method, we can easily get the image dimensions with the help of shape attribute.


The reason this technique works is because images in the scikit-image module are represented by NumPy arrays. Here is an example:



The shape attribute gives us a tuple where the first element is the height of the image, the second element is the width of the image and the third element represents the number of channels. In our case, the baboon.png image has three channels for red, green and blue values so we got the value 3.


Here is an example which loads another image:



You can also load your images as grayscale by setting the value of second parameter as_gray in the imread() function to be False. The size attribute tells us the number of elements in the array. In case of grayscale images, this value is equal to the number of pixels in the image. Here is an example:



Manipulating Individual Pixels


You can easily modify individual pixels of any images loaded using the scikit-image library. There are few conventions that should be kept in mind.


When directly accessing pixels of an image, the first value indicates the row number and the second values indicates the column number. The origin or the position that corresponds to img[0, 0] is the top-left corner of the image. You can make the pixel at the 200th row and 200th column blue by using the line img[200, 200] = [0, 0,  255].


It is also possible to modify a set of pixels together. Here is an example that adds a red border to our image.



This is the result:


Red Border ImageRed Border ImageRed Border Image

Color to Grayscale


In this section, we would like to convert the original colored Pizzeria image into a grayscale 2D image (black and white). This can be simply done using the following script:



We simply passed as_gray as True to the imread() method that we learned about in the previous section.


The imsave() method accepts a file name and the image array as its first and second parameters. By default, the method also checks if the image you are saving has low contrast and warns you if that's the case.


Another way to make an image grayscale is with the help of rgb2gray() method from the color module. We simply pass an array that represents our image as the first parameter. The output gives us a new array that represents the grayscale image. The final luminance calculations are done by using the following weights for different channels.



Here is the Python code that creates the grayscale image:



In order to show the new grayscale image, add the following to the end of the script:



The result looks like this:


Grayscale ImageGrayscale ImageGrayscale Image

Applying a Filter to an Image


In image processing, filtering is performed to make some enhancements in the image. In general, filtering encompasses the following operations: edge enhancement, sharpening, and smoothing.


In this section, I'm going to show you how we can apply the Sobel filter on our image, and see what the output looks like after performing such an operation.


The script for applying the Sobel filter on our image looks as follows:



You will most probably get a warning while trying to execute the above script. We couldn't apply the operation since the image has to be a 2D image. One solution to this problem is the use of second parameter and set as_gray to True. The output of this operation looks as follows:


Sobel FilterSobel FilterSobel Filter

There are many other filters that you can apply such as the gaussian filter for blurring. It accepts many parameter with the first one being the source image and the second one being the standard deviation for the gaussian filter. You can either pass a single value or a sequence of values (one for each axis). Here are two examples:



Here is the result of applying the Gaussian filter with a standard deviation of 10 to the Pizzeria image:


Pizzeria Image Gaussian FilterPizzeria Image Gaussian FilterPizzeria Image Gaussian Filter

Here is the result of applying the Gaussian filter with a standard deviation of 20 and 1 for the vertical and horizontal axes:


Gaussian Filter Different SigmaGaussian Filter Different SigmaGaussian Filter Different Sigma

Now, let's see how we can apply the threshold filter to our image. First, we calculate the threshold value based on the mean of all the grayscale values in our image by using the threshold_mean() method. After that, we binarize our image and set pixels as True or False depending on whether they are above the threshold or not. This binary image is then converted to 8-bit uint data by using the img_as_ubyte() method.



The above code produces the following result for our image:


Threshold FilterThreshold FilterThreshold Filter

Conclusion


There are many image processing operations, and the scikit-image Python library provides us with many interesting operations we can perform on our images. You can see more image processing operations using this library on the scikit-image website.


Learn Python


Learn Python with our complete python tutorial guide, whether you're just getting started or you're a seasoned coder looking to learn new skills.



Original Link: https://code.tutsplus.com/tutorials/image-processing-using-python--cms-25772

Share this article:    Share on Facebook
View Full Article

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code