OpenCV 相机校准,断言失败 <ni > 0 && ni==ni1>

OpenCV Camera Calibration, Assertion Failed <ni > 0 && ni==ni1>

我正在尝试使用 openCV 执行相机校准。在来自 openCV 样本的相同示例数据上,这很好用,但我的来源生成错误:

Assertion failed <ni > 0 && ni==ni1>... collectCallibrationData..    calibration.cpp line 3193"
objectPoints and imagePoints are the same size. Changing other inputs dont   affect error code. 

谁能帮我处理一下?代码如下:

void main()//enter code here
{
//initial operations, declatarions etc
int *Asize; float *APoints; 
float AAPoints[]= {/* My Array */};
int AAsize[] = {4,54,2}; //My array size
Asize = AAsize; APoints = AAPoints; // will be called as dll that is why this wierd attribution
int page = *(Asize); int row = *(Asize+1); int col = *(Asize +2);
Point3f pointBuf;
vector<vector<Point3f>> imagePoints;
vector<Point3f> vectBuf;
Size boardSize; 
boardSize.height = 6; boardSize.width = 9;
Mat cameraMatrix, distCoeffs;
float squareSize=50;


//change 1d array to vector<vector<point3f>>
for (int i = 0; i<page; i++)
{
    for (int j = 0; j<row; j++)
    {
        pointBuf.x = *(APoints + (i*(row*col))+j*2);
        pointBuf.y = *(APoints + (i*(row*col))+j*2+1);
        pointBuf.z = 0;
        vectBuf.push_back(pointBuf);
    }
    imagePoints.push_back(vectBuf);
    vectBuf.clear();
}

// create objectPoint vector<vector<Points3f>>
vector<vector<Point3f>> objectPoints;
 vectBuf.clear();
  for( int i = 0; i < boardSize.height; ++i )
   {
    for( int j = 0; j < boardSize.width; ++j )
         vectBuf.push_back(Point3f(float( j*squareSize ), float( i*squareSize ), 0));
  }
objectPoints.resize(imagePoints.size(),vectBuf);

//initialize starting variables for calibration
cameraMatrix = Mat::eye(3, 3, CV_64F);
distCoeffs = Mat::zeros(1, 1, CV_64F);
cameraMatrix.at<double>(0,0) = 1.0;
vector<Mat> rvecs, tvecs;
Size imageSize; imageSize.width = 2040; imageSize.height = 2040; 


double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs);
}

cv::calibrateCamera() 的使用有问题。它需要 std::vector<std::vector<cv::Point3f>> 作为第一个参数,std::vector<std::vector<cv::Point2f>> 作为第二个参数。

例如:

cv::Size imageSize(2040, 2040);
cv::Mat cameraMatrix, distCoeffs;
std::vector<cv::Mat> rvecs,  tvecs;
std::vector<std::vector<cv::Point2f> > imagePoints;  
std::vector<std::vector<cv::Point3f> > objectPoints;

// Fill imagePoints and objectPoints
...

cv::calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs);

我也遇到了同样的问题。就我而言,存在将 vector< vector < Point3f >> 转换为 InputArrayOfArrays 的错误。 您可以运行以下示例代码在您的系统中进行检查。

using namespace std;
using namespace cv;

/** @function main */
int main( void )
{   
    vector<Point3f> objectPoints_tmp(54);
    objectPoints_tmp.clear();

    for( int i = 0; i < 9; ++i )
        for( int j = 0; j < 6; ++j )
            objectPoints_tmp.push_back(Point3f(j*50, i*50, 0.0f));

    vector<vector<Point3f> > objectPoints(7, objectPoints_tmp); 
    InputArrayOfArrays OBJPOINT = objectPoints; std::cout << (int) OBJPOINT.total() << std::endl;  

    for( int i = 0; i < 7; ++i )
        std::cout << OBJPOINT.getMat(i).checkVector(3, CV_32F) << std::endl;  

    waitKey(0);
    return 0;
}

正常应该打印出来

7
54
54
54
54
54
54
54

在我的 NG 案例中,它打印出随机数。

我猜opencv 库不适合我的系统(win7 x64 + vs2012)。自己重建opencv后,它现在可以工作了。您可以 google 关键字 "Win7x64 VS2012 OpenCV CMake TBB".