【问题标题】:Training svm for images in opencv在opencv中训练图像的svm
【发布时间】:2016-03-29 21:56:09
【问题描述】:

我已经参考了以下链接: Link 1 和 Link 2

从上面我必须设法写出以下内容:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>

using namespace cv;
using namespace std;

int main(){
    int num_files = 2;
    int width = 128, height = 128;

    Mat image[2];
    image[0] = imread("Tomato.jpg");
    image[1] = imread("Melon.jpg");

    Mat new_image(2,height*width,CV_32FC1); //Training sample from input images

    int ii = 0;
    for (int i = 0; i < num_files; i++){
        Mat temp = image[i];
        ii = 0;
        for (int j = 0; j < temp.rows; j++){
            for (int k = 0; k < temp.cols; k++){
                new_image.at<float>(i, ii++) = temp.at<uchar>(j, k);
            }
        }
    }
    //new_image.push_back(image[0].reshape(0, 1));
    //new_image.push_back(image[1].reshape(0, 1));
    Mat labels(num_files, 1, CV_32FC1);
    labels.at<float>(0, 0) = 1.0;//tomato
    labels.at<float>(1, 0) = -1.0;//melon

    imshow("New image", new_image);
    printf("%f %f", labels.at<float>(0, 0), labels.at<float>(1, 0));

    CvSVMParams params;
    params.svm_type = CvSVM::C_SVC;
    params.kernel_type = CvSVM::LINEAR;
    params.gamma = 3;
    params.degree = 3;
    params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
    CvSVM svm;
    svm.train(new_image, labels, Mat(), Mat(), params);

    svm.save("svm.xml"); // saving
    svm.load("svm.xml"); // loading

    Mat test_img = imread("Tomato.jpg");
    test_img=test_img.reshape(0, 1);
    imshow("shit_image", test_img);
    test_img.convertTo(test_img, CV_32FC1);
    svm.predict(test_img);

    waitKey(0);
}

我收到以下错误:

不支持的格式或格式组合,输入样本在cvPreparePredictData中必须有32FC1类型...

我遵循了第二个链接中的所有步骤。所有矩阵都是 32FC1 类型。 我错过了什么? svm 参数有问题吗? 当我尝试预测结果时导致错误。

【问题讨论】:

  • 旁注:您声明了两次变量ì、j 和k。一次就足够了(在'for'内)
  • 有人刚刚在这里发布了正确答案,然后删除了评论。
  • 问题是由于色彩空间的差异。用灰度读取文件( imread("tomato.jpg",0) ) 修复它。

标签: c++ opencv


【解决方案1】:

检查

1) Tomato.jpg 和 Melon.jpg 大小为 128*128 ?
2)两个图像都是灰度的?

如果不。试试这个代码:
我只是添加 resize()、cvtColor() 和打印结果
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
#include <opencv2\imgproc\imgproc.hpp>

using namespace cv;
using namespace std;

int main(){
    int num_files = 2;
    int width = 128, height = 128;

    Mat image[2];
    image[0] = imread("Tomato.jpg", 0);
    image[1] = imread("Melon.jpg", 0);

    resize(image[0], image[0], Size(width, height));
    resize(image[1], image[1], Size(width, height));

    Mat new_image(2, height*width, CV_32FC1); //Training sample from input images

    int ii = 0;
    for (int i = 0; i < num_files; i++){
        Mat temp = image[i];
        ii = 0;
        for (int j = 0; j < temp.rows; j++){
            for (int k = 0; k < temp.cols; k++){
                new_image.at<float>(i, ii++) = temp.at<uchar>(j, k);
            }
        }
    }
    //new_image.push_back(image[0].reshape(0, 1));
    //new_image.push_back(image[1].reshape(0, 1));
    Mat labels(num_files, 1, CV_32FC1);
    labels.at<float>(0, 0) = 1.0;//tomato
    labels.at<float>(1, 0) = -1.0;//melon

    imshow("New image", new_image);
    printf("%f %f", labels.at<float>(0, 0), labels.at<float>(1, 0));

    CvSVMParams params;
    params.svm_type = CvSVM::C_SVC;
    params.kernel_type = CvSVM::LINEAR;
    params.gamma = 3;
    params.degree = 3;
    params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
    CvSVM svm;
    svm.train(new_image, labels, Mat(), Mat(), params);

    svm.save("svm.xml"); // saving
    svm.load("svm.xml"); // loading

    Mat test_img = imread("Tomato.jpg", 0);
    resize(test_img, test_img, Size(width, height));
    test_img = test_img.reshape(0, 1);
    imshow("shit_image", test_img);
    test_img.convertTo(test_img, CV_32FC1);
    float res = svm.predict(test_img);
    if (res > 0)
        cout << endl << "Tomato";
    else
        cout << endl << "Melon";
    waitKey(0);
}

【讨论】:

  • 我能够解决这个问题。问题是因为训练图像和测试图像不在空间颜色空间中。我将它们全部设为灰度,从而修复了代码。
  • @PurusharthDwivedi 请关闭您的问题,或者如果上面的答案有帮助 - 标记它
猜你喜欢
  • 2015-06-03
  • 2015-05-04
  • 2015-07-25
  • 2016-11-16
  • 1970-01-01
  • 2015-04-18
  • 2015-01-22
  • 2017-08-25
相关资源
最近更新 更多