-
-
-
- #include "stdafx.h"
- #include <opencv2/opencv.hpp>
- #include <highgui.hpp>
- #include "cv.h"
- #include <cv.hpp>
- #include <iostream>
-
- using namespace std;
- using namespace cv;
-
- const int imageWidth = 640;
- const int imageHeight = 480;
- const int boardWidth = 9;
- const int boardHeight = 6;
- const int boardCorner = boardWidth * boardHeight;
- const int frameNumber = 13;
- const int squareSize = 20;
- const Size boardSize = Size(boardWidth, boardHeight);
-
- Mat intrinsic;
- Mat distortion_coeff;
- vector<Mat> rvecs;
- vector<Mat> tvecs;
- vector<vector<Point2f>> corners;
- vector<vector<Point3f>> objRealPoint;
-
-
- vector<Point2f> corner;
-
- Mat rgbImage, grayImage;
-
-
- void calRealPoint(vector<vector<Point3f>>& obj, int boardwidth,int boardheight, int imgNumber, int squaresize)
- {
-
- vector<Point3f> imgpoint;
- for (int rowIndex = 0; rowIndex < boardheight; rowIndex++)
- {
- for (int colIndex = 0; colIndex < boardwidth; colIndex++)
- {
-
- imgpoint.push_back(Point3f(rowIndex * squaresize, colIndex * squaresize, 0));
- }
- }
- for (int imgIndex = 0; imgIndex < imgNumber; imgIndex++)
- {
- obj.push_back(imgpoint);
- }
- }
-
-
- void guessCameraParam(void )
- {
-
- intrinsic.create(3, 3, CV_64FC1);
- distortion_coeff.create(5, 1, CV_64FC1);
-
-
-
-
-
-
- intrinsic.at<double>(0,0) = 256.8093262;
- intrinsic.at<double>(0, 2) = 160.2826538;
- intrinsic.at<double>(1, 1) = 254.7511139;
- intrinsic.at<double>(1, 2) = 127.6264572;
-
- intrinsic.at<double>(0, 1) = 0;
- intrinsic.at<double>(1, 0) = 0;
- intrinsic.at<double>(2, 0) = 0;
- intrinsic.at<double>(2, 1) = 0;
- intrinsic.at<double>(2, 2) = 1;
-
-
-
-
- distortion_coeff.at<double>(0, 0) = -0.193740;
- distortion_coeff.at<double>(1, 0) = -0.378588;
- distortion_coeff.at<double>(2, 0) = 0.028980;
- distortion_coeff.at<double>(3, 0) = 0.008136;
- distortion_coeff.at<double>(4, 0) = 0;
- }
-
- void outputCameraParam(void )
- {
-
-
-
-
-
-
- cout << "fx :" << intrinsic.at<double>(0, 0) << endl << "fy :" << intrinsic.at<double>(1, 1) << endl;
- cout << "cx :" << intrinsic.at<double>(0, 2) << endl << "cy :" << intrinsic.at<double>(1, 2) << endl;
-
- cout << "k1 :" << distortion_coeff.at<double>(0, 0) << endl;
- cout << "k2 :" << distortion_coeff.at<double>(1, 0) << endl;
- cout << "p1 :" << distortion_coeff.at<double>(2, 0) << endl;
- cout << "p2 :" << distortion_coeff.at<double>(3, 0) << endl;
- cout << "p3 :" << distortion_coeff.at<double>(4, 0) << endl;
- }
-
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- Mat img;
- int goodFrameCount = 0;
- namedWindow("chessboard");
- cout << "按Q退出 ..." << endl;
- while (goodFrameCount < frameNumber)
- {
- char filename[100];
- sprintf_s(filename,"image\\left%02d.jpg", goodFrameCount + 1);
-
- rgbImage = imread(filename, CV_LOAD_IMAGE_COLOR);
- cvtColor(rgbImage, grayImage, CV_BGR2GRAY);
- imshow("Camera", grayImage);
-
- bool isFind = findChessboardCorners(rgbImage, boardSize, corner,0);
- if (isFind == true)
- {
-
-
-
-
-
- cornerSubPix(grayImage, corner, Size(5,5), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 0.1));
- drawChessboardCorners(rgbImage, boardSize, corner, isFind);
- imshow("chessboard", rgbImage);
- corners.push_back(corner);
-
-
-
- goodFrameCount++;
- cout << "The image is good" << endl;
- }
- else
- {
- cout << "The image is bad please try again" << endl;
- }
-
-
-
- if (waitKey(10) == 'q')
- {
- break;
- }
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- guessCameraParam();
- cout << "guess successful" << endl;
-
- calRealPoint(objRealPoint, boardWidth, boardHeight,frameNumber, squareSize);
- cout << "cal real successful" << endl;
-
- calibrateCamera(objRealPoint, corners, Size(imageWidth, imageHeight), intrinsic, distortion_coeff, rvecs, tvecs, 0);
- cout << "calibration successful" << endl;
-
- outputCameraParam();
- cout << "out successful" << endl;
-
-
- Mat cImage;
- undistort(rgbImage, cImage, intrinsic, distortion_coeff);
- imshow("Corret Image", cImage);
- cout << "Correct Image" << endl;
- cout << "Wait for Key" << endl;
- waitKey(0);
- system("pause");
- return 0;
- }
OPENCV3.0版本跟2.x版本是有一点差距的,这个程序在2.4.11版本里面跑不起来。
找了很久都没有找到错误,主要是在calibrateCamera()函数的时候出错。
也参考了官方的calibration例程,但还是找不到错误在什么地方。
下面是3.0版本的代码 注释都在代码里面