0

我已经在我的图像上应用了期望最大化。但是当我运行可执行文件时,它会显示一个透明屏幕。谁能告诉我我的代码有什么问题?谢谢

这是我的代码

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/legacy/legacy.hpp>

using namespace std;
using namespace cv;

Mat asSamplesVectors(Mat& img);

/** @function main */
int main(int argc, char** argv)
{
    Mat image; int no_of_clusters = 2;
    image = imread("/home/rishav/flower.jpg");
    Mat samples = asSamplesVectors(image);

    cout << "Starting EM training" << endl;
    EM em(no_of_clusters);
    em.train(samples);
    cout << "Finished training EM" << endl;

    vector<Mat> segmented;
    for (int i = 0; i < no_of_clusters; i++)
        segmented.push_back(Mat::zeros(image.rows, image.cols, CV_8UC3));

    int index = 0;
    for (int y = 0; y < image.rows; y++) {
        for (int x = 0; x < image.cols; x++) {
            int result = em.predict(samples.row(index++))[1];
            segmented[result].at<Point3i>(y, x, 0) = image.at<Point3i>(y, x, 0);
        }
    }
    imshow("result", samples);
    waitKey(0);
    return(0);
}

Mat asSamplesVectors(Mat& img) {
    Mat float_img;
    img.convertTo(float_img, CV_32F);

    Mat samples(img.rows * img.cols, 3, CV_32FC1);

    /* Flatten  */
    int index = 0;
    for (int y = 0; y < img.rows; y++) {
        Vec3f* row = float_img.ptr<Vec3f>(y);
        for (int x = 0; x < img.cols; x++)
            samples.at<Vec3f>(index++, 0) = row[x];
    }
    return samples;
}
4

0 回答 0