SVM图像预测的OpenCV



我能够训练系统,但是当我尝试预测时,抛出Bad argument异常。

OpenCV错误:cvPreparePredictData中的坏参数(示例不是有效向量),文件........ OpenCV modulesmlsrcinner_function .cpp,第1099行线程"main"中的异常CvException [org.opencv.core.]CvException: cv::Exception: ........opencvmodulesmlsrcinner_functions.cpp:1099:错误:(-5)样本不是函数cvPreparePredictData中的有效向量)

这是我的代码:

        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(),Highgui.CV_LOAD_IMAGE_GRAYSCALE);
            con.convertTo(img, CvType.CV_32FC1,1.0/255.0);
                img.reshape(1, 1);
                trainingImages.push_back(img);
               trainingLabels.push_back(Mat.ones(new Size(1, 75), CvType.CV_32FC1));
            }
        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_32FC1);
                Mat con = Highgui.imread(file.getAbsolutePath(),Highgui.CV_LOAD_IMAGE_GRAYSCALE);
                con.convertTo(img, CvType.CV_32FC1,1.0/255.0);
                img.reshape(1, 1);
                trainingImages.push_back(img);
                trainingLabels.push_back(Mat.zeros(new Size(1, 75), CvType.CV_32FC1));
              }
            trainingLabels.copyTo(classes);
            CvSVMParams params = new CvSVMParams();
            params.set_kernel_type(CvSVM.LINEAR);
            CvType.typeToString(trainingImages.type());
            CvSVM svm=new CvSVM();

            clasificador = new CvSVM(trainingImages,classes, new Mat(), new Mat(), params);
            clasificador.save("C:\java workspace\ora\images\svm.xml");
            Mat out=new Mat();
            clasificador.load("C:\java workspace\ora\images\svm.xml");
            Mat sample=Highgui.imread("C:\java workspace\ora\images\Color_Sad_jpg\EMBfemale20-2happy.jpg",Highgui.CV_LOAD_IMAGE_GRAYSCALE);
           sample.convertTo(out, CvType.CV_32FC1,1.0/255.0);               
            out.reshape(1, 75);
            System.out.println(clasificador.predict(out));

1.

你的trainLabels还是错的

你需要一个带有numrows==numimages和1 col.所以,每个图像1个标签的浮动垫。

所以你悲伤的脸应该有:

trainingLabels.push_back(-1.0);

和你的快乐的人应该:

trainingLabels.push_back(1.0);

2。

用于预测的样本必须以与训练相同的方式处理。

sample.convertTo(out, CvType.CV_32FC1,1.0/255.0);               
out.reshape(1, 1);

相关内容

  • 没有找到相关文章

最新更新