Spaces:
Runtime error
Runtime error
Harris
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
from scipy import signal as sig
|
4 |
+
import numpy as np
|
5 |
+
from scipy.ndimage.filters import convolve
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
def gradient_x(imggray):
|
9 |
+
##Sobel operator kernels.
|
10 |
+
kernel_x = np.array([[-1, 0, 1],[-2, 0, 2],[-1, 0, 1]])
|
11 |
+
return sig.convolve2d(imggray, kernel_x, mode='same')
|
12 |
+
|
13 |
+
def gradient_y(imggray):
|
14 |
+
kernel_y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
|
15 |
+
return sig.convolve2d(imggray, kernel_y, mode='same')
|
16 |
+
|
17 |
+
def gaussian_kernel(size, sigma=1):
|
18 |
+
size = int(size) // 2
|
19 |
+
x, y = np.mgrid[-size:size+1, -size:size+1]
|
20 |
+
normal = 1 / (2.0 * np.pi * sigma**2)
|
21 |
+
g = np.exp(-((x**2 + y**2) / (2.0*sigma**2))) * normal
|
22 |
+
return g
|
23 |
+
|
24 |
+
|
25 |
+
def harris(img):
|
26 |
+
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
27 |
+
I_x = gradient_x(img_gray)
|
28 |
+
I_y = gradient_y(img_gray)
|
29 |
+
Ixx = convolve(I_x**2, gaussian_kernel(3, 1))
|
30 |
+
Ixy = convolve(I_y*I_x, gaussian_kernel(3, 1))
|
31 |
+
Iyy = convolve(I_y**2, gaussian_kernel(3, 1))
|
32 |
+
|
33 |
+
|
34 |
+
k = 0.05
|
35 |
+
|
36 |
+
# determinant
|
37 |
+
detA = Ixx * Iyy - Ixy ** 2
|
38 |
+
# trace
|
39 |
+
traceA = Ixx + Iyy
|
40 |
+
|
41 |
+
harris_response = detA - k * traceA ** 2
|
42 |
+
|
43 |
+
window_size = 3
|
44 |
+
offset = window_size//2
|
45 |
+
width, height = img_gray.shape
|
46 |
+
|
47 |
+
for y in range(offset, height-offset):
|
48 |
+
for x in range(offset, width-offset):
|
49 |
+
Sxx = np.sum(Ixx[y-offset:y+1+offset, x-offset:x+1+offset])
|
50 |
+
Syy = np.sum(Iyy[y-offset:y+1+offset, x-offset:x+1+offset])
|
51 |
+
Sxy = np.sum(Ixy[y-offset:y+1+offset, x-offset:x+1+offset])
|
52 |
+
|
53 |
+
det = (Sxx * Syy) - (Sxy**2)
|
54 |
+
trace = Sxx + Syy
|
55 |
+
r = det - k*(trace**2)
|
56 |
+
|
57 |
+
|
58 |
+
img_copy_for_corners = np.copy(img)
|
59 |
+
img_copy_for_edges = np.copy(img)
|
60 |
+
|
61 |
+
for rowindex, response in enumerate(harris_response):
|
62 |
+
for colindex, r in enumerate(response):
|
63 |
+
if r > 0:
|
64 |
+
# this is a corner
|
65 |
+
img_copy_for_corners[rowindex, colindex] = [255,0,0]
|
66 |
+
elif r < 0:
|
67 |
+
# this is an edge
|
68 |
+
img_copy_for_edges[rowindex, colindex] = [0,255,0]
|
69 |
+
|
70 |
+
return img_copy_for_corners
|
71 |
+
|
72 |
+
|
73 |
+
interface = gr.Interface(
|
74 |
+
title = "Harris Corner Detector 🤖",
|
75 |
+
description = "<h3>The idea is to locate interest points where the surrounding neighbourhood shows edges in more than one direction.</h3> <br> <b>Select an image 🖼</b>",
|
76 |
+
article='Step-by-step on GitHub <a href="https://github.com/Ivanrs297/machine-learning-projects/blob/main/computer-vision/edge_detection/main.ipynb"> notebook </a> <br> ~ Ivanrs',
|
77 |
+
allow_flagging = "never",
|
78 |
+
fn = harris,
|
79 |
+
inputs = [
|
80 |
+
gr.Image(),
|
81 |
+
],
|
82 |
+
outputs = "image"
|
83 |
+
)
|
84 |
+
|
85 |
+
interface.launch(share = False)
|