Ask Your Question
1

Assertion Failed (ni>=0) in calibration.cpp

asked 2014-05-26 06:21:43 -0600

Judes gravatar image

updated 2014-05-26 06:22:31 -0600

Hello everyone,

I'm a beginner in OpenCV.

I'm trying to create a tool for calibrate my ActionCam and I have issue with the Calibration Part.

The code part below, extracted from my "camera.cpp" (sources directory --> runCalibration function), stuck at calibrateCamera()

Error Code :

OpenCV Error: Assertion Failed (ni >=0) in unknown function, file ......\src\opencv\modules\calib3d\src\calibration.cpp, line 3173

//----------- Code Original ------------
vector<vector<Point3f> > objectPoints(1);
calcChessboardCorners(boardSize, squareSize, objectPoints[0], patternType);
objectPoints.resize(imagePoints.size(),objectPoints[0]);
//Find intrinsic and extrinsic camera parameters
double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix,
                distCoeffs, rvecs, tvecs, flags|CV_CALIB_FIX_K4|CV_CALIB_FIX_K5);
                ///*|CV_CALIB_FIX_K3*/|CV_CALIB_FIX_K4|CV_CALIB_FIX_K5);
printf("RMS error reported by calibrateCamera: %g\n", rms);

I locate where the message is coming from in Calibration.cpp. It's a function collectCalibrationData() used in cameraCalibrate()

static void collectCalibrationData( InputArrayOfArrays objectPoints,
                                InputArrayOfArrays imagePoints1,
                                InputArrayOfArrays imagePoints2,
                                Mat& objPtMat, Mat& imgPtMat1, Mat* imgPtMat2,
                                Mat& npoints )
{
int nimages = (int)objectPoints.total();
int i, j = 0, ni = 0, total = 0;
CV_Assert(nimages > 0 && nimages == (int)imagePoints1.total() &&
    (!imgPtMat2 || nimages == (int)imagePoints2.total()));

for( i = 0; i < nimages; i++ )
{
    ni = objectPoints.getMat(i).checkVector(3, CV_32F);
    CV_Assert( ni >= 0 );
    total += ni;
}

npoints.create(1, (int)nimages, CV_32S);
objPtMat.create(1, (int)total, CV_32FC3);
imgPtMat1.create(1, (int)total, CV_32FC2);
Point2f* imgPtData2 = 0;

if( imgPtMat2 )
{
    imgPtMat2->create(1, (int)total, CV_32FC2);
    imgPtData2 = imgPtMat2->ptr<Point2f>();
}

Point3f* objPtData = objPtMat.ptr<Point3f>();
Point2f* imgPtData1 = imgPtMat1.ptr<Point2f>();

for( i = 0; i < nimages; i++, j += ni )
{
    Mat objpt = objectPoints.getMat(i);
    Mat imgpt1 = imagePoints1.getMat(i);
    ni = objpt.checkVector(3, CV_32F);
    int ni1 = imgpt1.checkVector(2, CV_32F);
    CV_Assert( ni > 0 && ni == ni1 );
    npoints.at<int>(i) = ni;
    memcpy( objPtData + j, objpt.data, ni*sizeof(objPtData[0]) );
    memcpy( imgPtData1 + j, imgpt1.data, ni*sizeof(imgPtData1[0]) );

    if( imgPtData2 )
    {
        Mat imgpt2 = imagePoints2.getMat(i);
        int ni2 = imgpt2.checkVector(2, CV_32F);
        CV_Assert( ni == ni2 );
        memcpy( imgPtData2 + j, imgpt2.data, ni*sizeof(imgPtData2[0]) );
    }
 }
}

I used a SPY to visualize my ObjectPoints & ImagePoints vector, but there are complete and have the same size.

image description

Anyone had a clue for this ???

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-10-27 03:08:30 -0600

I met the same issue, too. In my case, there is a bug to convert vector< vector < Point3f > > to InputArrayOfArrays. You may run the following sample code to check it in your system.

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;
}

Normally, it should print out

7
54
54
54
54
54
54
54

In my NG case, it prints out random numbers.

I guess that the opencv lib is not fit with my system(win7 x64 + vs2012). After rebuild opencv myself, it may work now. You may google keyword "Win7x64 VS2012 OpenCV CMake TBB".

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-05-26 06:21:43 -0600

Seen: 3,945 times

Last updated: May 26 '14