OpenCV calibrateCamera() 断言失败

2024-02-11

我已经尝试使用 Opencv calibrateCamera() 函数校准我的相机有一段时间了。我遵循了 opencv 示例程序中描述的相同过程。我尝试首先加载 10 个 9 x 6 棋盘图像。然后找到棋盘角。如果找到角点,则角点的像素位置将存储在矢量>ImagePoints中。对所有图像执行此操作后,将执行 runCalibrationAndSave 部分。在 runCalibrationAndSave 中,执行第一个 runCalibration 部分,其中填充了 ObjectPoints(类型为 vector>) 与角点的真实坐标值。到目前为止,代码运行良好,没有出现问题。棋盘角点被准确找到,ImagePoints向量也被向量填充。但是当它进入 calibrateCamera() 部分时,OpenCV:: 断言失败并出现以下错误:

OpenCV 错误:

断言失败 (nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total())) 在 收集校准数据、文件 /............/modules/calib3d/src/calibration.cpp,第 3164 行

我针对同一问题做了一些研究,发现这个问题通常发生在ObjectPoints向量和ImagePoints向量长度不相等或者填充不正确时。但就我而言,我在调试模式下检查了两个向量是否正确填充了相等的长度。 作为参考,我附加了在 calibrateCamera() 部分之前正确运行的代码部分,然后断言失败。

#include <iostream>
#include <sstream>
#include <time.h>
#include <stdio.h>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/highgui/highgui.hpp>


using namespace cv;
using namespace std;
class Settings
{public:
Size boardSize;            
float squareSize;          


};
bool runCalibrationAndSave(Settings& s, Size imageSize, Mat&  cameraMatrix, Mat& distCoeffs,
                       vector<vector<Point2f> > imagePoints );

int main()
{
Settings s;
s.boardSize.width =9;
s.boardSize.height=6;
s.squareSize=50;
  Mat cameraMatrix, distCoeffs;
Size imageSize;
char filename[512];
vector<vector<Point2f> > imagePoints;
for(int counter=0; counter<10; counter++)        
{sprintf( filename, "chessboard%d.jpg", counter );

IplImage* img = cvLoadImage(filename);


 cv::Mat& m = cv::cvarrToMat(img);

Mat pointBuf = Mat::zeros(54,2,CV_32FC1);
vector<Point2f> pointBuf_vec;

bool found=false;


found = findChessboardCorners( m,s.boardSize, pointBuf,CV_CALIB_CB_ADAPTIVE_THRESH |    CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE);
if(found)
{
cout<<"check"<<endl;
Mat viewGray;
                cvtColor(m, viewGray, CV_BGR2GRAY);
                cornerSubPix( viewGray, pointBuf, Size(11    ,11),Size(-1,-1),         TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
 drawChessboardCorners( m, s.boardSize, Mat(pointBuf), found );


pointBuf_vec.clear();

 for(int i=0;i<54;i++)
 {
 Point2f temp;
 temp.x=pointBuf.at<float>(i,0);
 temp.y=pointBuf.at<float>(i,1);
 pointBuf_vec.push_back(temp);

 }

 imagePoints.push_back(pointBuf_vec);




}


imshow("Example1",m);
cvWaitKey(); 

imageSize = m.size();
}
runCalibrationAndSave(s, imageSize,  cameraMatrix, distCoeffs, imagePoints);
return 0;


}



static void calcBoardCornerPositions(Size boardSize, float squareSize,     vector<Point3f>& corners)

{
corners.clear();

    for( int i = 0; i < boardSize.height; i++ )
        for( int j = 0; j < boardSize.width; j++ )
           { corners.push_back(Point3f(float( j*squareSize ), float( i*squareSize ), 0));

        }

}

 static bool runCalibration( Settings& s, Size& imageSize, Mat& cameraMatrix, Mat&   distCoeffs,
                        vector<vector<Point2f> > imagePoints, vector<Mat>& rvecs,   vector<Mat>& tvecs,
                        vector<float>& reprojErrs,  double& totalAvgErr)

 {

cameraMatrix = Mat::eye(3, 3, CV_64F);
 //   if( s.flag & CV_CALIB_FIX_ASPECT_RATIO )
 //      cameraMatrix.at<double>(0,0) = 1.0;

  distCoeffs = Mat::zeros(8, 1, CV_64F);

  vector<vector<Point3f> > objectPoints;
  Mat object_pointBuf = Mat::zeros(s.boardSize.width*s.boardSize.height,3,CV_32FC1);
    vector<Point3f> object_pointBuf_vec;

  calcBoardCornerPositions(s.boardSize, s.squareSize, object_pointBuf_vec);
  for(int k=0;k<imagePoints.size();k++)
  {
    objectPoints.push_back(object_pointBuf_vec);
  }





 // objectPoints.resize(imagePoints.size(),objectPoints[0]);

  //Find intrinsic and extrinsic camera parameters
  double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix,
                             distCoeffs, rvecs, tvecs, /*s.flag|*  /CV_CALIB_FIX_K4|CV_CALIB_FIX_K5);

  cout << "Re-projection error reported by calibrateCamera: "<< rms << endl;

  bool ok = checkRange(cameraMatrix) && checkRange(distCoeffs);

 //   totalAvgErr = computeReprojectionErrors(objectPoints, imagePoints,
                                  //       rvecs, tvecs, cameraMatrix, distCoeffs,   reprojErrs);

  return ok;
}



bool runCalibrationAndSave(Settings& s, Size imageSize, Mat&  cameraMatrix, Mat&     distCoeffs,vector<vector<Point2f> > imagePoints )

{
vector<Mat> rvecs, tvecs;
vector<float> reprojErrs;
double totalAvgErr = 0;

 bool ok = runCalibration(s,imageSize, cameraMatrix, distCoeffs, imagePoints, rvecs,   tvecs,
                         reprojErrs, totalAvgErr);
  cout << (ok ? "Calibration succeeded" : "Calibration failed")
    << ". avg re projection error = "  << totalAvgErr ;


 return ok;
} 

我正在使用 Visual C++ 和 Opencv 2.4.9。请帮我解决问题。这是我第一次在 SO 中提出任何问题,如果我在提问时犯了任何错误,请告诉我。提前感谢您的帮助。


我发现解决这个问题的唯一方法是下载 OpenCV 源代码,然后使用 CMAKE 和 Visual Studio 2010 构建它。现在使用 cmake 构建的库,消除所有这些问题。还存在“imread”和“imshow”相关的问题;当您从 CMAKE 构建 openCV 库时,这些问题也不会出现。 有关如何从 CMAKE 构建库,请查看此链接http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html我希望它会有所帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

OpenCV calibrateCamera() 断言失败 的相关文章

随机推荐