Skip to content

Added Harris Feature Detector to Digital Image Processing #801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions digital_image_processing/feature_detectors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Computation of Harris Detector
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be really helpful if you can add this file in Algorithms-Explanation repo. That will help us in future to organise the code and documentation and parse them using scripts.

Copy link
Author

@GreatestCapacity GreatestCapacity May 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already Added. Pull request 34.


## Dependencies

- opencv-python
- Numpy
- Scipy

## Steps

Given image $I$, $n\times n$ size Gaussian Kernel $G_{n\times n}$,

1. Compute the gradients of the image, both horizontal and vertical directions. $X=(-1, 0, 1)\otimes I​$, $Y=(-1, 0, 1)^T \otimes I​$
2. Compute the matrix $M$, where $A = G_{n\times n} \otimes X^2$, $B=G_{n\times n}\otimes Y^2$, $C=G_{n\times n}\otimes XY$
3. Compute the response function $R​$, where $R=AB-C^2-k(A+B)​$
4. Classify all points in $R​$.

## Reference

C. Harris and M. Stephens, “A Combined Corner and Edge Detector,” in Proceedings of Alvey Vision Conference 1988, Manchester, 1988, pp. 23.1-23.6.
35 changes: 35 additions & 0 deletions digital_image_processing/feature_detectors/harris.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Implementation of Harris Detector

Reference:
C. Harris and M. Stephens, “A Combined Corner and Edge Detector,”
in Proceedings of Alvey Vision Conference 1988, Manchester, 1988, pp. 23.1-23.6.
"""

import cv2 as cv
import numpy as np
from scipy.signal import convolve2d

# Read original image and get gaussian kernel
img = cv.imread('example.png')
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

G = cv.getGaussianKernel(3, 1)

# Begin Harris Detector Computation

I = img_gray.astype('float32')
X = convolve2d(I, [[-1, 0, 1]], mode='same')
Y = convolve2d(I, [[-1], [0], [1]], mode='same')
A = convolve2d(X*X, G, mode='same')
B = convolve2d(Y*Y, G, mode='same')
C = convolve2d(X*Y, G, mode='same')
R = A*B - C*C - 0.04 * (A + B)

# Finished Harris Detector Computation

# Classify all points in R and show corners in image
maxima = np.max(R)
img[R > maxima*0.1] = [255, 0, 0]
cv.imshow('harris_detector', img)
cv.waitKey(0)