Image processing using scikit image

Break through the fear and complexity of image analysis

Antony Paulson Chazhoor
Towards Data Science

--

One of the positive sides of the data science job hunt process is the exposure to challenges and problems in the real world. One such challenge was that of detecting object locations in an image. The project could be approached using two methods.

  • Computer Vision without using Neural Networks
  • Computer Vision with Convolutional Neural Networks

For a guy who had no idea on how to do basic image analysis without neural networks, I ended up completing the project successfully within 4 days. The most crucial component in doing so was to make sense of what images actually are and how to process them.

The main focus of the post is to do just that, simply understand images with scikit image. Following along these points will do a world of good for budding data scientists interested in computer vision.

1. What are Images?

They are just numpy arrays. Getting this straight reduces half of the complexity in this field for a data scientist.

Don’t believe me, try this out. You’ll be drawing an image of how your Television set looked like without any signal reception in the 80's.

import numpy as np
import matplotlib.pyplot as plt
random_image = np.random.random([500,500])
plt.imshow(random_image, cmap = 'gray')
plt.colorbar();
TV set from the past

2. Broadly there are three layers to images Red, Blue and Green

Images are just a collection of numbers. You can change these on a real image by playing with the values and edit images. Here is a demo which will show the scale of the image and also the shape of it.

#Checking out a color image
from skimage import data
cat = data.chelsea()
print(f'Shape: {cat.shape}')
print("Values min/max", cat.min(), cat.max())
plt.imshow(cat)

Pay attention to the 3 in the shape. These signify a the red, blue and green layers. Editing the values in these arrays should edit the image. Let’s put this to the test. Lets draw a square on the image.

#Drawing a red square on the image
cat[10:110, 10:110, :] = [255, 0, 0] #{red, green, blue }
plt.imshow(cat)

Just like a student bored in class, I just defaced an image using python code. If anyone’s interested try making the cat cooler with some shades and a gold chain(An entire side project). Let’s move on.

3. Working with images on your system

Learning to upload images from your system to a python environment is essential and can be done using the following code

from skimage import io
image = io.imread('image path')

4. Converting images to greyscale

Most of the time image processing is less complex on grayscale images, in lay man terms Black and white images. Skimage can convert a colored(red, blue, green) image to a grayscale images in the following way:

gray_image = color.rgb2gray(image)
plt.imshow(gray_image, cmap = 'gray');

5. Finding the edges of an image

A very crucial element in object detection and localization in an image is the ability to detect its edges. Lets take this image of a man operating a camera.

skimage filters and blurs this image to not go into much detail but find the outline of the man. This is called pixelation.

####Code for smooth pixelation####from skimage import filters, img_as_float
import scipy.ndimage as ndi
#Convolution step(Like in CNN's)smooth_mean = ndi.convolve(image, mean_kernel)
image = data.camera()
pixelated = image[::10, ::10]
pixelated_float = img_as_float(pixelated)
sigma =1
smooth = filters.gaussian(pixelated_float, sigma)
plt.imshow(smooth);

Using sobel filteration from the filter, the edges of this image can be easily found. Use the code below:

#Using the Sobel filter
plt.imshow(filters.sobel(pixelated_float))

Building on these basic knowledge points will definitely be a great start to enter into computer vision. The methods can be ventured into much more practical detail by watching and learning from the SciPy conference 2019. It is freely available on youtube and is exactly what taught me the basics.

Follow this link to get there

My final intention for every reader going through this post is to remove the fear and intimidation over any image analysis task. Images are just basic numbers and thats one thing every data scientist is comfortable with.

--

--