Mat OpenCV中的断言失败 [英] Assertion failed in Mat OpenCV

查看:643
本文介绍了Mat OpenCV中的断言失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我多次在OpenCV中运行EM算法.最初,EM使用默认的初始参数运行.在后续迭代中,我们根据先前迭代的输出将参数传递给EM算法.这是代码

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.

这篇关于Mat OpenCV中的断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆