Harris corner detection and localization in OpenCV with Python

8.8k views Asked by At

I'm using the following code to try to detect corners of polylines in order to 'measure' the lines. The code is based on a snippet I found somewhere on SO and is based on cv2.cornerHarris():

cornerimg = cv2.cornerHarris( gray, # src
                              2,    # blockSize
                              3,    # ksize / aperture
                              0.04  # k 
                                    # dst
                                    # borderType
                              )

# ?
cornerimg = cv2.normalize(  cornerimg,        # src
                            None,             # dst
                            0,                # alpha
                            255,              # beta
                            cv2.NORM_MINMAX,  # norm type
                            cv2.CV_32FC1,     # dtype
                            None              # mask
                            )

# ?
cornerimg = cv2.convertScaleAbs( cornerimg )

cornershow = cornerimg.copy()

# iterate over pixels to get corner positions
w, h = gray.shape
for y in range(0, h):
  for x in range (0, w):
    #harris = cv2.cv.Get2D( cv2.cv.fromarray(cornerimg), y, x)
    #if harris[0] > 10e-06:
    if cornerimg[x,y] > 64:
      print("corner at ", x, y)
      cv2.circle( cornershow,  # dest
                  (x,y),      # pos
                  4,          # radius
                  (115,0,25)  # color
                  )

cv2.imshow('harris cornerimg', cornershow)

The original code results in white spots at the corner location and the level seems to be an indicator of "corneryness". The snippet (updated to use cv2) iterates over the resulting image and scans for values lager than 10e-06 for some reason, I have replaced this with a comparison of what I think should be the brightness in the image.

However, the circles drawn at those locations are nowhere near the actual hot-spots found in the normalized harris output.

What am I doing wrong?

Alternatively, cv2.goodFeaturesToTrack() can be set to use Harris (useHarrisDetector=True) but my attempt to use it does not result in what cornerHarris appears to detect properly:

cv2.goodFeaturesToTrack(  blurred,                # img
                                500,                    # maxCorners
                                0.03,                   # qualityLevel
                                10,                     # minDistance
                                None,                   # corners, 
                                None,                   # mask, 
                                2,                      # blockSize, 
                                useHarrisDetector=True, # useHarrisDetector, 
                                k=0.04                  # k
                                )

What would be the equivalent function call to cv2.cornerHarris()?

2

There are 2 answers

1
PostingAsAGuest On BEST ANSWER

The output seems to be transposed, swapping x and y indices on a square image fixes it (circles are on corner maxima).

0
Bumseok On

try below:

  cv2.circle( cornershow,  # dest
              (y,x),      # pos
              4,          # radius
              (115,0,25)  # color
              )