OPENCV中基于EM聚类的背景-前景分割
我尝试使用下面的代码执行基于EM的后台前景分割…我也在Stackoverflow中找到了…但似乎在某个地方出现了一些错误,因为我从未看到要执行的第二个printf语句。基本上,它永远不会到达代码的分类/聚类部分。下面给出了代码。有人能帮我吗OPENCV中基于EM聚类的背景-前景分割,opencv,background,em,background-foreground,Opencv,Background,Em,Background Foreground,我尝试使用下面的代码执行基于EM的后台前景分割…我也在Stackoverflow中找到了…但似乎在某个地方出现了一些错误,因为我从未看到要执行的第二个printf语句。基本上,它永远不会到达代码的分类/聚类部分。下面给出了代码。有人能帮我吗 #include <opencv2/opencv.hpp> #include <opencv2/legacy/legacy.hpp> char str1[60]; int main() { cv::Mat source
#include <opencv2/opencv.hpp>
#include <opencv2/legacy/legacy.hpp>
char str1[60];
int main()
{
cv::Mat source = cv::imread("C:\\Image Input\\part1.bmp" );
if(!source.data)
printf(" No data \n");
//ouput images
cv::Mat meanImg(source.rows, source.cols, CV_32FC3);
cv::Mat fgImg(source.rows, source.cols, CV_8UC3);
cv::Mat bgImg(source.rows, source.cols, CV_8UC3);
//convert the input image to float
cv::Mat floatSource;
source.convertTo(floatSource, CV_32F);
//now convert the float image to column vector
cv::Mat samples(source.rows * source.cols, 3, CV_32FC1);
int idx = 0;
for (int y = 0; y < source.rows; y++) {
cv::Vec3f* row = floatSource.ptr<cv::Vec3f > (y);
for (int x = 0; x < source.cols; x++) {
samples.at<cv::Vec3f > (idx++, 0) = row[x];
}
}
printf(" After Loop \n");
//we need just 2 clusters
cv::EMParams params(2);
cv::ExpectationMaximization em(samples, cv::Mat(), params);
//the two dominating colors
cv::Mat means = em.getMeans();
//the weights of the two dominant colors
cv::Mat weights = em.getWeights();
//we define the foreground as the dominant color with the largest weight
const int fgId = weights.at<float>(0) > weights.at<float>(1) ? 0 : 1;
printf(" After Training \n");
//now classify each of the source pixels
idx = 0;
for (int y = 0; y < source.rows; y++)
{
printf(" Now Classify\n");
for (int x = 0; x < source.cols; x++)
{
//classify
const int result = cvRound(em.predict(samples.row(idx++), NULL));
//get the according mean (dominant color)
const double* ps = means.ptr<double>(result, 0);
//set the according mean value to the mean image
float* pd = meanImg.ptr<float>(y, x);
//float images need to be in [0..1] range
pd[0] = ps[0] / 255.0;
pd[1] = ps[1] / 255.0;
pd[2] = ps[2] / 255.0;
//set either foreground or background
if (result == fgId) {
fgImg.at<cv::Point3_<uchar> >(y, x, 0) = source.at<cv::Point3_<uchar> >(y, x, 0);
} else {
bgImg.at<cv::Point3_<uchar> >(y, x, 0) = source.at<cv::Point3_<uchar> >(y, x, 0);
}
}
}
printf(" Show Images \n");
cv::imshow("Means", meanImg);
cv::imshow("Foreground", fgImg);
cv::imshow("Background", bgImg);
cv::waitKey(0);
return 0;
}
#包括
#包括
char-str1[60];
int main()
{
cv::Mat source=cv::imread(“C:\\Image Input\\part1.bmp”);
如果(!source.data)
printf(“无数据”);
//输出图像
cv::Mat meanImg(source.rows、source.cols、cv_32FC3);
cv::Mat fgImg(source.rows、source.cols、cv_8UC3);
cv::Mat bgImg(source.rows、source.cols、cv_8UC3);
//将输入图像转换为浮点
cv::Mat-floatSource;
source.convertTo(floatSource,CV_32F);
//现在将浮点图像转换为列向量
cv::Mat样本(source.rows*source.cols,3,cv_32FC1);
int-idx=0;
对于(int y=0;y在(1)处的权重?0:1;
printf(“培训后”\n);
//现在对每个源像素进行分类
idx=0;
对于(int y=0;y
代码工作正常。我认为你使用的图像太大,学习时间太长。尝试处理小图像
仅需1次校正,用零初始化图像:
//ouput images
cv::Mat meanImg=Mat::zeros(source.rows, source.cols, CV_32FC3);
cv::Mat fgImg=Mat::zeros(source.rows, source.cols, CV_8UC3);
cv::Mat bgImg=Mat::zeros(source.rows, source.cols, CV_8UC3);
谢谢你的友好回复,但是如果我像你提到的那样用0初始化矩阵,它会给我编译错误…其他东西是const int fgId=weights.at(0)>weights.at(1)?0 : 1; 这一行也给了我一个运行时错误。因此,如果只是将int-fgId初始化为0或1,那么它运行良好。。。。你能告诉我为什么吗?问题在哪里!!!