您的位置:首页 > 运维架构

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  相机标定