利用opencv3标定单目相机(图片标定)和畸变校正

opencv菜鸟一枚,就一个标定搞了许久,哎( ▼-▼ ),写个简单的教程记录一下.

本次标定采用自己的标定板拍摄的图片进行标定,基于opencv自带的标定例程实现,我不打算解释各个文件的意思,按照步骤来一遍就明白了。

第一步:找到自己安装opencv的路径下:*\opencv3.4\sources\samples\cpp\tutorial_code\calib3d\camera_calibration这个位置

第二步:将上面的文件复制到自己建立的文件夹下,在此新建工程,添加camera_calibration.cpp,或者打开camer_calibration复制代码到自己新建的工程的源文件中,紧接着需要修改第261行的输入设置文件,也就是将下面的第一行修改为第二行

const string inputSettingsFile = argc > 1 ? argv[1] : "default.xml";
const string inputSettingsFile = argc > 1 ? argv[1] : "in_VID5.xml";

第三步:好,现在去看看这个in_VID5.xml是什么,又需要修改些什么?下面我将介绍利用自己的图片进行标定需要修改的地方

① 首先打开用文档编辑器打开in_VID5.xml文件,修改第5第6行,这个是我的,行40,列41,自己数一下即可(如果行数不对应搜索一下关键字即可).

<BoardSize_Width> 40</BoardSize_Width>
<BoardSize_Height>41</BoardSize_Height>

 

② 修改第9行,我的棋盘格每个30mm,这里填入自己棋盘格的方格尺寸即可.

<Square_Size>30</Square_Size>

③ 修改第19行,填入自己的VID5.xml(里面存放和用来标定图片的路径)的路径即可

<Input>"E:/learning_opencv/camera_calibration/calibration/calibration/VID5.xml"</Input>

④ 修改第27行,填入用来标定图片的张数,我用了16张(推荐15-20张)

 <Calibrate_NrOfFrameToUse>16</Calibrate_NrOfFrameToUse>

然后对in_VID5.xml具体设置细节感兴趣参考:https://www.cnblogs.com/li-yao7758258/p/5933653.html

第四步:修改VID5.xml文件,只需要修改自己的文件路劲就可以了

<?xml version="1.0"?>
<opencv_storage>
<images>
E:\learning_opencv\camera_calibration\calibration\calibration\chess1.BMP
E:\learning_opencv\camera_calibration\calibration\calibration\chess2.BMP
E:\learning_opencv\camera_calibration\calibration\calibration\chess3.BMP
E:\learning_opencv\camera_calibration\calibration\calibration\chess4.BMP
E:\learning_opencv\camera_calibration\calibration\calibration\chess5.BMP
E:\learning_opencv\camera_calibration\calibration\calibration\chess6.BMP
E:\learning_opencv\camera_calibration\calibration\calibration\chess7.BMP
E:\learning_opencv\camera_calibration\calibration\calibration\chess8.BMP
E:\learning_opencv\camera_calibration\calibration\calibration\chess9.BMP
E:\learning_opencv\camera_calibration\calibration\calibration\chess10.BMP
E:\learning_opencv\camera_calibration\calibration\calibration\chess11.BMP
E:\learning_opencv\camera_calibration\calibration\calibration\chess12.BMP
E:\learning_opencv\camera_calibration\calibration\calibration\chess13.BMP
E:\learning_opencv\camera_calibration\calibration\calibration\chess14.BMP
E:\learning_opencv\camera_calibration\calibration\calibration\chess15.BMP
E:\learning_opencv\camera_calibration\calibration\calibration\chess16.BMP
</images>
</opencv_storage>

第五步:好了,现在准备工作几乎都搞定了,直接ctrl+F5调试吧

可能出现"localtime:This function or variable may be unsafe ..." Google方法有三种,我用的是在前面加上

#pragma warning(disable:4996)

好了,到这里我工程可以完美运行!你的呢?有bug?take it easy!Google一下一般都能解决!

附一张图,当然为了好看些可以将用于标定的图片放在一个文件夹中,具体修改一下设置文件的路径就可以了.

畸变矫正:

新建一个工程文件,将标定得到的out_camera_data.xml复制到工程文件中,源文件代码如下:


#include <opencv2/opencv.hpp>
#include <opencv2/calib3d/calib3d.hpp>
using namespace std;
using namespace cv;
 
/**
 * @主函数
 */
int main( int argc, char** argv )
{
 
	/// 读取一副图片,不改变图片本身的颜色类型(该读取方式为DOS运行模式)
		Mat src = imread( "p1.BMP", 1 );
		imshow("1", src);
		Mat distortion = src.clone();
		imshow("2", distortion);
		Mat camera_matrix = Mat(3, 3, CV_32FC1);
		Mat distortion_coefficients;
 
 
		//导入相机内参和畸变系数矩阵
		FileStorage file_storage("out_camera_data.xml", FileStorage::READ);
		file_storage["camera_matrix"] >> camera_matrix;
		cout << camera_matrix << endl;
		file_storage["distortion_coefficients"] >> distortion_coefficients;
		cout << distortion_coefficients << endl;
		file_storage.release();
 
		//矫正
		undistort(src, distortion, camera_matrix, distortion_coefficients);
 
		imshow("img", src);
		imshow("undistort", distortion);
		imwrite("undistort.jpg", distortion);
 
		waitKey(0);
		return 0;
}

其实这些代码和网上查到的差不多,直接用还有个bug,只是稍微修改了两个地方:Camera_Matrix改为camera_matrix,Distortion_Coefficients改为distortion_coefficients等,具体可以自己设置断点调试一下问题出现在哪里,结合out_camera_data.xml的一些参数名看一下一般都能找到bug的来源,那么标定和畸变校正到此结束,后面就可以对校正完的这张图片做自己的工作了.

参考连接:

https://blog.csdn.net/u013498583/article/details/71404323

https://blog.csdn.net/qq_23845067/article/details/52105811

https://blog.csdn.net/shawn_zhangguang/article/details/77801833

http://www.cnblogs.com/li-yao7758258/p/5933653.html

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

无情的搬砖机器

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

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

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

打赏作者

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

抵扣说明:

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

余额充值