Log in

Previous Entry | Next Entry

Thanks to factory method FastFeatureDetector::create, configuring FastFeatureDetector in native C++ code is straightforward:

Ptr fastFeatureDetector = FastFeatureDetector::create(10, true, FastFeatureDetector::TYPE_9_16);
On other hand, Android Java wrapper for OpenCV has no explicit notion of any specific detector: in order to create the particular detector, one uses very generic factory method  FeatureDetector.create(), like

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

We have a generic detector, one of the large family in which any member has several methods in common (detect() etc.), but we want an specific detector, our FAST, configured with its own parameters. And surely, in other cases, we will need other detectors with the another configuration. For this, the enginners of OpenCV came with strange solution: YAML files.

Generally speaking, YAML is much similar to old INI files. I'm wander to find these in the modern software (as far as OpenCV may be considered as morern), but YAML is very intuitive. The properties of the configured class becomes to lines of the YAML file. Say, our FastFeatureDetector needs to have the threshold of 10. The corresponding property just called 'threshold' and so will be the line in YAML:

threshold: 10
nonmaxSuppression: 1
type : 1

Other properties are the same andhe whole configuration procedure will be as following:
1. Somewhere long before the actual creation of FastFeatureDetector:

String fastConfig = "%YAML:1.0\nthreshold: 10\nnonmaxSuppression: 1\ntype : 1\n";
writeToFile(mDetectorConfigFile, fastConfig);

(Pay attention to numerical value of type as defined in features2d.hpp

TYPE_5_8 = 0, TYPE_7_12 = 1, TYPE_9_16 = 2,
}; )
2. Within onCameraFrame() :
FeatureDetector mDetector = FeatureDetector.create(FeatureDetector.FAST);
mDetector.detect(mRgba, keyPoints);

Aside the reading cost, now we have Java equivalent to C++

C++ Java
flip(mGr, mGr, 1);

vector keypoints;

Ptr<FastFeatureDetector> fastFeatureDetector = FastFeatureDetector::create(10, true, FastFeatureDetector::TYPE_7_12);
if( fastFeatureDetector.empty())
fastFeatureDetector->detect(mGr, keypoints);

drawKeypoints(mGr, keypoints, mRgb);
Core.flip(mGray, mGray, 1);
MatOfKeyPoint keyPoints = new MatOfKeyPoint();

FeatureDetector detector = FeatureDetector.create(FeatureDetector.FAST);
if( detector.empty() )
return mRgba;
detector.detect(mRgba, keyPoints);

Features2d.drawKeypoints(mGray, keyPoints, mRgba);

Now we going to compare the performance of both.
The first device to measure is Samsung Note II that equipped with ARMv7-based quad-core 1.6 Ghz Exynos (Cortex-A9) with Mali-400 MP GPU.
The mentioned Java code is executed at this device with 40 ms.
The corresponding C++ code is executed there for 20 ms.
Twice performance gain in favor of C++.