我正在尝试对图像进行分类(下一步我将根据特征进行分类,但现在只想尝试我是否做得对)
这是我的代码。
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
using namespace cv;
using namespace std;
int main(){
Mat image[2];
image[0]= imread("image.jpg",0);
image[1]= imread("wrongimage.jpg",0);
Mat rotated = imread("image.jpg",0);
image[0] = image[0].reshape(0, 1); //SINGLE LINE
image[1] = image[1].reshape(0, 1); //SINGLE LINE
// image[0].convertTo(image[0], CV_32FC1); //CONVERT TO 32FC1
// image[1].convertTo(image[1], CV_32FC1); //CONVERT TO 32FC1
Mat new_image(2,1,CV_32FC1,image); //CONVERT TO 32FC1
float labels[2] = {1.0, -1.0};
Mat labelsmat(2,1,CV_32FC1,labels); //correct labels 1
labelsmat.convertTo(labelsmat, CV_32FC1);
CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
params.gamma = 3;
params.degree = 3;
CvSVM svm;
svm.train(new_image, labelsmat, Mat(),Mat(),params);
// svm.train(training_mat2, labelsmat, Mat(),Mat(),params);
// svm.train(training_mat2, labelsmat, Mat(), Mat(), params);
svm.save("svm.xml"); // saving
svm.load("svm.xml"); // loading
rotated = rotated.reshape(0,1);
rotated.convertTo(rotated, CV_32FC1);
svm.predict(rotated);
}
由于使用 opencv svm 训练图像缺乏记录,我尝试通过阅读来管理某些内容使用 OpenCV 和 SVM 处理图像 https://stackoverflow.com/questions/14694810/using-opencv-and-svm-with-images and http://docs.opencv.org/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html http://docs.opencv.org/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html
不知何故,我设法训练我的图像,但我强烈认为这个训练 xml 文件不正确,因为我没有指出哪个图像是正确的 (1) 或错误的 (-1)
而且当我尝试用我训练过的 svm 图像进行预测时,也会出现错误
OpenCV 错误:输入参数的大小不匹配(样本大小
与训练中使用的不同)
cvPreparePredictData,文件
/tmp/opencv-DXLLi8/opencv-2.4.9/modules/ml/src/inner_functions.cpp,
第 1114 行 libc++abi.dylib:以未捕获的类型异常终止
简历::例外:
/tmp/opencv-DXLLi8/opencv-2.4.9/modules/ml/src/inner_functions.cpp:1114:
错误:(-209) 样本大小与所使用的不同
函数 cvPreparePredictData 中的训练
这里也是由 SVM 生成的 xml。
<?xml version="1.0"?>
<opencv_storage>
<my_svm type_id="opencv-ml-svm">
<svm_type>C_SVC</svm_type>
<kernel><type>LINEAR</type></kernel>
<C>1.</C>
<term_criteria><epsilon>1.1920928955078125e-07</epsilon>
<iterations>1000</iterations></term_criteria>
<var_all>1</var_all>
<var_count>1</var_count>
<class_count>2</class_count>
<class_labels type_id="opencv-matrix">
<rows>1</rows>
<cols>2</cols>
<dt>i</dt>
<data>
-1 1</data></class_labels>
<sv_total>1</sv_total>
<support_vectors>
<_>
-1.56709105e-02</_></support_vectors>
<decision_functions>
<_>
<sv_count>1</sv_count>
<rho>-1.</rho>
<alpha>
1.</alpha>
<index>
0</index></_></decision_functions></my_svm>
</opencv_storage>
UPDATE
我已经根据建议更改了我的代码古奈凯伊姆但现在我得到了
EXC_BAD_ACCESS(代码=1 地址=...)错误。我更新的代码如下。
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
using namespace cv;
using namespace std;
int main(){
Mat image[2];
image[0]= imread("image.jpg",0);
image[1]= imread("wrongimage.jpg",0);
Mat rotated = imread("image.jpg",0);
image[0] = image[0].reshape(0, 1); //SINGLE LINE
image[1] = image[1].reshape(0, 1); //SINGLE LINE
// int size = sizeof(image)/sizeof(Mat);
// image[0].convertTo(image[0], CV_32FC1); //CONVERT TO 32FC1
// image[1].convertTo(image[1], CV_32FC1); //CONVERT TO 32FC1
Mat new_image(2,341318,CV_32FC1,image); //CONVERT TO 32FC1
float labels[2] = {1.0, -1.0};
Mat labelsmat(2,1,CV_32FC1,labels); //correct labels 1
labelsmat.convertTo(labelsmat, CV_32FC1);
cout<<image[0].size()<<endl;
cout<<new_image.size()<<endl;
CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
params.gamma = 3;
params.degree = 3;
CvSVM svm;
svm.train_auto(new_image, labelsmat,Mat(),Mat(),params);
// svm.train_(new_image, labelsmat, Mat(),Mat(),params);
// svm.train(training_mat2, labelsmat, Mat(),Mat(),params);
// svm.train(training_mat2, labelsmat, Mat(), Mat(), params);
svm.save("svm.xml"); // saving
svm.load("svm.xml"); // loading
rotated = rotated.reshape(0,1);
rotated.convertTo(rotated, CV_32FC1);
cout<<svm.predict(rotated)<<endl;
}
我的图像尺寸是:[170569 x 1]
new_image 大小为 [341318 x 2]