Ask Your Question

marcus's profile - activity

2017-09-05 15:58:47 -0600 received badge  Enthusiast
2017-08-18 14:15:05 -0600 commented question SVM application causing my system to hang

I reshaped the HOG descriptors as you suggested. But the error keeps being generated because the training data and the label matrices are not equally sized in order for the train() method to work - labels matrix is 1x200 whereas the SVMtrainingData matrix is 1x424116003 . My understanding is that each image from the data set is given a flag value (for example 1 or 0) according to whether it is a positive image or a negative one. However, the training data for the HOG features will produce a much larger matrix. How can i resolve this conundrum?

2017-08-18 10:40:46 -0600 commented question SVM application causing my system to hang

I made the changes as you suggested regarding the images, I dont know how to reshape the hog. The size of the training data is 1x424116003 and since the labels are 1x200 this is now throwing the error: 'Sizes of input arguments do not match (Response array must contain as many elements as the total number of samples) in cvPreprocessCategoricalResponses' when training the classifier: svmClassifier.train(SVMtrainingData, labels, new Mat(), new Mat(), params);

2017-08-18 08:49:29 -0600 asked a question SVM application causing my system to hang

I am using OpenCV java to build a leaf classifier. I extract HOG features from two datasets, one containing 100 images of a target leaf and another containing 100 negative images, images are sized to 50x50 pixels for both datasets. The extracted HOG descriptors are feed into a SVM for training and the application then tests the classifier with a sample dataset containing images sized to the training set. Here is my code:

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

    // create new file to contain positive and negative images
    File images = null;
    // create String to hold absolute path of image file
    String imageAbsPathAsStr = "";
    // create an array of image file paths
    File[] imageFilePaths;

    // set size of leaf image data and labels for SVM
    final int leafFileSize = 100;
    // set size of negative image data
    final int negFileSize = 100;
    final int numFiles = leafFileSize + negFileSize;

    // set leaf/neg image width
    final int width = 64;
    // set leaf/neg image height
    final int height = 128;
    final int imageArea = width * height;

    // create matrix for gray scale leaf image
    Mat leafGrayMat = new Mat(1, 1, CvType.CV_32FC1);

    // create matrix for neg image
    Mat negGrayMat = new Mat(1, 1, CvType.CV_32FC1);

    // create matrix of SVMtrainingData
    MatOfFloat SVMtrainingData = new MatOfFloat(numFiles, imageArea, CvType.CV_32FC1);

    // create matrix of image labels
    Mat labels = new Mat(numFiles, 1, CvType.CV_32FC1);

    // create HOG descriptor
    HOGDescriptor hog = new HOGDescriptor();
    // display hog params
    //System.out.println("winSize: " + hog.get_winSize());
    //System.out.println("winSigma: " + hog.get_winSigma());
    //System.out.println("blockSize: " + hog.get_blockSize());
    //System.out.println("cellSize: " + hog.get_cellSize());
    //System.out.println("blockStride: " + hog.get_blockStride());
    //System.out.println("descriptorSize: " + hog.getDescriptorSize());
    //System.out.println("L2HysThrehold: " + hog.get_L2HysThreshold());

    // create HOG matrices
    MatOfFloat ders = new MatOfFloat(1, 1, CvType.CV_32FC1);
    MatOfPoint locs = new MatOfPoint();

    // try opening leaf image folder
    try
    {
        // initialise leaf images with file path to leaf image directory
        images = new File("Images//acer_campestre_100_images");

        // assign leaf image path names to imageFilePaths
        imageFilePaths = images.listFiles();

        // traverse leaf image file paths
        for(File path : imageFilePaths)
        {
            // get absolute path of leaf image file
            imageAbsPathAsStr = path.getAbsolutePath();

            // initialise gray leaf matrix with gray scale version of leaf image
            leafGrayMat = Highgui.imread(imageAbsPathAsStr, Highgui.CV_LOAD_IMAGE_GRAYSCALE);

            // resize leafGrayMat to HOG default size 64x128
            Imgproc.resize(leafGrayMat, leafGrayMat, new Size(64, 128));

            // reshape leafGrayMat from 2D to 1D
            leafGrayMat = leafGrayMat.reshape(0, 1);

            // compute HOG features winStride of 8 x 8 to match hog cellSize and padding to 64 x 128 to match hog winSize
            hog.compute(leafGrayMat, ders, new Size(8, 8), new Size(64, 128), locs);

            // push ders to SVM training data
            SVMtrainingData.push_back(ders);

            // update labels
            labels.push_back(Mat.ones(1, 1, CvType.CV_32FC1));
        }
    }
    catch(Exception e)
    {
        // display error
        e.printStackTrace();
    }

    // try opening negative image folder
    try
    {
        // initialise images with file path to negative image directory
        images = new File("Images//neg_images_100");

        // assign negative image path names to imageFilePaths
        imageFilePaths = images.listFiles();

        // traverse negative image file paths
        for(File path : imageFilePaths)
        {
            // get absolute path of negative image file
            imageAbsPathAsStr = path.getAbsolutePath();

            // initialise gray negative matrix with gray scale ...
(more)
2017-08-15 11:08:49 -0600 received badge  Self-Learner (source)
2017-08-15 09:53:16 -0600 answered a question I am using the opencv_annotation tool with java version 2.4.9 and running the utility from Windows 10 command line - I keep getting the following error :

Sorry for not getting back, I was on my hols.

The solution for the problem is to use -images=/data/text.txt and -annotations=/data/annotations.txt, i.e. a single horizontal dash (-) in front of the key words.

2017-08-15 06:33:41 -0600 asked a question How to Surf Descriptors as input for a SVM Classifier

I am using the Java implementation to build a SVM classifier to classify tree leaves. I get the following errors when I try to train the SVM:

OpenCV Error: Sizes of input arguments do not match () in cv::Mat::push_back, file ........\opencv\modules\core\src\matrix.cpp, line 650 CvException [org.opencv.core.CvException: cv::Exception: ........\opencv\modules\core\src\matrix.cpp:650: error: (-209) in function cv::Mat::push_back ] at org.opencv.core.Mat.n_push_back(Native Method) at org.opencv.core.Mat.push_back(Mat.java:1863) at mark.smart.csc7057.surfdetectorandsvm.SurfDetectorAndSVM.main(SurfDetectorAndSVM.java:126) OpenCV Error: Sizes of input arguments do not match () in cv::Mat::push_back, file ........\opencv\modules\core\src\matrix.cpp, line 650 CvException [org.opencv.core.CvException: cv::Exception: ........\opencv\modules\core\src\matrix.cpp:650: error: (-209) in function cv::Mat::push_back ] at org.opencv.core.Mat.n_push_back(Native Method) at org.opencv.core.Mat.push_back(Mat.java:1863) at mark.smart.csc7057.surfdetectorandsvm.SurfDetectorAndSVM.main(SurfDetectorAndSVM.java:167)

OpenCV Error: Bad argument (There is only a single class) in cvPreprocessCategoricalResponses, file ........\opencv\modules\ml\src\inner_functions.cpp, line 729 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ........\opencv\modules\ml\src\inner_functions.cpp:729: error: (-5) There is only a single class in function cvPreprocessCategoricalResponses ] at org.opencv.ml.CvSVM.train_0(Native Method) at org.opencv.ml.CvSVM.train(CvSVM.java:270) at mark.smart.csc7057.surfdetectorandsvm.SurfDetectorAndSVM.main(SurfDetectorAndSVM.java:228)

I have read that a BOW trainer should be employed to cluster the surf descriptors, unfortunately this feature is currently not available with the java implementation. I would appreciate any guidance (with sample code if possible) to resolve this problem. The code I have used is as follows:

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

File leafImages = null;

String leafImageAbsPathAsStr = "";

File[] leafImageFilePaths;

File negImages = null;

String negImageAbsPathAsStr = "";

File[] negImageFilePaths;

final int leafImageWidth = 50; final int leafImageHeight = 50;

final int size = 200;

Mat leafGrayMat;
Mat leafBinMat = new Mat();

Mat negGrayMat; Mat negBinMat = new Mat();

Mat SVMtrainingData = new Mat(size, 1, CvType.CV_32FC1);

Mat labels = new Mat(size, 1, CvType.CV_32FC1);

List<float> trainingLabels = new ArrayList<float>();

MatOfKeyPoint keyPoints = new MatOfKeyPoint(); MatOfKeyPoint descriptors = new MatOfKeyPoint();

FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.SURF);

DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.SURF);

try { leafImages = new File("Images//acer_campestre_100_images");

leafImageFilePaths = leafImages.listFiles();

for(File path : leafImageFilePaths)
{
  leafImageAbsPathAsStr = path.getAbsolutePath();

  leafGrayMat = Highgui.imread(leafImageAbsPathAsStr, Highgui.CV_LOAD_IMAGE_GRAYSCALE);

  Imgproc.adaptiveThreshold(leafGrayMat, leafBinMat, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,    Imgproc.THRESH_BINARY, 55, 5);

  featureDetector.detect(leafBinMat, keyPoints);

  descriptorExtractor.compute(leafBinMat, keyPoints, descriptors);

  descriptors.convertTo(descriptors, CvType.CV_32FC1);

  SVMtrainingData.push_back(descriptors);

  trainingLabels.add(1.0f);
}

} catch(Exception e) { e.printStackTrace(); } try { negImages = new File("Images//neg_images_100");

   negImageFilePaths = negImages.listFiles();

   for(File path : negImageFilePaths)
   {
 negImageAbsPathAsStr = path.getAbsolutePath();

 negGrayMat = Highgui.imread(negImageAbsPathAsStr, Highgui.CV_LOAD_IMAGE_GRAYSCALE);

 Imgproc.adaptiveThreshold(negGrayMat, negBinMat, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 55, 5);

 featureDetector.detect(leafBinMat, keyPoints);

 descriptorExtractor.compute(leafBinMat, keyPoints, descriptors);

 SVMtrainingData.push_back(descriptors);

 trainingLabels.add(-1.0f);
  }

} catch(Exception e) { e.printStackTrace(); } Float[] trainingLabelsArray = trainingLabels.toArray(new ... (more)

2017-08-02 14:13:40 -0600 asked a question I am using the opencv_annotation tool with java version 2.4.9 and running the utility from Windows 10 command line - I keep getting the following error :

"The path for the output file contains an error and could not be opened. Please check again!"

I don't understand why I am getting this error since I am using the same directory for both the --images and --annotations params and I am supplying the absolute filename in both cases.

2017-07-01 11:21:42 -0600 commented question problem using msbuild

I have decided to use an older version of OpenCV (2.4.11) which includes the nonfree SIFT/SURF features.

2017-06-30 11:45:56 -0600 asked a question problem using msbuild

When trying to build the SIFT/SURF nonfree features into the latest version of opencv I get the following error in cmake: CMake error in CMakeLists.txt: Failed to run MSBuild command: MSBuild.exe: to get the value of VCTargetsPath:

I am using OpenCV java implementation for Eclipse