Ask Your Question
3

Java: How to set parameters to ORB FeatureDetector?

asked 2012-10-15 04:26:27 -0600

Andriy gravatar image

updated 2012-10-15 05:39:11 -0600

Rui Marques gravatar image

As it says in the documentation, in OpenCV there is a constructor called ORB, where I can specify a lot of parameters. But I'm using Java and I can't find how to specify those parameters. All I can to is to initialize detector with:

FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);

So how I can specify parameters from the documentation? Is there another way to initialize ORB feature detector in Java? Thanks.

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
10

answered 2012-10-15 04:38:04 -0600

Rui Marques gravatar image

updated 2012-10-15 05:31:22 -0600

You have to do a work-around like it was answered here in the past.

Check this test to see how it is done: http://code.opencv.org/projects/opencv/repository/revisions/master/entry/modules/java/android_test/src/org/opencv/test/features2d/ORBDescriptorExtractorTest.java

These comments have ORB default parameters values that I saw within the source code, but these might be outdated and might have errors. Chech the source yourself to make sure.

    // ORB Default values:
    // float scaleFactor = 1.2f // Coefficient by which we divide the dimensions from one scale pyramid level to the next
    // uint nLevels      = 8   // The number of levels in the scale pyramid
    // uint firstLevel   = 0   // The level at which the image is given
    // int edgeThreshold = 31  // How far from the boundary the points should be.
    // int patchSize     = 31  // You can not change this, it is allways 31

    // int WTA_K = 2           // How many random points are used to produce each cell of the descriptor (2, 3, 4 ...)  
    // scoreType = 0           // 0 for HARRIS_SCORE / 1 for FAST_SCORE
    // nFeatures = 500        // not sure if 500 is default

String filename = "/path/to/file/orb_params.yml";
writeFile(paramFile, "%YAML:1.0\nscaleFactor: 1.2\nnLevels: 8\nfirstLevel: 0 \nedgeThreshold: 31\npatchSize: 31\nWTA_K: 2\nscoreType: 0\nnFeatures: 500\n");

extractor.read(filename); //note: this will fail if the above parameters are not the ones expected by method

detector.detect(img, keypoints);
(...)
edit flag offensive delete link more

Comments

Rui, I have to create yml file by my own, because I can't import package org.opencv.test. Is it possible to know if extractor.read(filename); failed? I'm asking it because I can't see any changes in results, neither exceptions.

Andriy gravatar imageAndriy ( 2012-10-15 05:14:03 -0600 )edit

You do not need to import it, the code I posted creates the yml file, but make sure the file points to the sdcard (or somewhere you can write files on your android), and also that your app has permissions to write to external storage.

Rui Marques gravatar imageRui Marques ( 2012-10-15 05:18:26 -0600 )edit

The version which I've installed doesn't include OpenCVTestRunner.java, because It is not Android OpenCV. It is a new version of OpenCV with support for Java desktop applications.

Andriy gravatar imageAndriy ( 2012-10-15 05:25:48 -0600 )edit

You do not need OpenCVTestRunner.java. Check the edited answer, but then again, this work-around was for OpenCV 4 Android, I don't know it it will work for desktop Java...

Rui Marques gravatar imageRui Marques ( 2012-10-15 05:32:54 -0600 )edit

Looks like I had a bad-formated yml file, because I've tried same thing with xml file and it worked.

Andriy gravatar imageAndriy ( 2012-10-24 11:42:03 -0600 )edit

I tried building a xml file for changing parameters, but it is not taking any effect, help would be really appreciated.

Pallavi gravatar imagePallavi ( 2015-10-15 05:00:02 -0600 )edit

I agree - this is not working. If I deliberately mis-type a parameter name in the YML flie, nothing happens. If I try to use the write() function to verify the internal state of the detector, I do get a File with a '%YAML 1.0' header, but no other content.

Bob W gravatar imageBob W ( 2015-11-11 16:46:30 -0600 )edit
2

answered 2015-11-13 11:03:27 -0600

Bob W gravatar image

To save time for the next person who stumbles across this post, the workaround described above does not work for ORB in OpenCV 3.0, as described here: http://answers.opencv.org/question/76...

edit flag offensive delete link more
2

answered 2013-07-22 23:38:11 -0600

If you don't have the OpenCVTestRunner, this does the job (Exception-Handling omitted):

private void init() {

    [...]

    FeatureDetector orbDetector = FeatureDetector.create(FeatureDetector.ORB);

    File outputDir = getCacheDir(); // If in an Activity (otherwise getActivity.getCacheDir();
    File outputFile = File.createTempFile("orbDetectorParams", ".YAML", outputDir);
    writeToFile(outputFile, "%YAML:1.0\nscaleFactor: 1.2\nnLevels: 8\nfirstLevel: 0 \nedgeThreshold: 31\npatchSize: 31\nWTA_K: 2\nscoreType: 1\nnFeatures: 500\n");
    orbDetector.read(outputFile.getPath());

    [... use detector ... ]
}

private void writeToFile(File file, String data) {
    FileOutputStream stream = new FileOutputStream(file);
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(stream);
    outputStreamWriter.write(data);
    outputStreamWriter.close();
    stream.close();
}
edit flag offensive delete link more

Comments

Note to readers: this completes my answer by showing how to actually write/read yaml the file. I forgot to place that detail in my answer :)

Rui Marques gravatar imageRui Marques ( 2013-08-02 11:09:52 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2012-10-15 04:26:27 -0600

Seen: 12,400 times

Last updated: Nov 13 '15