opencv标定程序

资源下载见:http://download.csdn.net/detail/zhouyelihua/7726907

在Vs2012中只要直接按f5就可以直接对资源中采集的图像进行标定。

而我们将最后的结果以.xml的形式存储下来。这个在工程文件下可以直接找到的

其中外参数,中旋转矩阵存在rotation.xml

位移向量存在translation.xml

内参数:内部矩阵存在Intrinsics.xml

畸变系数存在Distortion.xml

具体程序如下:(改自于<learning opencv>)

#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <stdlib.h>

int n_boards = 0; //Will be set by input list
const int board_dt = 10;
int board_w;
int board_h;

void help(){
	printf("Calibration from disk. Call convention:\n\n"
		"  ch11_ex11_1_fromdisk board_w board_h image_list\n\n"
		"Where: board_{w,h} are the # of internal corners in the checkerboard\n"
		"       width (board_w) and height (board_h)\n"
		"       image_list is space separated list of path/filename of checkerboard\n"
		"       images\n\n"
		"Hit 'p' to pause/unpause, ESC to quit.  After calibration, press any other key to step through the images\n\n");
}

int main() {

	CvCapture* capture;
	board_w =12;
	board_h = 12;
	int board_n  = board_w * board_h;
	CvSize board_sz = cvSize( board_w, board_h );
	FILE *fptr = fopen("..\\ConsoleApplication26\\sb.txt","r");
	char names[2048];
	//COUNT THE NUMBER OF IMAGES:
	while(fscanf(fptr,"%s ",names)==1){
		n_boards++;
	}
	rewind(fptr);

	cvNamedWindow( "Calibration" );
	//ALLOCATE STORAGE
	CvMat* image_points      = cvCreateMat(n_boards*board_n,2,CV_32FC1);
	CvMat* object_points     = cvCreateMat(n_boards*board_n,3,CV_32FC1);
	CvMat* point_counts      = cvCreateMat(n_boards,1,CV_32SC1);
	CvMat* rotation  = cvCreateMat(3,1,CV_32FC1);
	CvMat* translation_vector  = cvCreateMat(3,1,CV_32FC1);
	CvMat* rotation_mat_final  = cvCreateMat(3,3,CV_32FC1);
	CvMat* intrinsic_matrix  = cvCreateMat(3,3,CV_32FC1);
	CvMat* distortion_coeffs = cvCreateMat(4,1,CV_32FC1);

	IplImage* image = 0;// = cvQueryFrame( capture );
	IplImage* gray_image = 0; //for subpixel
	CvPoint2D32f* corners = new CvPoint2D32f[ board_n ];
	int corner_count;
	int successes = 0;
	int step;

	for( int frame=0; frame<n_boards; frame++ ) {
		fscanf(fptr,"%s ",names);

		if(image){
			cvReleaseImage(&image);
			image = 0;
		}
		image = cvLoadImage( names);
		if(gray_image == 0  && image) //We'll need this for subpixel accurate stuff
			gray_image = cvCreateImage(cvGetSize(image),8,1);

		if(!image)
			printf("null image\n");

		int found = cvFindChessboardCorners(
			image,
			board_sz,
			corners,
			&corner_count,
			CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS
			);

		//Get Subpixel accuracy on those corners
		cvCvtColor(image, gray_image, CV_BGR2GRAY);
		cvFindCornerSubPix(gray_image, corners, corner_count,
			cvSize(11,11),cvSize(-1,-1), cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
		//Draw it

		cvDrawChessboardCorners(image, board_sz, corners, corner_count, found);
		cvShowImage( "Calibration", image );

		// If we got a good board, add it to our data
		//
		if( corner_count == board_n ) {
			step = successes*board_n;
			//	printf("Found = %d for %s\n",found,names);
			for( int i=step, j=0; j<board_n; ++i,++j ) {
				///         CV_MAT_ELEM(*image_points, CvPoint2D32f,0,i) = cvPoint2D32f(corners[j].x,corners[j].y);
				///         CV_MAT_ELEM(*object_points,CvPoint3D32f,0,i) = cvPoint3D32f(j/board_w, j%board_w, 0.0f);
				CV_MAT_ELEM(*image_points, float,i,0) = corners[j].x;
				CV_MAT_ELEM(*image_points, float,i,1) = corners[j].y;
				CV_MAT_ELEM(*object_points,float,i,0) = j/board_w;
				CV_MAT_ELEM(*object_points,float,i,1) = j%board_w;
				CV_MAT_ELEM(*object_points,float,i,2) = 0.0f;

			}
			//        CV_MAT_ELEM(*point_counts, int,0,successes) = board_n;
			CV_MAT_ELEM(*point_counts, int,successes,0) = board_n;
			successes++;
		}

		//    if( successes == n_boards ) break;

		int c = cvWaitKey(15);
		if(c == 'p') {
			c = 0;
			while(c != 'p' && c != 27){
				c = cvWaitKey(250);
			}
		}
		if(c == 27)
			return 0;
	}
	printf("successes = %d, n_boards=%d\n",successes,n_boards);
	//ALLOCATE MATRICES ACCORDING TO HOW MANY IMAGES WE FOUND CHESSBOARDS ON
	///  CvMat* image_points2      = cvCreateMat(1,successes*board_n,CV_32FC2);
	///  CvMat* object_points2     = cvCreateMat(1,successes*board_n,CV_32FC3);
	///  CvMat* point_counts2      = cvCreateMat(1,successes,CV_32SC1);
	CvMat* object_points2     = cvCreateMat(successes*board_n,3,CV_32FC1);
	CvMat* image_points2      = cvCreateMat(successes*board_n,2,CV_32FC1);
	CvMat* point_counts2      = cvCreateMat(successes,1,CV_32SC1);
	//TRANSFER THE POINTS INTO THE CORRECT SIZE MATRICES
	for(int i = 0; i<successes*board_n; ++i){
		///      CV_MAT_ELEM(*image_points2, CvPoint2D32f,0,i)  = CV_MAT_ELEM(*image_points, CvPoint2D32f,0,i);
		///      CV_MAT_ELEM(*object_points2,CvPoint3D32f,0,i)  = CV_MAT_ELEM(*object_points,CvPoint3D32f,0,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,0, i) = CV_MAT_ELEM(*point_counts, int,0,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);

	// cvWaitKey();//Now we have to reallocate the matrices
	// return 0;
	// At this point we have all of the chessboard corners we need.
	//

	// Initialize the intrinsic matrix such that the two focal
	// lengths have a ratio of 1.0
	//
	CV_MAT_ELEM( *intrinsic_matrix, float, 0, 0 ) = 1.0f;
	CV_MAT_ELEM( *intrinsic_matrix, float, 1, 1 ) = 1.0f;
	printf("cvCalibrateCamera2\n");
	cvCalibrateCamera2(
		object_points2,
		image_points2,
		point_counts2,
		cvGetSize( image ),
		intrinsic_matrix,
		distortion_coeffs,
		NULL,
		NULL,
		0//CV_CALIB_FIX_ASPECT_RATIO
		);

	cvFindExtrinsicCameraParams2(
		object_points2, image_points2,
		intrinsic_matrix,distortion_coeffs,
		rotation,translation_vector
		);

	// Save our work

	cvRodrigues2(rotation,rotation_mat_final);
	cvSave("rotation.xml",rotation_mat_final);
	cvSave("translation.xml",translation_vector);
	cvSave("Intrinsics.xml",intrinsic_matrix);
	cvSave("Distortion.xml",distortion_coeffs);
	// Load test
	CvMat *intrinsic = (CvMat*)cvLoad("Intrinsics.xml");
	CvMat *distortion = (CvMat*)cvLoad("Distortion.xml");

	// Build the undistort map which we will use for all
	// subsequent frames.
	//
	IplImage* mapx = cvCreateImage( cvGetSize(image), IPL_DEPTH_32F, 1 );
	IplImage* mapy = cvCreateImage( cvGetSize(image), IPL_DEPTH_32F, 1 );
	printf("cvInitUndistortMap\n");
	cvInitUndistortMap(
		intrinsic,
		distortion,
		mapx,
		mapy
		);
	// Just run the camera to the screen, now only showing the undistorted
	// image.
	//
	rewind(fptr);
	cvNamedWindow( "Undistort" );
	printf("\n\nPress any key to step through the images, ESC to quit\n\n");
	while(fscanf(fptr,"%s ",names)==1){
		if(image){
			cvReleaseImage(&image);
			image = 0;
		}
		image = cvLoadImage( names);
		IplImage *t = cvCloneImage(image);
		cvShowImage( "Calibration", image );
		cvRemap( t, image, mapx, mapy );
		cvReleaseImage(&t);
		//	  cvUndistort2(
		cvShowImage("Undistort", image);
		if((cvWaitKey()&0x7F) == 27) break;
	}

	return 0;
} 

opencv标定程序

时间: 2024-12-17 05:39:47

opencv标定程序的相关文章

opencv标定程序(改动)

转载请注明来自:http://blog.csdn.net/zhouyelihua/article/details/38421377 资源下载见:点击打开链接 百度云盘免积分下载:https://pan.baidu.com/s/1bzy20u 有关于标定的基本原理详见点击打开链接 看到本文被转载非常多次非常高兴可是之前的代码有点小问题且不易懂.于是乎写了一个简洁的代码 首先介绍用本程序标定的步骤: 1,. 拍完图片之后将图片重命名为下图所看到的形式 2.将照片放在程序的cam目录下 3.然后执行程

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

使用Opencv实现张正友法相机标定之前,有几个问题事先要确认一下,那就是相机为什么需要标定,标定需要的输入和输出分别是哪些? 相机标定的目的:获取摄像机的内参和外参矩阵(同时也会得到每一幅标定图像的选择和平移矩阵),内参和外参系数可以对之后相机拍摄的图像就进行矫正,得到畸变相对很小的图像. 相机标定的输入:标定图像上所有内角点的图像坐标,标定板图像上所有内角点的空间三维坐标(一般情况下假定图像位于Z=0平面上). 相机标定的输出:摄像机的内参.外参系数. 这三个基础的问题就决定了使用Openc

双目视觉标定程序讲解

#include "opencv2/calib3d/calib3d.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <vector> #include <string> #include <algorithm> #include <iostream> #includ

利用opencv进行相机标定程序

1 #include "Stafx.h" 2 3 int board_w=12; //棋盘上有13个格子,那么角点的数目12 4 int board_h=12; 5 int image_count=23; //图片的总张数 6 int main(int argc, char** argv) 7 { 8 int count=0; 9 int board_n=board_h*board_w; //一张图像上,角点的数目 10 CvSize board_sz=cvSize(board_w,b

关于opencv标定的一些疑问,首先声明这篇文章转载于纸异兽,由于暂时联系不到他本人,只好请教各位了。有些问题想请教,各位大神可以留下联系方式帮我解决,万分感谢

在做SLAM时,希望用到深度图来辅助生成场景,所以要构建立体视觉,在这里使用OpenCV的Stereo库和python来进行双目立体视觉的图像处理. 立体标定 应用标定数据 转换成深度图 标定 在开始之前,需要准备的当然是两个摄相头,根据你的需求将两个摄像头进行相对位置的固定,我是按平行来进行固定的(如果为了追求两个双目图像更高的生命度,也可以将其按一定钝角固定,这样做又限制了场景深度的扩展,根据实际需求选择) 由于摄像头目前是我们手动进行定位的,我们现在还不知道两张图像与世界坐标之间的耦合关系

python中使用opencv检测程序效率

涉及到的函数主要有两个: 1.cv2.getTickCount()函数返回从参考点到这个函数被执行的时钟数.所以当你在一个函数前后都调用它的话,你就会得到这个函数的执行时间(时钟数). 2.cv2.getTickFrequency()返回时钟频率,或者说每秒钟的时钟数. 所以你可以按照以下的方式得到一个函数运行了多少秒: 1 # -*- coding: utf-8 -*- 2 3 import cv2 4 import numpy as np 5 6 e1 = cv2.getTickCount(

基于OpenCV立体视觉标定和校正

这几天学习双目视觉标定,分别使用了两种工具:OpenCV和Matlab.Matlab的效果非常稳定,但是一开始OpenCV的效果很糟糕,要不是出现中断就是标定出来的结果数值很大.经过了几天的不断调试和更改,终于把OpenCV的立体视觉标定和校正的程序写出来了.立体标定时计算空间上的两台摄像机几何关系的过程,立体校正则是对个体图像进行纠正,保证这些图像可以从平面对准的两幅图像获得.程序的框架如下: 1.读取左右相机图片序列 双目相机的图片序列放在Demon的路径下,左右相机的图像的名字分别存放在两

matlab、opencv、halcon双目标定汇总

相机的标定对于测距和重建还是很重要的,特把用过的工具和方法进行一次汇总,以便查阅.分析和讨论(本人扣扣1256635840) ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 1.MATLAB camera calib

【opencv学习】使用opencv与两个摄像头实现双目标定与测距

目录 目录 说明 之前文章中的双目测距代码 效果更好的双目视觉代码 效果更好的双目视觉代码的实现 1 标定过程 2 测距过程 一些问题以及解决方法 要说的 1 说明 我之前写过一篇文章<完全基于opencv的双目景深与测距的实现>:http://blog.csdn.net/hysteric314/article/details/50456570 但是之前文章中的双目视觉代码并不完善,所以就想再找找看有没有更好的实现方法. 然后就在youtube上找到一个视频:https://www.youtu