Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV 3.4 Assertion failed (checkDetectorSize()) in setSVMDetector

I'm trying to train SVM with my own images and feed that to the HOGDescriptor in OpenCV. I find a similar question (question). I add -rho how it do author in that question. But setSVMDetector throw Exception train image - size 28*28

code train svm

 HOGDescriptor hog = new HOGDescriptor(new Size(28,28),new Size(8,8),new Size(4,4),new Size(4,4),9);
  Mat trainingLabels = new Mat();
  MatOfFloat temp = new MatOfFloat();
  File negativeDir = new File("/home/kurenchuksergey/HogSignTrainingData/negative");
  File positiveDir = new File("/home/kurenchuksergey/HogSignTrainingData/positive");
  int i = -1;
  float[][] hogdescriptor = new float[13992][]; //13992 - image count
  for(File file:negativeDir.listFiles()){
              Mat con = Imgcodecs.imread(file.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
              hog.compute(con, temp);
              trainingLabels.push_back(Mat.zeros(new Size(1,1),CvType.CV_32S));
  }  
  for(File file:negativeDir.listFiles()){
        Mat con = Imgcodecs.imread(file.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
        hog.compute(con, temp);
        hogdescriptor[++i] = temp.toArray();
        trainingLabels.push_back(Mat.ones(new Size(1,1),CvType.CV_32S));
    }


    Mat Labels = new Mat();
    trainingLabels.copyTo(Labels);
    Labels.convertTo(Labels,CvType.CV_32S);

    Mat mat  = new Mat(hogdescriptor.length,hogdescriptor[0].length,CvType.CV_32FC1);
    for(int j = 0;j<hogdescriptor.length;j++)
        mat.put(i,0,hogdescriptor[i]);



    TrainData trainDataHog = TrainData.create(mat,Ml.ROW_SAMPLE,Labels);

    SVM svm = SVM.create();
    svm.setType(SVM.C_SVC);
    svm.setKernel(SVM.RBF);
    svm.setDegree(0.1);
    // 1.4 bug fix: old 1.4 ver gamma is 1
    svm.setGamma(0.1);
    svm.setCoef0(0.1);
    svm.setC(1);
    svm.setNu(0.1);
    svm.setP(0.1);
    svm.setTermCriteria(new TermCriteria(1, 20000, 0.0001));
    svm.train(trainDataHog.getSamples(),Ml.ROW_SAMPLE,trainDataHog.getResponses());
    svm.save("SVMHog");

And no exception, but when i try set svm to hog, i get Exception code where i'm use setSVMDetector:

 SVM svm1 = SVM.load("SVMHog");
 HOGDescriptor hog = new HOGDescriptor(new Size(28,28),new Size(8,8),new Size(4,4),new Size(4,4),9);
 Double rho = svm1.getDecisionFunction(0,new Mat(),new Mat());
 int size = (int)(svm1.getSupportVectors().total() + 1) * svm1.getSupportVectors().channels();
 float[] temp = new float[size];
 svm1.getSupportVectors().get(0, 0, temp);
 temp[temp.length - 1] = (float)-rho;
 MatOfFloat vector = new MatOfFloat(temp);
 hog.setSVMDetector(vector);

And exception:

OpenCV Error: Assertion failed (checkDetectorSize()) in setSVMDetector, file /home/kurenchuksergey/opencv/modules/objdetect/src/hog.cpp, line 117
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: /home/kurenchuksergey/opencv/modules/objdetect/src/hog.cpp:117: error: (-215) checkDetectorSize() in function 
setSVMDetector

OpenCV 3.4 Assertion failed (checkDetectorSize()) in setSVMDetector

I'm trying to train SVM with my own images and feed that to the HOGDescriptor in OpenCV. I find a similar question (question). I add -rho how it do author in that question. But setSVMDetector throw Exception train image - size 28*28

code train svm

 HOGDescriptor hog = new HOGDescriptor(new Size(28,28),new Size(8,8),new Size(4,4),new Size(4,4),9);
  Mat trainingLabels = new Mat();
  MatOfFloat temp = new MatOfFloat();
  File negativeDir = new File("/home/kurenchuksergey/HogSignTrainingData/negative");
  File positiveDir = new File("/home/kurenchuksergey/HogSignTrainingData/positive");
  int i = -1;
  float[][] hogdescriptor = new float[13992][]; //13992 - image count
  for(File file:negativeDir.listFiles()){
              Mat con = Imgcodecs.imread(file.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
              hog.compute(con, temp);
              trainingLabels.push_back(Mat.zeros(new Size(1,1),CvType.CV_32S));
  }  
  for(File file:negativeDir.listFiles()){
        Mat con = Imgcodecs.imread(file.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
        hog.compute(con, temp);
        hogdescriptor[++i] = temp.toArray();
        trainingLabels.push_back(Mat.ones(new Size(1,1),CvType.CV_32S));
    }


    Mat Labels = new Mat();
    trainingLabels.copyTo(Labels);
    Labels.convertTo(Labels,CvType.CV_32S);

    Mat mat  = new Mat(hogdescriptor.length,hogdescriptor[0].length,CvType.CV_32FC1);
    for(int j = 0;j<hogdescriptor.length;j++)
        mat.put(i,0,hogdescriptor[i]);



    TrainData trainDataHog = TrainData.create(mat,Ml.ROW_SAMPLE,Labels);

    SVM svm = SVM.create();
    svm.setType(SVM.C_SVC);
    svm.setKernel(SVM.RBF);
    svm.setDegree(0.1);
    // 1.4 bug fix: old 1.4 ver gamma is 1
    svm.setGamma(0.1);
    svm.setCoef0(0.1);
    svm.setC(1);
    svm.setNu(0.1);
    svm.setP(0.1);
    svm.setTermCriteria(new TermCriteria(1, 20000, 0.0001));
    svm.train(trainDataHog.getSamples(),Ml.ROW_SAMPLE,trainDataHog.getResponses());
    svm.save("SVMHog");

And no exception, but when i try set svm to hog, i get Exception code where i'm use setSVMDetector:

 SVM svm1 = SVM.load("SVMHog");
 HOGDescriptor hog = new HOGDescriptor(new Size(28,28),new Size(8,8),new Size(4,4),new Size(4,4),9);
 Double rho = svm1.getDecisionFunction(0,new Mat(),new Mat());
 int size = (int)(svm1.getSupportVectors().total() + 1) * svm1.getSupportVectors().channels();
 float[] temp = new float[size];
 svm1.getSupportVectors().get(0, 0, temp);
 temp[temp.length - 1] = (float)-rho;
 MatOfFloat vector = new MatOfFloat(temp);
 hog.setSVMDetector(vector);

And exception:

OpenJDK 64-Bit Server VM warning: You have loaded library /home/kurenchuksergey/opencv/build/lib/libopencv_java340.so which might have disabled stack guard. The VM will try to fix the stack guard now.
It's highly recommended that you fix the library with 'execstack -c <libfile>', or link it with '-z noexecstack'.
OpenCV Error: Assertion failed (checkDetectorSize()) in setSVMDetector, file /home/kurenchuksergey/opencv/modules/objdetect/src/hog.cpp, line 117
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: /home/kurenchuksergey/opencv/modules/objdetect/src/hog.cpp:117: error: (-215) checkDetectorSize() in function 
setSVMDetector
]
at org.opencv.objdetect.HOGDescriptor.setSVMDetector_0(Native Method)
at org.opencv.objdetect.HOGDescriptor.setSVMDetector(HOGDescriptor.java:306)
at com.company.Test.main(Test.java:47)