|
1 |
| -# 1st version written by Shivam Chourey |
2 |
| -# Zhankun corrected the definition of Sobel operators, and changed the "CornerStrengthThreshold" |
3 |
| -# Implementation of Harris Corner detection algorithm |
4 |
| -# This algoruthm is very useful in corner detection and is used in a number of applications |
5 |
| -# It's also used in algorithms like FAST and ORB(which uses FAST and BREIF) |
6 |
| - |
7 |
| -import numpy as np |
8 |
| -import cv2 |
9 |
| - |
10 |
| - |
11 |
| - |
12 |
| -# Kernel operation using input operator of size 3*3 |
13 |
| -def GetSobel(image, Sobel, width, height): |
14 |
| - # Initialize the matrix |
15 |
| - I_d = np.zeros((width, height), np.float32) |
16 |
| - |
17 |
| - # For every pixel in the image |
18 |
| - for rows in range(width): |
19 |
| - for cols in range(height): |
20 |
| - # Run the Sobel kernel for each pixel |
21 |
| - if rows >= 1 or rows <= width-2 and cols >= 1 or cols <= height-2: |
22 |
| - for ind in range(3): |
23 |
| - for ite in range(3): |
24 |
| - I_d[rows][cols] += Sobel[ind][ite] * image[rows - ind - 1][cols - ite - 1] |
25 |
| - else: |
26 |
| - I_d[rows][cols] = image[rows][cols] |
27 |
| - |
28 |
| - return I_d |
29 |
| - |
30 |
| - |
31 |
| -# Method implements the Harris Corner Detection algorithm |
32 |
| -def HarrisCornerDetection(image): |
33 |
| - |
34 |
| - # The two Sobel operators - for x and y direction |
35 |
| - SobelX = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]]) |
36 |
| - SobelY = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) |
37 |
| - |
38 |
| - w, h = image.shape |
39 |
| - |
40 |
| - # X and Y derivative of image using Sobel operator |
41 |
| - ImgX = GetSobel(image, SobelX, w, h) |
42 |
| - ImgY = GetSobel(image, SobelY, w, h) |
43 |
| - |
44 |
| - # # Eliminate the negative values |
45 |
| - # There are multiple ways this can be done |
46 |
| - # 1. Off setting with a positive value (commented out below) |
47 |
| - # 2. Setting negative values to Zero (commented out) |
48 |
| - # 3. Multiply by -1 (implemented below, found most reliable method) |
49 |
| - # ImgX += 128.0 |
50 |
| - # ImgY += 128.0 |
51 |
| - for ind1 in range(w): |
52 |
| - for ind2 in range(h): |
53 |
| - if ImgY[ind1][ind2] < 0: |
54 |
| - ImgY[ind1][ind2] *= -1 |
55 |
| - # ImgY[ind1][ind2] = 0 |
56 |
| - if ImgX[ind1][ind2] < 0: |
57 |
| - ImgX[ind1][ind2] *= -1 |
58 |
| - # ImgX[ind1][ind2] = 0 |
59 |
| - |
60 |
| - # # Display the output results after Sobel operations |
61 |
| - # cv2.imshow("SobelX", ImgX) |
62 |
| - # cv2.imshow("SobelY", ImgY) |
63 |
| - |
64 |
| - ImgX_2 = np.square(ImgX) |
65 |
| - ImgY_2 = np.square(ImgY) |
66 |
| - |
67 |
| - ImgXY = np.multiply(ImgX, ImgY) |
68 |
| - ImgYX = np.multiply(ImgY, ImgX) |
69 |
| - |
70 |
| - #Use Gaussian Blur |
71 |
| - Sigma = 1.4 |
72 |
| - kernelsize = (3, 3) |
73 |
| - |
74 |
| - ImgX_2 = cv2.GaussianBlur(ImgX_2, kernelsize, Sigma) |
75 |
| - ImgY_2 = cv2.GaussianBlur(ImgY_2, kernelsize, Sigma) |
76 |
| - ImgXY = cv2.GaussianBlur(ImgXY, kernelsize, Sigma) |
77 |
| - ImgYX = cv2.GaussianBlur(ImgYX, kernelsize, Sigma) |
78 |
| - # print(ImgXY.shape, ImgYX.shape) |
79 |
| - |
80 |
| - alpha = 0.06 |
81 |
| - R = np.zeros((w, h), np.float32) |
82 |
| - # For every pixel find the corner strength |
83 |
| - for row in range(w): |
84 |
| - for col in range(h): |
85 |
| - M_bar = np.array([[ImgX_2[row][col], ImgXY[row][col]], [ImgYX[row][col], ImgY_2[row][col]]]) |
86 |
| - R[row][col] = np.linalg.det(M_bar) - (alpha * np.square(np.trace(M_bar))) |
87 |
| - return R |
88 |
| - |
89 |
| -if __name__ == "__main__": |
90 |
| - #### Main Program #### |
91 |
| - firstimagename = "checkerboard.png" |
92 |
| - |
93 |
| - # Get the first image |
94 |
| - bgr = cv2.imread(firstimagename) |
95 |
| - firstimage = cv2.cvtColor(bgr, cv2.COLOR_RGB2GRAY) |
96 |
| - w, h = firstimage.shape |
97 |
| - |
98 |
| - # Covert image to color to draw colored circles on it |
99 |
| - bgr = cv2.cvtColor(firstimage, cv2.COLOR_GRAY2RGB) |
100 |
| - |
101 |
| - # Corner detection |
102 |
| - R = HarrisCornerDetection(firstimage) |
103 |
| - |
104 |
| - # Empirical Parameter |
105 |
| - # This parameter will need tuning based on the use-case |
106 |
| - CornerStrengthThreshold = 20000 |
107 |
| - |
108 |
| - # Plot detected corners on image |
109 |
| - radius = 1 |
110 |
| - color = (0, 255, 0) # Green |
111 |
| - thickness = 1 |
112 |
| - |
113 |
| - PointList = [] |
114 |
| - # Look for Corner strengths above the threshold |
115 |
| - for row in range(w): |
116 |
| - for col in range(h): |
117 |
| - if R[row][col] > CornerStrengthThreshold: |
118 |
| - # print(R[row][col]) |
119 |
| - max = R[row][col] |
120 |
| - |
121 |
| - # Local non-maxima suppression |
122 |
| - skip = False |
123 |
| - for nrow in range(5): |
124 |
| - for ncol in range(5): |
125 |
| - if row + nrow - 2 < w and col + ncol - 2 < h: |
126 |
| - if R[row + nrow - 2][col + ncol - 2] > max: |
127 |
| - skip = True |
128 |
| - break |
129 |
| - |
130 |
| - if not skip: |
131 |
| - # Point is expressed in x, y which is col, row |
132 |
| - cv2.circle(bgr, (col, row), radius, color, thickness) |
133 |
| - PointList.append((row, col)) |
134 |
| - |
135 |
| - # Display image indicating corners and save it |
136 |
| - cv2.imshow("Corners", bgr) |
137 |
| - outname = "Output_" + str(CornerStrengthThreshold) + ".png" |
138 |
| - cv2.imwrite(outname, bgr) |
139 |
| - |
140 |
| - cv2.waitKey(0) |
141 |
| - cv2.destroyAllWindows() |
| 1 | +# 1st version written by Shivam Chourey |
| 2 | +# Zhankun corrected the definition of Sobel operators, and changed the "CornerStrengthThreshold" |
| 3 | +# Implementation of Harris Corner detection algorithm |
| 4 | +# This algoruthm is very useful in corner detection and is used in a number of applications |
| 5 | +# It's also used in algorithms like FAST and ORB(which uses FAST and BREIF) |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import cv2 |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +# Kernel operation using input operator of size 3*3 |
| 13 | +def GetSobel(image, Sobel, width, height): |
| 14 | + # Initialize the matrix |
| 15 | + I_d = np.zeros((width, height), np.float32) |
| 16 | + |
| 17 | + # For every pixel in the image |
| 18 | + for rows in range(width): |
| 19 | + for cols in range(height): |
| 20 | + # Run the Sobel kernel for each pixel |
| 21 | + if rows >= 1 or rows <= width-2 and cols >= 1 or cols <= height-2: |
| 22 | + for ind in range(3): |
| 23 | + for ite in range(3): |
| 24 | + I_d[rows][cols] += Sobel[ind][ite] * image[rows - ind - 1][cols - ite - 1] |
| 25 | + else: |
| 26 | + I_d[rows][cols] = image[rows][cols] |
| 27 | + |
| 28 | + return I_d |
| 29 | + |
| 30 | + |
| 31 | +# Method implements the Harris Corner Detection algorithm |
| 32 | +def HarrisCornerDetection(image): |
| 33 | + |
| 34 | + # The two Sobel operators - for x and y direction |
| 35 | + SobelX = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]]) |
| 36 | + SobelY = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) |
| 37 | + |
| 38 | + w, h = image.shape |
| 39 | + |
| 40 | + # X and Y derivative of image using Sobel operator |
| 41 | + ImgX = GetSobel(image, SobelX, w, h) |
| 42 | + ImgY = GetSobel(image, SobelY, w, h) |
| 43 | + |
| 44 | + # # Eliminate the negative values |
| 45 | + # There are multiple ways this can be done |
| 46 | + # 1. Off setting with a positive value (commented out below) |
| 47 | + # 2. Setting negative values to Zero (commented out) |
| 48 | + # 3. Multiply by -1 (implemented below, found most reliable method) |
| 49 | + # ImgX += 128.0 |
| 50 | + # ImgY += 128.0 |
| 51 | + for ind1 in range(w): |
| 52 | + for ind2 in range(h): |
| 53 | + if ImgY[ind1][ind2] < 0: |
| 54 | + ImgY[ind1][ind2] *= -1 |
| 55 | + # ImgY[ind1][ind2] = 0 |
| 56 | + if ImgX[ind1][ind2] < 0: |
| 57 | + ImgX[ind1][ind2] *= -1 |
| 58 | + # ImgX[ind1][ind2] = 0 |
| 59 | + |
| 60 | + # # Display the output results after Sobel operations |
| 61 | + # cv2.imshow("SobelX", ImgX) |
| 62 | + # cv2.imshow("SobelY", ImgY) |
| 63 | + |
| 64 | + ImgX_2 = np.square(ImgX) |
| 65 | + ImgY_2 = np.square(ImgY) |
| 66 | + |
| 67 | + ImgXY = np.multiply(ImgX, ImgY) |
| 68 | + ImgYX = np.multiply(ImgY, ImgX) |
| 69 | + |
| 70 | + #Use Gaussian Blur |
| 71 | + Sigma = 1.4 |
| 72 | + kernelsize = (3, 3) |
| 73 | + |
| 74 | + ImgX_2 = cv2.GaussianBlur(ImgX_2, kernelsize, Sigma) |
| 75 | + ImgY_2 = cv2.GaussianBlur(ImgY_2, kernelsize, Sigma) |
| 76 | + ImgXY = cv2.GaussianBlur(ImgXY, kernelsize, Sigma) |
| 77 | + ImgYX = cv2.GaussianBlur(ImgYX, kernelsize, Sigma) |
| 78 | + # print(ImgXY.shape, ImgYX.shape) |
| 79 | + |
| 80 | + alpha = 0.06 |
| 81 | + R = np.zeros((w, h), np.float32) |
| 82 | + # For every pixel find the corner strength |
| 83 | + for row in range(w): |
| 84 | + for col in range(h): |
| 85 | + M_bar = np.array([[ImgX_2[row][col], ImgXY[row][col]], [ImgYX[row][col], ImgY_2[row][col]]]) |
| 86 | + R[row][col] = np.linalg.det(M_bar) - (alpha * np.square(np.trace(M_bar))) |
| 87 | + return R |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + #### Main Program #### |
| 91 | + firstimagename = "checkerboard.png" |
| 92 | + |
| 93 | + # Get the first image |
| 94 | + bgr = cv2.imread(firstimagename) |
| 95 | + firstimage = cv2.cvtColor(bgr, cv2.COLOR_RGB2GRAY) |
| 96 | + w, h = firstimage.shape |
| 97 | + |
| 98 | + # Covert image to color to draw colored circles on it |
| 99 | + bgr = cv2.cvtColor(firstimage, cv2.COLOR_GRAY2RGB) |
| 100 | + |
| 101 | + # Corner detection |
| 102 | + R = HarrisCornerDetection(firstimage) |
| 103 | + |
| 104 | + # Empirical Parameter |
| 105 | + # This parameter will need tuning based on the use-case |
| 106 | + CornerStrengthThreshold = 20000 |
| 107 | + |
| 108 | + # Plot detected corners on image |
| 109 | + radius = 1 |
| 110 | + color = (0, 255, 0) # Green |
| 111 | + thickness = 1 |
| 112 | + |
| 113 | + PointList = [] |
| 114 | + # Look for Corner strengths above the threshold |
| 115 | + for row in range(w): |
| 116 | + for col in range(h): |
| 117 | + if R[row][col] > CornerStrengthThreshold: |
| 118 | + # print(R[row][col]) |
| 119 | + max = R[row][col] |
| 120 | + |
| 121 | + # Local non-maxima suppression |
| 122 | + skip = False |
| 123 | + for nrow in range(5): |
| 124 | + for ncol in range(5): |
| 125 | + if row + nrow - 2 < w and col + ncol - 2 < h: |
| 126 | + if R[row + nrow - 2][col + ncol - 2] > max: |
| 127 | + skip = True |
| 128 | + break |
| 129 | + |
| 130 | + if not skip: |
| 131 | + # Point is expressed in x, y which is col, row |
| 132 | + cv2.circle(bgr, (col, row), radius, color, thickness) |
| 133 | + PointList.append((row, col)) |
| 134 | + |
| 135 | + # Display image indicating corners and save it |
| 136 | + cv2.imshow("Corners", bgr) |
| 137 | + outname = "Output_" + str(CornerStrengthThreshold) + ".png" |
| 138 | + cv2.imwrite(outname, bgr) |
| 139 | + |
| 140 | + cv2.waitKey(0) |
| 141 | + cv2.destroyAllWindows() |
0 commit comments