如何在ocr中增强MSER进行文本检测

时间:2018-05-17 20:11:59

标签: java android opencv image-processing google-vision

`private void sgmnt(Mat mImg){

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

    Scalar CONTOUR_COLOR = new Scalar(1, 255, 128, 0);
    //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(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 = 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);
        }
    }
}Output`[![ ][1]][![]][1]

在分段按钮中,我使用opencv lib使用MSER / Maximally stable extremal regions。如何使用某些算法增强分割?在传递给tesseract的文本识别之前,我应该按字符,单词或某些平滑等对其进行分段。这是我最后的学校项目。

0 个答案:

没有答案
相关问题