From 6105a92ad9298971eb727dcb2dab91d005b6c7c2 Mon Sep 17 00:00:00 2001 From: GreatestCapacity Date: Sun, 12 May 2019 03:11:22 +0800 Subject: [PATCH 1/3] Add Harris feature detector --- .../feature_detectors/README.md | 20 +++++++++++ .../feature_detectors/harris.py | 35 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 digital_image_processing/feature_detectors/README.md create mode 100644 digital_image_processing/feature_detectors/harris.py diff --git a/digital_image_processing/feature_detectors/README.md b/digital_image_processing/feature_detectors/README.md new file mode 100644 index 000000000000..a00b88865efb --- /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 Procedings 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..0319b0873b5a --- /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 Procedings 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_RGB2GRAY) + +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) From 1c6321189222dc72c203bb72022a2c5ea02e4cae Mon Sep 17 00:00:00 2001 From: GreatestCapacity Date: Sun, 12 May 2019 03:14:03 +0800 Subject: [PATCH 2/3] Add Harris feature detector --- digital_image_processing/feature_detectors/harris.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/digital_image_processing/feature_detectors/harris.py b/digital_image_processing/feature_detectors/harris.py index 0319b0873b5a..81d0cd4731ba 100644 --- a/digital_image_processing/feature_detectors/harris.py +++ b/digital_image_processing/feature_detectors/harris.py @@ -12,7 +12,7 @@ # Read original image and get gaussian kernel img = cv.imread('example.png') -img_gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY) +img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) G = cv.getGaussianKernel(3, 1) From 9655961a41f68d3fde6ccd6c127665eb926dc507 Mon Sep 17 00:00:00 2001 From: GreatestCapacity Date: Sun, 12 May 2019 03:26:49 +0800 Subject: [PATCH 3/3] Add Harris feature detector --- digital_image_processing/feature_detectors/README.md | 2 +- digital_image_processing/feature_detectors/harris.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/digital_image_processing/feature_detectors/README.md b/digital_image_processing/feature_detectors/README.md index a00b88865efb..eea71b6ee5ec 100644 --- a/digital_image_processing/feature_detectors/README.md +++ b/digital_image_processing/feature_detectors/README.md @@ -17,4 +17,4 @@ Given image $I$, $n\times n$ size Gaussian Kernel $G_{n\times n}$, ## Reference -C. Harris and M. Stephens, “A Combined Corner and Edge Detector,” in Procedings of Alvey Vision Conference 1988, Manchester, 1988, pp. 23.1-23.6. \ No newline at end of file +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 index 81d0cd4731ba..b7ff231c1107 100644 --- a/digital_image_processing/feature_detectors/harris.py +++ b/digital_image_processing/feature_detectors/harris.py @@ -3,7 +3,7 @@ Reference: C. Harris and M. Stephens, “A Combined Corner and Edge Detector,” -in Procedings of Alvey Vision Conference 1988, Manchester, 1988, pp. 23.1-23.6. +in Proceedings of Alvey Vision Conference 1988, Manchester, 1988, pp. 23.1-23.6. """ import cv2 as cv