About Lesson
In this Python OpenCV lesson we are going to learn about Python OpenCV Corner Detection, for this purpose we are going to use CornerHarris algorithm.
This is the complete code for this lesson.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import cv2 import numpy as np img = cv2.imread("board.png") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = np.float32(gray) dst = cv2.cornerHarris(gray, 2, 3,0.04) img[dst>0.01 * dst.max()] = [0,0,255] cv2.imshow("Harris Corner Detection", img) cv2.waitKey() cv2.destroyAllWindows() |
In this code we have loaded the image and also we have done conversion on our image, make sure that you have already added the image in your working directory.
1 2 3 |
img = cv2.imread("board.png") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = np.float32(gray) |
OpenCV has the function cv2.cornerHarris() for this purpose. Its arguments are :
- img – Input image, it should be grayscale and float32 type.
- blockSize – It is the size of neighbourhood considered for corner detection
- ksize – Aperture parameter of Sobel derivative used.
- k – Harris detector free parameter in the equation.
1 |
dst = cv2.cornerHarris(gray, 2, 3,0.04) |
Run the complete code and this is the result.