我在循环中多次在OpenCV中运行EM算法。最初,EM使用默认初始参数运行。在随后的迭代中,我们基于先前迭代的输出将参数传递给EM算法。这是代码
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);
}
运行此代码会给我以下断言失败消息。
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
无法追踪错误。提前致谢。
答案 0 :(得分:1)
断言通常表明以下代码是在考虑某些假设的情况下编写的 - 并且您的参数不符合假设。 (你能做的最蠢事就是删除断言 - 是的,代码可能会起作用,但是在所有的假设都没有得到满足之后,你将来会在某个地方射击自己。)
断言有时很复杂,因为它们可能由您无法控制的变量或代码流触发。
通常断言很容易调试。当断言发生时,只需在调试器中运行代码:查看回溯。
回溯将通过您上面显示的代码告诉您调用的位置。
通过沿着回溯的帧步进,您可以检查所有变量的值 - 这将告诉您断言为什么会消失。