2016-11-23 12:24:20 -0600
| commented question | Cannot detect a simple rectangle @berak, I trained it using 9000 images of airplanes at gates, and 4000 randomly generated images, and I had the same problem, it definitely didn't only have edges then |
2016-11-23 11:16:54 -0600
| commented question | Cannot detect a simple rectangle @berak, it was something I made in microsoft paint. It didn't work on 14000 positive and negative samples for this, and it didn't work for 20 samples in this circumstance either, same issue of no detections |
2016-11-23 11:15:50 -0600
| commented question | Cannot detect a simple rectangle It was essentially only a 256x128 picture of a black solid rectangle on top of a white background.. The rectangle itself was probably around 100x50 in size, and I had 6 positive images of it placed in various spots on top of the white background. My negatives were simply a white 256x128 picture @berak |
2016-11-21 16:37:14 -0600
| asked a question | Cannot detect a simple rectangle I am using code inspired by https://github.com/opencv/opencv/blob.... My code is barely any different, and yet cannot detect ANYTHING. Any help would be appreciated. When running the program, it says (as I have instructed it to if this happens) that the list of detections is empty... #include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>
#include <opencv2/objdetect.hpp>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <time.h>
using namespace cv;
using namespace cv::ml;
using namespace std;
void getSvmDetector(Ptr<SVM> & svm, vector < float > & detector);
void draw_locations(Mat & img, const vector < Rect > & locations, const Scalar & color);
void load_images(string dir, vector< Mat > & images, int numFiles);
void compute_hog(vector < Mat > & mats, vector < Mat > & gradient, Size & s);
void train_svm(const vector < Mat > & gradient_list, const vector < int > & labels);
void convert_to_ml(const vector< Mat > & train_samples, Mat & trainData);
int main(int, char**)
{
int numPos = 6;
int numNeg = 20;
vector< Mat > pos_lst;
vector< Mat > neg_lst;
vector< Mat > gradient_list;
vector < int > labels;
string posDir = "C:/Users/danekstein/Desktop/rectangle/";
string negDir = "C:/Users/danekstein/Desktop/nr/";
// load and label positive images
cout << "positive images loading..." << endl;
load_images(posDir, pos_lst, numPos);
labels.assign(pos_lst.size(), +1);
// size with only positives
const unsigned int old = (unsigned)labels.size();
// load and label positive images
cout << "negative images loading..." << endl;
load_images(negDir, neg_lst, numNeg);
labels.insert(labels.end(), neg_lst.size(), -1);
// check the labels were successfully added
CV_Assert(old < labels.size());
cout << "computing hog for positives" << endl;
compute_hog(pos_lst, gradient_list, Size(256, 128));
cout << "computing hog for negatives" << endl;
compute_hog(neg_lst, gradient_list, Size(256, 128));
train_svm(gradient_list, labels);
Scalar border_color(0, 255, 0);
Mat img, draw;
Ptr<SVM> svm;
HOGDescriptor hog = HOGDescriptor::HOGDescriptor();
hog.winSize = Size(256, 128);
// locations where a plane is detected
vector< Rect > locations;
// loading the trained svm
svm = StatModel::load<SVM>("C:/Users/danekstein/Desktop/r.yml");
// set the trained svm to the hog
vector< float > hog_detector;
getSvmDetector(svm, hog_detector);
// set the detector
hog.setSVMDetector(hog_detector);
Mat image = imread("C:/Users/danekstein/Desktop/rectangle/1.jpg");
locations.clear();
cout << "detecting features from sample" << endl;
hog.detectMultiScale(image,locations);
draw = image.clone();
draw_locations(draw, locations, border_color);
imshow("Image", draw);
waitKey(0);
destroyAllWindows();
}
void load_images(string dir, vector< Mat > & images, int numImages) {
for (int i = 0; i < numImages; i++) {
Mat im = imread(dir + to_string(i) + ".jpg");
if (im.empty()) cout << "not good";
#ifdef _DEBUG
#endif
images.push_back(im.clone());
}
}
void compute_hog(vector < Mat > & mats, vector < Mat > & gradients, Size & size) {
HOGDescriptor hog;
hog.winSize = size;
Mat gray;
vector< Point > location;
vector< float > descriptors;
vector< Mat >::const_iterator img = mats.begin();
vector< Mat >::const_iterator end = mats.end();
for (; img != end; ++img) {
cvtColor(*img, gray, COLOR_BGR2GRAY);
hog.compute(gray, descriptors, Size(1,1), Size(0, 0), location);
gradients.push_back(Mat(descriptors).clone());
}
}
void getSvmDetector(Ptr<SVM> & svm, vector< float> & detector) {
// grab the support vectors... yay!
Mat sv = svm->getSupportVectors();
const int sv_total = sv.rows;
// get the decision function
Mat alpha, svidx;
double rho = svm->getDecisionFunction ... (more) |
2016-11-16 14:47:37 -0600
| asked a question | Custom HOG not detecting features 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) |
2016-11-07 15:20:33 -0600
| received badge | ● Editor
(source)
|
2016-11-07 15:19:54 -0600
| received badge | ● Scholar
(source)
|
2016-11-07 15:19:48 -0600
| answered a question | SVM xml file support vectors all 0 The answer is that the featuresG[i] is 1x23328 in size. Being used to linear algebra where mxn signifies m rows and n columns, I believed that copying featuresG[i] into (trainingMatG.rowRange(i,i+1) was matching the correct column size of 23328, but no. In actuality, OpenCV's Mat.size() method returns columns x rows... So, after addressing the issue from this standpoint by setting features[count] = descriptors.t() , I was able to fix the problem. |
2016-11-07 15:16:00 -0600
| received badge | ● Critic
(source)
|
2016-11-07 14:33:00 -0600
| commented answer | SVM xml file support vectors all 0 it's because my features in my array are not copying to my training matrix, but i'm not sure how to fix this problem |
2016-11-07 13:45:44 -0600
| commented answer | SVM xml file support vectors all 0 can you explain the reasoning behind this? all the examples I see use CV_32FC1, and when I switched to what you suggested I got a bad argument arrow saying it has to be a floating point matrix anyway |
2016-11-04 18:41:49 -0600
| asked a question | SVM xml file support vectors all 0 Hello. I am attempting to train a model using gradients from an HOG descriptor. In total, there are about 7000 images whose feature vectors I am writing to my trainingData matrix. My svm finishes training within a minute and outputs a file whose support vectors all take on the form
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. // our labels are all initialized to being -1, Negative
Mat gLabels = new Mat(posGCount + negCount, 1, CvType.CV_32FC1, new Scalar(-1.0));
Mat aLabels = new Mat(posACount + negCount, 1, CvType.CV_32FC1, new Scalar(-1.0));
// we overwrite the positive portion of the matrix with 1, Positive
gLabels.rowRange(0,posGCount).setTo(new Scalar(1.0));
aLabels.rowRange(0,posACount).setTo(new Scalar(1.0));
Mat trainingMatG = new Mat(posGCount + negCount, 23328, gLabels.type());
// Mat trainingMatA = new Mat(posACount + negCount, 23328, gLabels.type());
//creating arrays for our feature vectors
Mat[] featuresG = new Mat[posGCount];
Mat[] featuresA = new Mat[posACount];
Mat[] featuresN = new Mat[negCount];
// TODO - add each feature vector to corresponding array
// TODO - fix issue with insufficient memory
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(640,360),new Size(512,288), new Size(64,36), new Size(32,16), 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(trainingSetG)) featuresG[count] = descriptors;
if(set.equals(negative)) featuresN[count] = descriptors;
count++;
System.out.println(count);
}
}
System.out.println("Adding features to training matrix...");
for(int i=0;i<posGCount;i++){
featuresG[i].copyTo(trainingMatG.rowRange(i,i+1));
}
for(int i=0;i<negCount;i++){
featuresN[i].copyTo(trainingMatG.rowRange(i+posGCount,i+posGCount+1));
}
System.out.println("Added to Matrix");
System.out.println("Training model...");
CvSVM svm = new CvSVM();
CvSVMParams params = new CvSVMParams();
params.set_kernel_type(CvSVM.LINEAR);
params.set_svm_type(CvSVM.C_SVC);
params.set_C(1);
svm.train(trainingMatG, gLabels, new Mat(), new Mat(), params);
svm.save("C:/Users/danekstein/Desktop/svm.xml");
|