Ask Your Question

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

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:

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");; //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


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; 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, 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 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

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:

edit flag offensive delete link more

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");;

    [... use detector ... ]

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


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


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

Seen: 12,454 times

Last updated: Nov 13 '15