2

I am new using OpenCV and I am having a problem detecting corners in a image. Supposing I have an image-grid like this one where there are multiple corners.

enter image description here

I'd like to catch just the 4 extreme corners, highlighted in red.

Using cv2.cornerHarris() I've managed to highlight all the possible corners really accurately but I can't find a way to adjust these values and let just the 4 extreme remain.

This is my code:

import cv2
import numpy as np

filename = 'start.jpg'
img = cv2.imread(filename)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

gray = np.float32(gray)
dst = cv2.cornerHarris(gray,2,29,0.04)

#result is dilated for marking the corners, not important
dst = cv2.dilate(dst,None)

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

cv2.imshow('dst',img)
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()

I've got this code from the official OpenCV website and I've adjusted few values.

Anyone be able to help? Am I in the completely wrong direction? If I've not been clear what I'd like to have is the output-image the I've attached in my question while my code is just giving me all the possible corners in the grid.

Grid does not start from coordinates x,y = 0,0

Thank you in advance guys

1
  • Probably, you can try using Hough Transform on the image. This will give you coordinates(start,end) of all the line segments in the image. Then, you can compare them, if the square is not rotated in the image, with respect to the corners found in corner harris. Commented Jan 6, 2017 at 14:29

2 Answers 2

2

I have another solution using contours.

  1. Convert image to grayscale
  2. Obtain binary image
  3. Perform morphological close operation
  4. Find contour present externally

    contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

  5. Obtain the bounding rectangle around this contour. You will get the coordinates of this rectangle, which are the corners of the grid

    cnts = contours[0] x,y,w,h = cv2.boundingRect(cnts)

enter image description here

  1. Draw those corners on the grid

    cv2.circle(im,(x,y), 3, (0,0,255), -1) cv2.circle(im,(x+w,y), 3, (0,0,255), -1) cv2.circle(im,(x+w,y+h), 3, (0,0,255), -1) cv2.circle(im,(x,y+h), 3, (0,0,255), -1) cv2.imshow("Corners of grid", img)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

There is no easy solution if the grid is in a generic position in the image. The grid extremal corners might even not be detected by the Harris filter, depending on the quality of the image itself. For a robust solution you will need to explicitly model the topology of the graph of detected corners (i.e. find out which corner is neighbor of which ones in the grid). Here is an example of how this is done for the case of a checkerboard.

For a somewhat less robust solution, you could compute the convex hull of the detected corner distribution, then fit 4 lines to it. The lines' intersections would then yield the extremal corners.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.