利用opencv进行相机标定(calibrateCamera)

环境:ubuntu16.04 + opencv3.3.0
该代码是根据 Learing Opencv 3第十八章标定示例代码修改的。
先上代码:

#include	
#include	
using	namespace	std;
using namespace cv;
void	help( )	{
    cout<<"输入参数示例如下:./xxx 9 6 10 1 \n"
      <<"参数解释如下:\n"
      <<"   ./xxx是生成的可执行文件\n    9是棋盘中每一行的内角点数\n"
      <<"    6是棋盘中每一列的内角点数\n  10表示拍摄十张图片进行标定\n"
      <<"    1表示图片缩放比例(默认是0.5,此参数不一定要输入)"<	5)	{
                cout	<<	"\nERROR:	Wrong	number	of	input	parameters";
                help();
                return	-1;
        }
        board_w		=	atoi(	argv[1]	);   //atoi时间ASCII码转化为int型
        board_h		=	atoi(	argv[2]	);
        n_boards	=	atoi(	argv[3]	);
        if(	argc	>	4	)	image_sf	=	atof(	argv[4]	);  //atoi时间ASCII码转化为float型
        int						board_n		=	board_w	*	board_h;
        cv::Size	board_sz	=	cv::Size(	board_w,	board_h	);

		//打开摄像头 
        cv::VideoCapture	capture(0);
        if(	!capture.isOpened()	)	{
                cout	<<	"\nCouldn't	open	the	camera\n";
                return	-1;
        }
        //	ALLOCATE	STORAGE
        //
        vector<	vector	>	image_points;
        vector<	vector	>	object_points;
        //	Capture	corner	views:	loop	until	we've	got	n_boards	successful
        //	captures	(all	corners	on	the	board	are	found).
        //
        double last_captured_timestamp	=	0;
        cv::Size	image_size;
        while(	image_points.size()	<	(size_t)n_boards	)	{
//                double t1 = getTickCount();
                cv::Mat	image0,	image;
                capture	>>	image0;

                image_size	=	image0.size();
                //缩小图片,可能是为了找到的角点更加准确(mcorners	*=	(1./image_sf); //	scale	the	corner	coordinates)
                cv::resize(image0,	image,	cv::Size(),	image_sf,	image_sf,	cv::INTER_LINEAR	);
                //	Find	the	board
                //
                vector	corners;
                bool	found	=	cv::findChessboardCorners(	image,	board_sz,	corners	);
//                double t2 = getTickCount();
//                cout<<"time:"<<(t2-t1)/getTickFrequency()*1000<	1	)	{
                    cv::drawChessboardCorners(	image,	board_sz,	corners,	found	);

                        last_captured_timestamp	=	timestamp;
                        image	^=	cv::Scalar::all(255);
                        cv::Mat	mcorners(corners);      //	do	not	copy	the	data
                        mcorners	*=	(1./image_sf);		//	scale	the	corner	coordinates
                        image_points.push_back(corners);
                        //生成世界坐标系的点
                        object_points.push_back(vector());
                        vector&	opts	=	object_points.back();
                        opts.resize(board_n);  //opts此时可以存放board_n个三维点
                        for(	int	j=0;	j (0,4) = 0;   //最后一个畸变系数置零
        fs	<<	"image_width"	<<	image_size.width	<<	"image_height"	<<	image_size.height
             <<"camera_matrix"	<<	intrinsic_matrix	<<	"distortion_coefficients"<<	distortion_coeffs;
        fs.release();
        //	EXAMPLE	OF	LOADING	THESE	MATRICES	BACK	IN:
        fs.open(	"intrinsics.yaml",	cv::FileStorage::READ	);
        cout	<<	"\nimage	width:	"	<<	(int)fs["image_width"];
        cout	<<	"\nimage	height:	"	<<	(int)fs["image_height"];
        cv::Mat	intrinsic_matrix_loaded,	distortion_coeffs_loaded;
        fs["camera_matrix"]	>>	intrinsic_matrix_loaded;
        fs["distortion_coefficients"]	>>	distortion_coeffs_loaded;
        cout	<<	"\nintrinsic	matrix:"	<<	intrinsic_matrix_loaded;
        cout	<<	"\ndistortion	coefficients:	"	<<	distortion_coeffs_loaded	<<	endl;
        //	Build	the	undistort	map	which	we	will	use	for	all
        //	subsequent	frames.
        //
        cv::Mat	map1,	map2;
        cv::initUndistortRectifyMap(
                intrinsic_matrix_loaded,
                distortion_coeffs_loaded,
                cv::Mat(),
                intrinsic_matrix_loaded,
                image_size,
                CV_16SC2,
                map1,
                map2
        );
        //	Just	run	the	camera	to	the	screen,	now	showing	the	raw	and
        //	the	undistorted	image.
        //
        for(;;)	{
                cv::Mat	image,	image0;
                capture	>>	image0;
                if(	image0.empty()	)	break;
                cv::remap(
                        image0,
                        image,
                        map1,
                        map2,
                        cv::INTER_LINEAR,
                        cv::BORDER_CONSTANT,
                        cv::Scalar()
                );
                cv::imshow("Undistorted",	image);
                if((cv::waitKey(30)	&	255)	==	27)	break;
        }
        return	0;
}

编译步骤:
1、建立一个cpp文件,比如main.cpp,将代码放进去,保存。
2、接着进行编译,可以用g++编译或者编写CMakeLists.txt 文件。下面看一下两种方法的怎么进行编译。
a、g++:在文件目录下打开终端,输入 g++ main.cpp -o main pkg-config --cflags --libs opencv // pkg-config --cflags --libs opencv是添加opencv的库依赖
b、CMakeLists.txt :(编写CMakeLists.txt 文件)
cmake_minimum_required(VERSION 2.8)
project( main )
find_package( OpenCV REQUIRED )
add_executable( mian mian.cpp ) // 将main.cpp更改为自己的.cpp文件
//main也可以更改名字,但这里更改的话下面target_link_libraries的main也要改
target_link_libraries( main ${OpenCV_LIBS} )
将上面文件保存和cpp文件同一个目录下。接着:
cmake .
make
上面两个编译方法不出错的意外的话,到此编译完成了。并生成了一个可执行文件main。
3、接着就是执行程序,输入 ./main 9 6 10 1 程序就能正常运行了。

标定所需函数的解释可以参考:https://blog.csdn.net/dcrmg/article/details/52939318

你可能感兴趣的:(OpneCV,calibrateCamera)