How to Detect Corners in an Image in Python using OpenCV



Python


In this article, we show how to detect corners in an image in Python using the OpenCV module.

OpenCV has algorithms available that can allow us to detect corners in an image.

There are 2 main algorithms used in OpenCV for corner detection: the Harris corner detection method and the goodFeaturesToTrack() method.

We will go over both of these methods in this article for corner detection.

Harris Corner Detection Method

The first method we will go over is the harris corner detection method.

Below is a image of rectangles (or boxes).



After applying harris corner detection, we have the following image below.

Rectangles with harris corner detection method in Python using OpenCV

As another image, let's say that we have the chess board shown below.

Chess board

After applying harris corner detection, we have the following image shown below.

Chess board with harris corner detection method in Python using OpenCV

So let's see how to get this functioning in code.



Let's now go over this code.

First, we import OpenCV using the line, import cv2

Next, we read in the image, which in this case is, Boxes.png

We then create a grayscale version of the image.

In order to use the harris corner detection method, the grayscale image must be converted to float32 type.

We then apply the harris corner detection method to the grayscale image using the cv2.cornerHarris() method.

We then want to be able to add rectangles around each corner that is detected.

We create a variable, kernel, to create a rectangle box.

We then use the cv2.dilate() function to place this kernel at the harris corners.

We then set the color to blue.

We then show the image

We then have a for loop in which we go through each of the contours in the image.

We create a variable, accuracy, that wet set equal to 0.03 * cv2.arcLength(c,True). You can adjust this value a little, but this value works well in this case.

We then create another variable, approx, which we set equal to the approxPolyDP() function, which uses the accuracy we set previously on the contour.

We then draw the contours based on the approx variable with a green color with a line thickness of 2.

We then show the image.

So this is the harris corner detection method.

goodFeaturesToTrack Detection Method

The other method to detect corners in Python using OpenCV is the goodFeaturesToTrack() method.

So with the same original images shown above, the goodFeaturesToTrack() method gives us the following images.

Rectangles with goodFeaturesToTrack detection method in Python using OpenCV

The chess board is shown below.

Chess board with goodFeaturesToTrack detection method in Python using OpenCV

So now we will go over the code to perform corner detection with the goodFeaturesToTrack detection method.



So first, we import the OpenCV module.

We then read in the image, Boxes.png.

We then create the grayscale version of this image.

We then create a variable, corners, which stores the corners in an image.

We then create a for loop that loops through of the corners, which we then use the cv2.rectangle() function to create a rectangle around each corner.

We then show the image.

And this is corner detection using the goodFeaturesToTrack() method.

So we can use either the Harris Corner detection method or the goodFeaturesToTrack() detection method for corner detection in Python with OpenCV.

And this is how we can detect corners in an image in Python using OpenCV.


Related Resources

How to Draw a Rectangle in Python using OpenCV

How to Draw a Circle in Python using OpenCV

How to Draw a Line in Python using OpenCV

How to Add Text to an Image in Python using OpenCV

How to Display an OpenCV image in Python with Matplotlib

How to Use Callback functions to Connect Images to Events in Python using OpenCV

How to Check for Multiple Events in Python using OpenCV



HTML Comment Box is loading comments...