简体   繁体   English

OpenCV calibrateCamera()断言失败

[英]OpenCV calibrateCamera() Assertion failed

I have been trying to calibrate my camera quite for a while using Opencv calibrateCamera() function. 我一直在尝试使用Opencv calibrateCamera()函数校准我的相机。 I have followed the same procedure as described in opencv sample program. 我遵循了与opencv示例程序中所述相同的过程。 I am trying to first load 10 9 x 6 chessboard images. 我正在尝试先加载10 9 x 6棋盘图像。 Then finding chessboard corners. 然后找到棋盘角。 If corners are found then corners' pixel location is stored in vector< vector < Point2f>> ImagePoints. 如果找到了角点,则角点的像素位置存储在vector <vector <Point2f >> ImagePoints中。 After doing this for all images, runCalibrationAndSave part is executed. 对所有图像执行此操作后,将执行runCalibrationAndSave部分。 In runCalibrationAndSave , first runCalibration part is executed where ObjectPoints (of type vector< vector < Point3f>>) are filled with corners' real coordinate values. 在runCalibrationAndSave中,执行第一个runCalibration部分,其中用角的真实坐标值填充ObjectPoints(类型为vector <vector <Point3f >>)。 Upto this point code works well and no problem occurs.Chessboard corners are accurately found and ImagePoints vector is also filled with vectors. 到此为止,代码运行良好,并且没有问题。准确地找到了棋盘角,并且ImagePoints矢量也填充了矢量。 But when it goes to calibrateCamera() part OpenCV:: assertion is failed with following error: 但是,当转到calibrateCamera()部分时,OpenCV ::断言将失败,并出现以下错误:

OpenCV Error: OpenCV错误:

Assertion failed (nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total())) in collectCalibrationData, file /............/modules/calib3d/src/calibration.cpp, line 3164 断言失败(nimages> 0 && nimages ==(int)imagePoints1.total()&&(!imgPtMat2 || nimages ==(int)imagePoints2.total()))在setCalibrationData,文件/ ........中.... / modules / calib3d / src / calibration.cpp,第3164行

I did some research for the same problem and found that this problem usually occurs when ObjectPoints vector and ImagePoints vector are not of equal length or if they are not properly filled. 我对同一问题进行了一些研究,发现当ObjectPoints向量和ImagePoints向量的长度不相等或填充不正确时,通常会发生此问题。 But In my case I have checked in debugging mode that both vectors are properly filled with equal lengths. 但就我而言,我已经在调试模式下检查了两个向量均正确地填充了相等的长度。 For the reference I have attached the code part which properly runs upto before calibrateCamera() part and then assertion is failed. 作为参考,我附加了代码部分,该部分可以正确运行到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;
} 

I am using Visual C++ and Opencv 2.4.9. 我正在使用Visual C ++和Opencv 2.4.9。 Please help me in figuring out the problem. 请帮助我解决问题。 This is first time I am asking any question in SO , please let me know if I made any mistake in asking question. 这是我第一次在SO中提出任何问题,如果我在提出问题时犯了任何错误,请告诉我。 Thanks for help in advance. 预先感谢您的帮助。

The only way which I found to tackle with this problem was to download OpenCV source code and then build it using CMAKE and Visual Studio 2010. Now using cmake built libraries, eliminate all of these problems. 我发现要解决此问题的唯一方法是下载OpenCV源代码,然后使用CMAKE和Visual Studio 2010对其进行构建。现在,使用cmake构建的库消除所有这些问题。 There have also been 'imread' and 'imshow' related problems; 还存在与“ imread”和“ imshow”相关的问题; those problems also don't appear, when you build openCV libraries from CMAKE. 从CMAKE构建openCV库时,这些问题也不会出现。 For how to build libraries from CMAKE check this link http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html I hope it would help. 有关如何从CMAKE构建库的信息,请检查此链接http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html ,希望对您有所帮助。

I just ran into a similar issue where the same calibrate code crashed inside calibrateCamera using MSVC 2013 even though everything looked OK input, count matched etc and when i skipped the exception the image did actually calibrate. 我只是遇到了一个类似的问题,即即使一切看上去输入正常,计数匹配等,同样的校准代码也使用MSVC 2013在calibrateCamera内崩溃了,当我跳过异常时,图像确实进行了校准。

In my case the issue was that the OpenCV compiled library I was using was compiled as a shared/DLL and my application was using it in static library mode, so changing to Multi Threaded Debug DLL vs Multi Threaded Debug in MSVC fixed it (/MTd vs /MDd). 就我而言,问题是我使用的OpenCV编译库被编译为共享库/ DLL,而我的应用程序则在静态库模式下使用它,因此在MSVC中更改为多线程调试DLL与多线程调试可修复它(/ MTd与/ MDd)。 Or alternatively switching to a static build of OpenCV. 或者,切换到静态版本的OpenCV。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM