簡體   English   中英

OpenCV calibrateCamera()斷言失敗

[英]OpenCV calibrateCamera() Assertion failed

我一直在嘗試使用Opencv calibrateCamera()函數校准我的相機。 我遵循了與opencv示例程序中所述相同的過程。 我正在嘗試先加載10 9 x 6棋盤圖像。 然后找到棋盤角。 如果找到了角點,則角點的像素位置存儲在vector <vector <Point2f >> ImagePoints中。 對所有圖像執行此操作后,將執行runCalibrationAndSave部分。 在runCalibrationAndSave中,執行第一個runCalibration部分,其中用角的真實坐標值填充ObjectPoints(類型為vector <vector <Point3f >>)。 到此為止,代碼運行良好,並且沒有問題。准確地找到了棋盤角,並且ImagePoints矢量也填充了矢量。 但是,當轉到calibrateCamera()部分時,OpenCV ::斷言將失敗,並出現以下錯誤:

OpenCV錯誤:

斷言失敗(nimages> 0 && nimages ==(int)imagePoints1.total()&&(!imgPtMat2 || nimages ==(int)imagePoints2.total()))在setCalibrationData,文件/ ........中.... / 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 ,希望對您有所幫助。

我只是遇到了一個類似的問題,即即使一切看上去輸入正常,計數匹配等,同樣的校准代碼也使用MSVC 2013在calibrateCamera內崩潰了,當我跳過異常時,圖像確實進行了校准。

就我而言,問題是我使用的OpenCV編譯庫被編譯為共享庫/ DLL,而我的應用程序則在靜態庫模式下使用它,因此在MSVC中更改為多線程調試DLL與多線程調試可修復它(/ MTd與/ MDd)。 或者,切換到靜態版本的OpenCV。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM