// 包含OpenCV库中用于3D校准的相关头文件
#include "opencv2/calib3d.hpp"
// 包含OpenCV库中用于图像编码解码的相关头文件
#include "opencv2/imgcodecs.hpp"
// 包含OpenCV库中用于GUI操作的相关头文件
#include "opencv2/highgui.hpp"
// 包含OpenCV库中用于图像处理的相关头文件
#include "opencv2/imgproc.hpp"
// 包含OpenCV库中用于处理ChArUco板的相关头文件
#include "opencv2/objdetect/charuco_detector.hpp"
// 引入一些常用的标准库
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// 使用cv和std名称空间中的变量和函数,避免每次调用时都写cv::和std::
using namespace cv;
using namespace std;
// 声明一个静态函数print_help,用来打印程序的使用说明
// print_help函数的实现:打印使用帮助说明
static int print_help(char** argv)
{
// 输出程序的使用方法,该方法包括双目校准过程中所需的参数说明
cout <<
" Given a list of chessboard or ChArUco images, the number of corners (nx, ny)\n"
" on the chessboards and the number of squares (nx, ny) on ChArUco,\n"
" and a flag: useCalibrated for \n"
" calibrated (0) or\n"
" uncalibrated \n"
" (1: use stereoCalibrate(), 2: compute fundamental\n"
" matrix separately) stereo. \n"
" Calibrate the cameras and display the\n"
" rectified results along with the computed disparity images. \n" << endl;
// 输出程序的具体使用格式,包括棋盘宽度,高度,模式类型(棋盘或ChArUco),平方大小,标记大小,预定义的aruco字典名称,aruco字典文件和图像列表XML/YML文件
cout << "Usage:\n " << argv[0] << " -w=<board_width default=9> -h=<board_height default=6>"
<<" -t=<pattern type: chessboard or charucoboard default=chessboard> -s=<square_size default=1.0> -ms=<marker size default=0.5>"
<<" -ad=<predefined aruco dictionary name default=DICT_4X4_50> -adf=<aruco dictionary file default=None>"
<<" <image list XML/YML file default=stereo_calib.xml>\n" << endl;
// 打印可用的Aruco字典列表信息
cout << "Available Aruco dictionaries: DICT_4X4_50, DICT_4X4_100, DICT_4X4_250, "
<< "DICT_4X4_1000, DICT_5X5_50, DICT_5X5_100, DICT_5X5_250, DICT_5X5_1000, "
<< "DICT_6X6_50, DICT_6X6_100, DICT_6X6_250, DICT_6X6_1000, DICT_7X7_50, "
<< "DICT_7X7_100, DICT_7X7_250, DICT_7X7_1000, DICT_ARUCO_ORIGINAL, "
<< "DICT_APRILTAG_16h5, DICT_APRILTAG_25h9, DICT_APRILTAG_36h10, DICT_APRILTAG_36h11\n";
// 函数返回0,表示成功执行
return 0;
}
// 声明一个静态函数StereoCalib,用于执行双目相机的校准
// StereoCalib函数的实现:执行双目摄像头的校准
static void
// 函数定义,包括所需的参数
StereoCalib(const vector<string>& imagelist, Size inputBoardSize, string type, float squareSize, float markerSize, cv::aruco::PredefinedDictionaryType arucoDict, string arucoDictFile, bool displayCorners = false, bool useCalibrated=true, bool showRectified=true)
{
// 检查图像列表的数量是否为偶数,否则返回错误
if( imagelist.size() % 2 != 0 )
{
cout << "Error: the image list contains odd (non-even) number of elements\n";
return;
}
// 定义变量和存储来进行校准过程
const int maxScale = 2;
// ARRAY AND VECTOR STORAGE:
// 创建两个图像点数组和一个对象点向量,以及图像大小变量
vector<vector<Point2f> > imagePoints[2];
vector<vector<Point3f> > objectPoints;
Size imageSize;
// 定义一些需要的索引变量和图像数量
int i, j, k, nimages = (int)imagelist.size()/2;
// 调整图像点数组的大小以匹配图像的数量
imagePoints[0].resize(nimages);
imagePoints[1].resize(nimages);
// 创建一个存储良好图像的列表
vector<string> goodImageList;
// 定义棋盘的两种尺寸,内角尺寸和单位尺寸
Size boardSizeInnerCorners, boardSizeUnits;
// 检查棋盘的类型,并依此计算板大小
if (type == "chessboard") {
// 若是普通棋盘,则内角大小即为给定的板大小
boardSizeInnerCorners = inputBoardSize;
// 棋盘单位尺寸需要增加1,因为边缘的格子也要计算进去
boardSizeUnits.height = inputBoardSize.height+1;
boardSizeUnits.width = inputBoardSize.width+1;
}
else if (type == "charucoboard") {
// 若是ChArUco棋盘,板大小则是以方块为单位给出的
boardSizeUnits = inputBoardSize;
// 减去1以得到内角尺寸
boardSizeInnerCorners.width = inputBoardSize.width - 1;
boardSizeInnerCorners.height = inputBoardSize.height - 1;
}
else {
// 若棋盘类型未知,则输出错误并返回
std::cout << "unknown pattern type " << type << "\n";
return;
}
// 定义并初始化Aruco字典
cv::aruco::Dictionary dictionary;
// 如果未指定字典文件,则使用预定义字典
if (arucoDictFile == "None") {
dictionary = cv::aruco::getPredefinedDictionary(arucoDict);
}
else {
// 否则从文件中加载字典
cv::FileStorage dict_file(arucoDictFile, cv::FileStorage::Mode::READ);
cv::FileNode fn(dict_file.root());
dictionary.readDictionary(fn);
}
// 创建ChArUco板和检测器对象
cv::aruco::CharucoBoard ch_board(boardSizeUnits, squareSize, markerSize, dictionary);
cv::aruco::CharucoDetector ch_detector(ch_board);
// 创建一个用来存储标记的ID的容器
std::vector<int> markerIds
【opencv】示例-stereo_calib.cpp 基于OpenCV的立体视觉相机校准的完整示例
最新推荐文章于 2024-04-13 13:14:06 发布