|
| 1 | +import numpy as np |
| 2 | +import cv2 |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +import math |
| 5 | + |
| 6 | + |
| 7 | +def gaussian_kernel(size, sigma=1): |
| 8 | + size = int(size) // 2 |
| 9 | + x, y = np.mgrid[-size:size+1, -size:size+1] |
| 10 | + normal = 1 / (2.0 * np.pi * sigma**2) |
| 11 | + g = np.exp(-((x**2 + y**2) / (2.0*sigma**2))) * normal |
| 12 | + return g |
| 13 | + |
| 14 | +def sobelfilter(image): |
| 15 | + gx = np.array([[1.0, 0.0, -1.0], [2.0, 0.0, -2.0], [1.0, 0.0, -1.0]]) |
| 16 | + gy = np.array([[1.0, 2.0, 1.0], [0.0, 0.0, 0.0], [-1.0, -2.0, -1.0]]) |
| 17 | + rows, columns = np.shape(image) |
| 18 | + sobel_filtered_image = np.zeros((rows, columns),np.float64) |
| 19 | + Gradient = np.zeros((rows, columns),np.float64) |
| 20 | + for i in range(rows - 2): |
| 21 | + for j in range(columns - 2): |
| 22 | + Gx = np.multiply(gx, image[i:i + 3, j:j + 3]).sum() / 8 |
| 23 | + Gy = np.multiply(gy, image[i:i + 3, j:j + 3]).sum() / 8 |
| 24 | + Gradient[i+1,j+1]=np.degrees(np.arctan2(Gy,Gx)) |
| 25 | + sobel_filtered_image[i + 1, j + 1] = np.sqrt(np.square(Gx)+np.square(Gy)) |
| 26 | + sobel_filtered_image=sobel_filtered_image/np.max(sobel_filtered_image) |
| 27 | + return sobel_filtered_image,Gradient |
| 28 | + |
| 29 | +def NonMaxSup(Gmag, Grad): |
| 30 | + NMS = np.zeros((Gmag.shape[0],Gmag.shape[1]),np.float64) |
| 31 | + for i in range(1, Gmag.shape[0] - 1): |
| 32 | + for j in range(1, Gmag.shape[1] - 1): |
| 33 | + if((Grad[i,j] >= -22.5 and Grad[i,j] <= 22.5) or (Grad[i,j] <= -157.5 and Grad[i,j] >= 157.5)): |
| 34 | + if((Gmag[i,j] > Gmag[i,j+1]) and (Gmag[i,j] > Gmag[i,j-1])): |
| 35 | + NMS[i,j] = Gmag[i,j] |
| 36 | + else: |
| 37 | + NMS[i,j] = 0 |
| 38 | + if((Grad[i,j] >= 22.5 and Grad[i,j] <= 67.5) or (Grad[i,j] <= -112.5 and Grad[i,j] >= -157.5)): |
| 39 | + if((Gmag[i,j] > Gmag[i+1,j+1]) and (Gmag[i,j] > Gmag[i-1,j-1])): |
| 40 | + NMS[i,j] = Gmag[i,j] |
| 41 | + else: |
| 42 | + NMS[i,j] = 0 |
| 43 | + if((Grad[i,j] >= 67.5 and Grad[i,j] <= 112.5) or (Grad[i,j] <= -67.5 and Grad[i,j] >= -112.5)): |
| 44 | + if((Gmag[i,j] > Gmag[i+1,j]) and (Gmag[i,j] > Gmag[i-1,j])): |
| 45 | + NMS[i,j] = Gmag[i,j] |
| 46 | + else: |
| 47 | + NMS[i,j] = 0 |
| 48 | + if((Grad[i,j] >= 112.5 and Grad[i,j] <= 157.5) or (Grad[i,j] <= -22.5 and Grad[i,j] >= -67.5)): |
| 49 | + if((Gmag[i,j] > Gmag[i+1,j-1]) and (Gmag[i,j] > Gmag[i-1,j+1])): |
| 50 | + NMS[i,j] = Gmag[i,j] |
| 51 | + else: |
| 52 | + NMS[i,j] = 0 |
| 53 | + return NMS |
| 54 | + |
| 55 | +def DoThreshHyst(img): |
| 56 | + highThresholdRatio = 0.2 |
| 57 | + lowThresholdRatio = 0.15 |
| 58 | + GSup = np.copy(img) |
| 59 | + h = int(GSup.shape[0]) |
| 60 | + w = int(GSup.shape[1]) |
| 61 | + highThreshold = np.max(GSup) * highThresholdRatio |
| 62 | + lowThreshold = highThreshold * lowThresholdRatio |
| 63 | + x = 0.1 |
| 64 | + oldx=0 |
| 65 | + while(oldx != x): |
| 66 | + oldx = x |
| 67 | + for i in range(1,h-1): |
| 68 | + for j in range(1,w-1): |
| 69 | + if(GSup[i,j] > highThreshold): |
| 70 | + GSup[i,j] = 1 |
| 71 | + elif(GSup[i,j] < lowThreshold): |
| 72 | + GSup[i,j] = 0 |
| 73 | + else: |
| 74 | + if((GSup[i-1,j-1] > highThreshold) or |
| 75 | + (GSup[i-1,j] > highThreshold) or |
| 76 | + (GSup[i-1,j+1] > highThreshold) or |
| 77 | + (GSup[i,j-1] > highThreshold) or |
| 78 | + (GSup[i,j+1] > highThreshold) or |
| 79 | + (GSup[i+1,j-1] > highThreshold) or |
| 80 | + (GSup[i+1,j] > highThreshold) or |
| 81 | + (GSup[i+1,j+1] > highThreshold)): |
| 82 | + GSup[i,j] = 1 |
| 83 | + x = np.sum(GSup == 1) |
| 84 | + |
| 85 | + GSup = (GSup == 1) * GSup |
| 86 | + return GSup |
| 87 | + |
| 88 | +def canny_edges(img): |
| 89 | + img=cv2.GaussianBlur(img,(5,5),0) |
| 90 | + sobelfilterimage,gradient=sobelfilter(img) |
| 91 | + NMS=NonMaxSup(sobelfilterimage,gradient) |
| 92 | + canny=DoThreshHyst(NMS) |
| 93 | + return canny |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +img=cv2.imread('Building.jpg',0) |
| 98 | +canny=canny_edges(img) |
| 99 | +cv2.imshow("cannyedge",canny) |
| 100 | +cv2.waitKey(0) |
| 101 | +cv2.destroyAllWindows() |
| 102 | + |
| 103 | + |
0 commit comments