我正在使用Java版Opencv开发OCR软件进行表检测。我能够检测到图像的几乎所有文本边框,但我已经问过"圈出"字/数字。
对于文本检测,我执行以下操作:
我使用形态学操作检测表格中的水平和垂直线条,并从执行减法的原始Mat图像中删除这些线条(不确定是否是执行此操作的最佳方法):
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
// apply adaptive threshold at the bitwise_not of gray
Mat bw = new Mat();
Core.bitwise_not(gray, gray);
Imgproc.threshold(gray, bw, 0.0, 255.0, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);
// create the images that will use to extrat the horizontal and vertical lines
Mat horizontal = bw.clone();
Mat vertical = bw.clone();
// Specify size on horizontal axis
int horizontalSize = horizontal.cols() / 20;
Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(horizontalSize, 1));
// apply morphology operations
Imgproc.erode(horizontal, horizontal, horizontalStructure, new Point(-1, -1), 1);
Imgproc.dilate(horizontal, horizontal, horizontalStructure, new Point(-1, -1), 1);
// Imgproc.blur(horizontal, horizontal, new Size(3,3));
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_DILATE, new Size(3, 3));
Imgproc.dilate(horizontal, horizontal, kernel, new Point(-1, -1), 1);
// Specify size on vertical axis
int verticalSize = vertical.rows() / 20;
Mat verticalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1, verticalSize));
// apply morphology operations
Imgproc.erode(vertical, vertical, verticalStructure, new Point(-1, -1), 1);
Imgproc.dilate(vertical, vertical, verticalStructure, new Point(-1, -1), 1);
kernel = Imgproc.getStructuringElement(Imgproc.MORPH_DILATE, new Size(3, 3));
Imgproc.dilate(vertical, vertical, kernel, new Point(-1, -1), 1);
//Is the correct way to do this?
// delete lines from binary image
Core.subtract(bw, horizontal, bw);
Core.subtract(bw, vertical, bw);
然后使用findContours函数我得到单词边界:
ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
Imgproc.findContours(bw, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
中间和最终结果显示在图像中:
Inv binary image with no lines and final result
问题在于无法识别带圈数字,我无法检测并移除表格底部数字周围的圆圈。
我尝试过使用 Hough Circles Transform 和fitEllipse
方法,结果不佳。尝试通过将椭圆与椭圆相匹配来删除圆圈是最好的方法吗?
有人能建议一个有效的程序来实现这个目标吗?