diff --git a/digital_image_processing/feature_detectors/README.md b/digital_image_processing/feature_detectors/README.md new file mode 100644 index 000000000000..eea71b6ee5ec --- /dev/null +++ b/digital_image_processing/feature_detectors/README.md @@ -0,0 +1,20 @@ +# Computation of Harris Detector + +## 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. \ No newline at end of file diff --git a/digital_image_processing/feature_detectors/harris.py b/digital_image_processing/feature_detectors/harris.py new file mode 100644 index 000000000000..b7ff231c1107 --- /dev/null +++ b/digital_image_processing/feature_detectors/harris.py @@ -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)