Jimit Vaghela
Jimit Vaghela

Reputation: 758

How to find corners of an object using OpenCV?

So I am trying to find the corners of an object using Harris Corner detection in opencv. I should be getting 5 exact corners but instead I am getting 6. There seems to be a problem.

    import cv2
import numpy as np

def find_centroids(dst):
    ret, dst = cv2.threshold(dst, 0.01 * dst.max(), 255, 0)
    dst = np.uint8(dst)

    # find centroids
    ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
    # define the criteria to stop and refine the corners
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 
                0.001)
    corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5), 
              (-1,-1),criteria)
    return corners

image = cv2.imread("C:\\Users\\Jimit\\Desktop\\Project\\lmao.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

gray = np.float32(gray)

dst = cv2.cornerHarris(gray, 3, 3, 0.04)

dst = cv2.dilate(dst, None)

# Threshold for an optimal value, it may vary depending on the image.
# image[dst > 0.01*dst.max()] = [0, 0, 255]

# Get coordinates
corners = find_centroids(dst)
# To draw the corners
for corner in corners:
    image[int(corner[1]), int(corner[0])] = [0, 0, 255]
int_corners = np.asarray(corners, dtype = int)
print (int_corners)
print ("Pixels for corner 1 is: ", int_corners[0])
print ("Pixels for corner 2 is: ", int_corners[1])
print ("Pixels for corner 3 is: ", int_corners[2])

cv2.imshow('dst', image)
cv2.imwrite('corners.jpg', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Original Image

Desired Output corner points

.

The extra corner

6 corner pixels

.

Upvotes: 2

Views: 1982

Answers (1)

api55
api55

Reputation: 11420

Your problem is in this line:

ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)

You assume that all the centroids/labels are for each of the corners... but one is actually for the background (label 0) as it is explained in the documentation:

centroid output for each label, including the background label. Centroids are accessed via centroids(label, 0) for x and centroids(label, 1) for y. The data type CV_64F.

and also:

returns N, the total number of labels [0, N-1] where 0 represents the background label.

Now, knowing this, the solution is easy, just replace this instruction:

corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5), 
          (-1,-1),criteria)

with:

corners = cv2.cornerSubPix(gray,np.float32(centroids[1:]),(5,5), 
          (-1,-1),criteria)

note the [1:] in centroids. This will give you the following points:

[[223 121]
 [153 191]
 [290 194]
 [152 275]
 [287 277]]

As you can see the first point is removed.

Upvotes: 2

Related Questions