(单/双目)图像标定全流程(C++/Opencv实现)---代码篇

代码分为几个部分,c++部分使用g++进行编译

(1)双目摄像头标定图像数据集收集保存

//image_save.cpp
#include <iostream>
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
 
 
int main()
{
   

    cv::VideoCapture capl(0);
    cv::VideoCapture capr(1);
 
    int i = 0;
 
    cv::Mat cam_left;
    cv::Mat cam_right;
 
    char filename_l[15];
    char filename_r[15];
    while(capl.read(cam_left) && capr.read(cam_right))
    {
   
        cv::imshow("cam_left", cam_left);
        cv::imshow("cam_right", cam_right);
	
        char c = cv::waitKey(1);
        char s[40];
        
        if(c==' ') //按空格采集图像
        {
   
	        sprintf(filename_l, "left%d.jpg",i);
            imwrite(filename_l, cam_left);
	        sprintf(filename_r, "right%d.jpg",i++);
            imwrite(filename_r, cam_right);
            //printf(s, "%s%d%s\n", "the ",i++,"th image");
            cout << "save the "<< i <<"th image\n"<< endl;
        }
        if(c=='q' || c=='Q') // 按q退出
        {
   
            break;
        }
    }
 
    return 0;
}

显示结果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

(2)单目标定

//calibration.cpp
#include <iostream>
#include <sstream>
#include <time.h>
#include <stdio.h>
#include <fstream>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;
#define calibration

int main()
{
   
#ifdef calibration

	ifstream fin("right_img.txt");             /* 标定所用图像文件的路径 */
	ofstream fout("caliberation_result_right.txt");  /* 保存标定结果的文件 */

	// 读取每一幅图像,从中提取出角点,然后对角点进行亚像素精确化
	int image_count = 0;  /* 图像数量 */
	Size image_size;      /* 图像的尺寸 */
	Size board_size = Size(11,8);             /* 标定板上每行、列的角点数 */
	vector<Point2f> image_points_buf;         /* 缓存每幅图像上检测到的角点 */
	vector<vector<Point2f>> image_points_seq; /* 保存检测到的所有角点 */
	string filename;      // 图片名
	vector<string> filenames;

	while (getline(fin, filename))
	{
   
		++image_count;
		Mat imageInput = imread(filename);
		filenames.push_back(filename);

		// 读入第一张图片时获取图片大小
		if (image_count == 1)
		{
   
			image_size.width = imageInput.cols;
			image_size.height = imageInput.rows;
		}

		/* 提取角点 */
		if (0 == findChessboardCorners(imageInput, board_size, image_points_buf))
		{
   
			//cout << "can not find chessboard corners!\n";  // 找不到角点
			cout << "**" << filename << "** can not find chessboard corners!\n";
			exit(1);
		}
		else
		{
   
			Mat view_gray;
			cvtColor(imageInput, view_gray, CV_RGB2GRAY);  // 转灰度图

			/* 亚像素精确化 */
			// image_points_buf 初始的角点坐标向量,同时作为亚像素坐标位置的输出
			// Size(5,5) 搜索窗口大小
			// (-1,-1)表示没有死区
			// TermCriteria 角点的迭代过程的终止条件, 可以为迭代次数和角点精度两者的组合
			cornerSubPix(view_gray, image_points_buf, Size(5, 5), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));

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

			/* 在图像上显示角点位置 */
			drawChessboardCorners(view_gray, board_size, image_points_buf, false); // 用于在图片中标记角点

			imshow("Camera Calibration", view_gray);       // 显示图片

			waitKey(500); //暂停0.5S      
		}
	}
	int CornerNum = board_size.width * board_size.height;  // 每张图片上总的角点数

	//-------------以下是摄像机标定------------------

	/*棋盘三维信息*/
	Size square_size = Size(60, 60);         /* 实际测量得到的标定板上每个棋盘格的大小 */
	vector<vector<Point3f>> object_points;   /* 保存标定板上角点的三维坐标 */

	/*内外参数*/
	Mat cameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0));  /* 摄像机内参数矩阵 */
	vector<int> point_counts;   // 每幅图像中角点的数量
	Mat distCoeffs = Mat(1, 5
  • 17
    点赞
  • 182
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
双目相机标定是计算机视觉中的一个重要步骤,它可以通过计算双目相机之间的相对位置和姿态,将两个相机的图像进行联合,实现三维重构和深度测量等功能。OpenCV提供了一套完整的双目相机标定工具,下面是一个简标定流程: 1.采集双目图像数据,包括左右相机的内参矩阵、畸变系数、图像尺寸等信息; 2.通过对图像数据进行预处理,包括去畸变、矫正等操作,使得标定结果更加精确; 3.提取双目图像中的特征点,并进行匹配,计算出左右相机之间的基础矩阵和本质矩阵; 4.通过标定板上的特征点的三维坐标和它们在相机图像中的对应点的二维坐标,计算出左右相机之间的外参矩阵; 5.对标定结果进行评估,包括重投影误差、立体重建误差等指标,以判断标定结果的准确性和可靠性。 下面是一个基于OpenCV双目相机标定示例代码: ```c #include <opencv2/opencv.hpp> #include <iostream> #include <vector> using namespace cv; using namespace std; int main() { //读取标定图像 vector<vector<Point3f>> objectPoints; //标定板上的三维坐标 vector<vector<Point2f>> imagePoints1, imagePoints2; //左右相机上对应的二维图像点 Size imageSize; //图像尺寸 Mat cameraMatrix1, distCoeffs1; //左相机内参矩阵和畸变系数 Mat cameraMatrix2, distCoeffs2; //右相机内参矩阵和畸变系数 Mat R, T, E, F; //左右相机之间的旋转矩阵、平移矩阵、本质矩阵、基础矩阵 //设置标定板参数 Size boardSize(9, 6); //标定板内部角点数目 float squareSize = 30; //标定板内部边长,位毫米 //生成标定板上的三维坐标 vector<Point3f> corners; for (int i = 0; i < boardSize.height; i++) { for (int j = 0; j < boardSize.width; j++) { corners.push_back(Point3f(j * squareSize, i * squareSize, 0)); } } //读取标定图像 vector<String> filenames1, filenames2; glob("left/*.jpg", filenames1); //左相机图像文件夹 glob("right/*.jpg", filenames2); //右相机图像文件夹 for (int i = 0; i < filenames1.size(); i++) { Mat image1 = imread(filenames1[i]); Mat image2 = imread(filenames2[i]); imageSize = image1.size(); //提取标定板上的角点 vector<Point2f> corners1, corners2; bool found1 = findChessboardCorners(image1, boardSize, corners1); bool found2 = findChessboardCorners(image2, boardSize, corners2); if (found1 && found2) { //亚像素精确化角点位置 Mat gray1, gray2; cvtColor(image1, gray1, COLOR_BGR2GRAY); cvtColor(image2, gray2, COLOR_BGR2GRAY); cornerSubPix(gray1, corners1, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 30, 0.1)); cornerSubPix(gray2, corners2, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 30, 0.1)); //保存角点坐标 objectPoints.push_back(corners); imagePoints1.push_back(corners1); imagePoints2.push_back(corners2); } } //标定相机 double rms = stereoCalibrate(objectPoints, imagePoints1, imagePoints2, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, imageSize, R, T, E, F, CALIB_FIX_INTRINSIC + CALIB_USE_INTRINSIC_GUESS + CALIB_SAME_FOCAL_LENGTH + CALIB_RATIONAL_MODEL + CALIB_FIX_K3 + CALIB_FIX_K4 + CALIB_FIX_K5, TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 100, 1e-5)); cout << "Stereo calibration done with RMS error = " << rms << endl; //保存标定结果 FileStorage fs("stereo_calib.xml", FileStorage::WRITE); fs << "cameraMatrix1" << cameraMatrix1; fs << "distCoeffs1" << distCoeffs1; fs << "cameraMatrix2" << cameraMatrix2; fs << "distCoeffs2" << distCoeffs2; fs << "R" << R; fs << "T" << T; fs << "E" << E; fs << "F" << F; fs.release(); return 0; } ``` 以上代码仅供参考,实际应用中需要根据具体情况进行修改和调整。
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值