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. Show 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-imageThe library we are going to use in order to carry out our image-processing tasks is from skimage import io44. 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 from skimage import io44. 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 macOS machine, as this is what I'm currently using in writing this tutorial. As from skimage import io44 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 from skimage import io47, but if you have from skimage import io48 and higher, or from skimage import io49 and higher, you already have from skimage import io47! from skimage import io44 now can be simply installed by typing the following command: from skimage import io52 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: Dimensions of an ImageSometimes 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 from skimage import io53 method, we can easily get the image dimensions with the help of the from skimage import io54 attribute. The reason this technique works is because images in the from skimage import io44 module are represented by from skimage import io56 arrays. Here is an example: 1 from skimage import io 2 3 img = io.imread('baboon.png') 4 5 # Outputs: (512, 512, 3)
6 print(img.shape) from skimage import io0 from skimage import io1 from skimage import io2 from skimage import io3 from skimage import io4 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: 1 from skimage import io 2 3 from skimage import io9 4 5 22 6 print(img.shape) from skimage import io0 from skimage import io1 27 from skimage import io3 from skimage import io4 You can also load your images as grayscale by setting the value of the second parameter from skimage import io57 in the from skimage import io53 function to be from skimage import io59. The from skimage import io60 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: 1 from skimage import io 2 3 34 4 5 37 6 print(img.shape) from skimage import io0 from skimage import io1 img = io.imread('baboon.png')2 from skimage import io3 from skimage import io4 Manipulating Individual PixelsYou 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 from skimage import io61 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 from skimage import io62. It is also possible to modify a set of pixels together. Here is an example that adds a red border to our image. 1 from skimage import io 2 3 from skimage import io9 4 5 42 6 44 from skimage import io0 from skimage import io1 47 from skimage import io3 49 50 51 52 53 54 55 56 57 58 59 # Outputs: (512, 512, 3)
0# Outputs: (512, 512, 3)
1# Outputs: (512, 512, 3)
2This is the result: Color to GrayscaleIn 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: 1 from skimage import io 2 3 34 4 # Outputs: (512, 512, 3)
9We simply passed from skimage import io57 as from skimage import io64 to the from skimage import io53 method that we learned about in the previous section. The from skimage import io66 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 from skimage import io67 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. 1 61 Here is the Python code that creates the grayscale image: 1 63 2 3 from skimage import io9 4 68 5 6 print(img.shape)1 In order to show the new grayscale image, add the following to the end of the script: 1 print(img.shape)3 2 print(img.shape)5 The result looks like this: Applying a Filter to an ImageIn image processing, filtering is performed to make some enhancements to 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 to 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: 1 print(img.shape)7 2 3 from skimage import io9 4 from skimage import io02 5 6 from skimage import io05 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 from skimage import io57 to from skimage import io64. 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: 1 from skimage import io07 2 3 from skimage import io9 4 from skimage import io12 5 from skimage import io14 6 from skimage import io0 from skimage import io17 from skimage import io1 from skimage import io19 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 from skimage import io70 method. After that, we binarize our image and set pixels as from skimage import io64 or from skimage import io59 depending on whether they are above the threshold or not. This binary image is then converted to 8-bit uint data by using the from skimage import io73 method. 1 from skimage import io21 2 from skimage import io23 3 4 34 5 6 from skimage import io29 from skimage import io0 from skimage import io1 from skimage import io32 from skimage import io3 from skimage import io34 50 51 53 from skimage import io38 55 from skimage import io40 56 58 from skimage import io43 The above code produces the following result for our image: ConclusionThere are many image-processing operations, and the from skimage import io44 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 PythonLearn Python with our complete Python tutorial guide, whether you're just getting started or you're a seasoned coder looking to learn new skills. |