java - drawContours 不填充/JAVA - OpenCV

标签 java opencv

需要堆栈溢出的强大帮助。实际上,我开发的应用程序必须通过 OCR(我正在使用 tesseract)文档进行分析,并提取我可以从中提取的所有文本。这是图像类型的示例:

Image including text to extract

这是我在预处理中所做的,以去除所有的线条。将来我可能还必须分别分析每个“矩形”(将由给定线定义的区域提供给 tesseract)所以我想有比这更简单的方法,但我不会有“线”坐标。

package formRecog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import static org.opencv.core.Core.bitwise_not;
import org.opencv.core.MatOfPoint;


public class testMat {

    public static void main(String[] args) {

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat source  = Imgcodecs.imread("./image.png",Imgcodecs.CV_LOAD_IMAGE_ANYCOLOR);
        Mat destination  = new Mat(source.rows(), source.cols(), source.type());
        Imgproc.cvtColor(source, destination, Imgproc.COLOR_RGB2GRAY);  
        Imgcodecs.imwrite("gray.jpg", destination);

        Imgproc.GaussianBlur(destination, destination, new Size(3, 3), 0, 0, Core.BORDER_DEFAULT);  

        Imgproc.Canny(destination, destination, 30, 90);
        Imgcodecs.imwrite("postcanny.jpg", destination);

        Mat houghlines = new Mat(); 
        Imgproc.HoughLinesP(destination, houghlines, 1, Math.PI / 180,  250, 185,5);

        //DESSINER LES LIGNES
        Mat result = new Mat(source.rows(), source.cols(), source.type());
        for (int i = 0; i < houghlines.rows(); i++) {
            double[] val = houghlines.get(i, 0);
            Imgproc.line(destination, new Point(val[0], val[1]), new Point(val[2], val[3]), new Scalar(0, 0, 255), 5);
            Imgproc.line(result, new Point(val[0], val[1]), new Point(val[2], val[3]), new Scalar(0, 0, 255),5);
        }

        Imgcodecs.imwrite("lines.jpg", result);

        Mat contourImg = new Mat(source.rows(), source.cols(), source.type());
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Mat hierarchy = new Mat();
        //Point offset = new Point();

        Imgproc.findContours(destination, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_NONE );
        Imgproc.drawContours(contourImg, contours, -1, new Scalar(255, 0, 0),-1);

        Imgcodecs.imwrite("contour.jpg", contourImg);

        bitwise_not(destination,destination);


        Imgcodecs.imwrite("final.jpg", destination);

    }
}

这是最终的图像

Final image after processing

问题是,tesseract 没有读到任何关于这个的内容:

11m ÈEZÈ@ÜDS@ 7 C@mpû@ 515 îf@5@??ûäû ©©m@@@ @@ vësw??a? PF©@MÜGS @"@X@Ü©ÜÊQÜ©IÏÙ 1111 175515

是我得到的第一条“线”。

我认为这是因为字母不再“填充”并且 tesseract 无法读取它们,因为 tesseract 实际上先给了我很好的结果,但是删除行的方法并不好。 我想用黑色填充字母但是

Imgproc.drawContours(contourImg, contours, -1, new Scalar(255, 0, 0),-1);

不做任何事情,虽然我很确定 findContours 工作正常,因为如果我输入它的结果,我会得到与以前完全相同的图像。

我搜索了类似的问题 cv2.drawContours will not draw filled contourContour shows dots rather than a curve when retrieving it from the list, but shows the curve otherwise 但没有找到任何我可以使用的东西(也许没有得到它)​​。

就像你知道的那样,我在 9 月份开始了编程类(class),所以我对这件事很陌生(如果这里写了一些令人毛骨悚然的东西请原谅我),但我在这个主题上别无选择致力于:)

我希望我说得足够清楚并且我的英语不是太差。

非常感谢。

编辑:感谢 Rick.M 它变得越来越好,在 findcontours 中使用 CHAIN_APPROX_SIMPLE 并在 drawcontours 中通过 ldx 迭代达到了目的。 New final

有没有办法改善这个结果?我猜 tesseract 也不会吃这个? 谢谢

正在上传 postcanny 图片:Image after canny

最佳答案

drawContours 没有按要求工作的原因是标志:CHAIN_APPROX_NONE 绝对存储所有轮廓点。因此,使用 CHAIN_APPROX_SIMPLE 压缩水平、垂直和对角线段并仅保留它们的端点 为您提供完成的轮廓。在这种情况下,您还可以使用 Imgproc.drawContours(contourImg, contours, -1, new Scalar(255, 0, 0),-1); 没有循环,应该可以正常工作。

现在,对于评论中的讨论,Canny 图像看起来不错,但正如您在缩放后看到的那样,findContours 未检测到的字母未完全连接。我建议使用 erosion使用小内核(您必须使用参数)以获得更好的结果。

关于java - drawContours 不填充/JAVA - OpenCV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50406682/

相关文章:

python - 将 OpenCV 与 Django 结合使用

java - Java 和 opencv 3.0 中的 OpenCV 错误 : Assertion failed (! empty()) 是什么意思?

python - cv2.ml_KNearest 对象没有属性 find_nearest

java - 使用流重定向从 Java 代码启动外部可执行文件

java - 获取 org.w3c.dom.DOMException : HIERARCHY_REQUEST_ERR adding root node to document

java - 实例化 DefaultBoundedRangeModel

java - 无法在 CQ 中部署 SlingServlet

opencv - OpenCV Mat访问和使用Mat_常见错误

android - Android 版 OpenCV : Convert Camera preview from YUV to RGB with Imgproc. cvtColor

java - 在java中进行xml转换和过滤