OpenCV 期望最大化

标签 opencv gaussian expectation-maximization

我正在尝试在 OpenCV 2.4.5 上使用 EM 来分离背景和前景图像。然而,与以前版本的 C 类不同,C++ 对我来说非常困惑,并且由于缺乏文档(从我的观点来看..),几个例程相当困惑

我写了下面的代码,但是好像不行。它给出了错误,我非常努力地调试但仍然无法正常工作。

Mat image;
image = imread("rose.jpg",1);

Mat _m(image.rows, image.cols, CV_32FC3);
Mat _f(image.rows, image.cols, CV_8UC3);
Mat _b(image.rows, image.cols, CV_8UC3);

Mat sample(image.rows * image.cols, 3, CV_32FC1);

Mat float_image;
image.convertTo(float_image,CV_64F);

Mat background_ = Mat(image.rows * image.cols, 3, CV_64F);

int counter = 0;
//Converting from Float image to Column vector
for (int j = 0; j < image.rows; j++)
{
    Vec3f* row = float_image.ptr<Vec3f > (j);

    for (int i = 0; i < image.cols; i++)
    {
        sample.at<Vec3f> (counter++, 0) = row[i];
    }
}

//sample.reshape(1,image.rows * image.cols);
cout<<"Training"<<endl;
EM params = EM(2);
params.train(sample);
    Mat _means = params.get<Mat>("means");
Mat _weights = params.get<Mat> ("weights");
cout<<"Finished Training"<<endl;

基本上,我将图像转换为 CV_64F 类型的 float 并将其传递到训练例程中。也许我认为我错了,我能得到帮助吗?谢谢

最佳答案

您正在混合 float 类型。

如果需要 double ,请将 Vec3f 更改为 Vec3d

否则

image.convertTo(float_image,CV_64F);
Mat background_ = Mat(image.rows * image.cols, 3, CV_64F);

应该是

image.convertTo(float_image,CV_32F);
Mat background_ = Mat(image.rows * image.cols, 3, CV_32F);

关于OpenCV 期望最大化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16789687/

相关文章:

opencv - 什么样的特征向量更适合检测 parking 场是否有车?

android - OpenCV HoughCircles cvRound

c++ - 在image-OpenCV和C++中添加高斯噪声然后去噪?

machine-learning - 如何证明EM的收敛性?

c++ - OpenCV:期望最大化的预测函数的输出

python - 加速高斯 EM 算法

python - 属性错误 : 'numpy.ndarray' object has no attribute 'getdraw'

c++ - OpenCV,C++ : Distance between two points

matlab - MATLAB 中的 3D 高斯滤波器

python - 数组名下划线含义