简介
本篇是使用opencv函数:cvFindChessboardCorners、cvFindCornerSubPix、cvDrawChessboardCorners,来找到、优化并显示出来标定棋盘
图片的角点。
关于这三个函数得讲解看,可以参考:http://www.360doc.cn/article/10724725_367761079.html
角点检测
具体代码
具体代码如下:
#include <opencv2/opencv.hpp>
#include <stdio.h>
using namespace cv;
using namespace std;
int boardWidth, boardHeight;
int boardTotal;
CvSize boardSize;
CvPoint2D32f * image_points_buf;
Mat srcColor, srcGray;
IplImage srcIp;
CvMat cvmatSrc;
int nowNumber, found;
char* picName;
void initFindCorner(){
boardSize = cvSize(10, 7);
boardWidth = boardSize.width;
boardHeight = boardSize.height;
boardTotal = boardWidth*boardHeight;
image_points_buf = new CvPoint2D32f[boardTotal];
srcColor = imread(picName);
imshow("源图像", srcColor);
cvtColor(srcColor, srcGray, COLOR_BGR2GRAY);
imshow("灰阶图", srcGray);
srcIp = srcGray;
cvmatSrc = srcColor;
}
void findCornersWork(){
found=cvFindChessboardCorners(&srcIp, boardSize, image_points_buf, \
&nowNumber, CV_CALIB_CB_ADAPTIVE_THRESH|CV_CALIB_CB_FILTER_QUADS);
printf("捕获角点数量:%d\n", nowNumber);
cvFindCornerSubPix(&srcIp, image_points_buf, nowNumber, cvSize(11,11), cvSize(-1,-1),
cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,0.1));
cvDrawChessboardCorners(&cvmatSrc, boardSize, image_points_buf, nowNumber, found);
imshow("角点标识图", srcColor);
}
int main(int argc, char* argv[]){
if(argc < 2){
cout << "Please input Picture name !!\n" << endl;
return -1;
}
picName = argv[1];
initFindCorner();
findCornersWork();
waitKey();
return 0;
}
代码讲解
1、初始化
void initFindCorner(){
boardSize = cvSize(10, 7);
boardWidth = boardSize.width;
boardHeight = boardSize.height;
boardTotal = boardWidth*boardHeight;
image_points_buf = new CvPoint2D32f[boardTotal];
srcColor = imread(picName);
imshow("源图像", srcColor);
cvtColor(srcColor, srcGray, COLOR_BGR2GRAY);
imshow("灰阶图", srcGray);
srcIp = srcGray;
cvmatSrc = srcColor;
}
首先设置预先设定图片的角点个数,本例使用的棋盘图片角点个数为:10X7,创建保存角点的结构:image_points_buf,接着导入棋盘图片,
并转为灰阶图像。
2、角点检测和显示
void findCornersWork(){
found=cvFindChessboardCorners(&srcIp, boardSize, image_points_buf, \
&nowNumber, CV_CALIB_CB_ADAPTIVE_THRESH|CV_CALIB_CB_FILTER_QUADS);
printf("捕获角点数量:%d\n", nowNumber);
cvFindCornerSubPix(&srcIp, image_points_buf, nowNumber, cvSize(11,11), cvSize(-1,-1),
cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,0.1));
cvDrawChessboardCorners(&cvmatSrc, boardSize, image_points_buf, nowNumber, found);
imshow("角点标识图", srcColor);
}
使用cvFindChessboardCorners进行角点检测,检测到的角点保存在image_points_buf,检测到的角点数量保存在nowNumber,如果nowNumber的值,和
实际图片上的角点数量相等,就表示角点检测成功。
接着cvFindCornerSubPix、cvDrawChessboardCorners将这些检测到的角点位置在图片:cvmatSrc上,显示输出。
结果显示
显示的结果如下:
畸变校正
前面已经讲解了如果找到棋盘标点图片的角点,这里在此基础上,继续进行后续的校正处理。
首先是找到多张图片的角点,接着将这些角点导入到函数cvCalibrateCamera2,进行camera内参数矩阵和畸变系数向量的生成。通过cvInitUndistortMap,
利用之前生成的内参数矩阵和畸变向量,计算出畸变映射到mapx和mapy中。最后cvRemap利用mapx、mapy对输入图像进行畸变校正。
可以参考文档:http://blog.csdn.net/guvcolie/article/details/7454632
具体代码
#include <opencv2/opencv.hpp>
#include <stdio.h>
using namespace cv;
using namespace std;
int main(int argc, char* argv[]){
int cube_length=10;
int cam_Dx = 100; //横轴方向长度
int cam_Dy = 100; //纵轴方向长度
int number_image = 7;
int a=1;
int number_image_copy= 7;
CvSize board_size=cvSize(10,7);
int board_width=board_size.width;
int board_height=board_size.height;
int total_per_image=board_width*board_height;
CvPoint2D32f * image_points_buf = new CvPoint2D32f[total_per_image];
CvMat * image_points=cvCreateMat(number_image*total_per_image,2,CV_32FC1);//图像坐标系
CvMat * object_points=cvCreateMat(number_image*total_per_image,3,CV_32FC1);//世界坐标系
CvMat * point_counts=cvCreateMat(number_image,1,CV_32SC1); //角点存放位置
CvMat * intrinsic_matrix=cvCreateMat(3,3,CV_32FC1); //内参数矩阵
CvMat * distortion_coeffs=cvCreateMat(4,1,CV_32FC1); //畸变系数向量
char picName[7][10] = {"1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg"};
IplImage * show;
int count;
int found;
int step;
int successes=0;
while(a<=number_image_copy){
show=cvLoadImage(picName[a-1],-1);
found=cvFindChessboardCorners(show,board_size,image_points_buf,&count,
CV_CALIB_CB_ADAPTIVE_THRESH|CV_CALIB_CB_FILTER_QUADS);
if(found==0){
cout<<"第"<<a<<"帧图片无法找到棋盘格所有角点!\n\n";
cvNamedWindow("RePlay",1);
cvShowImage("RePlay",show);
cvWaitKey(0);
}else{
cout<<"第"<<a<<"帧图像成功获得"<<count<<"个角点...\n";
IplImage * gray_image= cvCreateImage(cvGetSize(show),8,1);
cvCvtColor(show,gray_image,CV_BGR2GRAY);
cout<<"获取源图像灰度图过程完成...\n";
cvFindCornerSubPix(gray_image,image_points_buf,count,cvSize(11,11),cvSize(-1,-1),
cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,0.1));
cout<<"灰度图亚像素化过程完成...\n";
cvDrawChessboardCorners(show,board_size,image_points_buf,count,found);
cout<<"在源图像上绘制角点过程完成...\n\n";
}
if(total_per_image==count){
step=successes*total_per_image;
for(int i=step,j=0;j<total_per_image;++i,++j){
CV_MAT_ELEM(*image_points,float,i,0)=image_points_buf[j].x;
CV_MAT_ELEM(*image_points,float,i,1)=image_points_buf[j].y;// 求完每个角点横纵坐标值都存在image_point_buf里
CV_MAT_ELEM(*object_points,float,i,0)=(float)((j/cube_length) * cam_Dx);
CV_MAT_ELEM(*object_points,float,i,1)=(float)((j%cube_length) * cam_Dy);
CV_MAT_ELEM(*object_points,float,i,2)=0.0f;
}
CV_MAT_ELEM(*point_counts,int,successes,0)=total_per_image;
successes++;
}
a++;
}
cout<<"*********************************************\n";
cout<<number_image<<"帧图片中,标定成功的图片为"<<successes<<"帧...\n";
cout<<number_image<<"帧图片中,标定失败的图片为"<<number_image-successes<<"帧...\n\n";
cout<<"*********************************************\n\n";
IplImage * show_colie;
show_colie = show;
CvMat * object_points2=cvCreateMat(successes*total_per_image,3,CV_32FC1);
CvMat * image_points2=cvCreateMat(successes*total_per_image,2,CV_32FC1);
CvMat * point_counts2=cvCreateMat(successes,1,CV_32SC1);
for(int i=0;i<successes*total_per_image;++i){
CV_MAT_ELEM(*image_points2,float,i,0)=CV_MAT_ELEM(*image_points,float,i,0);//用来存储角点提取成功的图像的角点
CV_MAT_ELEM(*image_points2,float,i,1)=CV_MAT_ELEM(*image_points,float,i,1);
CV_MAT_ELEM(*object_points2,float,i,0)=CV_MAT_ELEM(*object_points,float,i,0);
CV_MAT_ELEM(*object_points2,float,i,1)=CV_MAT_ELEM(*object_points,float,i,1);
CV_MAT_ELEM(*object_points2,float,i,2)=CV_MAT_ELEM(*object_points,float,i,2);
}
for(int i=0;i<successes;++i){
CV_MAT_ELEM(*point_counts2,int,i,0)=CV_MAT_ELEM(*point_counts,int,i,0);
}
cvReleaseMat(&object_points);
cvReleaseMat(&image_points);
cvReleaseMat(&point_counts);
CV_MAT_ELEM(*intrinsic_matrix,float,0,0)=1.0f;
CV_MAT_ELEM(*intrinsic_matrix,float,1,1)=1.0f;
cvCalibrateCamera2(object_points2,image_points2,point_counts2,cvGetSize(show_colie),
intrinsic_matrix,distortion_coeffs,NULL,NULL,0);
cvSave("Intrinsics.xml",intrinsic_matrix);
cvSave("Distortion.xml",distortion_coeffs);
cout<<"摄像机矩阵、畸变系数向量已经分别存储在名为Intrinsics.xml、Distortion.xml文档中\n\n";
CvMat * intrinsic=(CvMat *)cvLoad("Intrinsics.xml");
CvMat * distortion=(CvMat *)cvLoad("Distortion.xml");
IplImage * mapx=cvCreateImage(cvGetSize(show_colie),IPL_DEPTH_32F,1);
IplImage * mapy=cvCreateImage(cvGetSize(show_colie),IPL_DEPTH_32F,1);
cvInitUndistortMap(intrinsic,distortion,mapx,mapy);
cvNamedWindow("原始图像",1);
cvNamedWindow("非畸变图像",1);
show_colie = cvLoadImage(argv[1],-1);
IplImage * clone=cvCloneImage(show_colie);
cvShowImage("原始图像",show_colie);
cvRemap(clone,show_colie,mapx,mapy);
cvReleaseImage(&clone);
cvShowImage("非畸变图像",show_colie);
cvWaitKey(0);
return 0;
}
代码讲解
1、首先这里是使用了了7张棋盘图片用来标定,所以cvFindChessboardCorners函数,会依次寻找7次角点。如果找到角点成功,则将对应结果保存到
image_points、object_points中,注意保存到object_points的时候需要做计算:(float)((j/cube_length) * cam_Dx);
while(a<=number_image_copy){
show=cvLoadImage(picName[a-1],-1);
found=cvFindChessboardCorners(show,board_size,image_points_buf,&count,
CV_CALIB_CB_ADAPTIVE_THRESH|CV_CALIB_CB_FILTER_QUADS);
if(found==0){
cout<<"第"<<a<<"帧图片无法找到棋盘格所有角点!\n\n";
cvNamedWindow("RePlay",1);
cvShowImage("RePlay",show);
cvWaitKey(0);
}else{
cout<<"第"<<a<<"帧图像成功获得"<<count<<"个角点...\n";
IplImage * gray_image= cvCreateImage(cvGetSize(show),8,1);
cvCvtColor(show,gray_image,CV_BGR2GRAY);
cout<<"获取源图像灰度图过程完成...\n";
cvFindCornerSubPix(gray_image,image_points_buf,count,cvSize(11,11),cvSize(-1,-1),
cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,0.1));
cout<<"灰度图亚像素化过程完成...\n";
cvDrawChessboardCorners(show,board_size,image_points_buf,count,found);
cout<<"在源图像上绘制角点过程完成...\n\n";
}
if(total_per_image==count){
step=successes*total_per_image;
for(int i=step,j=0;j<total_per_image;++i,++j){
CV_MAT_ELEM(*image_points,float,i,0)=image_points_buf[j].x;
CV_MAT_ELEM(*image_points,float,i,1)=image_points_buf[j].y;// 求完每个角点横纵坐标值都存在image_point_buf里
CV_MAT_ELEM(*object_points,float,i,0)=(float)((j/cube_length) * cam_Dx);
CV_MAT_ELEM(*object_points,float,i,1)=(float)((j%cube_length) * cam_Dy);
CV_MAT_ELEM(*object_points,float,i,2)=0.0f;
}
CV_MAT_ELEM(*point_counts,int,successes,0)=total_per_image;
successes++;
}
a++;
}
其中successes用来保存,需找角点成功的次数,如果7次都成功,则successes为7。
2、将找到的角点信息,重新存储到image_points2和object_points2中,利用cvCalibrateCamera2来计算矩阵、向量系数到intrinsic_matrix、distortion_coeffs,本保存到本地文件。
IplImage * show_colie;
show_colie = show;
CvMat * object_points2=cvCreateMat(successes*total_per_image,3,CV_32FC1);
CvMat * image_points2=cvCreateMat(successes*total_per_image,2,CV_32FC1);
CvMat * point_counts2=cvCreateMat(successes,1,CV_32SC1);
for(int i=0;i<successes*total_per_image;++i){
CV_MAT_ELEM(*image_points2,float,i,0)=CV_MAT_ELEM(*image_points,float,i,0);//用来存储角点提取成功的图像的角点
CV_MAT_ELEM(*image_points2,float,i,1)=CV_MAT_ELEM(*image_points,float,i,1);
CV_MAT_ELEM(*object_points2,float,i,0)=CV_MAT_ELEM(*object_points,float,i,0);
CV_MAT_ELEM(*object_points2,float,i,1)=CV_MAT_ELEM(*object_points,float,i,1);
CV_MAT_ELEM(*object_points2,float,i,2)=CV_MAT_ELEM(*object_points,float,i,2);
}
for(int i=0;i<successes;++i){
CV_MAT_ELEM(*point_counts2,int,i,0)=CV_MAT_ELEM(*point_counts,int,i,0);
}
cvReleaseMat(&object_points);
cvReleaseMat(&image_points);
cvReleaseMat(&point_counts);
CV_MAT_ELEM(*intrinsic_matrix,float,0,0)=1.0f;
CV_MAT_ELEM(*intrinsic_matrix,float,1,1)=1.0f;
cvCalibrateCamera2(object_points2,image_points2,point_counts2,cvGetSize(show_colie),
intrinsic_matrix,distortion_coeffs,NULL,NULL,0);
cvSave("Intrinsics.xml",intrinsic_matrix);
cvSave("Distortion.xml",distortion_coeffs);
3、函数cvInitUndistortMap和cvRemap,通过之前计算的矩阵、向量系数,对需要校正的图像:show_colie进行处理,并分别显示出来。
IplImage * mapx=cvCreateImage(cvGetSize(show_colie),IPL_DEPTH_32F,1);
IplImage * mapy=cvCreateImage(cvGetSize(show_colie),IPL_DEPTH_32F,1);
cvInitUndistortMap(intrinsic,distortion,mapx,mapy);
cvNamedWindow("原始图像",1);
cvNamedWindow("非畸变图像",1);
show_colie = cvLoadImage(argv[1],-1);
IplImage * clone=cvCloneImage(show_colie);
cvShowImage("原始图像",show_colie);
cvRemap(clone,show_colie,mapx,mapy);
cvReleaseImage(&clone);
cvShowImage("非畸变图像",show_colie);
video畸变校正
在之前的基础上,修改被校正的输入即可,简单的话,在前一个例子中,将如下代码:
IplImage * mapx=cvCreateImage(cvGetSize(show_colie),IPL_DEPTH_32F,1);
IplImage * mapy=cvCreateImage(cvGetSize(show_colie),IPL_DEPTH_32F,1);
cvInitUndistortMap(intrinsic,distortion,mapx,mapy);
cvNamedWindow("原始图像",1);
cvNamedWindow("非畸变图像",1);
show_colie = cvLoadImage(argv[1],-1);
IplImage * clone=cvCloneImage(show_colie);
cvShowImage("原始图像",show_colie);
cvRemap(clone,show_colie,mapx,mapy);
cvReleaseImage(&clone);
cvShowImage("非畸变图像",show_colie);
</source lang>
替换为:
<source lang="cpp" line>
VideoCapture capture(argv[1]);
if (!capture.isOpened()){
return 0;
}
while(1){
if (!capture.read(frame)){
break;
}
ipFrame = frame;
IplImage * clone=cvCloneImage(&ipFrame);
cvShowImage("原始图像", &ipFrame);
cvRemap(clone,show_colie,mapx,mapy);
cvReleaseImage(&clone);
cvShowImage("非畸变图像", show_colie);
if (waitKey(5) == 'q'){
break;
}
}
就是将被校正的图像修改为,从video中获取,循环校正显示。
效果演示
对应的图片畸变校正效果如下:
原图像 校正后图像
代码下载:http://download.csdn.net/detail/u011630458/9268829