使用opencv从图像中检测android中的文本

时间:2017-08-17 09:27:32

标签: opencv

请告诉我。我创建了一个Android应用程序,它使用OpenCV的MSER algorthim实时检测文本区域。我需要在照片中识别,而不是实时识别。

如何传输照片,而不是视频流?

我的代码:

 private void detectText() {
    Scalar CONTOUR_COLOR = new Scalar(255);
    MatOfKeyPoint keypoint = new MatOfKeyPoint();
    List<KeyPoint> listpoint;
    KeyPoint kpoint;
    Mat mask = Mat.zeros(mGrey.size(), CvType.CV_8UC1);
    int rectanx1;
    int rectany1;
    int rectanx2;
    int rectany2;
    int imgsize = mGrey.height() * mGrey.width();
    Scalar zeos = new Scalar(0, 0, 0);

    List<MatOfPoint> contour2 = new ArrayList<MatOfPoint>();
    Mat kernel = new Mat(1, 50, CvType.CV_8UC1, Scalar.all(255));
    Mat morbyte = new Mat();
    Mat hierarchy = new Mat();

    Rect rectan3;
    //
    FeatureDetector detector = FeatureDetector
            .create(FeatureDetector.MSER);
    detector.detect(mGrey, keypoint);
    listpoint = keypoint.toList();
    //
    for (int ind = 0; ind < listpoint.size(); ind++) {
        kpoint = listpoint.get(ind);
        rectanx1 = (int) (kpoint.pt.x - 0.5 * kpoint.size);
        rectany1 = (int) (kpoint.pt.y - 0.5 * kpoint.size);
        rectanx2 = (int) (kpoint.size);
        rectany2 = (int) (kpoint.size);
        if (rectanx1 <= 0)
            rectanx1 = 1;
        if (rectany1 <= 0)
            rectany1 = 1;
        if ((rectanx1 + rectanx2) > mGrey.width())
            rectanx2 = mGrey.width() - rectanx1;
        if ((rectany1 + rectany2) > mGrey.height())
            rectany2 = mGrey.height() - rectany1;
        Rect rectant = new Rect(rectanx1, rectany1, rectanx2, rectany2);
        try {
            Mat roi = new Mat(mask, rectant);
            roi.setTo(CONTOUR_COLOR);
        } catch (Exception ex) {
            Log.d("mylog", "mat roi error " + ex.getMessage());
        }
    }

    Imgproc.morphologyEx(mask, morbyte, Imgproc.MORPH_DILATE, kernel);
    Imgproc.findContours(morbyte, contour2, hierarchy,
            Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
    for (int ind = 0; ind < contour2.size(); ind++) {
        rectan3 = Imgproc.boundingRect(contour2.get(ind));
        rectan3 = Imgproc.boundingRect(contour2.get(ind));
        if (rectan3.area() > 0.5 * imgsize || rectan3.area() < 100
                || rectan3.width / rectan3.height < 2) {
            Mat roi = new Mat(morbyte, rectan3);
            roi.setTo(zeos);

        } else
            Imgproc.rectangle(mRgba, rectan3.br(), rectan3.tl(),
                    CONTOUR_COLOR);
    }
}

0 个答案:

没有答案