Reputation: 5422
I'm working on a project of 3D calibration with OpenCV using the Chessboard. The calibration works fine, but I want to recognize objects in the chessboard that are also black and should be different from one another, as in the image below. I don't know how to do this. Which OpenCV functions would be helpful to achieve this goal?
after the suggestion of @Aurelius I tried to use the cv::matchTemplate, it works fine when I run it in the first but when I run it on a capture the result is totally wrong see the next image
any idea how this could be solve
Upvotes: 3
Views: 1174
Reputation: 3408
Template matching is not rotation invariant. Do you rotate the chessboard image before template matching (this is what calibration is needed for).
Upvotes: 0
Reputation: 11329
If you know what the shapes will look like ahead of time and your chessboard image is taken straight-on like your example, it looks like a perfect case for cv::matchTemplate()
. The code below searches the image for areas which best match the template images.
cv::Mat chessboard = cv::imread(path_to_image);
cv::Mat template1 = cv::imread(temp1_path);
cv::Mat template2 = cv::imread(temp2_path);
cv::Mat cross_corr;
cv::Point maxloc;
// Find the first template
cv::matchTemplate(chessboard, template1, cross_corr, CV_TM_CCORR_NORMED);
cv::minMaxLoc(cross_corr, nullptr, nullptr, nullptr, &maxloc); //Only want location of maximum response
cv::Rect t1rect(maxloc,template1.size());
//Find the second template
cv::matchTemplate(chessboard, template2, cross_corr, CV_TM_CCORR_NORMED);
cv::minMaxLoc(cross_corr, nullptr,nullptr,nullptr,&maxloc);
cv::Rect t2rect(maxloc, template2.size());
//Draw the results
cv::rectangle(chessboard, t1rect, cv::Scalar(255,0,0), 3);
cv::rectangle(chessboard, t2rect, cv::Scalar(0,0,255), 3);
cv::imshow("detection", chessboard);
Using these templates:
The code above results in the following output:
Upvotes: 3