Training SVM using HOG features using JAVA
i want to pass a license plate to the SVM and classify either a plate or not
but i still didnt get enough images to train my SVM so i am using another database which is cars from such link (http://www.medialab.ntua.gr/research/...) which consists of 1050 images i divided the positive(550) and negative(500) on different files
protected static final String PATH_POSITIVE = "/home/tomna/NetBeansProjects/main/src/main/PostiveTrain";
protected static final String PATH_NEGATIVE = "/home/tomna/NetBeansProjects/main/src/main/NegativeTrain";
protected static final String XML = "/home/tomna/NetBeansProjects/main/src/main/data.xml";
protected static final String FILE_TEST = "/home/tomna/NetBeansProjects/main/src/main/Dadcar3.png";
private static Mat trainingImages = new Mat();
private static Mat trainingLabels = new Mat();
private static Mat trainingData = new Mat();
private static Mat classes = new Mat();
private static SVM clasificador= SVM.create();
public static MatOfPoint loacation = new MatOfPoint();
public static MatOfDouble descriptors = new MatOfDouble();
private static HOGDescriptor hog;
here i am looping on the folder of positive images to identify such samples are positive
public static void trainPositives() {
for (File file : new File(PATH_POSITIVE).listFiles()) {
Mat img = getMat(file.getAbsolutePath());
trainingImages.push_back(img.reshape(1, 1));
trainingLabels.push_back(Mat.ones(new Size(1, 1), CvType.CV_32FC1));
}
}
as well as for the negative images
public static void trainNegatives() {
for (File file : new File(PATH_NEGATIVE).listFiles()) {
Mat img = getMat(file.getAbsolutePath());
trainingImages.push_back(img.reshape(1, 1));
trainingLabels.push_back(Mat.zeros(new Size(1, 1), CvType.CV_32FC1));
}
}
My question when it comes to such part how am i supposed to get HOG features from each and every image and save it to be used for the SVM training
i tried such code on a single image as in to extract the images features but i didnt understand the output of the image MAT Mat [ 01CV_64FC1, isCont=true, isSubmat=false, nativeObj=0x7f332461cd90, dataAddr=0x0 ]
public static MatOfPoint loacation = new MatOfPoint();
private static HOGDescriptor hog= new HOGDescriptor();
public static MatOfDouble descriptors = new MatOfDouble();
void run() throws IOException {
Mat ro = Imgcodecs.imread("/home/tomna/NetBeansProjects/main/src/main/PostiveTrain/pos-1.pgm");
Imgproc.resize(ro, ro, new Size(64,128));
hog.detect(ro, loacation, descriptors);
System.out.println(descriptors);
}
}
i didn't yet train or test my SVM because i think the features should be extracted properly first but here is an attempt not complete
public static void test() {
Mat input = Imgcodecs.imread(new File(FILE_TEST).getAbsolutePath());
clasificador.save(new File(XML).getAbsolutePath());
System.out.println(clasificador);
Mat output = new Mat();
input.convertTo(output, CV_32FC1);
output = output.reshape(64, 64);
System.out.println(output);
System.out.println(clasificador.predict(output));
}
public static void train() {
trainingImages.copyTo(trainingData);
trainingData.convertTo(trainingData, CV_32FC1);
trainingLabels.copyTo(classes);
clasificador.setKernel(SVM.LINEAR);
clasificador.setC(10);
// hog.setSVMDetector(classes);
// hog.compute(classes, new Size(16,16),);
// clasificador.train(trainingData, 0, classes);
// clasificador.save(XML);
}
There is a sample explaining in nice detail how HOG has to be calculated as feature. Take a look here: https://github.com/opencv/opencv/blob...