Reputation: 41
My aim is to obtain the recognition of colour and position of pawns in Chessgame. The recognition was pretty easy because of many tutorials on github. With opencv functions cv2.findChessboardCorners and cv2.drawChessboardCorners (clear Chessboard) I obtained satisfied result as in the picture below:
But when I try to get the corners of image with recognized pawns or of image of pawns on Chessboard (not empty Chessboard) it fail and I don't get any result.
I don't attach any code because I use only this two functions.
How could i fix that? Do i have to do any manual calibration?
I hope that someone has done something similiar and could solve my problem.
Upvotes: 4
Views: 1337
Reputation: 941
My answer is in C++ but is easily transferable to Python.
You are using the function findChessboardCorners
of OpenCV which is actually not a function for finding corners on any possible chessboard. The function is usually used for camera calibration (determination of camera paramters) and requires e.g. a self-printed black and white chessboard. An older example can be found here. Interesting that it works also for your brown chessboard.
For finding chessboard corners I would recommend a different OpenCV workflow.
I am reading the image, applying a bilateral filter on the image to smooth the image but preserve edges and then I use the function goodFeaturesToTrack
to find the strongest corners in the image. Since you did not provide an image with no additional lines I used a random internet image of a brown chessboard: The C++ code is as follows:
int main(int argc, char** argv)
{
// Reading the images
cv::Mat img = cv::imread("chessboard_2.jpg", cv::IMREAD_GRAYSCALE);
cv::Mat img_rgb = cv::imread("chessboard_2.jpg", cv::IMREAD_COLOR);
cv::Mat bilateral_filtered_image;
// Applying a bilateral filter to smooth the image
cv::bilateralFilter(img, bilateral_filtered_image, 5, 75, 75);
cv::namedWindow("Bilateral Filter", cv::WINDOW_NORMAL);
cv::imshow("Bilateral Filter", bilateral_filtered_image);
cv::waitKey(0);
// Find corner points using goodFeaturestoTrack with appropriate parameters
std::vector<cv::Point2f> corners;
cv::goodFeaturesToTrack(bilateral_filtered_image, corners, 200, 0.01, 120);
std::cout << corners.size() << std::endl;
// Draw all corners on the original image and show the image
for (size_t i = 0; i < corners.size(); ++i) {
cv::circle(img_rgb, cv::Point2f(corners[i].x, corners[i].y), 7, (0,0,255), 3);
}
cv::namedWindow("Corners", cv::WINDOW_NORMAL);
cv::imshow("Corners", img_rgb);
cv::waitKey(0);
}
Upvotes: 3