findChessboardCorners 在 opencv 4.1.2 c++ 中不起作用
findChessboardCorners don't work in opencv 4.1.2 c++
我正在尝试校准我的相机。对于我阅读的内容,我需要进行一些测试 -> findChessboard;
我有真正的国际象棋,我拍了它的照片(它是 8x4),我设置了 Size(8-1,4-1)->Size(7,3)
机器人面板设置为 32(因为 4 乘以 8 得到 32(或者我应该写 7*3->21))。当我想找到 ChessBoard 时它说我是假的;
会有这个棋盘的图片:
我已经尝试了所有方法来找到 ChessboardCorners->我给它标记为 CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FILTER_QUADS 或 CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_FILTER_QUADS。它也没有用。我试图给它 image colored(non-gray) image 它也没有用。我该怎么办?
编码我尝试做的事情:
VideoCapture vc(0);
int numBoards = 0;
int numCornersHor;
int numCornersVer;
printf("Enter number of corners along width: ");
scanf("%d", &numCornersHor);
printf("Enter number of corners along height: ");
scanf("%d", &numCornersVer);
printf("Enter number of boards: ");
scanf("%d", &numBoards);
int numSquares = numCornersHor * numCornersVer;
Size board_sz = Size(numCornersHor, numCornersVer);
vector<vector<Point3f>> object_points;
vector<vector<Point2f>> image_points;
vector<Point2f> corners;
int successes = 0;
Mat image;
Mat gray_image;
vc >> image;
for (int j = 0; j < numSquares; j++)
obj.push_back(Point3f(j / numCornersHor, j % numCornersHor, 0.0f));
while (successes < numBoards)
{
if (image.empty())break;
cvtColor(image, gray_image, COLOR_BGR2GRAY);
bool found = findChessboardCorners
(image, board_sz, corners, CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FILTER_QUADS);
if (found)
{
cornerSubPix(gray_image, corners, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS | TermCriteria::MAX_ITER, 30, 0.1));
drawChessboardCorners(gray_image, board_sz, corners, found);
}
imshow("win1", image);
imshow("win2", gray_image);
if (image.empty())break;
vc >> image;
int key = waitKey(1);
if (key == 27)
return 0;
if (key == ' ' && found != 0)
{
image_points.push_back(corners);
object_points.push_back(obj);
printf("Snap stored!");
successes++;
if (successes >= numBoards)
break;
}
}
查看您的输入图像,我看到两个可能导致角点检测失败的问题。
首先,图像太暗了。提供一些漫射照明。否则,即使检测到角点,传感器噪声也很大,检测精度会很差。
其次,如findChessboardCorners()
的OpenCV documentation所述:
Note: The function requires white space (like a square-thick border, the wider the better) around the board to make the detection more robust in various environments. Otherwise, if there is no border and the background is dark, the outer black squares cannot be segmented properly and so the square grouping and ordering algorithm fails.
我建议像 this 一样打印出具有足够白色边框的高质量电路板图像,将其粘贴到一些坚固的平面(例如木板)上并在光线充足的环境中进行校准。这样它不仅应该检测角点,而且检测的准确性应该比您使用的设置更好。
如果您需要有关高质量相机校准的更多提示,请查看 this answer。
我正在尝试校准我的相机。对于我阅读的内容,我需要进行一些测试 -> findChessboard; 我有真正的国际象棋,我拍了它的照片(它是 8x4),我设置了 Size(8-1,4-1)->Size(7,3) 机器人面板设置为 32(因为 4 乘以 8 得到 32(或者我应该写 7*3->21))。当我想找到 ChessBoard 时它说我是假的;
会有这个棋盘的图片:
我已经尝试了所有方法来找到 ChessboardCorners->我给它标记为 CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FILTER_QUADS 或 CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_FILTER_QUADS。它也没有用。我试图给它 image colored(non-gray) image 它也没有用。我该怎么办?
编码我尝试做的事情:
VideoCapture vc(0);
int numBoards = 0;
int numCornersHor;
int numCornersVer;
printf("Enter number of corners along width: ");
scanf("%d", &numCornersHor);
printf("Enter number of corners along height: ");
scanf("%d", &numCornersVer);
printf("Enter number of boards: ");
scanf("%d", &numBoards);
int numSquares = numCornersHor * numCornersVer;
Size board_sz = Size(numCornersHor, numCornersVer);
vector<vector<Point3f>> object_points;
vector<vector<Point2f>> image_points;
vector<Point2f> corners;
int successes = 0;
Mat image;
Mat gray_image;
vc >> image;
for (int j = 0; j < numSquares; j++)
obj.push_back(Point3f(j / numCornersHor, j % numCornersHor, 0.0f));
while (successes < numBoards)
{
if (image.empty())break;
cvtColor(image, gray_image, COLOR_BGR2GRAY);
bool found = findChessboardCorners
(image, board_sz, corners, CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FILTER_QUADS);
if (found)
{
cornerSubPix(gray_image, corners, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS | TermCriteria::MAX_ITER, 30, 0.1));
drawChessboardCorners(gray_image, board_sz, corners, found);
}
imshow("win1", image);
imshow("win2", gray_image);
if (image.empty())break;
vc >> image;
int key = waitKey(1);
if (key == 27)
return 0;
if (key == ' ' && found != 0)
{
image_points.push_back(corners);
object_points.push_back(obj);
printf("Snap stored!");
successes++;
if (successes >= numBoards)
break;
}
}
查看您的输入图像,我看到两个可能导致角点检测失败的问题。
首先,图像太暗了。提供一些漫射照明。否则,即使检测到角点,传感器噪声也很大,检测精度会很差。
其次,如findChessboardCorners()
的OpenCV documentation所述:
Note: The function requires white space (like a square-thick border, the wider the better) around the board to make the detection more robust in various environments. Otherwise, if there is no border and the background is dark, the outer black squares cannot be segmented properly and so the square grouping and ordering algorithm fails.
我建议像 this 一样打印出具有足够白色边框的高质量电路板图像,将其粘贴到一些坚固的平面(例如木板)上并在光线充足的环境中进行校准。这样它不仅应该检测角点,而且检测的准确性应该比您使用的设置更好。
如果您需要有关高质量相机校准的更多提示,请查看 this answer。