Image processing with Python(scikit-image)

Pawel Stasinski
Analytics Vidhya
Published in
3 min readSep 20, 2019

Scikit-image is a good library to start with image processing. This an article is a walkthrough for some techniques and tricks to start with image processing.

Read and plot the image

import matplotlib.pyplot as plt
from skimage import io
img = io.imread("image.png") #path to the image
plt.imshow(img)
plt.show()
print(img.shape) # show dimension of image
dim1, dim2 = img.shape[0], img.shape[1]
num_channels = img.shape[2]

Read image is a numpy array, this image has 494 on 494 shape and 4 channels. Check what happens, when you change:

plt.imshow(img) --> plt.imshow(img[:,:,0])

Change of size of the image

from skimage.transform import rescale, resizedef resized_img(img, scale=2):
image_resized = resize(img, (img.shape[0] / scale, img.shape[1] / scale),anti_aliasing=True)
return image_resized

Show resized images

img = io.imread("image.png")
image_resized = resized_img(img, 2) # I choosed 2, but you could all integers
def plot_compared_img(img1, img2, title1="Original image", title2="New one image"):
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 10))
ax = axes.ravel()
ax[0].imshow(img1)
ax[0].set_title(title1)
ax[1].imshow(img2, cmap='gray')
ax[1].set_title(title2)
plt.show()

plot_compared_img(img, image_resized,"Original image", "Resized image")

Please note that scale is changed.

Add a noise to image

import numpy as np
k = 0.2 # you could set any any real number
noise = np.ones_like(img) * k * (img.max() - img.min())
noise[np.random.random(size=noise.shape) > 0.5] *= -1
img_noise = img + noise # new image with noiseplot_compared_img(img, img_noise, "Original image", "The image with noise")

Comparing images with ssim

from skimage.measure import compare_ssim as ssim
ssim_noise = ssim(img, img_noise,
data_range=img_noise.max() - img_noise.min(), multichannel=True)
print(ssim_noise) #0.10910931410753069

Structural similarity index(ssim) indicates on similarity of images.

  • Value of ssim equals zero indicates no similarity between images.
  • Value ssim equals one suggest that photos are the same.

Above I am comparing original image(img) with img with noise(img_noise).

Convert the image to grey scale

from skimage.color import rgb2graygrayscale = rgb2gray(img)
plot_compared_img(img, grayscale,"Original image", "The image with greyscale")

Entropy of the image

In information theory, information entropy is the log-base-2 of the number of possible outcomes for a message. For an image, local entropy is related to the complexity contained in a neighborhood. More about entropy(Shannon entropy) at link

https://en.wiktionary.org/wiki/Shannon_entropy

from skimage.measure.entropy import shannon_entropy
print(shannon_entropy(img[:,:,0])) #7.5777861360050265
#plot entropyfrom skimage.filters.rank import entropy
from skimage.morphology import disk
entr_img = entropy(img[:,:,0], disk(10))
plt.imshow(entr_img, cmap='viridis')
plt.show()

Wrapping Up

Of course, it’s only most basics techniques to manipulate or extract features from the image. I encourage to read link from bottom. All code included in jupyter-notebook:

https://github.com/fuwiak/medium_scikit-image

Sources:

--

--