Ask Your Question
0

Android: Draw contours arround points detected by MSER

asked 2015-03-04 05:19:02 -0600

SARGE553413 gravatar image

I'm trying to draw countours to indicate the points detected by MSER in a bitmap.

I can detect the points with FeatureDectector.detect() but it returns them as MatOfKeyPoint.

Now I want to dray the contour of these points using Imgproc.drawContours(), but this function receives the points as a List<MatOfPoint>.

I have tried to convert MatOfKeyPoint to List<MatOfPoint>, then use drawContours(). This is what I have tested:

private void processBitmap(Bitmap bmp){
    Mat mat = new Mat(bmp.getWidth(), bmp.getHeight(),
            CvType.CV_16S, new Scalar(4));

    Utils.bitmapToMat(bmp, mat);

    Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGB2GRAY, 4);

    FeatureDetector fd = FeatureDetector.create(FeatureDetector.MSER);
    MatOfKeyPoint mokp = new MatOfKeyPoint();
    fd.detect(mat, mokp);

    KeyPoint[] kps = mokp.toArray();

    List<MatOfPoint> mops = new ArrayList<MatOfPoint>(kps.length);
    for(int i = 0; i < kps.length; i++)
        mops.set(i, new MatOfPoint(kps[i].pt));

    Scalar s = new Scalar(0);

    Imgproc.drawContours(mat, mops, 1, s);


    Utils.matToBitmap(mat, bmp);
}

In this case, there is no exceptions, but drawContours() does nothing.

I have tested this too:

import org.opencv.core.Point;
[...]

private void processBitmap(Bitmap bmp){
    Mat mat = new Mat(bmp.getWidth(), bmp.getHeight(),
            CvType.CV_16S, new Scalar(4));
    Utils.bitmapToMat(bmp, mat);
    Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGB2GRAY, 4);

    FeatureDetector fd = FeatureDetector.create(FeatureDetector.MSER);
    MatOfKeyPoint mokp = new MatOfKeyPoint();
    fd.detect(mat, mokp);

    KeyPoint[] kps = mokp.toArray();
    Scalar s = new Scalar(0); //color black

    Point[] points = new Point[kps.length]; //
    for(int i = 0; i < points.length; i++)
        points[i] = kps[i].pt;

    MatOfPoint mop = new MatOfPoint();
    mop.fromArray(points);

    List<MatOfPoint> lmop = new ArrayList<MatOfPoint>();
    lmop.add(mop);

    Imgproc.drawContours(mat, lmop, 5, s);      

    Utils.matToBitmap(mat, bmp);
}

In this case there is an exception:

OpenCV Error: Assertion failed (0 <= contourIdx && contourIdx < (int)last

I have read the documentation of OpenCV for java here:

http://docs.opencv.org/java/

But the example is only with findContours().

I have read that MatOfPoint is only a way used by OpenCV to avoid to copy points between native part and java part,

(See: http://www.answers.opencv.org/questio...)

so that, I assume that the List<MatOfPoint> must have only 1 component due to MatOfPoint contains all points I need, is this correct?

How can I convert the MatOfKeyPoint in List<MatOfPoint> properly?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-03-04 06:54:13 -0600

Guanta gravatar image

First, a KeyPoint is a class which not only holds the location but more information, like size, etc. , see http://docs.opencv.org/modules/features2d/doc/common_interfaces_of_feature_detectors.html?highlight=cv2.keypoint#keypoint-keypoint. Second, a Feature-detector like MSER gives you a list of KeyPoints which are not related to Contours at all. You can however draw them using the drawKeypoints() function, see http://docs.opencv.org/java/org/openc....

If you were aware of both points and you really want to use drawContours, then your second approach should actually work (but haven't tested it myself), however you'd need to set the index to 0 (since you only have one contour), i.e. Imgproc.drawContours(mat, lmop, 0, s);

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-03-04 05:19:02 -0600

Seen: 3,393 times

Last updated: Mar 04 '15