转载请注明来自: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. python3 geohash 导入错误及解决

    方法一: pip3 install  python-geohash 方法二: 1.保证 pip3 install geohash 包 2. 进入包的下载目录 /usr/local/lib/python ...

  2. [CQOI2013]新Nim游戏(线性基)

    P4301 [CQOI2013]新Nim游戏 题目描述 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个火柴堆拿走若干根火柴. ...

  3. ECNUOJ 2147 字符环

    字符环 Time Limit:1000MS Memory Limit:65536KBTotal Submit:562 Accepted:146 Description  字符环:就是将给定的一个字符串 ...

  4. 设计模式之九:建造者模式(Builder)

    建造者模式: 将一个复杂对象的建造过程和它的表示分离开来,这样同样的建造过程能够创建不同的表示. Separate the construction of a complex object from ...

  5. Bitmap缓存机制

    Bitmap缓存机制 载入一个bitmap到UI里面比較简单直接.可是,假设我们一次载入大量的bitmap数据的时候就变得复杂了.很多情况下(比方这些组件:ListVIew,GridView或者Vie ...

  6. Java 8 时间日期库的20个使用演示样例

    除了lambda表达式,stream以及几个小的改进之外,Java 8还引入了一套全新的时间日期API,在本篇教程中我们将通过几个简单的任务演示样例来学习怎样使用Java 8的这套API.Java对日 ...

  7. 免费WiFi初体验——个小白的WiFi旅程

    说来羞愧,真正接触到WiFi还是在毕业后,此前自己封闭在一个人的世界,再加上外在学校的包围,我还成了个"山里"的孩子. 去年毕业了,也算是个90后,可自觉得心态过于成熟.了解外界太 ...

  8. js插件---图片懒加载echo.js结合 Amaze UI ScrollSpy 使用

    js插件---图片懒加载echo.js结合 Amaze UI ScrollSpy 使用 一.总结 一句话总结:图片懒加载echo.js结合 Amaze UI ScrollSpy 使用的效果就是:懒加载 ...

  9. Gym - 100625F Count Ways 快速幂+容斥原理

    题意:n*m的格子,中间有若干点不能走,问从左上角到右下角有多少种走法. 思路:CountWay(i,j) 表示从 i 点到 j 点的种数.然后用容斥原理加加减减解决 #pragma comment( ...

  10. Python(八) 正则表达式与JSON

    一.初识正则表达式 正则表达式 是一个特殊的字符序列,一个字符串是否与我们所设定的这样的字符序列,相匹配 快速检索文本.实现替换文本的操作 json(xml) 轻量级 web 数据交换格式 impor ...