转载请注明来自:http://blog.csdn.net/zhouyelihua/article/details/38421377

资源下载见:点击打开链接

百度云盘免积分下载:https://pan.baidu.com/s/1bzy20u

有关于标定的基本原理详见点击打开链接

看到本文被转载非常多次非常高兴可是之前的代码有点小问题且不易懂。于是乎写了一个简洁的代码

首先介绍用本程序标定的步骤:

1,。

拍完图片之后将图片重命名为下图所看到的形式

2.将照片放在程序的cam目录下

3.然后执行程序。依照提示输入信息

4.最后在output目录找到result_data_no_optim.txt

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhvdXllbGlodWE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

代码例如以下:

//author:YeahPingYE
//function:auto calibration
//time:2014/11/25
//
//
#include<highgui.h>
#include<cv.h>
#include<iostream>
#include<fstream>
using namespace std; void save_result(CvMat*cam_rotation_all, CvMat*cam_translation_vector_all,
CvMat*cam_intrinsic_matrix, CvMat*cam_distortion_coeffs,char*pathc,int sucesses);
int main()
{
/*initialation
//input:the number of images......num_image
// prj_board_w,prj_board_h
// cam_board_w,cam_board_h
*/
CvMat*cam_object_points2;
CvMat*cam_image_points2;
int cam_board_n;
int successes = 0;
int img_num, cam_board_w, cam_board_h,cam_Dx,cam_Dy;
cout << "输入的图像的组数\n";
cin >> img_num;
cout << "输入**真实**棋盘格的##横轴##方向的角点个数\n";
cin >> cam_board_w;
cout << "输入**真实**棋盘格的##纵轴##方向的角点个数\n";
cin >> cam_board_h;
cout << "输入**真实**棋盘格的##横轴##方向的长度\n";
cin >> cam_Dx;
cout << "输入**真实**棋盘格的##纵轴##方向的长度\n";
cin >> cam_Dy;
cam_board_n = cam_board_w*cam_board_h;
/*
//init
//
//
*/ //camera init
CvSize cam_board_sz = cvSize(cam_board_w, cam_board_h);
CvMat*cam_image_points = cvCreateMat(cam_board_n*(img_num), 2, CV_32FC1);
CvMat*cam_object_points = cvCreateMat(cam_board_n*(img_num), 3, CV_32FC1);
CvMat*cam_point_counts = cvCreateMat((img_num), 1, CV_32SC1);
CvPoint2D32f*cam_corners = new CvPoint2D32f[cam_board_n];
int cam_corner_count;
int cam_step;
CvMat*cam_intrinsic_matrix = cvCreateMat(3, 3, CV_32FC1);
CvMat*cam_distortion_coeffs = cvCreateMat(4, 1, CV_32FC1);
CvSize cam_image_sz;
//window intit
cvNamedWindow("window", 0); //get image size
IplImage *cam_image_temp = cvLoadImage("..\\cam\\cam1.bmp", 0);
cam_image_sz = cvGetSize(cam_image_temp);
char failurebuf[20] = { 0 };
/*
//extract cornner
// camera image
//
// pattern
*/ /*
//extrat the cam cornner
//
//
//
*/
fstream cam_data;
cam_data.open("..\\output\\TXT\\cam_corners.txt", ofstream::out);
fstream cam_object_data;
cam_object_data.open("..\\output\\TXT\\cam_object_data.txt", ofstream::out);
//process the prj image so that we can easy find cornner
for (int ii = 1; ii < img_num + 1; ii++)
{
char cambuf[20] = { 0 };
sprintf(cambuf, "..\\cam\\cam%d.bmp", ii);
IplImage *cam_image = cvLoadImage(cambuf, 0); //extract cam cornner
int cam_found = cvFindChessboardCorners(cam_image, cam_board_sz, cam_corners, &cam_corner_count,
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);
cvFindCornerSubPix(cam_image, cam_corners, cam_corner_count,
cvSize(11, 11), cvSize(-1, -1), cvTermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
cvDrawChessboardCorners(cam_image, cam_board_sz, cam_corners, cam_corner_count, cam_found); if (cam_corner_count != cam_board_n)
cout << "find cam"<<ii<<" corner failed!\n"; //when cam and prj are success store the result
if ( cam_corner_count == cam_board_n) {
//store cam result
cam_step = successes*cam_board_n;
for (int i = cam_step, j = 0; j < cam_board_n; ++i, ++j) {
CV_MAT_ELEM(*cam_image_points, float, i, 0) = cam_corners[j].x;
CV_MAT_ELEM(*cam_image_points, float, i, 1) = cam_corners[j].y;
CV_MAT_ELEM(*cam_object_points, float, i, 0) = (j/cam_board_w)*cam_Dx;
CV_MAT_ELEM(*cam_object_points, float, i, 1) = (j % cam_board_w)*cam_Dy;
CV_MAT_ELEM(*cam_object_points, float, i, 2) = 0.0f;
cam_data << cam_corners[j].x << "\t" << cam_corners[j].y << "\n";
cam_object_data << (j/cam_board_w)*cam_Dx << "\t" << (j %cam_board_w)*cam_Dy << "\t0\n";
}
CV_MAT_ELEM(*cam_point_counts, int, successes, 0) = cam_board_n;
successes++;
cout << "success number" << successes << endl;
cvShowImage("window", cam_image);
cvWaitKey(500);
} }
if (successes < 2)
exit(0);
/*
//restore the success point
*/
//cam
cam_image_points2 = cvCreateMat(cam_board_n*(successes), 2, CV_32FC1);
cam_object_points2 = cvCreateMat(cam_board_n*(successes), 3, CV_32FC1);
CvMat*cam_point_counts2 = cvCreateMat((successes), 1, CV_32SC1);
for (int i = 0; i < successes*cam_board_n; ++i){
CV_MAT_ELEM(*cam_image_points2, float, i, 0) = CV_MAT_ELEM(*cam_image_points, float, i, 0);
CV_MAT_ELEM(*cam_image_points2, float, i, 1) = CV_MAT_ELEM(*cam_image_points, float, i, 1);
CV_MAT_ELEM(*cam_object_points2, float, i, 0) = CV_MAT_ELEM(*cam_object_points, float, i, 0);
CV_MAT_ELEM(*cam_object_points2, float, i, 1) = CV_MAT_ELEM(*cam_object_points, float, i, 1);
CV_MAT_ELEM(*cam_object_points2, float, i, 2) = CV_MAT_ELEM(*cam_object_points, float, i, 2); }
for (int i = 0; i < successes; ++i){
CV_MAT_ELEM(*cam_point_counts2, int, i, 0) = CV_MAT_ELEM(*cam_point_counts, int, i, 0);
}
cvSave("..\\output\\XML\\cam_corners.xml", cam_image_points2); cvReleaseMat(&cam_object_points);
cvReleaseMat(&cam_image_points);
cvReleaseMat(&cam_point_counts); /*
//calibration for camera
//
*/
//calib for cam
CV_MAT_ELEM(*cam_intrinsic_matrix, float, 0, 0) = 1.0f;
CV_MAT_ELEM(*cam_intrinsic_matrix, float, 1, 1) = 1.0f;
CvMat* cam_rotation_all = cvCreateMat( successes, 3, CV_32FC1);
CvMat* cam_translation_vector_all = cvCreateMat( successes,3, CV_32FC1);
cvCalibrateCamera2(
cam_object_points2,
cam_image_points2,
cam_point_counts2,
cam_image_sz,
cam_intrinsic_matrix,
cam_distortion_coeffs,
cam_rotation_all,
cam_translation_vector_all,
0//CV_CALIB_FIX_ASPECT_RATIO
);
cvSave("..\\output\\XML\\cam_intrinsic_matrix.xml", cam_intrinsic_matrix);
cvSave("..\\output\\XML\\cam_distortion_coeffs.xml", cam_distortion_coeffs);
//calib
cvSave("..\\output\\XML\\cam_rotation_all.xml", cam_rotation_all);
cvSave("..\\output\\XML\\cam_translation_vector_all.xml", cam_translation_vector_all);
char path1[100] = "..\\output\\result_data_no_optim.txt";
save_result(cam_rotation_all, cam_translation_vector_all,
cam_intrinsic_matrix, cam_distortion_coeffs,path1,successes);
}
void save_result(CvMat*cam_rotation_all, CvMat*cam_translation_vector_all,
CvMat*cam_intrinsic_matrix, CvMat*cam_distortion_coeffs,char*pathc,int sucesses)
{
fstream Yeah_result;
Yeah_result.open(pathc, ofstream::out);
Yeah_result << setprecision(12) << "fc[0] =" << CV_MAT_ELEM(*cam_intrinsic_matrix, float, 0, 0
) << "; fc[1] =" << CV_MAT_ELEM(*cam_intrinsic_matrix, float, 1, 1) << "; //CAM的焦距\n";
Yeah_result << setprecision(12) << "cc[0] = " << CV_MAT_ELEM(*cam_intrinsic_matrix, float, 0, 2)
<< "; cc[1] = " << CV_MAT_ELEM(*cam_intrinsic_matrix, float, 1, 2) << ";//CAM中心点\n";
Yeah_result << setprecision(12) << "kc[0] =" << CV_MAT_ELEM(*cam_distortion_coeffs, float, 0, 0) <<
"; kc[1] =" << CV_MAT_ELEM(*cam_distortion_coeffs, float, 1, 0) <<
"; kc[2] =" << CV_MAT_ELEM(*cam_distortion_coeffs, float, 2, 0) <<
"; kc[3] =" << CV_MAT_ELEM(*cam_distortion_coeffs, float, 3, 0)
<< "; kc[4] =0;//畸变參数,请參照MATLAB里的定义\n\n外參数:\n";
for(int i=0;i<sucesses;++i)
{
Yeah_result<<"r:("<<setprecision(12) <<CV_MAT_ELEM(*cam_rotation_all, float, i, 0)<<"\t,"<<CV_MAT_ELEM(*cam_rotation_all, float, i, 1)<<"\t,"<<CV_MAT_ELEM(*cam_rotation_all, float, i, 2)<<")\n";
Yeah_result<<"t:("<<setprecision(12) <<CV_MAT_ELEM(*cam_translation_vector_all, float, i, 0)<<"\t,"<<CV_MAT_ELEM(*cam_translation_vector_all, float, i, 1)<<"\t,"<<CV_MAT_ELEM(*cam_translation_vector_all, float, i, 2)<<")\n\n\n";
} }

假设您认为此博客对您实用,欢迎对我进行小额赞助

不筹钱娶媳妇的程序猿不是好程序猿。

opencv标定程序(改动)的更多相关文章

  1. 【opencv】相机标定程序内存溢出

    运行相机内参标定程序出现内存溢出的错误 opencv的alloc.cpp报cv::OutOfMemoryError 因为同时开了多个线程,每个线程标定一台相机,每个线程都会imread读入所有标定图片 ...

  2. 张正友相机标定Opencv实现以及标定流程&&标定结果评价&&图像矫正流程解析(附标定程序和棋盘图)

    使用Opencv实现张正友法相机标定之前,有几个问题事先要确认一下,那就是相机为什么需要标定,标定需要的输入和输出分别是哪些? 相机标定的目的:获取摄像机的内参和外参矩阵(同时也会得到每一幅标定图像的 ...

  3. openCV训练程序申请内存不足

     openCV训练程序申请内存不足   在用OpenCV训练分类器(特别是训练Adaboost类型的分类器)的时候,当样本的数量特别大的时候,就会出现申请内存不够的情况,很早以前碰到过这样的情况,最近 ...

  4. 解决Windows Server 2012 R2 Datacenter云服务器无法运行opencv python程序的问题

    写了个基于opencv的python程序,pyinstaller 32位机打包后在win7/win10 32/64正常运行,在Windows Server 2012 R2 Datacenter云服务器 ...

  5. 利用opencv进行相机标定程序

    #include "Stafx.h" ; //棋盘上有13个格子,那么角点的数目12 ; ; //图片的总张数 int main(int argc, char** argv) { ...

  6. 通过C语言程序改动控制台的背景和前景颜色

    本文主要解说怎样通过C语言来改动dos背景和前景颜色.我们首先来看一下dos的背景颜色的属性. 打开開始菜单,点击执行,弹出执行对话框.输入cmd,回车. (打开dos控制台) 在命令提示符界面下,输 ...

  7. 项目实战:Qt+Ffmpeg+OpenCV相机程序(打开摄像头、支持多种摄像头、分辨率调整、翻转、旋转、亮度调整、拍照、录像、回放图片、回放录像)

    若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...

  8. opencv 小程序170323

    1.滤波 GaussianBlur(imgThresholded, imgThresholded, Size(5, 5), 0, 0);//高斯滤波 medianBlur(imgThresholded ...

  9. VS2012+openCV 2.4.8进行编译:VS2012 64位 使用OPENCV应用程序不能正常启动 (0xc000007b)怎么处理?

    [OpenCV入门教程之一] 安装OpenCV:OpenCV 2.4.8 +VS 开发环境配置 http://blog.csdn.net/poem_qianmo/article/details/198 ...

随机推荐

  1. gitHub上如何设置或者取消电子邮箱提醒

    原文链接:点我 我们正常注册的gitHub一般应该都是电子邮箱的方式,在注册账号时可能选择或者默认给了各种提醒,但是gitHub的邮箱提醒真的就比较烦人了,特别是最近团队开发项目,什么动态都有提醒,就 ...

  2. uname---用于打印当前系统相关信息

    uname命令用于打印当前系统相关信息(内核版本号.硬件架构.主机名称和操作系统类型等). 语法 uname(选项) 选项 -a或--all:显示全部的信息: -m或--machine:显示电脑类型: ...

  3. CSU 1378 Shipura 简单模拟

    上周末中南的题,当时就知道是个简单模拟题,可是一个多小时就是没写出来,代码能力啊 >.< 题意: 某人发明了一种程序语言,只支持一种运算">>",和一种函数 ...

  4. 关于Github Pages

    迁移Github Pages 我稍微有一点强迫症,实在是忍受不了整洁的界面有一些推广的广告.正所谓博客平台不重要,重要的是要有干货,CSDN首页满屏的广告也就忍受了,但是自己的文章的页面有广告看着实在 ...

  5. [PostCss] Easily Load Google Fonts with PostCSS Font Magician

    Configuring Google Fonts can be quite an annoying process to setup. Using Font Magician with PostCSS ...

  6. JSTL标准标签库具体解释

    JSTL标签库的使用是为类弥补html表的不足.规范自己定义标签的使用而诞生的. 在告别modle1模式开发应用程序后.人们開始注重软件的分层设计,不希望在jsp页面中出现java逻辑代码,同一时候也 ...

  7. MFC Wizard创建的空应用程序中各个文件内容的解析

    创建的MFC应用程序名为:wd,那么: 一.wd.h解析 // wd.h : main header file for the WD application // #if !defined(AFX_W ...

  8. Controller接口控制器3

    11.AbstractWizardFormController 向导控制器类提供了多步骤(向导)表单的支持(如完善个人资料时分步骤填写基本信息.工作信息.学校信息等) 假设现在做一个完善个人信息的功能 ...

  9. 远程带参数POST访问接口,返回数据

    1. string token = GetRequest.GetString("token"); int customer_id = GetRequest.GetInt(" ...

  10. 关于servlet的web.xml映射

    1.原理 <servlet> <!-- servlet的名字,随便起个名,但和下面的servlet名一致 --> <servlet-name>hello</s ...