0

I am trying to identify squares on a chess Board and assign them them a ID which will be their square numbers like a1, a2, h1, ...h8. I applied contour detection on the image of the chess Board with some pre-editing like first converting the to grayscale, then canny edge detection along with houghlines. This is the result that I got. Image with contours detected.

I successfully got the required 64 contours but I am not able to assign them their respective IDs like this : Chess Squares Notations I tried iterating through the contours but they were differently arranged every time.

How do I assign IDs(which are basically chess square notations) to the 64 contours ?

Arsh
  • 3
  • 1
  • 1
    Calculate the center of each contour, and sort using the centers' `(x, y)` coordinates. – HansHirse Jul 01 '21 at 12:56
  • You could also build a dictionary mapping your IDs with the square centroids. – stateMachine Jul 01 '21 at 14:20
  • @HansHirse In this way , I am either able to sort them either vertically or horizontally but not in a grid like system. If I do it wrt x coordinate of the center of contour, then the columns (example - b1, b2, b3, b4...) are randomly arrange and a similar problem occurs if I do it wrt y coordinate. How do I sort using both x and y. – Arsh Jul 01 '21 at 18:31
  • You can have sort functions w.r.t. two variables!? [In this answer](https://stackoverflow.com/a/65901643/11089932), I've actually implemented that before. – HansHirse Jul 01 '21 at 19:29

1 Answers1

0

What you can do is for ever contours calculate its centroid and give that point some id , now when you iterate through new set of contours calculate their centroids. Now you have 2 set for centroids , previous_c and current_c, You have already assigned every previous_c a given id and now for that previous_c find the closest current_c (you can use Euclidian distance or Manhattan distance) and assign it the same ID. So basically you are doing object tracking but for contours .

Atharva Gundawar
  • 475
  • 3
  • 10