I'm trying to use OpenCV to read the state of a chess board. I couldn't figure out any of the built-in ways to recognize shapes, so I decided to try and make my own square detection. My approach was to find a line of continuous contours in the direction of the x axis, moving on to the next if nothing was found or trying to find a line in the y direction if an x line had been found. It ended up looking like
def xLineFinder(pixels, contours, base):
if (pixels == 0):
return True
for contour in contours:
if contour[0] == base[0] + 1 and contour[1] - base[1] > -4 and contour[1] - base[1] < 4:
isLine = xLineFinder(pixels - 1, contours, contour)
if (isLine == True):
return True
return False
def bottomCornerFinder(pixels, contours, base):
if (xLineFinder(pixels, contours, base) == True):
for i in range(9):
guessBase = base
guessBase[0] = guessBase[0] + pixels
guessBase[1] = guessBase[1] - 4 + i
found = yLineFinder(pixels, contours, guessBase)
if (found == True):
return True
return False
The yLineFinder was identical to the xLineFinder, but with contour[0]/base[0] as contour1/base1 and vice versa. pixels refers to the length of the line desired, contours is the contour map I got from cv2.findContours, and base refers to the coordinate where I'm looking for a line from. The output I got was
File "...\boardReader.py", line 16, in xLineFinder
if contour[0] == base[0] + 1 and contour[1] - base[1] > -4 and contour[1] - base[1] < 4:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I can only think that I'm misunderstanding something about what contours are, but that also makes me think my approach is also fundamentally wrong. This is my first time using OpenCV (and Python, for that matter) and would appreciate some help with how to proceed.
Also, if it's helpful, here's my input, with and without the contours drawn on it.