import cv2
import numpy as np
from matplotlib import pyplot as plt
img_path = 'inputs/checkerboard.png'
k = 0.05
window_size = 5
threshold = 1000000000.00
input_img = cv2.imread(img_path, 0)
def find_harris_corners(input_img, k, window_size, threshold):
corner_list = []
output_img = cv2.cvtColor(input_img.copy(), cv2.COLOR_GRAY2RGB)
offset = int(window_size/2)
y_range = input_img.shape[0] - offset
x_range = input_img.shape[1] - offset
dy, dx = np.gradient(input_img)
Ixx = dx**2
Ixy = dy*dx
Iyy = dy**2
for y in range(offset, y_range):
for x in range(offset, x_range):
#Values of sliding window
start_y = y - offset
end_y = y + offset + 1
start_x = x - offset
end_x = x + offset + 1
#The variable names are representative to
#the variable of the Harris corner equation
windowIxx = Ixx[start_y : end_y, start_x : end_x]
windowIxy = Ixy[start_y : end_y, start_x : end_x]
windowIyy = Iyy[start_y : end_y, start_x : end_x]
#Sum of squares of intensities of partial derevatives
Sxx = windowIxx.sum()
Sxy = windowIxy.sum()
Syy = windowIyy.sum()
#Calculate determinant and trace of the matrix
det = (Sxx * Syy) - (Sxy**2)
trace = Sxx + Syy
#Calculate r for Harris Corner equation
c = det - k*(trace**2)
if c > threshold:
corner_list.append([x, y, c])
output_img[y,x] = (255, 255, 255)
else:
grayscale = ((threshold - c)/threshold)*100
output_img[y,x] = (int(grayscale),int(grayscale),int(grayscale))
return corner_list, output_img
def main():
if input_img is not None:
print ("Detecting Corners Started!")
corner_list, corner_img = find_harris_corners(input_img, k, window_size, threshold)
# cv2.imshow('Detected Corners', corner_img)
# cv2.waitKey(0)
plt.figure(figsize=(12,8))
plt.imshow(corner_img)
plt.show()
if corner_img is not None:
cv2.imwrite("corners_img.png", corner_img)
else:
print ("Error in input image!")
print ("Detecting Corners Complete!")
if __name__ == "__main__":
main()