2020-06-04 14:02:51 +00:00
|
|
|
import cv2
|
2020-07-06 07:44:19 +00:00
|
|
|
import numpy as np
|
2020-06-04 14:02:51 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
Harris Corner Detector
|
|
|
|
https://en.wikipedia.org/wiki/Harris_Corner_Detector
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class Harris_Corner:
|
|
|
|
def __init__(self, k: float, window_size: int):
|
|
|
|
|
|
|
|
"""
|
|
|
|
k : is an empirically determined constant in [0.04,0.06]
|
|
|
|
window_size : neighbourhoods considered
|
|
|
|
"""
|
|
|
|
|
|
|
|
if k in (0.04, 0.06):
|
|
|
|
self.k = k
|
|
|
|
self.window_size = window_size
|
|
|
|
else:
|
|
|
|
raise ValueError("invalid k value")
|
|
|
|
|
2021-10-23 21:26:21 +00:00
|
|
|
def __str__(self) -> str:
|
2020-06-04 14:02:51 +00:00
|
|
|
|
|
|
|
return f"Harris Corner detection with k : {self.k}"
|
|
|
|
|
2021-10-23 21:26:21 +00:00
|
|
|
def detect(self, img_path: str) -> tuple[cv2.Mat, list[list[int]]]:
|
2020-06-04 14:02:51 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
Returns the image with corners identified
|
|
|
|
img_path : path of the image
|
|
|
|
output : list of the corner positions, image
|
|
|
|
"""
|
|
|
|
|
|
|
|
img = cv2.imread(img_path, 0)
|
|
|
|
h, w = img.shape
|
2021-10-23 21:26:21 +00:00
|
|
|
corner_list: list[list[int]] = []
|
2020-06-04 14:02:51 +00:00
|
|
|
color_img = img.copy()
|
|
|
|
color_img = cv2.cvtColor(color_img, cv2.COLOR_GRAY2RGB)
|
|
|
|
dy, dx = np.gradient(img)
|
2022-01-30 19:29:54 +00:00
|
|
|
ixx = dx**2
|
|
|
|
iyy = dy**2
|
2020-06-04 14:02:51 +00:00
|
|
|
ixy = dx * dy
|
|
|
|
k = 0.04
|
|
|
|
offset = self.window_size // 2
|
|
|
|
for y in range(offset, h - offset):
|
|
|
|
for x in range(offset, w - offset):
|
|
|
|
wxx = ixx[
|
|
|
|
y - offset : y + offset + 1, x - offset : x + offset + 1
|
|
|
|
].sum()
|
|
|
|
wyy = iyy[
|
|
|
|
y - offset : y + offset + 1, x - offset : x + offset + 1
|
|
|
|
].sum()
|
|
|
|
wxy = ixy[
|
|
|
|
y - offset : y + offset + 1, x - offset : x + offset + 1
|
|
|
|
].sum()
|
|
|
|
|
2022-01-30 19:29:54 +00:00
|
|
|
det = (wxx * wyy) - (wxy**2)
|
2020-06-04 14:02:51 +00:00
|
|
|
trace = wxx + wyy
|
2022-01-30 19:29:54 +00:00
|
|
|
r = det - k * (trace**2)
|
2020-06-04 14:02:51 +00:00
|
|
|
# Can change the value
|
|
|
|
if r > 0.5:
|
|
|
|
corner_list.append([x, y, r])
|
|
|
|
color_img.itemset((y, x, 0), 0)
|
|
|
|
color_img.itemset((y, x, 1), 0)
|
|
|
|
color_img.itemset((y, x, 2), 255)
|
|
|
|
return color_img, corner_list
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
edge_detect = Harris_Corner(0.04, 3)
|
|
|
|
color_img, _ = edge_detect.detect("path_to_image")
|
|
|
|
cv2.imwrite("detect.png", color_img)
|