Assignment 4: Canny Edge Detection

Image Link: https://drive.google.com/drive/folders/1USa7N0JCIeGGhZhkKP18-QuziOzh8riX?usp=share_link


Canny Edge Detection
Algorithm


Basic Code:

import cv2
import matplotlib.pyplot as plt
import numpy as np

imPth = r'cm.jpg'
img = plt.imread(imPth)

plt.imshow(img,cmap='gray')

E = cv2.Canny(img,150,250)

plt.subplot(121)
plt.imshow(img,cmap='gray')
plt.axis('off')
plt.subplot(122)
plt.imshow(E,cmap='gray')
plt.axis('off')
plt.show()



Gaussian filter




x = 0
y = -1
sigma = 1
v = (1/(2*np.pi*(sigma**2)))*np.exp(-(x**2 + y**2)/(2*(sigma**2)))
v

def f_getGaussianFilter(s,sigma):
    maxX = s//2
    minX = -maxX
    minY = minX
    maxY = maxX
    G = np.zeros((s,s))
    for x in range(minX,maxX+1):
        for y in range(minY,maxY+1):
            v = (1/(2*np.pi*(sigma**2)))*np.exp(-(x**2 + y**2)/(2*(sigma**2)))
            G[x-minX,y-minY] = v
    return G

G = f_getGaussianFilter(11,2)
np.sum(G)

cv2.getGaussianKernel?


Gaussian Filter Smoothing Implementation

G = f_getGaussianFilter(3,0.5)
img_S = cv2.filter2D(img,-1,G)
plt.subplot(121)
plt.imshow(img,cmap='gray')
plt.axis('off')
plt.subplot(122)
plt.imshow(img_S,cmap='gray')
plt.axis('off')
plt.show()

Image Gradients Implementation

img_S = img_S[:,:,0]
mask_x = np.zeros((2,1))
mask_x[0] = -1
mask_x[1] = 1
mask_x

img_S = np.float64(img_S)
Ix = cv2.filter2D(img_S,-1,mask_x)
mask_y = mask_x.T
Iy = cv2.filter2D(img_S,-1,mask_y)

print - Ix.dtype
print - Iy.shape
print - img_S.shape

plt.subplot(121)
plt.imshow(Ix,cmap='gray')
plt.title('X-derivative')
plt.axis('off')
plt.subplot(122)
plt.imshow(Iy,cmap='gray')
plt.title('Y-derivative')
plt.axis('off')
plt.show()

gv = np.array([Ix[5,3],Iy[5,3]])

Derivative of Gaussian

def Gx(s,sigma):
    maxX = s//2
    minX = -maxX
    minY = minX
    maxY = maxX
    Gx = np.zeros((s,s))
    for x in range(minX,maxX+1):
        for y in range(minY,maxY+1):
            v = (-x/(2*np.pi*(sigma**4)))*np.exp(-(x**2 + y**2)/(2*(sigma**2)))
            Gx[x-minX,y-minY] = v
    return Gx

def Gy(s,sigma):
    maxX = s//2
    minX = -maxX
    minY = minX
    maxY = maxX
    Gy = np.zeros((s,s))
    for x in range(minX,maxX+1):
        for y in range(minY,maxY+1):
            v = (-y/(2*np.pi*(sigma**4)))*np.exp(-(x**2 + y**2)/(2*(sigma**2)))
            Gy[x-minX,y-minY] = v
    return Gy

gx = Gx(3,0.5)
gy = Gy(3,0.5)

Ix = cv2.filter2D(img,-1,gx)
Iy = cv2.filter2D(img,-1,gy)

plt.subplot(121)
plt.imshow(Ix,cmap='gray')
plt.title('X-derivative')
plt.axis('off')
plt.subplot(122)
plt.imshow(Iy,cmap='gray')
plt.title('Y-derivative')
plt.axis('off')
plt.show()

Gm = (Ix**2 + Iy**2)**0.5
Gd = np.rad2deg(np.arctan2(Iy,Ix))

Gd.min(),Gd.max()

plt.subplot(121)
plt.imshow(Gm,cmap='gray')
plt.title('GM')
plt.axis('off')
plt.subplot(122)
plt.imshow(Gd,cmap='gray')
plt.title('Gd')
plt.axis('off')
plt.show()

plt.imshow(Gm>30,cmap='gray')

plt.hist(Gd.flatten())

bins = np.array([-180.,-135.,-90.,-45.,0.,45.,90.,135.,180.])
inds = np.digitize(Gd,bins)-1
Gd_bin = bins[inds.flatten()].reshape(Gd.shape)

i,j = 10,100
Gd[i,j],Gd_bin[i,j]

A = np.array([180.,134.,179.,135,-33.,-180.])
inds = np.digitize(A,bins)-1

bins[inds]

A = np.array([180.,134.,179.,135,-33.,-180.,12,-12,-170])
A_bin = 45*(np.round(A/45))

Gd_bin = 45*(np.round(Gd/45))

i,j = 12,100
Gd[i,j],Gd_bin[i,j]

Non Maxima Suppression

def NMS(Gm,Gd):
    Gd_bin = 45*(np.round(Gd/45))
    Gm_NMS = np.zeros(Gm.shape)
    numRows,numCols = Gm.shape[0],Gm.shape[1]
    a,b=0.,0.
    for r in range(1,numRows-1):
        for c in range(1,numCols-1):
            ang = Gd_bin[r,c]
            if ang == 180. or ang == -180. or ang == 0.0 or ang == -0.0:
                a,b = Gm[r-1,c], Gm[r+1,c]
            elif ang == 45. or ang == -135.:
                a,b = Gm[r+1,c+1],Gm[r-1,c-1]
            elif ang == 90. or ang == -90.:
                a,b = Gm[r,c-1],Gm[r,c+1]
            elif ang == 135. or ang == -45.:
                a,b = Gm[r-1,c+1],Gm[r+1,c-1]
            else:
                print('Error')
                return
            if Gm[r,c] >= a and Gm[r,c] >= b:
                Gm_NMS[r,c] = Gm[r,c]
    return Gm_NMS

Gm_NMS = NMS(Gm,Gd)


plt.subplot(121)
plt.imshow(Gm,cmap='gray')
plt.title('GM')
plt.axis('off')
plt.subplot(122)
plt.imshow(Gm_NMS,cmap='gray')
plt.title('GM_NMS')
plt.axis('off')
plt.show()

plt.imshow(Gm_NMS>50,cmap='gray')

Hesterysis Thresholding

from skimage import filters

L = Gm_NMS.mean()
H = L + Gm_NMS.std()
E = filters.apply_hysteresis_threshold(Gm_NMS,L,H)

plt.imshow(E,cmap='gray')







Comments

Popular posts from this blog

Assignment 1.2 - Transformation

Assignment 1.1: Image Transformation in Python