wc_3344 于 2014.05.14 10:46 提问

static void initGMMs( const Mat& img, const Mat& mask, GMM& bgdGMM, GMM& fgdGMM )

{

const int kMeansItCount = 10; //迭代次数

const int kMeansType = KMEANS_PP_CENTERS; //Use kmeans++ center initialization by Arthur and Vassilvitskii

``````Mat bgdLabels, fgdLabels; //记录背景和前景的像素样本集中每个像素对应GMM的哪个高斯模型，论文中的kn
vector<Vec3f> bgdSamples, fgdSamples; //背景和前景的像素样本集
int x,y;
#pragma omp parallel for
for (y = 0; y < img.rows; y++)
//for( p.y = 0; p.y < img.rows; p.y++ )
{
for (x = 0; x < img.cols; x++)
//for( p.x = 0; p.x < img.cols; p.x++ )
{
#pragma omp critical
{
Point p;
p.y = y;
p.x = x;
bgdSamples.push_back( (Vec3f)img.at<Vec3b>(p) );
else
fgdSamples.push_back( (Vec3f)img.at<Vec3b>(p) );
}
}
}

CV_Assert( !bgdSamples.empty() && !fgdSamples.empty() );

Mat _bgdSamples( (int)bgdSamples.size(), 3, CV_32FC1, &bgdSamples[0][0] );
kmeans( _bgdSamples, GMM::componentsCount, bgdLabels,
TermCriteria( CV_TERMCRIT_ITER, kMeansItCount, 0.0), 0, kMeansType );
Mat _fgdSamples( (int)fgdSamples.size(), 3, CV_32FC1, &fgdSamples[0][0] );
kmeans( _fgdSamples, GMM::componentsCount, fgdLabels,
TermCriteria( CV_TERMCRIT_ITER, kMeansItCount, 0.0), 0, kMeansType );

bgdGMM.initLearning();
try
{
#pragma omp parallel for
for( int i = 0; i < (int)bgdSamples.size(); i++ )
bgdGMM.endLearning();
}
catch( cv::Exception& e )
{
const char* err_msg = e.what();
printf ("%s\n", err_msg);
}
fgdGMM.initLearning();
int t = (int)fgdSamples.size();
try
{
#pragma omp parallel for
for( int i = 0; i < (int)fgdSamples.size(); i++ )
fgdGMM.endLearning();
}
catch(cv::Exception& e)
{
const char* err_msg = e.what();
printf ("%s\n", err_msg);
}
``````

}

1个回答

yzf060109   2015.10.28 08:54

push_back的问题