如下图所示的文本边框给 OCR 带来了非常糟糕的结果。
所以我正在使用 javaCV(OpenCV 的 java 包装器)删除图像中文本周围的边框和框。结果相当令人满意。但我现在面临的问题是,它正在删除文本的水平和垂直线,如下例所示。
被移除的水平线被重新绘制成不同的颜色。
我正在按照以下步骤去除边框
- 找到指定轮廓高度和宽度的水平和垂直轮廓。
- 用白色填充轮廓。
我在下面附上了我的代码片段。
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 没意见。
最佳答案
我认为更稳健的方法是首先检测边缘和轮廓。
在此之后,您应该找到与矩形对应的轮廓。为此,您可以比较所有轮廓的面积并找到最常见的轮廓,这很可能对应于矩形的面积,因为它们都是相同的。
关于opencv - 删除文本周围的黑色边框/框以获得更好的 OCR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52722156/