Ask Your Question

caiocanalli's profile - activity

2018-11-09 11:22:53 -0500 commented question Low accuracy of SVM on Android

VLAD is an improvement on BOW, also HOG My main fear is that I'm implementing BoW incorrectly. ... and STOP using #incl

2018-11-09 11:20:24 -0500 commented question Low accuracy of SVM on Android

Hi again @berak. Always helping me rsrs. 10000 features, but only 800 images (per side) Actually, I've tried several va

2018-11-09 09:32:20 -0500 asked a question Low accuracy of SVM on Android

Low accuracy of SVM on Android Hello guys, I have an Android project that uses the face detection feature (Cascade Clas

2018-11-09 06:46:26 -0500 answered a question OpenCV Contrib Android - Module appears in Java, but not in C ++

I found the problem. I needed to copy the SDK include folder into the project. After copying the folder, the modules beg

2018-11-04 19:08:30 -0500 asked a question OpenCV Contrib Android - Module appears in Java, but not in C ++

OpenCV Contrib Android - Module appears in Java, but not in C ++ Hello guys, I have an Android project, and I'm using a

2018-10-01 21:47:31 -0500 commented question BagOfWords works in C#, but not in C++

Hi @berek! I discovered this a bit after answering the question. Actually, this is the correct way to load the SVM. Pt

2018-10-01 21:47:06 -0500 commented question BagOfWords works in C#, but not in C++

Hi @berek! I discovered this a bit after answering the question. Actually, this is the correct way to load the SVM. Pt

2018-10-01 21:10:50 -0500 marked best answer BagOfWords works in C#, but not in C++

Hello guys,

As suggested in this post, I implemented in C++, the same method I used to train my SVM in C #. Initially, I load the vocabulary generated by the program in C# through a JNI call in C++, because the FileStorage class is not available in OpenCV4Android:

JNIEXPORT
void JNICALL Java_test_vision_MainActivity_loadVocabulary(
        JNIEnv* env,
        jobject,
        jstring vocabularyPath,
        jlong addrVocabulary)
{
    const char *str = env->GetStringUTFChars(vocabularyPath, 0);

    Mat& vocabulary = *(Mat*)addrVocabulary;

    cv::FileStorage opencv_file(str, cv::FileStorage::READ);
    opencv_file["vocabulary"] >> vocabulary;
    opencv_file.release();
}

After that, I load an image through Java, and pass the Mat object to a second C++ method, which has all the implementation of BagOfWords and SVM Predict.

Java:

Mat image = Utils.loadResource(MainActivity.this, R.raw.image, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);

predict(trainedData,
            vocabularyMat.getNativeObjAddr(),
            image.getNativeObjAddr(),
            bowDescriptor.getNativeObjAddr());

C++

JNIEXPORT
void JNICALL Java_test_vision_MainActivity_predict(
        JNIEnv* env,
        jobject,
        jstring trainedData,
        jlong addrVocabulary,
        jlong addrRgba,
        jlong addrBowDescriptor) {

    const char *str = env->GetStringUTFChars(trainedData, 0);

    Mat& vocabulary = *(Mat*)addrVocabulary;
    Mat& bowDescriptor = *(Mat*)addrBowDescriptor;

    int dictionarySize = 32;

    Ptr<DescriptorExtractor> extractor = KAZE::create();

    Ptr<BFMatcher> bFMatcher = new BFMatcher(cv::NORM_L2);

    Ptr<BOWKMeansTrainer> bOWKMeansTrainer
            = new BOWKMeansTrainer(
                    dictionarySize,
                    TermCriteria(
                            TermCriteria::MAX_ITER +
                            TermCriteria::EPS,
                            10,
                            0.0001),
                    1, KMEANS_PP_CENTERS);

    Ptr<BOWImgDescriptorExtractor> bOWImgDescriptorExtractor =
            new BOWImgDescriptorExtractor(extractor, bFMatcher);

    Ptr<ml::SVM> svm = ml::SVM::create();
    svm->setType(ml::SVM::C_SVC);
    svm->load(str);

    bOWImgDescriptorExtractor->setVocabulary(vocabulary);

    std::vector<KeyPoint> keyPoints;

    Mat& src = *(Mat*)addrRgba;

    try {
        extractor->detect(src, keyPoints);
        bOWImgDescriptorExtractor->compute(src, keyPoints, bowDescriptor);
    } catch(exception& e) {
        const char *teste = e.what();
        cout << teste;
    }

    float response = svm->predict(bowDescriptor);

    cout << response;
}

However, I have received this error when calling bOWImgDescriptorExtractor.compute:

"OpenCV(3.4.3) /build/3_4_pack-android/opencv/modules/core/src/batch_distance.cpp:238: error: (-215:Assertion failed) type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U) in function 'void cv::batchDistance(cv::InputArray, cv::InputArray, cv::OutputArray, int, cv::OutputArray, int, int, cv::InputArray, int, bool)'\n"

Sorry for the size of the question, but I tried to put as much information as possible.

Thank you.

2018-10-01 21:04:56 -0500 commented question BagOfWords works in C#, but not in C++

I found the problem. The issue was that I was creating an instance of KAZE detector, without setting extended and uprigh

2018-10-01 13:08:45 -0500 received badge  Editor (source)
2018-10-01 13:08:45 -0500 edited question BagOfWords works in C#, but not in C++

BagOfWords works in C #, but not in C ++ Hello guys, As suggested in this post, I implemented in C++, the same method I

2018-10-01 13:08:30 -0500 asked a question BagOfWords works in C#, but not in C++

BagOfWords works in C #, but not in C ++ Hello guys, As suggested in this post, I implemented in C++, the same method I

2018-10-01 12:18:01 -0500 commented question SVM predict error on OpenCV4Android

Hi @berek, sorry for the delay. I am making an application for people with disabilities, which do not move any part of

2018-09-28 08:32:18 -0500 received badge  Enthusiast
2018-09-27 08:19:25 -0500 commented question SVM predict error on OpenCV4Android

I already tried using Hog to train the SVM, but I did not succeed. Only with BoW did I get any results. Yes, my sample

2018-09-27 06:49:41 -0500 commented question SVM predict error on OpenCV4Android

Hi @berak, I trained svm based on 15 closed-eyed images and 17 open-eyed images (Subsequently, I will increase the samp

2018-09-26 21:06:22 -0500 asked a question SVM predict error on OpenCV4Android

SVM predict error on OpenCV4Android Hi guys, I performed the training of an SVM based on the code below. I used C # for

2018-09-24 20:25:52 -0500 commented answer Problem when training SVM with ORB descriptors (Android)

Ok, thank you!

2018-09-24 20:25:29 -0500 marked best answer Problem when training SVM with ORB descriptors (Android)

Hello guys!

I have a folder with 20 positive and 10 negative images and I'm trying to train an SVM through the image descriptors using ORB. I'm getting the following error when calling svm.TrainAuto:

E/cv::error(): OpenCV(3.4.1) Error: Assertion failed (samples.type() == 5 || samples.type() == 4) in void cv::ml::TrainDataImpl::setData(cv::InputArray, int, cv::InputArray, cv::InputArray, cv::InputArray, cv::InputArray, cv::InputArray, cv::InputArray), file /build/master_pack-android/opencv/modules/ml/src/data.cpp, line 259

E/org.opencv.ml: ml::trainAuto_11() caught cv::Exception: OpenCV(3.4.1) /build/master_pack-android/opencv/modules/ml/src/data.cpp:259: error: (-215) samples.type() == 5 || samples.type() == 4 in function void cv::ml::TrainDataImpl::setData(cv::InputArray, int, cv::InputArray, cv::InputArray, cv::InputArray, cv::InputArray, cv::InputArray, cv::InputArray)

The code I'm using is below:

public class TrainingActivity extends Activity {

private final String TAG = "OpenCV";

private Button mTrainingButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_training);

    mTrainingButton = findViewById(R.id.trainingButton);
}

@Override
protected void onResume() {
    super.onResume();
    initOpenCV();
}

public void onTrainingButtonClick(View view) {
    train();
}

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback() {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
                onLoaderCallbackSuccess();
                break;
            default:
                super.onManagerConnected(status);
        }
    }
};

private void initOpenCV() {
    if (OpenCVLoader.initDebug())
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    else
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_4_0, this, mLoaderCallback);
}

private void onLoaderCallbackSuccess() {
    mTrainingButton.setVisibility(View.VISIBLE);
}

private void train() {

    final ORB orb = ORB.create();

    Mat extractorTrainingData = new Mat();
    Mat extractorTrainingLabel = new Mat();

    String path = "/OpenCV/training/open/positives";

    File[] files = FileHelper.getFiles(path);

    // Positives

    for (File file : files) {

        try {
            Mat image = Imgcodecs.imread(file.getAbsolutePath());

            Mat grayImage = new Mat();
            Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_RGB2GRAY);

            Log.d(TAG, grayImage.dump());

            MatOfKeyPoint keyPoints = new MatOfKeyPoint();
            orb.detect(grayImage, keyPoints);

            Log.d(TAG, keyPoints.dump());

            MatOfFloat descriptors = new MatOfFloat();
            orb.compute(grayImage, keyPoints, descriptors);

            Log.d(TAG, descriptors.dump());

            extractorTrainingData.push_back(descriptors);
            extractorTrainingLabel.push_back(Mat.ones(new Size(1, 1), CvType.CV_32S));

        } catch (Exception e) {
            Log.d(TAG, e.getMessage());
        }
    }

    path = "/OpenCV/training/open/negatives";

    files = FileHelper.getFiles(path);

    // Negatives

    for (File file : files) {

        try {
            Mat image = Imgcodecs.imread(file.getAbsolutePath());

            Mat grayImage = new Mat();
            Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_RGB2GRAY);

            Log.d(TAG, grayImage.dump());

            MatOfKeyPoint keyPoints = new MatOfKeyPoint();
            orb.detect(grayImage, keyPoints);

            Log.d(TAG, keyPoints.dump());

            MatOfFloat descriptors = new MatOfFloat();
            orb.compute(grayImage, keyPoints, descriptors);

            Log.d(TAG, descriptors.dump());

            extractorTrainingData.push_back(descriptors);
            extractorTrainingLabel.push_back(Mat.zeros(new Size(1, 1), CvType.CV_32S));

        } catch (Exception e) {
            Log.d(TAG, e.getMessage());
        }
    }

    try {

        SVM svm = SVM.create();
        svm.setKernel(SVM.RBF);
        svm.setType(SVM.C_SVC);

        //Mat mat = extractorTrainingData.reshape(1, 1);

        //Boolean result = svm.trainAuto(
        //        mat, Ml.ROW_SAMPLE, extractorTrainingLabel);

        Boolean result = svm.trainAuto(
                extractorTrainingData, Ml.ROW_SAMPLE, extractorTrainingLabel);

        String xml = Environment
                .getExternalStorageDirectory() + "/images/svm.xml";

        if (result)
            svm.save(xml);

    } catch (Exception e) {
        Log.d(TAG, e.getMessage());
    }
}

}

I've tried using the reshape function in these settings:

Mat mat = extractorTrainingData.reshape(1, 1);
Boolean result = svm.trainAuto(mat, Ml.ROW_SAMPLE, extractorTrainingLabel);

Mat ...
(more)
2018-09-24 20:25:29 -0500 received badge  Scholar (source)
2018-09-24 19:04:53 -0500 commented answer Problem when training SVM with ORB descriptors (Android)

Hi @berak, thanks for the reply. ORB (or any binary) descriptors are useless... In case, I chose to use ORB because it

2018-09-23 16:41:36 -0500 asked a question Problem when training SVM with ORB descriptors (Android)

Problem when training SVM with ORB descriptors Hello guys! I have a folder with 20 positive and 10 negative images and