OpenCV相机标定
2017-04-10 03:32
309 查看
#include <iostream> #include <windows.h> #include <vector> #include <opencv2/opencv.hpp> //头文件 #include <sstream> using namespace cv; //包含cv命名空间 using namespace std; //棋盘标靶中每块的宽和高 int g_Height = 100; int g_Width = 100; int g_InnerHeigh = 6; int g_InnerWidth = 9; Size g_imgSize;//影像的大小 //创建一个棋盘标靶影像,显示在屏幕中,使用相机对标靶影像进行照射,创建10幅以上 bool CreateCalibImg() { Mat img = Mat(g_Height * 7, g_Width * 10, CV_8UC3, Scalar(255, 255, 255));//标靶内角点大小是Size(9,6) if (img.empty()) { cout << "创建标靶影像失败" << endl; return false; } string tile("标靶影像"); //每隔一个方块,绘制一个方形的影响块 for (int i = 0; i < 7;++i) { //交叉绘制黑色方块 for (int j = (i&1)?0:1; j < 10;j+=2)//i&1 如果是偶数,则结果为false,否则为true { Mat SmallRect = img(Rect(Point(j*g_Width, i*g_Height), Point((j + 1)*g_Width, (i + 1)*g_Height))); //提取一小块,作为兴趣矩形,然后将其变为黑色 SmallRect=0; } } namedWindow(tile, WINDOW_AUTOSIZE);//无缩放显示,以免影像每个方格的大小 imshow(tile, img); waitKey(0); return true; } //功能:计算每个影像的影像坐标和物体坐标 //@param filename 影像的名字 //@param ImgCoor 影像中每个角点坐标 //@param ObjCoor 影像中每个角点的物理坐标 //这个函数要使用/MDd 模式,不然的可能会出现 _pfirstblock == phead的错误 bool CalcImgAndObjCoor(vector<string> filename, vector<vector<Point2f> > &ImgCoor, vector<vector<Point3f> > &ObjCoor) { //这个for寻找主要是找到影像的角点坐标和空间中的物理坐标 Mat img; for (size_t i = 0; i < filename.size();++i) { img = imread(filename[i], IMREAD_GRAYSCALE);//以灰度读取影像,函数只能处理灰度影像 g_imgSize = img.size(); if (img.empty()) { continue;//如果影像读取失败话,那么继续下一副影像 } vector<Point2f> corner; vector<Point3f> ObjCorner; corner.clear();//每次都进行清除操作,不然的话,可能会累加操作 ObjCorner.clear(); bool result = findChessboardCorners(img, Size(g_InnerWidth, g_InnerHeigh), corner, CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_NORMALIZE_IMAGE); if (!result) { continue;//如果计算失败的话,那么就继续下一副影像 } //精确计算角点的坐标 cornerSubPix(img, corner, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS | TermCriteria::MAX_ITER, 30, 0.01)); //绘制计算的角点,可以在测试的时候进行使用 //drawChessboardCorners(img, Size(g_InnerWidth, g_InnerHeigh), corner, result); ImgCoor.push_back(corner); //计算角点的物理坐标 //计算每个像素的像素大小,以mm为单位,需要用到windows提供的GetDeviceCaps得到分辨率的大小,MSDN中参数说明 //HORZRES Width, in pixels, of the screen; or for printers, the width, in pixels, of the printable area of the page. //VERTRES Height, in raster lines, of the screen; or for printers, the height, in pixels, of the printable area of the page. //GetDeviceCaps(HORZRES)本来打算使用这个函数计算屏幕的物理尺寸的,但是由于这个函数在WIn7中得到结果是不正确,在网上寻找了一些办法 //有的通过注册表中得到物理尺寸,但是,我发现在我的电脑中,这个注册表的数值是不存在,苦于没有找到合适的办法,只能找到“鲁大师”了,其给出了 //屏幕的物理尺寸 30cm 17cm ,但是不知道精度如何。 HDC dc = GetDC(NULL); double xRadio= 17.0/GetDeviceCaps(dc,VERTRES); double yRadio= 30.0/ GetDeviceCaps(dc, HORZRES); for (int row = 0; row < g_InnerHeigh;++row) { for (int col = 0; col < g_InnerWidth;++col) { double x = col*g_Width*xRadio;//转换为以mm为单位 double y = row*g_Height*yRadio; ObjCorner.push_back(Point3f(x, y, 0)); } } ObjCoor.push_back(ObjCorner); } return true; } int main() { //CreateCalibImg();//显示影像,使用手机进行拍摄,拍摄超过十张以上 string Prefilename = "C:\\Users\\Administrator\\Desktop\\标准测试图片\\M5相机\\";//影像的前缀路径 vector<string> filename; for (int i = 1; i < 14;++i) { ostringstream os; os << i; string temp = Prefilename + os.str() + ".jpg"; filename.push_back(temp); } vector<vector<Point2f> > ImgCoor; vector<vector<Point3f> > ObjCoor; CalcImgAndObjCoor(filename, ImgCoor, ObjCoor); Mat CameraMatrix;//相机的内参数 3*3矩阵 Mat DisCoeffs;//相机的畸变参数 vector<Mat> RotationMatrix;//旋转矩阵 vector<Mat> TransMatrix;//平移变换矩阵 calibrateCamera(ObjCoor, ImgCoor, g_imgSize, CameraMatrix, DisCoeffs, RotationMatrix, TransMatrix); cout << "相机参数" << '\n' << CameraMatrix << endl; cout << "畸变参数" << '\n' << DisCoeffs << endl; for (int i = 0; i < RotationMatrix.size();++i) { cout << "第" << i << "个影像" << endl; cout << "旋转向量" << '\n' << RotationMatrix[i] << endl; Mat temp;//保存旋转矩阵---可以使用罗德里格变换,将其转换为旋转矩阵 Rodrigues(RotationMatrix[i], temp); cout << "旋转矩阵" << temp << endl; cout << "平移矩阵" << '\n' << TransMatrix[i] << endl; } waitKey(0); return 0; }
相关文章推荐
- 相机模型与标定之opencv圆形标志点检测算法(拓扑识别)
- opencv 双目相机标定 自带例子程序的使用
- 基于OpenCV进行相机标定
- 相机标定之OpenCV&Matlab
- 相机标定的理解及采用opencv和matlab工具箱的标定方法
- 用VISP+Opencv做相机到机械臂的标定
- 【计算机视觉】全景相机标定(MATLAB/opencv)
- 单目相机标定: 一个全自动化的使用opencv的标定程序
- 张正友相机标定算法原理与源代码(OpenCV+C++)
- 双目相机标定之OpenCV获取左右相机图像+MATLAB单目标定+双目标定
- Opencv 张正友相机标定傻瓜教程
- openCV---相机内外参标定
- Camera Calibration 相机标定:Opencv应用方法
- 【OpenCV3学习笔记 】相机标定函数 calibrateCamera( ) 使用详解(附相机标定程序和数据)
- 基于Opencv2.4.9的双目相机标定代码
- 三维重建学习(4):张正友相机标定程序实现(OpenCV)
- 相机标定的理解及采用opencv和matlab工具箱的标定方法
- matlab与opencv双目相机标定对比
- 五、(2)相机标定_OpenCV&Matlab
- 鱼眼相机与针孔相机的标定与校正基于MATLAB和Opencv