3 ms·
Thanks for the feedback. I did not think about this. Some of the square edges actually belongs to the surroundings squares because the cut is not 100% accurate
by matthiasb 15y ago
Thanks for the feedback. I did not think about this.
Some of the square edges actually belongs to the surroundings squares because the cut is not 100% accurate and the chess piece are not always position at the center of the square but I could get around that.
The way this algorithm works is simple. I compare the RGB values for each pixel of a square to the black color and white color. Black is Red=0, Green=0, Blue=0 and white is Red=255, Green=255 and Blue=255. I add up the 3 RGB values for a pixel, if it is closer to 0 and 255*3, I consider the pixel is mostly black. I keep a counter of black pixels and another for white pixels.
Currently, the algo is just comparing the two counters. If I found most of the pixel were black, I consider it is a black square.
However, I could improve it as following:
- > 90% of black pixels: empty black square or black square with black piece
- > 90% of white pixels: empty white square or white square with white piece
- other white: square with a piece of the opposite color.
For the 3rd case, I might need to implement your idea.
- max99x 15y agoMost pieces are positioned in the middle of a square. A simple way to recognize the color of the piece and its square is to do a flood fill with a bit of tolerance (say 32/256) from the center pixel. The area covered by the flood fill will only contain the pixels making up the piece, while the area outside the flood fill will have pixels the color of the square. Reflections might cause a bit of noise, but a slight gaussian blur should be enough to get over that.
- danohuiginn 15y agoRemember that not all chessboards are black and white. some are green and white, for instance.