Week10
Last updated
Was this helpful?
Last updated
Was this helpful?
Images are actually numpy arrays
import numpy as np
import matplotlib.pyplot as plt
Chess = np.zeros((7,7))
Chess[ ::2,1::2] = 1
Chess[1::2, ::2] = 1
plt.imshow(Chess)
plt.show()
You can both import an image from your disk, or from a URL. It can be done with the help of io library inside skimage.
You can import/load an image by following :
from skimage import io
my_image = io.imread('filename')
# OR
my_image = io.imread('URL')
And, you can save an image on your disk by following :
from skimage import io
io.imsave('filename', my_image)
Example :
from skimage import io
import matplotlib.pyplot as plt
my_image = io.imread('https://homepages.cae.wisc.edu/~ece533/images/lena.png')
plt.imshow(my_image)
plt.show()
from skimage.transform import rotate
rotated_image = rotate(image, deg)
# Example :
from skimage import data
from skimage.transform import rotate
image = data.horse()
plt.subplot(2,1,1)
plt.imshow(image)
rotated_image = rotate(image, 30)
plt.subplot(2,1,2)
plt.imshow(rotated_image)
plt.show()
from skimage.transform import resize
resized_image = resize(image, order)
# Example :
from skimage import data
from skimage.transform import resize
image = data.horse()
plt.subplot(2,1,1)
plt.imshow(image)
resized_image = resize(image, (150,150)) #you loose the resolution, as the original is 400x400
plt.subplot(2,1,2)
plt.imshow(resized_image)
plt.show()
from skimage.transform import rescale
rescaled_image = rescale(image, order)
# Example :
from skimage import data
from skimage.transform import rescale
image = data.horse()
plt.subplot(2,1,1)
plt.imshow(image)
rescaled_image = rescale(image, 0.5)
plt.subplot(2,1,2)
plt.imshow(rescaled_image)
plt.show()
from skimage.color import rgb2hsv
my_image = rgb2hsv(image)
from skimage.color import hsv2rgb
my_image = hsv2rgb(image)
# Example
import matplotlib.pyplot as plt
import numpy as np
from skimage.color import rgb2hsv
red_pixel_rgb = np.array([[[255, 0, 0]]], dtype=np.uint8)
rgb2hsv(red_pixel_rgb)
print(rgb2hsv(red_pixel_rgb))
plt.subplot(2,1,1)
plt.imshow(red_pixel_rgb)
plt.subplot(2,1,2)
plt.imshow(rgb2hsv(red_pixel_rgb))
plt.show()
from skimage.color import rgb2gray
my_image = rgb2grey(image)
# Example
import matplotlib.pyplot as plt
from skimage.color import rgb2grey
from skimage import data
my_image = data.astronaut()
plt.subplot(2,1,1)
plt.imshow(my_image)
plt.subplot(2,1,2)
plt.imshow(rgb2grey(my_image))
plt.show()
line(x0,y0, x1,y1)
#which x0,y0 are the starting coordinate of line and x1,y1 are the ending point coordinates
bezier_curve(x0, y0, x1, y1, x2, y2)
#x0,y0 are the first control point coordinate, x1,y1 are the middle point coordinates and x2,y2 are the last point coordinates.
polygon(row,column)
#row are row coordinates of polygon, and column are column coordinates
circle(row, column, radius)
#row and column are centre coordinate, and radius is the radius of circle
ellipse(row, column, r_radius, c_radius, rotation=deg)
#which row and column are centre coordinate, and r_radius and c_radius are minor and major axis. Also, you may rotate it by a certain degree
An edge is a curve that follows a path of rapid change in image intensity/brightness. Edges are often associated with the boundaries of objects in a scene. The term “edge‟ accounts for a local luminance change.
from skimage.filters import sobel, roberts, scharr, prewitt
edge_sobel = sobel(image)
edge_roberts = roberts(image)
edge_scharr = scharr(image)
edge_prewitt = prewitt(image)
from skimage.measure import find_contours
contours = find_contours(array, level)
# array : The array which the contours are going to be discovered on that
# level : Value which shows the contours need to be located along that
# contours : array consisting the coordinates around the contour
from skimage import data
from skimage.measure import find_contours
from skimage.color import rgb2gray
import matplotlib.pyplot as plt
#============= Construct some test data ==============#
x, y = np.ogrid[-np.pi:np.pi:100j, -np.pi:np.pi:100j]
r = np.sin(np.exp((np.sin(x)**3 + np.cos(y)**2)))
#====== Find contours at a constant value of 0.8 =====#
contours = find_contours(r, 0.8)
#=== Display the image and plot all contours found ===#
plt.imshow(r, interpolation='nearest', cmap=plt.cm.gray)
for n, contour in enumerate(contours):
plt.plot(contour[:, 1], contour[:, 0], linewidth=2)
plt.show()
from skimage.segmentation import active_contour
snake = active_contour (image, initial_snake, alpha=0.015,beta=10,gamma=0.001)
#initial_snake : the initial shape we define for snake
#alpha : Shape parameter – Higher values results in snake grasp faster
#Beta : Smoothness Parameter
#gamma : Time stepping parameter
#Example :
from skimage import data
from skimage.color import rgb2gray
import matplotlib.pyplot as plt
from skimage.filters import gaussian
from skimage.segmentation import active_contour
#================================================#
image = data.astronaut()
image = rgb2grey(image)
#===== Defining initial Snake as a circle =======#
s = np.linspace(0, 2*np.pi, 400)
x = 220 + 100*np.cos(s)
y = 100 + 100*np.sin(s)
initial_snake = np.array([x, y]).T
#====== Applying forces for Snake to grasp ======#
snake = active_contour(gaussian(image, 3),initial_snake,alpha=0.015,beta=10,gamma=0.001)
#================== Plotting ====================#
plt.imshow(image, cmap=plt.cm.gray)
plt.plot(init[:, 0], initial_snake[:, 1], '--r', lw=3)
plt.plot(snake[:, 0], snake[:, 1], '-c', lw=5)
plt.show()
Template matching is a technique for finding areas of an image that match (are similar) to a template image (patch).
from skimage.feature import match_template
result = match_template(image, template)
#Template : it’s the pattern/patch the system looks for in the image.
#Example:
import matplotlib.pyplot as plt
import numpy as np
from skimage.feature import match_template
from skimage import data
#=================== Import Dataset & Template ====================#
image = data.coins()
coin = image[170:220, 75:130] # Template
hight_coin, width_coin = coin.shape # Get the size of template
#======================= Template Matching =======================#
result = match_template(image, coin)
pixel = np.unravel_index(np.argmax(result), result.shape)
x, y = pixel[::-1]
#=========================== Plotting =============================#
ax1 = plt.subplot(1, 2, 1)
ax2 = plt.subplot(1, 2, 2)
ax1.imshow(coin, cmap=plt.cm.gray)
ax2.imshow(image, cmap=plt.cm.gray)
rect = plt.Rectangle((x, y), width_coin , hight_coin, edgecolor='r', facecolor='none')
ax2.add_patch(rect) # add a patch on that part of image which contains template
plt.show()
Three popular methods for filtering the noise:
Wavelet
Total Variation (TV)
Bilateral filters
from skimage.restoration import denoise_tv_chambolle
from skimage.restoration import denoise_bilateral
from skimage.restoration import denoise_wavelet
denoise_tv_chambolle(noisy_image, weight=0.1, multichannel=True)
denoise_wavelet(noisy_image, multichannel=True)
denoise_bilateral(noisy_image, sigma_color=0.1, sigma_spatial=15, multichannel=True)
# weight : Denoising weight. The greater weight, the more denoising.
# sigma_color : Standard deviation for grayshades / colour distance.
# sigma_spatial : Standard deviation for range distance.
# multichannel : Keep it ‘True’ for colour images
# Example
from skimage.restoration import (denoise_tv_chambolle, denoise_bilateral,denoise_wavelet)
from skimage import data, io
from skimage.util import random_noise
#================== Import Image =====================#
original_image = io.imread(c:/Users/MA/Desktop/pose.jpg) # The image is provided for you in this week’s Learning Material folder
#================ Add Random Noise ===================#
noisy_image = random_noise(original_image, var=0.22**2)
#================== Denoising ========================#
denoised_image_tv = denoise_tv_chambolle(noisy_image, weight=0.1, multichannel=True)
denoised_image_wv = denoise_wavelet(noisy_image, multichannel=True)
#denoised_image_bi = denoise_bilateral(noisy_image, sigma_color=0.15, sigma_spatial=35, multichannel=True) #!!!Bilateral filter is very slow to run !!!
#=================== Plotting ========================#
plt.subplot(1,5,1)
plt.imshow(original_image)
plt.title('Original')
plt.subplot(1,5,2)
plt.imshow(noisy_image)
plt.title('Noisy Image')
plt.subplot(1,5,3)
plt.imshow(denoised_image_tv)
plt.title('TV Chambolle denoised image')
plt.subplot(1,5,4)
plt.imshow(denoised_image_wv)
plt.title('Wavelet denoised image')
plt.show()
Otsu Method
Mean Method
Li Method
Yen Method
Minimum Method
Isodata Method
from skimage.filters import threshold_otsu,
from skimage.filters import threshold_mean
from skimage.filters import threshold_li
from skimage.filters import threshold_yen
from skimage.filters import threshold_minimum
from skimage.filters import threshold_isodata
Thresh = threshold_otsu(GreyScale_image)
Thresh = threshold_mean(GreyScale_image)
Thresh = threshold_li(GreyScale_image)
Thresh = threshold_yen(GreyScale_image)
Thresh = threshold_minimum(GreyScale_image)
Thresh = threshold_isodata(GreyScale_image)
Binary_Image = GreyScale_image > Thresh
From the data folder import the image called dice.jpg, and do the followings : 1. Read the input image, convert it to grayscale, and blur it slightly. 2. Use simple fixed-level thresholding to convert the grayscale image to a binary image. 3. Find contours corresponding to the outlines of each dice. 4. Print information on how many dices can be found in the image. 5. For illustrative purposes, mark the centre position of each dice in the image grid so we can visualize the results.
import matplotlib.pyplot as plt
import numpy as np
from skimage import data, io, filters
from skimage.color import rgb2grey
from skimage.filters import threshold_otsu,
threshold_mean,threshold_li,threshold_yen,threshold_minimum,
from skimage.morphology import reconstruction
from skimage.measure import find_contours
from skimage.feature import blob_dog, blob_log, blob_doh
#================= Importing the ‘Dice.jpg’ image ==============#
original_image = io.imread('c:/Users/MA/Desktop/dice.jpg')
#======================= Processing ============================#
Black_White_image = rgb2grey(original_image)
thresh = threshold_minimum(Black_White_image)
binary_image = Black_White_image > thresh
contours1 = find_contours(binary_image ,0.1)
binary_image_copy = np.copy(binary_image)
binary_image_copy[1:-1, 1:-1] = 1
filled_image = reconstruction(binary_image_copy,binary_image,
method='erosion')
filled_image [0:100,:] = 0
filled_image [:,0:200] = 0
contours2 = find_contours(filled_image ,0.1)
Blob_centres = blob_log(filled_image, min_sigma=100)
#===================== Plotting & Printing =======================#
print('The Total Number of Dices in Image = ', len(Blob_centres))
plt.subplot(2,4,1)
plt.imshow(original_image, cmap=plt.cm.gray)
plt.title('Original Image')
plt.subplot(2,4,2)
plt.imshow(Black_White_image, cmap=plt.cm.gray)
plt.title('Black & White Image')
plt.subplot(2,4,3)
plt.imshow(binary_image, cmap=plt.cm.gray)
plt.title('Binarized Image')
plt.subplot(2,4,4)
plt.imshow(binary_image, cmap=plt.cm.gray)
for n, contour in enumerate(contours1):
plt.plot(contour[:, 1], contour[:, 0], 'r-', linewidth=2.5)
plt.title('Contoured Image')
plt.subplot(2,4,5)
plt.imshow(filled_image, cmap=plt.cm.gray)
plt.title('Filled Image')
plt.subplot(2,4,6)
plt.imshow(filled_image , cmap=plt.cm.gray)
for n, contour in enumerate(contours2):
plt.plot(contour[:, 1], contour[:, 0], 'y-', linewidth=5.5)
plt.title('Outer Contoured Image')
plt.subplot(2,4,7)
plt.imshow(original_image , cmap=plt.cm.gray)
for n, contour in enumerate(contours2):
plt.plot(contour[:, 1], contour[:, 0], 'b-', linewidth=5.5)
plt.title('Outer Contour on Original Image')
plt.subplot(2,4,8)
plt.imshow(Blob_centres, cmap=plt.cm.gray)
for i, Blob in enumerate(Blob_centres):
plt.plot(Blob_centres[:, 1], Blob_centres[:, 0], 'go', markersize=15)
plt.grid()
plt.title('Binary Large Objects (BLOB)')
plt.show()