Custom HOG not detecting features

asked 2016-11-16 14:47:37 -0600

aneksteind gravatar image

I have an XML file with the contents of a trained SVM using HOG features from 14000 images in total, 9000 negative samples, and 5000 positive. I trained my SVM using OpenCV's Java extensions, but had to use C++ in order to extract the support vectors from my XML file because Java's bindings don't support that. When I load my XML file into my C++ program and try to detect an airplane, I am not getting any found locations of features. To make it ridiculously simple, I trained the SVM on about 15 negative images and 5 positive ones, then tried detecting features on one of the images I trained it with, and still am not getting any rectangles drawn on my screen because there are no features getting detected. If anyone can help with this, I'd appreciate it. The code below is the Java code I used to train the SVM, and the C++ code I am using to detect features for a given input image.

Training...

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.HOGDescriptor;
import org.opencv.ml.CvSVM;
import org.opencv.ml.CvSVMParams;

public class Detector {

    public static void main(String[] args) {
        System.loadLibrary( Core.NATIVE_LIBRARY_NAME );


        // the locations of the training data
        File[] trainingSetA = new File ("C:/Users/danekstein/Desktop/positiveSample/").listFiles();
        File[] negative = new File ("C:/Users/danekstein/Desktop/negativeSample/").listFiles();


        List<File[]> trainingSets = new ArrayList<File[]>();

        trainingSets.add(trainingSetA);
        trainingSets.add(negative);

        // the amount of examples in each set
        int posACount = trainingSetA.length;
        int negCount = negative.length;

        // our labels are all initialized to being -1, Negative
        Mat aLabels = new Mat(posACount + negCount, 1, 5, new Scalar(-1));

        // we overwrite the positive portion of the matrix with 1, Positive
        aLabels.rowRange(0,posACount).setTo(new Scalar(1));

        //creating arrays for our feature vectors
        Mat[] featuresA = new Mat[posACount];
        Mat[] featuresN = new Mat[negCount];

        for(File[] set : trainingSets){

            int count = 0;

            for(File image : set){

                // read in the image as a matrix
                Mat img = Highgui.imread(image.getAbsolutePath(), 0);

                // create a new descriptor
                HOGDescriptor descriptor = new HOGDescriptor(new Size(256,128),new Size(128,64), new Size(1,1), new Size(8,8), 9);

                // initialize a vector in which the features will be placed
                MatOfFloat descriptors = new MatOfFloat();

                // compute the feature vector and store it in 'descriptors'
                descriptor.compute(img, descriptors);

                if(set.equals(trainingSetA)) featuresA[count] = descriptors.t();
                if(set.equals(negative)) featuresN[count] = descriptors.t();

                count++;
                System.out.println(count);
            }
        }

        System.out.println("Adding features to training matrix...");


        Mat trainingMatA = new Mat(posACount + negCount, featuresA[0].cols(), featuresA[0].type());

        for(int i=0;i<posACount;i++){
            featuresA[i].copyTo(trainingMatA.rowRange(i,i+1));
        }
        for(int i=0;i<negCount;i++){
            featuresN[i].copyTo(trainingMatA.rowRange(i+posACount,i+posACount+1 ...
(more)
edit retag flag offensive close merge delete

Comments

1

can it be, you're using different opencv versions for java and c++ ?

please rather use this for training

berak gravatar imageberak ( 2016-11-17 00:09:14 -0600 )edit

I hope this works, cause i need it.

EdoTwentySix gravatar imageEdoTwentySix ( 2018-03-29 11:40:07 -0600 )edit

I confuse about getSupportVector() method, because there is no getSupportVector() method in CvSVM java ? Anybody know why ?

EdoTwentySix gravatar imageEdoTwentySix ( 2018-03-29 13:38:30 -0600 )edit