In this OpenCV article iam going to show you How to Detect Corner in OpenCV with CornerHarris.
basically in this article we are using CornerHarris algorithm.
Also you can check my previous articles on Python OpenCV
1: OpenCV Python Introduction And Reading Images
2: OpenCV Python Writing o An Image
3: OpenCV Python Reading Image In Matplotlib Graph
4: OpenCV Python Drawing Shapes
6: How To Play AVI Videos In OpenCV
7: Python OpenCV Creating Color Trackbar
8: Python OpenCV Reading Mp4 Videos
9: Python OpenCV Writing To A Videos
10: OpenCV Python Arithmetic Operations
11: OpenCV Python Image Blending
12: Python OpenCV Bitwise Operations On Images
13: Python OpenCV Color Spaces
14: Face Detection in Python OpenCV
15: OpenCV Python Eye Detection
16: Python OpenCV Color Detection
17: OpenCV Python Smile Detection With Haar Cascade
18: Python OpenCV Canny Edge Detection
19: Python OpenCV Contour Detection Example
20: Line Detection In Python OpenCV With HoughLines
21: Python OpenCV Circle Detection With HoughCircles
22: Python OpenCV Foreground Detection
So now this is the complete code for How to Detect Corner in OpenCV with CornerHarris
|
1 2 3 4 5 6 7 8 9 10 11 |
import cv2 import numpy as np img = cv2.imread('chess_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("HarrisCorner Detection", img) cv2.waitKey() cv2.destroyAllWindows() |
In this article iam using this image

OK in the above first of all we have loaded the image and also we have done conversion on our image
|
1 2 3 |
img = cv2.imread('chess_board.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = np.float32(gray) |
So this is the important point and our CornerHarris algorithm
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) |
So now run the complete code and this will be the result

Also you can watch the complete video for this article