相机标定示例(OpenCV /C++ /matlab工具箱TOOLBOX_calib)

这里相机标定主要内容包括:图像的采集、相机参数获取、TOOLBOX_calib可视化描述

 

一、相机标定

感谢博主的代码分享,这里主要参考了一些(https://download.csdn.net/download/u011574296/9918396

下边是详细步骤代码:

代码可以直接运行,只是有几条要求:

1、要将照片路径换成自己的,照片获取很简单,只要打印一张棋盘格,用手拿着在不同位置用摄像头拍摄就可以了

2、下边头文件很多,有些没有用,如果出现找不到头文件的问题就把相应头文件删除就行了

3、棋盘格上交点的个数要根据你的来做修改,我这里设置的是9*6的

#include <iostream>
#include <vector>
#include <iterator>
#include <tuple>
#include <set>
#include<opencv2\opencv.hpp>
#include <opencv2\imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui.hpp>
#include <opencv2\stitching.hpp>
#include "opencv2/features2d/features2d.hpp"  
#include <opencv2\core\ocl.hpp>
#include <opencv2/xfeatures2d.hpp>
#include<opencv2\features2d\features2d.hpp>
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_DEPRECATE
#pragma warning(disable:4996)
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <ctime>
#include<typeinfo>
#include <io.h> 


using namespace cv;
using namespace std;

/*
@param File_Directory 为文件夹目录
@param FileType 为需要查找的文件类型
@param FilesName 为存放文件名的容器
*/
void getFilesName(string& File_Directory, string& FileType, vector<string>& FilesName)
{
	string buffer = File_Directory + "\\*" + FileType;

	_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", FileType);
	else
	{
		string fullFilePath;
		do
		{
			fullFilePath.clear();

			//名字
			fullFilePath = File_Directory + "\\" + c_file.name;
			cout << fullFilePath << endl;
			FilesName.push_back(fullFilePath);

		} while (_findnext(hFile, &c_file) == 0);  //如果找到下个文件的名字成功的话就返回0,否则返回-1  
		_findclose(hFile);
	}
}




void m_calibration(vector<string>& FilesName, Size board_size, Size square_size, Mat& cameraMatrix, Mat& distCoeffs, vector<Mat>& rvecsMat, vector<Mat>& tvecsMat)
{
	ofstream fout("caliberation_result.txt");                       // 保存标定结果的文件 

	cout << "开始提取角点………………" << endl;
	int image_count = 0;                                            // 图像数量 
	Size image_size;                                                // 图像的尺寸 

	vector<Point2f> image_points;                                   // 缓存每幅图像上检测到的角点
	vector<vector<Point2f>> image_points_seq;                       // 保存检测到的所有角点

	for (int i = 0; i < FilesName.size(); i++)
	{
		image_count++;

		// 用于观察检验输出
		cout << "image_count = " << image_count << endl;
		Mat imageInput = imread(FilesName[i]);
		if (image_count == 1)  //读入第一张图片时获取图像宽高信息
		{
			image_size.width = imageInput.cols;
			image_size.height = imageInput.rows;
			cout << "image_size.width = " << image_size.width << endl;
			cout << "image_size.height = " << image_size.height << endl;
		}

		/* 提取角点 */
		bool ok = findChessboardCorners(imageInput, board_size, image_points, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE);
		if (0 == ok)
		{
			cout << "第" << image_count << "张照片提取角点失败,请删除后,重新标定!" << endl; //找不到角点
			imshow("失败照片", imageInput);
			waitKey(0);
		}
		else
		{
			Mat view_gray;
			cout << "imageInput.channels()=" << imageInput.channels() << endl;
			cvtColor(imageInput, view_gray, CV_RGB2GRAY);

			/* 亚像素精确化 */
			//find4QuadCornerSubpix(view_gray, image_points, Size(5, 5)); //对粗提取的角点进行精确化
			cv::cornerSubPix(view_gray, image_points, cv::Size(11, 11), cv::Size(-1, -1), cv::TermCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 20, 0.01));

			image_points_seq.push_back(image_points);  //保存亚像素角点

			/* 在图像上显示角点位置 */
			drawChessboardCorners(view_gray, board_size, image_points, true);

			imshow("Camera Calibration", view_gray);//显示图片
			waitKey(100);//暂停0.1S		
		}
	}
	cout << "角点提取完成!!!" << endl;


	/*棋盘三维信息*/
	vector<vector<Point3f>> object_points_seq;                     // 保存标定板上角点的三维坐标

	for (int t = 0; t < image_count; t++)
	{
		vector<Point3f> object_points;
		for (int i = 0; i < board_size.height; i++)
		{
			for (int j = 0; j < board_size.width; j++)
			{
				Point3f realPoint;
				/* 假设标定板放在世界坐标系中z=0的平面上 */
				realPoint.x = i * square_size.width;
				realPoint.y = j * square_size.height;
				realPoint.z = 0;
				object_points.push_back(realPoint);
			}
		}
		object_points_seq.push_back(object_points);
	}

	/* 运行标定函数 */
	double err_first = calibrateCamera(object_points_seq, image_points_seq, image_size, cameraMatrix, distCoeffs, rvecsMat, tvecsMat, CV_CALIB_FIX_K3);
	fout << "重投影误差1:" << err_first << "像素" << endl << endl;
	cout << "标定完成!!!" << endl;


	cout << "开始评价标定结果………………";
	double total_err = 0.0;            // 所有图像的平均误差的总和 
	double err = 0.0;                  // 每幅图像的平均误差
	double totalErr = 0.0;
	double totalPoints = 0.0;
	vector<Point2f> image_points_pro;     // 保存重新计算得到的投影点

	for (int i = 0; i < image_count; i++)
	{

		projectPoints(object_points_seq[i], rvecsMat[i], tvecsMat[i], cameraMatrix, distCoeffs, image_points_pro);   //通过得到的摄像机内外参数,对角点的空间三维坐标进行重新投影计算

		err = norm(Mat(image_points_seq[i]), Mat(image_points_pro), NORM_L2);

		totalErr += err * err;
		totalPoints += object_points_seq[i].size();

		err /= object_points_seq[i].size();
		//fout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;
		total_err += err;
	}
	fout << "重投影误差2:" << sqrt(totalErr / totalPoints) << "像素" << endl << endl;
	fout << "重投影误差3:" << total_err / image_count << "像素" << endl << endl;


	//保存定标结果  	
	cout << "开始保存定标结果………………" << endl;
	Mat rotation_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); /* 保存每幅图像的旋转矩阵 */
	fout << "相机内参数矩阵:" << endl;
	fout << cameraMatrix << endl << endl;
	fout << "畸变系数:\n";
	fout << distCoeffs << endl << endl << endl;
	for (int i = 0; i < image_count; i++)
	{
		fout << "第" << i + 1 << "幅图像的旋转向量:" << endl;
		fout << rvecsMat[i] << endl;

		/* 将旋转向量转换为相对应的旋转矩阵 */
		Rodrigues(rvecsMat[i], rotation_matrix);
		fout << "第" << i + 1 << "幅图像的旋转矩阵:" << endl;
		fout << rotation_matrix << endl;
		fout << "第" << i + 1 << "幅图像的平移向量:" << endl;
		fout << tvecsMat[i] << endl << endl;
	}
	cout << "定标结果完成保存!!!" << endl;
	fout << endl;
}

void m_undistort(vector<string>& FilesName, Size image_size, Mat& cameraMatrix, Mat& distCoeffs)
{

	Mat mapx = Mat(image_size, CV_32FC1);   //X 坐标重映射参数
	Mat mapy = Mat(image_size, CV_32FC1);   //Y 坐标重映射参数
	Mat R = Mat::eye(3, 3, CV_32F);
	cout << "保存矫正图像" << endl;
	string imageFileName;                  //校正后图像的保存路径
	stringstream StrStm;
	string temp;

	for (int i = 0; i < FilesName.size(); i++)
	{
		Mat imageSource = imread(FilesName[i]);

		Mat newimage = imageSource.clone();

		//方法一:使用initUndistortRectifyMap和remap两个函数配合实现
		//initUndistortRectifyMap(cameraMatrix,distCoeffs,R, Mat(),image_size,CV_32FC1,mapx,mapy);
		//	remap(imageSource,newimage,mapx, mapy, INTER_LINEAR);

		//方法二:不需要转换矩阵的方式,使用undistort函数实现
		undistort(imageSource, newimage, cameraMatrix, distCoeffs);

		StrStm << i + 1;
		StrStm >> temp;
		imageFileName = "矫正后图像//" + temp + "_d.jpg";
		imwrite(imageFileName, newimage);

		StrStm.clear();
		imageFileName.clear();
	}
	std::cout << "保存结束" << endl;
}

int main()
{
	string File_Directory1 = "C:\\Users\\86778\\source\\repos\\opencv_contrib_0202\\chess_image";   //文件夹目录1

	string FileType = ".jpg";    // 需要查找的文件类型

	//存放文件名的容器
	//vector<string>FilesName1 = { "left01.jpg","left03.jpg" };
	vector<string>FilesName1;
	getFilesName(File_Directory1, FileType, FilesName1);   // 标定所用图像文件的路径
	//cout << FilesName1.size();

	Size board_size = Size(9, 6);                         // 标定板上每行、列的角点数 
	Size square_size = Size(30, 30);                       // 实际测量得到的标定板上每个棋盘格的物理尺寸,单位mm

	Mat cameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0));        // 摄像机内参数矩阵
	Mat distCoeffs = Mat(1, 5, CV_32FC1, Scalar::all(0));          // 摄像机的5个畸变系数:k1,k2,p1,p2,k3
	vector<Mat> rvecsMat;                                          // 存放所有图像的旋转向量,每一副图像的旋转向量为一个mat
	vector<Mat> tvecsMat;                                          // 存放所有图像的平移向量,每一副图像的平移向量为一个mat

	m_calibration(FilesName1, board_size, square_size, cameraMatrix, distCoeffs, rvecsMat, tvecsMat);

	//m_undistort(FilesName1, image_size, cameraMatrix, distCoeffs);
	waitKey(0);
	return 0;
}

标定显示的结果:

这里只列举两张,实际上有十几张,然后会输出相机内参与畸变参数,这就是相机标定最想得到的东西

到此为止相机标定已经结束了,接下来使用matlab的TOOLBOX_calib工具箱给大家可视化展示一下

 

二、matlab工具箱标定+可视化

下载位置:http://www.vision.caltech.edu/bouguetj/calib_doc/download/index.html

首先把解压的TOOLBOX_calib文件夹的路径设置到MATLAB里,在主页->设置路径->选择工具箱路径,然后添加路径

然后输入指令calib_gui,选择第一项:

然后点击image name,注意路径要在你的照片目录里,然后会显示照片名

然后根据下边提示完成读取:

后边会弹出一个读取后图像的样子:

点击Extract grid corners,提取每幅图的角点,点击完成后出现如下界面,然后根据提示进行输入,第一个可以敲回车,后边输入格子的边长(数量),然后手工选取四个边界:(注意这里默认每个格子100mm长)

每个图片都要标记一次,然后点击Calibration,这样相机各个参数就计算出来了,可以再在面板上点击ShowExtrinsic,可以看到假定相机不动和假定棋盘格不动两种情况下的相机与棋盘格的空间位置关系:

比如下边是我的情况:

 

 

最后感谢另一位博主关于工具箱的使用的介绍(https://blog.csdn.net/panpan_jiang1/article/details/80414737

  • 9
    点赞
  • 74
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

负壹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值