用图像训练SVM并进行预测
Training an SVM with images and making predictions
感谢您的帮助...
我不久前编写了一些代码,这些代码成功地检测到了动人的流量视频中的汽车。因此,让我们考虑该代码的输出,该代码的最终输入为150x200尺寸的车辆图像。
我试图实施的是一种SVM,它可以采用这些车辆并可以在轿车和SUV之间进行分类。(假设只有轿车和SUV)。
通过紧随此链接的信息:https://docs.opencv.org/3.0-beta/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html和此链接:使用OpenCV和SVM与图像
请注意,与这些链接相关的语法对于我拥有的最新版本的SVM的SVM实现略有过时。
//Used to read multiple files from folder
stringstream ss;
string name = "Vehicle_";
string type = ".jpg";
int num_train_images = 29; //29 images will be used to train the SVM
int image_area = 150 * 200;
Mat training_mat(num_train_images, image_area, CV_32FC1); // Creates a 29 rows by 30000 columns... 29 150x200 images will be put into 1 row per image
//Converts 29 2D images into a really long row per image
for (int file_count = 1; file_count < (num_train_images + 1); file_count++)
{
ss << name << file_count << type; //'Vehicle_1.jpg' ... 'Vehicle_2.jpg' ... etc ...
string filename = ss.str();
ss.str("");
Mat training_img = imread(filename, 1); //Reads the training images from the folder
int ii = 0; //Scans each column
for (int i = 0; i < training_img.rows; i++)
{
for (int j = 0; j < training_img.cols; j++)
{
training_mat.at<float>(file_count - 1, ii) = training_img.at<uchar>(i, j); //Fills the training_mat with the read image
ii++;
}
}
}
//Labels are used as the supervised learning portion of the SVM. If it is a 1, its an SUV test image. -1 means a sedan.
float labels[29] = { 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, 1 };
//Place the labels into into a 29 row by 1 column matrix.
Mat labels_mat(num_train_images, 1, CV_32FC1, labels);
cout << "Beginning Training..." << endl;
//Set SVM Parameters (not sure about these values)
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
svm->train(training_mat, ROW_SAMPLE, labels_mat);
cout << "End Training" << endl;
waitKey(0);
Mat test_image(1, image_area, CV_32FC1); //Creates a 1 x 30000 matrix to house the test image.
Mat SUV_image = imread("SUV_1.jpg", 0); //Read the file folder
int jj = 0;
for (int i = 0; i < SUV_image.rows; i++)
{
for (int j = 0; j < SUV_image.cols; j++)
{
test_image.at<float>(0, jj) = SUV_image.at<uchar>(i, j); //Fills the training_mat
jj++;
}
}
//Should return a 1 if its an SUV, or a -1 if its a sedan
svm->predict(test_image);
waitKey(0);
所以我在这里做的是,我拍摄测试图像,然后在训练_mat中将每个150 x 200映像转换为1行乘30,000列行。
labels_mat是SVM的监督学习部分,它告诉训练图像是SUV还是轿车。
代码构建正常,但不幸的是,当它到达SVM->火车时,它会失败,我会遇到一个中止错误,上面写着:" openCV错误:不良参数(在分类问题的情况下,响应必须是分类的;要么要么;在CV :: ML :: SvMimpl :: Train中指定vartype时指定vartype或通过整数响应)
不太确定这意味着什么,我的参数可能有问题。一个朋友建议,我可能需要在将图像的功能提取到SVM中之前提取图像的功能,我不确定是否需要。
谢谢
通过将labels_mat更改为cv_32s的整数类型来解决此问题。不幸的是,仍然存在一个新问题,即SVM->预测(test_image)返回一个不是-1或1的值。
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 平均图像时图像损坏
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- Vulkan验证层不断在VkQueuePresentKHR()上抛出图像布局错误
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- OpenCV SVM 分类器图像识别
- 用图像训练SVM并进行预测
- OpenCV 3.2.0:读取 Canny 图像以训练 SVM
- OPENCV C 的SVM返回同一类图像的相同置信度得分
- 如何训练svm对英文字母图像进行分类
- 使用 SVM 和 BOW 进行图像分类
- 用于SVM OpenCV训练的交通标志图像集
- 提取一组样本图像的描述符,并在OpenCV中训练SVM
- 使用 SVM 和 opencv 3 进行图像训练
- 用不同维数的图像训练SVM