简体   繁体   English

Mat OpenCV中的断言失败

[英]Assertion failed in Mat OpenCV

I am running the EM algorithm in OpenCV several times in a loop. 我多次在OpenCV中运行EM算法。 Initially the EM runs with default initial parameters. 最初,EM使用默认的初始参数运行。 In subsequent iterations we pass parameters to the EM algorithm based on output of previous iteration. 在后续迭代中,我们根据先前迭代的输出将参数传递给EM算法。 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. 通过遍历回溯的框架,您可以检查所有变量的值-这将告诉您断言为何消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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