Cara menggunakan downsample image python

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 from skimage import io 44. 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 io 44. 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 io 44 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 io 47, but if you have from skimage import io 48 and higher, or from skimage import io 49 and higher, you already have from skimage import io 47!

from skimage import io 44 now can be simply installed by typing the following command:

from skimage import io 52

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 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 from skimage import io 53 method, we can easily get the image dimensions with the help of the from skimage import io 54 attribute.

The reason this technique works is because images in the from skimage import io 44 module are represented by from skimage import io 56 arrays. Here is an example:

1from skimage import io 23img = io.imread('baboon.png') 45# Outputs: (512, 512, 3) 6print(img.shape) from skimage import io 0from skimage import io 1from skimage import io 2from skimage import io 3from skimage import io 4

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:

1from skimage import io 23from skimage import io 945226print(img.shape) from skimage import io 0from skimage import io 127from skimage import io 3from skimage import io 4

You can also load your images as grayscale by setting the value of the second parameter from skimage import io 57 in the from skimage import io 53 function to be from skimage import io 59. The from skimage import io 60 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:

1from skimage import io 233445376print(img.shape) from skimage import io 0from skimage import io 1img = io.imread('baboon.png') 2from skimage import io 3from skimage import io 4

Manipulating Individual Pixels

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 from skimage import io 61 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 io 62.

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

1from skimage import io 23from skimage import io 94542644from skimage import io 0from skimage import io 147from skimage import io 34950515253545556575859# Outputs: (512, 512, 3) 0# Outputs: (512, 512, 3) 1# Outputs: (512, 512, 3) 2

This is the result:

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:

1from skimage import io 23344# Outputs: (512, 512, 3) 9

We simply passed from skimage import io 57 as from skimage import io 64 to the from skimage import io 53 method that we learned about in the previous section.

The from skimage import io 66 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 io 67 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.

161

Here is the Python code that creates the grayscale image:

16323from skimage import io 946856print(img.shape) 1

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

1print(img.shape) 32print(img.shape) 5

The result looks like this:

Applying a Filter to an Image

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.

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:

1print(img.shape) 723from skimage import io 94from skimage import io 0256from skimage import io 05

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 io 57 to from skimage import io 64. 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:

1from skimage import io 0723from skimage import io 94from skimage import io 125from skimage import io 146from skimage import io 0from skimage import io 17from skimage import io 1from skimage import io 19

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 io 70 method. After that, we binarize our image and set pixels as from skimage import io 64 or from skimage import io 59 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 io 73 method.

1from skimage import io 212from skimage import io 23343456from skimage import io 29from skimage import io 0from skimage import io 1from skimage import io 32from skimage import io 3from skimage import io 34505153from skimage import io 3855from skimage import io 405658from skimage import io 43

The above code produces the following result for our image:

Conclusion

There are many image-processing operations, and the from skimage import io 44 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.

Postingan terbaru

LIHAT SEMUA