0

I've tried using the findChessboardCorners function in open CV python. But it's not working. These are the images I'm trying to get it to detect these images.

board.jpg:

1

board2.jpg:

2

I want it to be able to detect where the squares are and if a piece is on it.

So far I've tried

import cv2 as cv
import numpy as np

def rescaleFrame(frame, scale=0.75):
    #rescale image
    width = int(frame.shape[1] * scale)
    height = int(frame.shape[0] * scale)

    dimensions = (width,height)

    return cv.resize(frame, dimensions, interpolation=cv.INTER_AREA)

img = cv.imread("board2.jpg")
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

ret, corners = cv.findChessboardCorners(gray, (8,8),None)

if ret == True:
   
   # Draw and display the corners
   img = cv.drawChessboardCorners(img, (8,8), corners,ret)

img=rescaleFrame(img)
cv.imshow("board",img)
v.waitKey(0)

I was expect it to work like how this tutorial shows

3

1 Answer 1

0

The function findChessboardCorners is used to calibrate cameras using a black-and-white chessboard pattern. As far as I know, is not designed to detect the corners of a chess board with chess pieces on it.

This site shows an example of calibration "chess boards." And this site shows how these calibration chess boards are used, this example uses the ROS Library.

You can still use OpenCV but will need to try other functions. Assuming you took the photos yourself, you've also made the problem harder on yourself by using a background that has a lot of lines and corners, meaning you'll have to differentiate between those corners and corners on the board. You can also see that the top corners of the board behind the rooks are occluded. If you can retake the photos, I would take a top-down photo and do it on a blank surface that contrasts with the chessboard.

One example of corner detection in OpenCV is Harris corner detection. I wrote up a short example for you. You'll need to play around with this and other corner detection methods to see what works best. I found that adding a sobel filter to strength the lines in your image gave much better results. But it's still going to detect corners in the background and the corners on the pieces. You'll need to figure out how to filter those out.

import cv2 as cv
from matplotlib import pyplot as plt
import numpy as np

def sobel(src_image, kernel_size):
    grad_x = cv.Sobel(src_image, cv.CV_16S, 1, 0, ksize=kernel_size, scale=1,
                      delta=0, borderType=cv.BORDER_DEFAULT)
    grad_y = cv.Sobel(src_image, cv.CV_16S, 0, 1, ksize=kernel_size, scale=1, 
                      delta=0, borderType=cv.BORDER_DEFAULT)
    abs_grad_x = cv.convertScaleAbs(grad_x)
    abs_grad_y = cv.convertScaleAbs(grad_y)

    grad = cv.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)

    return grad

def process_image(src_image_path):
    # load the image
    src_image = cv.imread(src_image_path)
    # convert to RGB (otherwise when you display this image the colors will look incorrect)
    src_image = cv.cvtColor(src_image, cv.COLOR_BGR2RGB)
    # convert to grayscale before attempting corner detection
    src_gray = cv.cvtColor(src_image, cv.COLOR_BGR2GRAY)

    # standard technique to eliminate noise
    blur_image = cv.blur(src_gray,(3,3))

    # strengthen the appearance of lines in the image
    sobel_image = sobel(blur_image, 3)

    # detect corners
    corners = cv.cornerHarris(sobel_image, 2, 3, 0.04)
    # for visualization to make corners easier to see
    corners = cv.dilate(corners, None)

    # overlay on a copy of the source image
    dest_image = np.copy(src_image)
    dest_image[corners>0.01*corners.max()]=[0,0,255]
    return dest_image 

src_image_path = "board1.jpg"
dest_image = process_image(src_image_path)
plt.imshow(dest_image)
plt.show()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.