How can I draw rectangle with MatOfKeyPoint for Text Detection | Java

asked 2015-08-21 11:53:30 -0600

updated 2017-09-11 15:27:45 -0600

Hi guys,

I' ve been working on real time text detection and recognition with OpenCV4Android. Recognition part is totally completed. However, I have to ask question about text detection. I' m using the MSER FeatureDetector for detection text.

This is the real time and calling the method part:

public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    carrierMat = inputFrame.gray();
    carrierMat = General.MSER(carrierMat);
    return carrierMat;
}

And this is the basic MSER implementation:

private static FeatureDetector fd = FeatureDetector.create(FeatureDetector.MSER);
private static MatOfKeyPoint mokp = new MatOfKeyPoint();
private static Mat edges = new Mat();

public static Mat MSER(Mat mat) {
    //for mask
    Imgproc.Canny(mat, edges, 400, 450);
    fd.detect(mat, mokp, edges);
    //for drawing keypoints
    Features2d.drawKeypoints(mat, mokp, mat);
    return mat;
}

It works fine for finding text with edges mask.

I would like to draw a rectangles for clusters like this: image description

or this

image description

You can assume that I have the right points.

As you can see, fd.detect() method is returning a MatOfKeyPoint. Hence I' ve tried this method for drawing rectangle:

public static Mat MSER_(Mat mat) {
    fd.detect(mat, mokp);
    KeyPoint[] refKp = mokp.toArray();
    Point[] refPts = new Point[refKp.length];

    for (int i = 0; i < refKp.length; i++) {
        refPts[i] = refKp[i].pt;
    }
    MatOfPoint2f refMatPt = new MatOfPoint2f(refPts);
    MatOfPoint2f approxCurve = new MatOfPoint2f();

    //Processing on mMOP2f1 which is in type MatOfPoint2f
    double approxDistance = Imgproc.arcLength(refMatPt, true) * 0.02;
    Imgproc.approxPolyDP(refMatPt, approxCurve, approxDistance, true);

    //Convert back to MatOfPoint
    MatOfPoint points = new MatOfPoint(approxCurve.toArray());
    // Get bounding rect
    Rect rect = Imgproc.boundingRect(points);
    // draw enclosing rectangle (all same color, but you could use variable i to make them unique)
    Imgproc.rectangle(mat, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), Detect_Color_, 5);
    //Features2d.drawKeypoints(mat, mokp, mat);
    return mat;
}

But when I was trying to Imgproc.arcLength() method, it suddenly stopped. I gave a random approxDistance value for Imgproc.approxPolyDP() method like 0.1, it doesn' t work really efficiently.

So how can I draw rectangle for detected text?

Cheers!

edit retag flag offensive close merge delete