我按照How to train an SVM with opencv based on a set of images?中的代码执行了以下异常
OpenCV错误:cvCheckTrainData中的错误参数(列车数据必须是浮点矩阵),文件...... \ src \ opencv \ modules \ ml \ src \ inner_functions.cpp,第857行 线程“main”中的异常CvException [org.opencv.core.CvException:...... \ src \ opencv \ modules \ ml \ src \ inner_functions.cpp:857:错误:( - 5)列车数据必须浮动函数cvCheckTrainData中的点矩阵 ]
我按照评论中提到的步骤进行操作。但仍然徒劳无功。
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat classes = new Mat();
Mat trainingData = new Mat();
Mat trainingImages = new Mat();
Mat trainingLabels = new Mat();
CvSVM clasificador;
String path="C:\\java workspace\\ora\\images\\Color_Happy_jpg";
for (File file : new File(path).listFiles()) {
Mat img=new Mat();
Mat con = Highgui.imread(path+"\\"+file.getName());
con.convertTo(img, CvType.CV_32F,1.0/255.0);
img.reshape(1, 1);
trainingImages.push_back(img);
trainingLabels.push_back(Mat.ones(new Size(1, 1), CvType.CV_32F));
}
System.out.println("divide");
path="C:\\java workspace\\ora\\images\\Color_Sad_jpg";
for (File file : new File(path).listFiles()) {
Mat img=new Mat();
Mat m=new Mat(new Size(640,480),CvType.CV_32FC3);
Mat con = Highgui.imread(file.getAbsolutePath());
con.convertTo(img, CvType.CV_32F,1.0/255.0);
img.reshape(1, 1);
trainingImages.push_back(img);
System.out.println((CvType.typeToString(img.type())));
trainingLabels.push_back(Mat.zeros(new Size(1, 1), CvType.CV_32F));
}
trainingLabels.copyTo(classes);
CvSVMParams params = new CvSVMParams();
params.set_kernel_type(CvSVM.LINEAR);
System.out.println(CvType.typeToString(trainingImages.type()));
CvSVM svm=new CvSVM();
System.out.println(svm.get_support_vector_count());
boolean b=svm.train(trainingImages, classes);
System.out.print(b);
}
}