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

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.

“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 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!

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 the 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 the second parameter as_gray in the imread() function to be False. The size attribute tells us the number of elements in the array. In the case of grayscale images, this value is equal to the number of pixels in the image. Here is an example:

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

When directly accessing the pixels of an image, the first value indicates the row number, and the second value 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:

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 the 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:

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

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 to use a second parameter and set as_gray to True. The output of this operation looks as follows:

There are many other filters that you can apply, such as the gaussian filter for blurring. It accepts many parameters, 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:

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

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:

source