【问题标题】:Remove black border/boxes around text for better OCR?删除文本周围的黑色边框/框以获得更好的 OCR?
【发布时间】:2019-03-14 07:10:03
【问题描述】:

如下图所示的文本边框对 OCR 的结果非常糟糕。

所以我使用 javaCV(OpenCV 的 java 包装器)来删除图像中文本周围的边框和框。结果相当令人满意。但我现在面临的问题是,它正在删除文本的水平和垂直行,就像下面的例子一样。

被移除的水平线以不同的颜色重新绘制。

我正在按照以下步骤删除边框

  1. 查找指定轮廓高度和宽度的水平和垂直轮廓。
  2. 用白色填充轮廓。

我在下面附上了我的代码 sn-ps。

public void removeBorder( String filePath )
{
    Mat grayImage = Imgcodecs.imread( filePath, Imgcodecs.IMREAD_GRAYSCALE );
    Mat thresholdInverted = new Mat();
    Imgproc.threshold( grayImage, thresholdInverted, 127.0, 255.0, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU );
    Imgcodecs.imwrite( "E:/threholded.jpg", thresholdInverted );


    List<MatOfPoint> horizontalContours = morphOpenAndFindContours( thresholdInverted, new Size( 5, 1 ));


    List<MatOfPoint> verticalContours = morphOpenAndFindContours( thresholdInverted, new Size( 1, 10 ));

    this.drawWhiteContours( verticalContours, grayImage );
    this.drawWhiteContours( horizontalContours, grayImage );
    Imgcodecs.imwrite( "E:/result.jpg", grayImage );
}

private List<MatOfPoint> morphOpenAndFindContours( Mat img, Size kSize)
{
    Mat kernel = Imgproc.getStructuringElement( Imgproc.MORPH_RECT, kSize );

    Mat openedImage = new Mat();
    Imgproc.morphologyEx( img, openedImage, Imgproc.MORPH_OPEN, kernel, new Point( -1, -1 ), 1 );
    Mat dilateKernel = Imgproc.getStructuringElement( Imgproc.MORPH_RECT, new Size( 5, 5 ) );

    Imgproc.dilate( openedImage, openedImage, dilateKernel );

    List<MatOfPoint> contours = new ArrayList<>();

    Imgproc.findContours( openedImage, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE );

    return contours;
}


private void drawWhiteContours( List<MatOfPoint> contours, Mat image )
{
    for ( int i = 0; i < contours.size(); i++ ) {
        Imgproc.drawContours( image, contours, i, new Scalar( 255 ), -1 );
    }
}

那么我怎样才能只删除边框而不影响文本呢? Java中的解决方案更可取,但我对python没问题。

【问题讨论】:

  • 查看我的一些问题和答案.. 可能是一个很好的起点。一切都在 python..

标签: opencv ocr opencv3.0 javacv image-preprocessing


【解决方案1】:

我认为更稳健的方法是首先检测边缘并检测轮廓。

在此之后,您应该找到与矩形对应的轮廓。为此,您可以比较所有轮廓的面积并找到最常见的一个,这很可能对应于矩形的面积,因为它们都是相同的。

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 2019-12-18
    • 2016-06-06
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多