简体   繁体   中英

Assertion failed in Mat OpenCV

I am running the EM algorithm in OpenCV several times in a loop. Initially the EM runs with default initial parameters. In subsequent iterations we pass parameters to the EM algorithm based on output of previous iteration. Here is the code

Mat meansCombine;
Mat weightsCombine;
vector<Mat> covsCombine;
for(int k=maxComponents; k>=minComponents; k--){

    EM model(k,EM::COV_MAT_DIAGONAL,TermCriteria(TermCriteria::COUNT+TermCriteria::EPS,2,0.0001));

    Mat labels;
    Mat probs;
    Mat log_likelihoods;

    if( k==maxComponents )
    {
        model.train(samples,log_likelihoods, labels, probs);
    }
    else
    {
        model.trainE(samples, meansCombine, covsCombine, weightsCombine, log_likelihoods, labels, probs); //provide parameters as per previous iteration results
    }

    double total_likelihood = 0.0;

    for(int i=0;i<log_likelihoods.rows;i++){
        double t = log_likelihoods.at<double>(i,0);
        total_likelihood += t;

    }

    int dimension =3;
    double l = k*(1 + dimension + ((dimension+1)*dimension)/2)-1;
    double penalty = 0.5*l*log(samples.rows*dimension);
    double mdl = -total_likelihood + penalty;
    mdl_output << "********** No. of components=" << k << "***********" << endl;
    mdl_output << "Total log likelihood=" << total_likelihood << endl;
    mdl_output << "Penalty=" << penalty << endl;
    mdl_output << "MDL value=" << mdl << endl;

    if(mdl < minMdl)
    {   
        minMdl = mdl;
        minK = k;
    }

    int c1,c2;
    Mat means = model.get<Mat>("means");
    Mat weights = model.get<Mat>("weights");
    vector<Mat> covs = model.get<vector<Mat> >("covs");

    leastBhattacharyaDist(means,covs,c1,c2);
    mdl_output << "Merging components" << c1 <<" and " << c2 <<endl;

    meansCombine = Mat(means.rows-1,means.cols,means.type());
    weightsCombine = Mat(weights.rows,(weights.cols)-1,weights.type());
    covsCombine.clear();

    mergeComponents(means,covs,weights,c1,c2,meansCombine,covsCombine,weightsCombine);

}

Running this code gives me the following assertion failed message.

Assertion failed (0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows) in Mat, file /home/one_more_step/Documents/OpenCV/opencv-2.4.7/modules/core/src/matrix.cpp, line 284

Not able to trace the bug. Thanks in advance.

Asserts usually indicate that the following code was written with certain assumptions in mind - and your parameters do not fit the assumptions. (The dumbest thing you can do is remove the assert - yes, the code may work, but after all the assumptions aren't met so you'll shoot yourself in the foot somewhere in the future).

Asserts sometimes are complicated because they may be triggered by variables or code flows you do not control.

Usually an assertion is pretty easy to debug. Just run your code within the debugger, when the assertion happens: look at the backtrace.

The backtrace will tell you where the call happens from the code you show above.

By stepping along the frames of the backtrace, you can inspect the value of all variables along - which will tell you why the assert went off.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM