OPENCV3.0 单目摄像头标定(使用官方自带的标定图片)





  
  
  1. // opencv_test.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <opencv2/opencv.hpp>
  5. #include <highgui.hpp>
  6. #include "cv.h"
  7. #include <cv.hpp>
  8. #include <iostream>
  9. using namespace std;
  10. using namespace cv;
  11. const int imageWidth = 640; //摄像头的分辨率
  12. const int imageHeight = 480;
  13. const int boardWidth = 9; //横向的角点数目
  14. const int boardHeight = 6; //纵向的角点数据
  15. const int boardCorner = boardWidth * boardHeight; //总的角点数据
  16. const int frameNumber = 13; //相机标定时需要采用的图像帧数
  17. const int squareSize = 20; //标定板黑白格子的大小 单位mm
  18. const Size boardSize = Size(boardWidth, boardHeight); //
  19. Mat intrinsic; //相机内参数
  20. Mat distortion_coeff; //相机畸变参数
  21. vector<Mat> rvecs; //旋转向量
  22. vector<Mat> tvecs; //平移向量
  23. vector< vector<Point2f>> corners; //各个图像找到的角点的集合 和objRealPoint 一一对应
  24. vector< vector<Point3f>> objRealPoint; //各副图像的角点的实际物理坐标集合
  25. vector<Point2f> corner; //某一副图像找到的角点
  26. Mat rgbImage, grayImage;
  27. /*计算标定板上模块的实际物理坐标*/
  28. void calRealPoint(vector<vector<Point3f>>& obj, int boardwidth,int boardheight, int imgNumber, int squaresize)
  29. {
  30. // Mat imgpoint(boardheight, boardwidth, CV_32FC3,Scalar(0,0,0));
  31. vector<Point3f> imgpoint;
  32. for ( int rowIndex = 0; rowIndex < boardheight; rowIndex++)
  33. {
  34. for ( int colIndex = 0; colIndex < boardwidth; colIndex++)
  35. {
  36. // imgpoint.at<Vec3f>(rowIndex, colIndex) = Vec3f(rowIndex * squaresize, colIndex*squaresize, 0);
  37. imgpoint.push_back(Point3f(rowIndex * squaresize, colIndex * squaresize, 0));
  38. }
  39. }
  40. for ( int imgIndex = 0; imgIndex < imgNumber; imgIndex++)
  41. {
  42. obj.push_back(imgpoint);
  43. }
  44. }
  45. /*设置相机的初始参数 也可以不估计*/
  46. void guessCameraParam(void )
  47. {
  48. /*分配内存*/
  49. intrinsic.create( 3, 3, CV_64FC1);
  50. distortion_coeff.create( 5, 1, CV_64FC1);
  51. /*
  52. fx 0 cx
  53. 0 fy cy
  54. 0 0 1
  55. */
  56. intrinsic.at< double>( 0, 0) = 256.8093262; //fx
  57. intrinsic.at< double>( 0, 2) = 160.2826538; //cx
  58. intrinsic.at< double>( 1, 1) = 254.7511139; //fy
  59. intrinsic.at< double>( 1, 2) = 127.6264572; //cy
  60. intrinsic.at< double>( 0, 1) = 0;
  61. intrinsic.at< double>( 1, 0) = 0;
  62. intrinsic.at< double>( 2, 0) = 0;
  63. intrinsic.at< double>( 2, 1) = 0;
  64. intrinsic.at< double>( 2, 2) = 1;
  65. /*
  66. k1 k2 p1 p2 p3
  67. */
  68. distortion_coeff.at< double>( 0, 0) = -0.193740; //k1
  69. distortion_coeff.at< double>( 1, 0) = -0.378588; //k2
  70. distortion_coeff.at< double>( 2, 0) = 0.028980; //p1
  71. distortion_coeff.at< double>( 3, 0) = 0.008136; //p2
  72. distortion_coeff.at< double>( 4, 0) = 0; //p3
  73. }
  74. void outputCameraParam(void )
  75. {
  76. /*保存数据*/
  77. //cvSave("cameraMatrix.xml", &intrinsic);
  78. //cvSave("cameraDistoration.xml", &distortion_coeff);
  79. //cvSave("rotatoVector.xml", &rvecs);
  80. //cvSave("translationVector.xml", &tvecs);
  81. /*输出数据*/
  82. cout << "fx :" << intrinsic.at< double>( 0, 0) << endl << "fy :" << intrinsic.at< double>( 1, 1) << endl;
  83. cout << "cx :" << intrinsic.at< double>( 0, 2) << endl << "cy :" << intrinsic.at< double>( 1, 2) << endl;
  84. cout << "k1 :" << distortion_coeff.at< double>( 0, 0) << endl;
  85. cout << "k2 :" << distortion_coeff.at< double>( 1, 0) << endl;
  86. cout << "p1 :" << distortion_coeff.at< double>( 2, 0) << endl;
  87. cout << "p2 :" << distortion_coeff.at< double>( 3, 0) << endl;
  88. cout << "p3 :" << distortion_coeff.at< double>( 4, 0) << endl;
  89. }
  90. int _tmain( int argc, _TCHAR* argv[])
  91. {
  92. Mat img;
  93. int goodFrameCount = 0;
  94. namedWindow( "chessboard");
  95. cout << "按Q退出 ..." << endl;
  96. while (goodFrameCount < frameNumber)
  97. {
  98. char filename[ 100];
  99. sprintf_s(filename, "image\\left%02d.jpg", goodFrameCount + 1);
  100. // cout << filename << endl;
  101. rgbImage = imread(filename, CV_LOAD_IMAGE_COLOR);
  102. cvtColor(rgbImage, grayImage, CV_BGR2GRAY);
  103. imshow( "Camera", grayImage);
  104. bool isFind = findChessboardCorners(rgbImage, boardSize, corner, 0);
  105. if (isFind == true) //所有角点都被找到 说明这幅图像是可行的
  106. {
  107. /*
  108. Size(5,5) 搜索窗口的一半大小
  109. Size(-1,-1) 死区的一半尺寸
  110. TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 0.1)迭代终止条件
  111. */
  112. cornerSubPix(grayImage, corner, Size( 5, 5), Size( -1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 0.1));
  113. drawChessboardCorners(rgbImage, boardSize, corner, isFind);
  114. imshow( "chessboard", rgbImage);
  115. corners.push_back(corner);
  116. //string filename = "res\\image\\calibration";
  117. //filename += goodFrameCount + ".jpg";
  118. //cvSaveImage(filename.c_str(), &IplImage(rgbImage)); //把合格的图片保存起来
  119. goodFrameCount++;
  120. cout << "The image is good" << endl;
  121. }
  122. else
  123. {
  124. cout << "The image is bad please try again" << endl;
  125. }
  126. // cout << "Press any key to continue..." << endl;
  127. // waitKey(0);
  128. if (waitKey( 10) == 'q')
  129. {
  130. break;
  131. }
  132. // imshow("chessboard", rgbImage);
  133. }
  134. /*
  135. 图像采集完毕 接下来开始摄像头的校正
  136. calibrateCamera()
  137. 输入参数 objectPoints 角点的实际物理坐标
  138. imagePoints 角点的图像坐标
  139. imageSize 图像的大小
  140. 输出参数
  141. cameraMatrix 相机的内参矩阵
  142. distCoeffs 相机的畸变参数
  143. rvecs 旋转矢量(外参数)
  144. tvecs 平移矢量(外参数)
  145. */
  146. /*设置实际初始参数 根据calibrateCamera来 如果flag = 0 也可以不进行设置*/
  147. guessCameraParam();
  148. cout << "guess successful" << endl;
  149. /*计算实际的校正点的三维坐标*/
  150. calRealPoint(objRealPoint, boardWidth, boardHeight,frameNumber, squareSize);
  151. cout << "cal real successful" << endl;
  152. /*标定摄像头*/
  153. calibrateCamera(objRealPoint, corners, Size(imageWidth, imageHeight), intrinsic, distortion_coeff, rvecs, tvecs, 0);
  154. cout << "calibration successful" << endl;
  155. /*保存并输出参数*/
  156. outputCameraParam();
  157. cout << "out successful" << endl;
  158. /*显示畸变校正效果*/
  159. Mat cImage;
  160. undistort(rgbImage, cImage, intrinsic, distortion_coeff);
  161. imshow( "Corret Image", cImage);
  162. cout << "Correct Image" << endl;
  163. cout << "Wait for Key" << endl;
  164. waitKey( 0);
  165. system( "pause");
  166. return 0;
  167. }

OPENCV3.0版本跟2.x版本是有一点差距的,这个程序在2.4.11版本里面跑不起来。

找了很久都没有找到错误,主要是在calibrateCamera()函数的时候出错。

也参考了官方的calibration例程,但还是找不到错误在什么地方。

下面是3.0版本的代码 注释都在代码里面


        </div>
            </div>
        </article>


猜你喜欢

转载自blog.csdn.net/monk1992/article/details/82655764