top of page
Search
jamescgonzales

Entry #2 - Handling Digital Images Using Scikit-image



Digital image processing is the use of digital computers to process digital images with the use of various algorithms. Thru digital processing, we can observe objects in the image but not visible to the naked eye. Furthermore, we can seek, measure, distinguish and enhance objects in an image.

But how do computer sees an image?

On this entry, I will attempt to discuss the following:

  1. Displaying Digital Images using Scikit-image and Matplotlib.

  2. Sampling and Quantization

  3. Image Type


 

1. Displaying Digital Images using Scikit-image and Matplotlib.

How do computers see a digital image? By being "digital" we define an image of having discrete values or defined level, in which can be represented by a two-dimensional matrix of cell values which can be referred as "pixels". With the help of numpy and scikit-image (skimage) we can visualize, understand and later modify, what's going on within the image.

Note: There are other image processing libraries in python which are commonly used however on this blog, skimage is used for mathematical formulations and transformations will be emphasized.

Now, let's try to read an image using skimage. Below shows an image with 256 x 256 (length x width)

As recalled, an image is represented as 2D matrix. How about showing the randomly generated matrices.



We can also make use of matplotlib library to display the images of different sizes and shape


and different types


 

2. Sampling and Quantization


In most cases in the physical world, images are analog, in which the values or information are continuous. This mean it has infinite values within that continuous range. In order to be processed digitally, or convert from continuous to thru sampling and quantization.





2.1 Sampling

Sampling as it describe itself involves taking the value of the image at regular spatial intervals. This means that an amplitude value is selected from different values of the space interval of the image. The sampling rate or the length of the intervals defines the spatial resolution of the image.


Here's the cube again with 512x512 resolution. The N on top of the image represents the number of pixel per side of the image. The higher the value of N, the denser and the higher the resolution of the image.

Using the downscale_local_mean of skimage, the image with N-dimensional image is is down-sampled by local averaging.


2.2 Quantization

Quantization in itself is the digitizing process of continuous data in which it involves discretizing the intensity values of the analog image. 𝑘k as number of bits is used to represent the bit depth or intensity values. The number of quantization levels should be high enough for human perception of fine shading details in the image. The occurrence of false contours is the main problem in image which has been quantized with insufficient brightness levels. Below is the application of quantization on the same cube image.


 

3. Image Type


There are types of images that will be discussed here are based on the pixels and how they are stored.

  1. Color or RGB Images - those images that is represented by 3 x 2d array of pixels with each channel representing the color channels- R (red), G (green), and B (blue)

  2. Grayscale Images - those images that is represented by 2D array of pixel and has certain number of bits per pixel such as 8 bits. Having 288 would yield to 256 (0-255) shades between white and black.

  3. Binary Images - those images that is represented by 2D array of pixels with only 1 bit per pixel


To convert a color image to grayscale, we compute its luminance or intensity values. We can easily use rgb2gray from skimage library.

from skimage.color import rgb2gray
fish_gray = rgb2gray(fish)

To convert a grayscale image to monochrome or binary image, we apply thresholding such that pixel values that are less than the threshold will be changed to the minimum allowed value (0) and those that are above the threshold will be replaced by the maximum allowed value (1.0 or 255).

img_as_uint of skimage would do the job.

from skimage import img_as_uint
fish_binary = img_as_uint(fish_gray > fish_gray.mean())

To convert a grayscale image into color (in RGB space), we look up the RGB value of each gray value. skimage's gray2rgb would execute this.

from skimage.color import gray2rgb
fish_color = gray2rgb(fish_gray)

Color spaces

By default, a color image is represented in RGB (Red, Green and Blue) color space. The third value on the image shape represents the color channel.

HSV (Hue, Saturation, and Value) is another color space To convert from RGB to HSV, call rgb2hsv.

To convert an image in HSV space back to RGB, we can use hsv2rgb from skimage.color library.

 

That's it for now. Thanks for reading and I hope you get a new learning.



78 views0 comments

Recent Posts

See All

Comments


Post: Blog2_Post
bottom of page