Determination of coordinates

Algorithm for determination of coordinates

The objective of this final stage is to correctly determine the chess coordinates (a1 to h8) of all the individual squares on the chessboard and label them in the original image.

The first step in this stage is to obtain the pixel location information from the output of the previous stage. This information is summarized in the Centroid property of the output of the regionprops() function. To facilitate our data processing, we will separate the two values for each Centroid pair into x and y.

Fig. 4.7.1

Fig. 4.7.2

Each Centroid measurement represents the location of the "center of mass" of each connected region (since our connected regions are roughly rectangular, the location is roughly the middle of each region). The x property represents the horizontal coordinate of the center of mass, while the y property represents the vertical coordinate of the center of mass. In order to match this pixel location information to chess square coordinates information, we need to sort the rows of the table (allIndividualSquareStats) in an orderly manner. For example, we can sort them such that the first row corresponds to the bottom left square (whose coordinates are a1), the second row corresponds to the second-from-bottom left square (whose coordinates are a2), the ninth row corresponds to the bottom second-from-left square (whose coordinates are b1), and so on ("bottom-to-top-and-then-left-to-right manner").

Now, even though our chessboard images are not taken such that the sides of the chessboard are perfectly horizontal and vertical, the chessboards in our images are "properly oriented" enough that all eight squares in a column have similar x values and all eight squares in a row have similar y values. With this assumption, we can convert the x and y values into integers between 0 and 7 (with 0 representing the leftmost/bottom-most location and 7 representing the rightmost/topmost location) by sorting the rows and columns of the table and performing integer-division and remainder operations.

Fig. 4.7.3

We also need to create the chess coordinates information in an orderly manner. This can be done with two for loops and string concatenation.

Fig. 4.7.4

We can now join the two pieces of information together.

Fig. 4.7.5

Finally, we use the insertText() function to label the coordinates on top of the squares of the original image.

Fig. 4.7.6

This concludes our discussion of our algorithm for processing a chessboard image in which the chessboard does not have any piece on it. (Note that the determination of whether or not an image contains a chessboard in the first place is accomplished in the last step of the stage of separation of connected squares, where the number of connected regions is compared to the value 64.)