Ask Your Question

Judes's profile - activity

2020-04-25 11:08:49 -0600 received badge  Notable Question (source)
2018-01-15 20:13:31 -0600 received badge  Popular Question (source)
2015-02-25 05:40:04 -0600 received badge  Student (source)
2014-05-26 06:22:31 -0600 received badge  Editor (source)
2014-05-26 06:21:43 -0600 asked a question Assertion Failed (ni>=0) in calibration.cpp

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 ???