2

我是opencv的初学者。我还没有详细了解opencv的主要概念。

所以也许我的代码太笨了;

出于好奇,我想尝试 KNN、ANN 等机器学习功能。我有一组大小为 28*28 像素的图像。我想做训练 cassifier 进行数字识别。所以首先我需要组装火车组和训练班;

    Mat train_data = Mat(rows, cols, CV_32FC1);
    Mat train_classes = Mat(rows, 1, CV_32SC1);
    Mat img = imread(image);
    Mat float_data(1, cols, CV_32FC1);
    img.convertTo(float_data, CV_32FC1);

如何用 float_data 填充 train_data ?我以为是这样的:

for (int i = 0; i < train_data.rows; ++i) 
{
    ... // image is a string which contains next image path
    image = imread(image);
    img.convertTo(float_data, CV_32FC1);
    for( int x = 0; x < train_data.cols; x++ ){
      train_data.at<float> (i, x) = float_data.at<float>( x);; 
    }
 }

 KNearest knn;
 knn.train(train_data, train_classes);

但这当然行不通。. . 请告诉我如何正确地做。或者至少为傻瓜推荐书籍:)

4

1 回答 1

2
Mat train_data; // initially empty
Mat train_labels; // empty, too.

// for each img in the train set : 
    Mat img = imread("image_path");
    Mat float_data;
   img.convertTo(float_data, CV_32FC1);             // to float
   train_data.push_back( float_data.reshape(1,1) ); // add 1 row (flattened image)
   train_labels.push_back( label_for_image );       // add 1 item

KNearest knn;
knn.train(train_data, train_labels);

其他毫升算法都是一样的!

于 2013-10-24T16:56:22.463 回答