网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
- }
- /*设置相机的初始参数 也可以不估计*/
- void CalibrationEvaluate(void)//标定结束后进行评价
- {
- double err=0;
- double total_err=0;
- //calibrateCamera(objRealPoint, corners, Size(imageWidth, imageHeight), intrinsic, distortion_coeff, rvecs, tvecs, 0);
- cout << “每幅图像的定标误差:” << endl;
- for (int i = 0; i < corners.size(); i++)
- {
- vector image_points2;
- vector tempPointSet = objRealPoint[i];
- projectPoints(tempPointSet, rvecs[i], tvecs[i], intrinsic, distortion_coeff, image_points2);
- vector tempImagePoint = corners[i];
- Mat tempImagePointMat = Mat(1, tempImagePoint.size(), CV_32FC2);
- Mat image_points2Mat = Mat(1, image_points2.size(), CV_32FC2);
- for (int j = 0; j < tempImagePoint.size(); j++)
- {
- image_points2Mat.at(0, j) = Vec2f(image_points2[j].x, image_points2[j].y);
- tempImagePointMat.at(0, j) = Vec2f(tempImagePoint[j].x, tempImagePoint[j].y);
- }
- err = norm(image_points2Mat, tempImagePointMat, NORM_L2);
- total_err = err + total_err;
- cout << “第” << i + 1 << “幅图像的平均误差:” << err << “像素” << endl;
- }
- cout << “总体平均误差:” << total_err / (corners.size() + 1) << “像素” << endl;
- }
- void guessCameraParam(void)
- {
- /*分配内存*/
- intrinsic.create(3, 3, CV_64FC1);
- distortion_coeff.create(5, 1, CV_64FC1);
- /*
- fx 0 cx
- 0 fy cy
- 0 0 1
- */
- intrinsic.at(0, 0) = 256.8093262; //fx
- intrinsic.at(0, 2) = 160.2826538; //cx
- intrinsic.at(1, 1) = 254.7511139; //fy
- intrinsic.at(1, 2) = 127.6264572; //cy
- intrinsic.at(0, 1) = 0;
- intrinsic.at(1, 0) = 0;
- intrinsic.at(2, 0) = 0;
- intrinsic.at(2, 1) = 0;
- intrinsic.at(2, 2) = 1;
- /*
- k1 k2 p1 p2 p3
- */
- distortion_coeff.at(0, 0) = -0.193740; //k1
- distortion_coeff.at(1, 0) = -0.378588; //k2
- distortion_coeff.at(2, 0) = 0.028980; //p1
- distortion_coeff.at(3, 0) = 0.008136; //p2
- distortion_coeff.at(4, 0) = 0; //p3
- }
- void outputCameraParam(void)
- {
- /*保存数据*/
- //cvSave(“cameraMatrix.xml”, &intrinsic);
- //cvSave(“cameraDistoration.xml”, &distortion_coeff);
- //cvSave(“rotatoVector.xml”, &rvecs);
- //cvSave(“translationVector.xml”, &tvecs);
- /*输出数据*/
- cout << “fx :” << intrinsic.at(0, 0) << endl << “fy :” << intrinsic.at(1, 1) << endl;
- cout << “cx :” << intrinsic.at(0, 2) << endl << “cy :” << intrinsic.at(1, 2) << endl;
- cout << “k1 :” << distortion_coeff.at(0, 0) << endl;
- cout << “k2 :” << distortion_coeff.at(1, 0) << endl;
- cout << “p1 :” << distortion_coeff.at(2, 0) << endl;
- cout << “p2 :” << distortion_coeff.at(3, 0) << endl;
- cout << “p3 :” << distortion_coeff.at(4, 0) << endl;
- }
- //int _tmain(int argc, _TCHAR* argv[])
- int main()
- {
- Mat img;
- int goodFrameCount = 0;
- namedWindow(“chessboard”);
- cout << “按Q退出 …” << endl;
- while (goodFrameCount < frameNumber)
- {
- char filename[100];
- sprintf_s(filename, “chao%d.bmp”, goodFrameCount);
- //sprintf_s(filename, “chess%d.jpg”, goodFrameCount);
- goodFrameCount++;
- rgbImage = imread(filename, 1);
- cvtColor(rgbImage, grayImage, CV_BGR2GRAY);
- imshow(“Camera”, grayImage);
- bool isFind = findChessboardCorners(rgbImage, boardSize, corner, 0);
- //bool isFind = findChessboardCorners(rgbImage, boardSize, corner, CV_CALIB_CB_NORMALIZE_IMAGE);
- if (isFind == true) //所有角点都被找到 说明这幅图像是可行的
- {
- /*
- Size(5,5) 搜索窗口的一半大小
- Size(-1,-1) 死区的一半尺寸
- TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 0.1)迭代终止条件
- */
- 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);
- //string filename = “res\image\calibration”;
- //filename += goodFrameCount + “.jpg”;
- //cvSaveImage(filename.c_str(), &IplImage(rgbImage)); //把合格的图片保存起来
- cout << “The image is good” << endl;
- }
- else
- {
- cout << “The image is bad please try again” << endl;
- }
- // cout << “Press any key to continue…” << endl;
- // waitKey(0);
- if (waitKey(10) == ‘q’)
- {
- break;
- }
- // imshow(“chessboard”, rgbImage);
- }
- /*
- 图像采集完毕 接下来开始摄像头的校正
- calibrateCamera()
- 输入参数 objectPoints 角点的实际物理坐标
- imagePoints 角点的图像坐标
- imageSize 图像的大小
- 输出参数
- cameraMatrix 相机的内参矩阵
- distCoeffs 相机的畸变参数
- rvecs 旋转矢量(外参数)
- tvecs 平移矢量(外参数)
- */
- /*设置实际初始参数 根据calibrateCamera来 如果flag = 0 也可以不进行设置*/
- 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();
- CalibrationEvaluate();
- 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;
- }
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
究,那么很难做到真正的技术提升。**
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!