2012-09-22 20 views
2

我正在使用opencv进行摄像机标定。我使用的是“烹饪书籍编程”中给出的相同代码。在OpenCV摄像机标定过程中出错

我从我的智能手机的棋盘上拍照。然后我使用opencv程序为我做相机校准。当我有非常大的棋盘时,程序只能处理一组图像。它不为另一组图像的工作,我得到运行时错误“断言失败enter image description here

我不知道在我的代码出错的代码如下: - 。

int main() 
{ 
CameraCalibrator calibrateCam; 
std::vector<std::string> filelist; 
char buff[100]; 

for(int i=0;i<21;i++) 
{ 
    sprintf(buff,"..\\Train\\3\\%d.jpg",i+1); 
    filelist.push_back(buff); 
} 
cv::Size boardSize(4,3); 
double calibrateError; 
int success; 
success = calibrateCam.addChessboardPoints(filelist,boardSize); 
} 

class CameraCalibrator{ 
public: 
    std::vector<std::vector<cv::Point3f>> objectPoints; 
    std::vector<std::vector<cv::Point2f>> imagePoints; 
    //Square Lenght 
    float squareLenght; 
    //output Matrices 
    cv::Mat cameraMatrix; //intrinsic 
    cv::Mat distCoeffs; 
    //flag to specify how calibration is done 
    int flag; 
    //used in image undistortion 
    cv::Mat map1,map2; 
    bool mustInitUndistort; 
public: 
    CameraCalibrator(): flag(0), squareLenght(36.0), mustInitUndistort(true){}; 
    int addChessboardPoints(const std::vector<std::string>& filelist,cv::Size& boardSize){ 
     std::vector<std::string>::const_iterator itImg; 
     std::vector<cv::Point2f> imageCorners; 
     std::vector<cv::Point3f> objectCorners; 
     //initialize the chessboard corners in the chessboard reference frame 
     //3d scene points 
     for(int i = 0; i<boardSize.height; i++){ 
      for(int j=0;j<boardSize.width;j++){ 
       objectCorners.push_back(cv::Point3f(float(i)*squareLenght,float(j)*squareLenght,0.0f)); 
      } 
     } 
     //2D Image points: 
     cv::Mat image; //to contain chessboard image 
     int successes = 0; 

     for(itImg=filelist.begin(); itImg!=filelist.end(); itImg++){ 
      image = cv::imread(*itImg,CV_LOAD_IMAGE_GRAYSCALE); 
      std::cout<<*itImg<<"\n"; 

      bool found = cv::findChessboardCorners(image, boardSize, imageCorners); 

      cv::drawChessboardCorners(image, boardSize, imageCorners, found);      
      cv::cornerSubPix(image, imageCorners, cv::Size(5,5),cv::Size(-1,-1), 
       cv::TermCriteria(cv::TermCriteria::MAX_ITER+cv::TermCriteria::EPS,30,0.1)); 
      //if we have a good board, add it to our data 
      if(imageCorners.size() == boardSize.area()){ 
       addPoints(imageCorners,objectCorners); 
       successes++; 
      } 
     } 

     return successes; 
    } 
    void addPoints(const std::vector<cv::Point2f>& imageCorners,const std::vector<cv::Point3f>& objectCorners){ 
     //2D image point from one view 
     imagePoints.push_back(imageCorners); 
     //corresponding 3D scene points 
     objectPoints.push_back(objectCorners); 
    } 
    double calibrate(cv::Size &imageSize){ 
     mustInitUndistort = true; 
     std::vector<cv::Mat> rvecs,tvecs; 
     return 
      cv::calibrateCamera(objectPoints, //the 3D points 
       imagePoints, 
       imageSize, 
       cameraMatrix, //output camera matrix 
       distCoeffs, 
       rvecs,tvecs, 
       flag); 

    } 
    void remap(const cv::Mat &image, cv::Mat &undistorted){ 
     std::cout << cameraMatrix; 
     if(mustInitUndistort){ //called once per calibration 
      cv::initUndistortRectifyMap(
       cameraMatrix, 
       distCoeffs, 
       cv::Mat(), 
       cameraMatrix, 
       image.size(), 
       CV_32FC1, 
       map1,map2); 
      mustInitUndistort = false; 
     } 
     //apply mapping functions 
     cv::remap(image,undistorted,map1,map2,cv::INTER_LINEAR); 
    } 
}; 

在摄像机标定类,它会成功打开图像,但它在findChessboardCorners行上失败......

Plz在这方面帮了我一个样本棋盘图像如下:该算法在第一张图像上找不到角落。 。 - enter image description here

+0

您尚未提供足够的信息。算法在上面的图像中失败了吗?如果没有,那么当算法失败时应该发布一些图像加上您的实际校准代码,而不仅仅是这部分。你画出检测到的点吗?另外,为什么你的黑色方块周围有红色边框? – Sassa

+0

我已经更新了我的答案。 PLZ看看,让我知道。 。 。 – user1388142

回答

2

首先,在cv::findChessboardCorners您的boardSize是错误的,您应将其定义为cv::Size boardSize(4,3);cv::Size boardSize(5,4);,因为您的主板每行有5个内角和每列有4个角。


此外,你应该增加一个检查的情况下,都没有发现的角落。在你的代码,后

bool found = cv::findChessboardCorners(image, boardSize, imageCorners); 

你继续和呼叫功能,如cv::drawChessboardCornerscv::cornerSubPix如果没有弯道,这将无法正常工作。这部分应该是:

for(itImg=filelist.begin(); itImg!=filelist.end(); itImg++) 
{ 
    image = cv::imread(*itImg,CV_LOAD_IMAGE_GRAYSCALE); 
    std::cout<<*itImg<<"\n"; 

    bool found = cv::findChessboardCorners(image, boardSize, imageCorners); 

    if (found) // continue only if corners have been found 
    { 
     cv::drawChessboardCorners(image, boardSize, imageCorners, found);      
     cv::cornerSubPix(image, imageCorners, cv::Size(5,5),cv::Size(-1,-1), cv::TermCriteria(cv::TermCriteria::MAX_ITER+cv::TermCriteria::EPS,30,0.1)); 

     //if we have a good board, add it to our data 
     if(imageCorners.size() == boardSize.area()) 
     { 
      addPoints(imageCorners,objectCorners); 
      successes++; 
     } 
    } 
    else // if no corners found 
    { 
     std::cout<<"No corners found in image"<<std::endl; 

     // Do anything else you want here 
    } 
} 


而且,为什么会出现在你的黑色正方形红色边框?我还没有看到cv::findChessboardCorners究竟是如何检测到角点的,但是这很可能导致算法失败。你可以尝试一下没有它的新板吗?虽然我们在上面,但上角的光线反射也会起到一定作用,因为黑色的亮度值非常浅。您是否在OpenCV提供的示例图像中尝试了算法?

+0

非常感谢您的建议。我会这样做,并会让你知道。 。 。 – user1388142