Ask Your Question

JCrasher's profile - activity

2015-02-15 06:28:21 -0500 commented question error Descriptor SURF c++ OpenCV on Android

Hi, I'm wondering did you find a solution for your problem. If yes, please let us know :)

2015-02-05 10:47:35 -0500 commented question error: (-215) (globalDescIdx>=0) && (globalDescIdx < size()) in function getLocalIdx

I'm having the same error right now. Did you find a solution?

2015-02-05 10:33:01 -0500 commented question throwing exception in matching function

Hi, I'm think I'm having the same problems too. Any solutions?

2015-02-04 16:27:29 -0500 asked a question OpenCV Error: Bad argument (The sample is not a valid vector)

I'm trying to create an object detector for detecting steering wheels using BoW + SVM approach. Unfortunately the following error is given when trying to predict:

OpenCV Error: Bad argument (The sample is not a valid vector) in cvPreparePredictData, file /build/buildd/opencv-2.4.8+dfsg1/modules/ml/src/inner_functions.cpp, line 1099
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/buildd/opencv-2.4.8+dfsg1/modules/ml/src/inner_functions.cpp:1099: error: (-5) The sample is not a valid vector in function cvPreparePredictData

Aborted (core dumped)

To achieve my goal I do the following steps:

Detecting and extracting the features

Adding them to the BOWKMeansTrainer and then clustering;

Computing the BoW features and assigning labels;

Saving the dictionary, training data and labels to file;

#include <stdio.h>
#include <stdlib.h>
#include <string>

#include <omp.h>

#include <opencv2/opencv.hpp>
#include <opencv2/legacy/legacy.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <fstream>
#include <iostream>
#include <string>

#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

using namespace cv;
using namespace std;

Mat compute_features(Mat image, Ptr<FeatureDetector>& detector, Ptr<DescriptorExtractor>& descriptors) {
    vector<KeyPoint> keypoints;
    Mat features;

        detector->detect(image, keypoints);
        KeyPointsFilter::retainBest(keypoints, 1500);
        descriptors->compute(image, keypoints, features);

        return features;

BOWKMeansTrainer addFeaturesToBOWKMeansTrainer(String dir, BOWKMeansTrainer& bowTrainer, Ptr<FeatureDetector>& detector, Ptr<DescriptorExtractor>& descriptors) {
        DIR *dp;
        struct dirent *dirp;
        struct stat filestat;

        dp = opendir(dir.c_str());

    Mat features;
    Mat img;

    string filepath;
    #pragma loop(hint_parallel(4))
        for (; (dirp = readdir(dp));) {
                filepath = dir + dirp->d_name;

                if (stat( filepath.c_str(), &filestat )) continue;
                if (S_ISDIR( filestat.st_mode ))         continue;

                cout << "Reading... " << filepath << endl;

                img = imread(filepath);

                features = compute_features(img, detector, descriptors);

        return bowTrainer;

void computeFeaturesWithBow(string dir, Ptr<FeatureDetector>& detector, Mat& trainingData, Mat& labels, BOWImgDescriptorExtractor& bowDE, int label) {
        DIR *dp;
        struct dirent *dirp;
        struct stat filestat;

        dp = opendir(dir.c_str());

        vector<KeyPoint> keypoints;
        Mat features;
        Mat img;

        string filepath;

        #pragma loop(hint_parallel(4))
        for (;(dirp = readdir(dp));) {
                filepath = dir + dirp->d_name;

                cout << "Reading: " << filepath << endl;

                if (stat( filepath.c_str(), &filestat )) continue;
                if (S_ISDIR( filestat.st_mode ))         continue;

                img = imread(filepath);

                detector->detect(img, keypoints);
                bowDE.compute(img, keypoints, features);
                KeyPointsFilter::retainBest(keypoints, 1500);

                labels.push_back((float) label);

void saveMat(string dir, string name, string extension, Mat& dictionary) {
        FileStorage fileStorage(dir + name + extension, FileStorage::WRITE);

        fileStorage << name << dictionary;

int main()

        cout << "========== BOW Creator ==========" << endl;

        Ptr<FeatureDetector> detector = FeatureDetector::create("DynamicSURF");
        Ptr<DescriptorExtractor> descriptors = DescriptorExtractor::create("OpponentSURF");
        Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");

        cout << "initialized detector, descriptors and matcher..." << endl;

        TermCriteria tc(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 10, 0.001);
    int dictionarySize = 1000;
    int retries = 1;
    int flags = KMEANS_PP_CENTERS;
    BOWKMeansTrainer bowTrainer(dictionarySize, tc, retries, flags);
    BOWImgDescriptorExtractor bowDE(descriptors, matcher);

    cout << "Adding Features to the bag of words..." << endl;
        addFeaturesToBOWKMeansTrainer("./dataset/training/positive_images/", bowTrainer, detector, descriptors);
        addFeaturesToBOWKMeansTrainer("./dataset/training/negative_images/", bowTrainer, detector, descriptors);
        cout << "Done.." << endl;

        cout << endl << "Clustering..." << endl;

        Mat dictionary = bowTrainer.cluster();
        saveMat("./BOW/", "dictionary", ".xml", dictionary);

        Mat labels(0, 1, CV_32FC1);
        Mat trainingData(0, dictionarySize, CV_32FC1); 

        cout << endl << "Extract ...
2015-02-03 16:07:17 -0500 received badge  Supporter (source)
2015-01-11 13:27:01 -0500 commented answer Object detection for Android application

As far as I know Vuforia doesn't provide tools for object detection, only for object recognition.

2015-01-04 05:28:33 -0500 received badge  Enthusiast
2015-01-03 00:34:06 -0500 commented question Object detection for Android application

I need to detect a car steering wheel and probably a car tire too. And the problem is that I'm really new to OpenCV and I get really confused. What approach should I do to create the detector. (I don't want to recognize an exact object, just to detect whether there is a steering wheel or not). My time is limited and I can't go through all the OpenCV books and papers. I'm trying to find good examples and explanation so I can build my app. Thank you very much for your help end for trying to help :)

2014-12-30 22:38:39 -0500 asked a question Object detection for Android application

I have a school project for object recognition in android application. I tried to use the Haar classifier but without success and I don't have much time so I decided to try different approach. Unfortunately I'm stuck.

Can you tell me what to use for object detection? What I need is when I start my app, the camera opens and when I see my object on the camera, the phone must know that the object is found (like face detection).

I'm sure that I'm also not the last one who'll try to do something similar to my project and sadly I couldn't find a nice reference for achieving my goal. My graduation depends on this project and I don't have much time to finish it. So can you tell what to use and give example/reference if possible? Should I use SVM or KNN or what? Are there any other ways?

2014-11-20 03:30:07 -0500 commented answer How to draw 3D objects in camerabridgeviewbase for Augmented Reality (Android + Opencv + Opengl)

After you figured it out, would you share your experience?

2014-11-15 11:32:46 -0500 asked a question Render 3D Obejct on top of the camera

Is it possible to render a 3D object with OpenGL on top of the camera in Android?

The idea is to recognize a haar classified object and add a 3D object in the area of the recognized object.

2014-11-09 13:49:51 -0500 asked a question Recognize more than one object from camera

I want to implement an application for Android which recognizes objects with trained haar cascade. Can I train it to recognize more than one object (I don't need them to be recognized at the same time)?

To get it more clear: Lets say I want to recognize a steering wheel and a tire. They wont be on the camera at the same time but I want the phone to recognize whether is a tire or steering wheel. (Some links would be helpful).