传感器标定在测量领域是不可或缺的一部分,相机标定较为简单,现在主流标定方法采用棋盘格标定、圆点标定等方式完成。本文从简单入手,应用opencv 中的标定方法实现相机标定。
相机标定的整体思路非常清晰,在已知某个标定的数学模型(以小孔成像为例)后,建立其数学表达式,进而采集多组标定数据进行优化求解即可。
网上已经有非常多的原理介绍,这里不展开详细说明。
[1] Zhang Z . A Flexible New Technique for Camera Calibration[J]. IEEE Transactions on Pattern Analysis and Machine Intelligence, 2000, 22(11):1330-1334.
[2]sylvester0510—张正友标定算法原理详解
直接上编写的代码,整体上的代码思路很简单,数据采集->角点检测->定义世界坐标点->数据回归拟合->保存标定参数。
首先是single_camera_calibrated.h文件,
#pragma once
#ifndef CAMERA_CALIBRATED
#define CAMERA_CALIBRATED
#include
#include
#include
class CameraCalibrated
{
public:
CameraCalibrated() {};
~CameraCalibrated() {};
/**
* @brief get_image_list
* 获取标定文件夹内部的图片路径
* @param image_dir_path 输入图片文件夹的路径
* @param format 输入图片的文件格式,默认为 .bmp 的格式
*/
std::vector<std::string> get_image_list(const char* image_dir_path,
const std::string format = ".jpg");
/**
* @brief single_camera_calibrated
* 单相机标定算法(仅用于debug模式)
* @param camera_grab_image 输入标定图片数据
*/
void single_camera_calibrated(const std::vector<cv::Mat> camera_grab_image);
/**
* @brief set_camera_paramter
* 设置棋盘格的参数
* @param pattern_width 输入棋盘格宽度的角点数量
* @param pattern_height 输入棋盘格高度的角点数量
* @param chessboard_size_mm 输入棋盘格中的小格物理尺寸(mm单位)
*/
void set_camera_paramter(const int pattern_width = 9,
const int pattern_height = 8,
const double chessboard_size_mm = 30) {
pattern_size.width = pattern_width;
pattern_size.height = pattern_height;
chessboard_size.width = chessboard_size_mm;
chessboard_size.height = chessboard_size_mm;
return;
}
private:
/**
* @brief image_corner_detection
* 单相机标定的图片角点检测函数
* @param input_image 输入标定图片数据
* @param corners_seq 输出对应的角点
* @param object_points 输出角点对应的世界坐标点
* @return 是否成功检测角点
*/
bool image_corner_detection(const cv::Mat input_image,
std::vector<std::vector<cv::Point2f>> &corners_seq,
std::vector<std::vector<cv::Point3f>> &object_points);
cv::Size pattern_size = cv::Size(9, 8); ///< 棋盘格行列数
cv::Size chessboard_size = cv::Size(30, 30); ///< 棋盘格物理尺寸
};
#endif //end CAMERA_CALIBRATED
其次是对应的single_camera_calibrated.cpp文件
#include "single_camera_calibrated.h"
std::vector<std::string> CameraCalibrated::get_image_list(const char* image_dir_path,
const std::string format) {
std::vector<std::string> image_list;
std::string image_dir_pathStr = (std::string)image_dir_path;
std::string buffer = image_dir_pathStr + "/*" + format;
_finddata_t c_file; // 存放文件名的结构体
intptr_t hFile;
hFile = _findfirst(buffer.c_str(), &c_file); // 找第一个文件命
if (hFile == -1L) // 检查文件夹目录下存在需要查找的文件
printf("No %s files in current directory!\n", format);
else {
std::string full_file_path;
do {
full_file_path.clear();
full_file_path = image_dir_pathStr + "/" + c_file.name;
image_list.push_back(full_file_path);
} while (_findnext(hFile, &c_file) == 0);
_findclose(hFile);
}
return image_list;
}
void CameraCalibrated::single_camera_calibrated(const std::vector<cv::Mat> camera_grab_image) {
if (camera_grab_image.size() == 0) {
printf("No picture found, please re-enter the calibration picture \n");
return;
}
cv::Size image_size;
if (camera_grab_image[0].data) {
image_size = camera_grab_image[0].size();
}
else { printf("No picture found, please re-enter the calibration picture \n"); return; };
cv::Mat camera_matrix = cv::Mat(3, 3, CV_32FC1, cv::Scalar::all(0));
cv::Mat distCoeffs_matrix = cv::Mat(1, 5, CV_32FC1, cv::Scalar::all(0));
std::vector<cv::Mat> rvec;
std::vector<cv::Mat> tvec;
std::vector<std::vector<cv::Point3f>> object_points;
std::vector<std::vector<cv::Point2f>> corners_seq;
for (size_t i = 0; i < camera_grab_image.size(); i++) {
cv::Mat grab_image = camera_grab_image[i].clone();
image_corner_detection(grab_image, corners_seq, object_points);
cv::namedWindow("cornersframe", 2);
cv::imshow("cornersframe", grab_image);
cv::waitKey(1);
}
cv::destroyWindow("cornersframe");
if (object_points.size() == 0) {
printf("No corner points on the image were found. Please detect the image again. \n");
return;
}
printf("Start calculating the camera_matrix of a single camera. \n");
cv::TermCriteria criteria = cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 50, DBL_EPSILON);
calibrateCamera(object_points, corners_seq, image_size,
camera_matrix, distCoeffs_matrix,
rvec, tvec, 0, criteria);
printf("The camera internal parameter matrix is: \n");
std::cout << camera_matrix << std::endl;
printf("The camera distortion matrix is: \n");
std::cout << distCoeffs_matrix << std::endl;
cv::FileStorage resultStore("./data/getCameraParams.txt", cv::FileStorage::APPEND);
resultStore << "camera_matrix" << camera_matrix;
resultStore << "camera_distCoeffs_matrix" << distCoeffs_matrix;
resultStore.release();
return;
}
bool CameraCalibrated::image_corner_detection(const cv::Mat input_image,
std::vector<std::vector<cv::Point2f>> &corners_seq,
std::vector<std::vector<cv::Point3f>> &object_points) {
bool is_detection = false;
std::vector<cv::Point2f> corners;
cv::Mat input_image_gray;
cv::cvtColor(input_image, input_image_gray, cv::COLOR_BGR2GRAY);
//cv::equalizeHist(input_image_gray, input_image);
//cv::GaussianBlur(input_image, input_image, Size(7, 7), 1);
if (cv::findChessboardCorners(input_image_gray, pattern_size, corners, 0)) {
cv::TermCriteria criteria = cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::MAX_ITER, 30, 0.001);
cv::cornerSubPix(input_image_gray, corners, cv::Size(23, 23), cv::Size(-1, -1), criteria);
corners_seq.push_back(corners); // 存入角点序列
cv::drawChessboardCorners(input_image, pattern_size, corners, true);
is_detection = true;
std::vector<cv::Point3f> real_point_set;
for (int i = 0; i < pattern_size.height; i++) {
for (int j = 0; j < pattern_size.width; j++) {
cv::Point3d realPoint;
realPoint.x = j * chessboard_size.width;
realPoint.y = i * chessboard_size.height;
realPoint.z = 0;
real_point_set.push_back(realPoint);
}
}
object_points.push_back(real_point_set);
return is_detection;
}
printf("Corner detection failed.\n");
return is_detection;
};
最后测试文件test_camera_calibrated.cpp文件
#include "single_camera_calibrated.h"
int main(int argc, char ** argv) {
CameraCalibrated cc;
std::string camera_image_dir_path = "./data/camera_calibrated";
cc.set_camera_paramter(10,7,30);
std::vector<std::string> image_list_path;
image_list_path = cc.get_image_list(camera_image_dir_path.c_str(),".bmp");
std::vector<cv::Mat> image_calibrated;
for (size_t i = 0; i < image_list_path.size(); i++){
cv::Mat image = cv::imread(image_list_path[i]);
if (!image.data){
printf("no picture imread ( %s ). \n", image_list_path[i].c_str());
continue;
}
image_calibrated.push_back(image);
}
cc.single_camera_calibrated(image_calibrated);
return 0;
}
需要注意的是,需要根据实际棋盘格的角点长宽数量来定义。