从最大稳定的末端区域中提取识别的文本

时间:2018-07-24 07:42:05

标签: java android opencv

我想使用createBitmap将识别的文本复制到干净的图像上,但是我不知道如何从原始图像的框中获取它。

此代码找到最大稳定的极值区域,并在位图上突出显示它们。下面是我在示例图像上看到的内容。

private void sgmnt(Mat mImg) {

    Imgproc.cvtColor(msImg, m2, Imgproc.COLOR_RGB2GRAY);
    Mat mRgba = mImg;
    Mat mGray = m2;

    Scalar CONTOUR_COLOR = new Scalar(1, 1, 255, 1);
    //Scalar CONTOUR_COLOR = new Scalar(255);
    MatOfKeyPoint keyPoint = new MatOfKeyPoint();
    List<KeyPoint> listPoint = new ArrayList<>();
    KeyPoint kPoint = new KeyPoint();
    Mat mask = Mat.zeros(mGray.size(), CvType.CV_8UC1);
    int rectanx1;
    int rectany1;
    int rectanx2;
    int rectany2;
    int imgSize = mGray.height() * mGray.width();
    Scalar zeros = new Scalar(255,1,1, 1);

    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 = new Rect();

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.MSER);
    detector.detect(mGray, 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) > mGray.width()) {
            rectanx2 = mGray.width() - rectanx1;
        }
        if ((rectany1 + rectany2) > mGray.height()) {
            rectany2 = mGray.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 i = 0; i<contour2.size(); ++i){
        rectan3 = Imgproc.boundingRect(contour2.get(i));
        if(rectan3.area() > 0.5 * imgSize || rectan3.area()<100 || rectan3.width / rectan3.height < 2){
            Mat roi = new Mat(morByte, rectan3);
            roi.setTo(zeros);
        }else{
            Imgproc.rectangle(mRgba, rectan3.br(), rectan3.tl(), CONTOUR_COLOR);
        }
    }
}

这是我得到的例子:

enter image description here

我的问题是我想以某种方式将文本插入框内。

0 个答案:

没有答案