OpenCV4Android conversion from MatOfKeyPoint to MatOfPoint2f

I am trying to use OpenCV for Android (OpenCV 2.4.3)
I am writing a program to track keypoints. I am trying to use
FeatureDetector to detect keypoints and then Video.calcOpticalFlowPyrLK
to track them.
The question that has me stumped is that the FeatureDetector function
returns a MatOfKeyPoint while calcOpticalFlowPyrLK takes a MatOfPoint2f.

Here is my code so far:

//Feature detector for LKT flow estimation
FeatureDetector cvFeatureDetector;
//Vector of keypoints
MatOfKeyPoint keypoints;


//intitialize detector
cvFeatureDetector = FeatureDetector.create(FeatureDetector.GFTT);

keypoints = new MatOfKeyPoint();


//mPrevImgMat is a grayscale image - previous frame
//mCurrentImgMat is a grayscale image - current frame

//Run feature detector in previous image
cvFeatureDetector.detect(mPrevImgMat, keypoints);

MatOfPoint2f keypointsFound = new MatOfPoint2f();
MatOfByte keypointsStatus = new MatOfByte();
MatOfFloat err = new MatOfFloat();
Size winSize = new Size(25,25);
int maxLevel = 3;

//Optical flow to find keypoints in current image
Video.calcOpticalFlowPyrLK(mPrevImgMat, mCurrentImgMat, keypoints,
keypointsFound, keypointsStatus, err, winSize, maxLevel);

//Obviously "keypoints" in the line above does not work. How does one
//keypoints to MatOfPoint2f?

Things I have tried unsuccessfully so far:
(1) keypoints.convertTo()
(2) Creating a vector from keypoints and then trying to populate a vector
of Point Vector<Point> pointList. Then typecast to MatOfPoint2f when
calling flow funciton
(MatOfPoint2f) pointList
(3) Trying to populate a MatOfPoint2f from scratch. Cant figure out how to
do this
(4) Using fromArray method in MatOfPoint2f - Not sure what this method
does. Documentation is blank for this method. Am I missing something


Search Discussions

  • Andrey Pavlenko at Jan 17, 2013 at 8:00 am
    We have in plans the creation of docs for Java-specific OpenCV classes.
    For now let me share a short description of `MatOfXxx` Java classes here:

    `MatOfXxx` classes (e.g. `MatOfPoint`) were introduced to avoid redundant
    copying of intermediate data between Java and native memory.
    E.g. you can get some large set of Points as the result of one OpenCV
    function and then pass it to another one.
    In C++ we use `std::vector<cv::Point>` for this. But use of
    `ArrayList<org.opencv.core.Point>` in Java caused copying all the Points
    data from native OpenCV level to Java when returning these Points and
    copying them back when calling the next OpenCV function using them.
    So for the sake of efficiency we switched to use of `MatOfPoint` class in
    such cases that is a kind of `Mat` of 1*n or n*1 dimensions that keeps a
    Point in each element (i.e. of type CV_32SC2 or CV_64FC2).
    As you may know, `Mat` keeps all the data on native level, so such objects
    can be passed between OpenCV calls without data copying.
    But if in your Java code at some point you need direct access to actual
    Points data there are `toArray()` and `fromArray` methods to explicit
    transfer data to/from Java.

    So turning back to your particular question, to create a `MatOfPoint2f`
    containing the points corresponding to ones from existing `MatOfKeyPoint`
    you need:
    1. load KeyPoints to Java via `MatOfKeyPoint.toArray()`
    2. iterate through `KeyPoint[]` and create a corresponding `Point[]` (all
    of `cv::Point`, `cv::Point2f` and `cv::Point2d` are represented as
    `org.opencv.core.Point` in Java)
    3. use `MatOfPoint2f.fromArray()` or c-tor `MatOfPoint2f(`to put
    your Points to native level


Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupandroid-opencv @
postedJan 16, '13 at 5:53a
activeJan 17, '13 at 8:00a

2 users in discussion

Andrey Pavlenko: 1 post Manjuna16: 1 post



site design / logo © 2019 Grokbase