【发布时间】:2014-03-24 06:28:39
【问题描述】:
我在循环中多次运行 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
无法追踪错误。提前致谢。
【问题讨论】: