我的mRgba对象的尺寸为0X0,因此它不返回图片上的任何行
完全没有。我想那是空的。代码有什么问题?有没有办法显示线条
黑色背景?

这是代码

    mat = new Mat();
    edges = new Mat();
    Size kernel = new Size(5, 5);
    Mat gauss = new Mat();
    Mat mRgba = new Mat(612,816, CvType.CV_8UC1);
    Mat lines = new Mat(612,816, CvType.CV_8UC1);
    binary_image = new Mat();
    Utils.bitmapToMat(bitmap, mat);
    Imgproc.GaussianBlur(mat, gauss, kernel, 10000, 10000);
    Imgproc.Canny(gauss, edges, 50, 90);
    Imgproc.threshold(edges, binary_image, 0, 255, Imgproc.THRESH_BINARY_INV);

    int threshold = 50;
    int minLineSize = 20;
    int lineGap = 20;

    Imgproc.HoughLinesP(binary_image, lines, 1, Math.PI / 180,threshold,minLineSize,lineGap);



    for (int x = 0; x < lines.cols(); x++) {
        double[] vec = lines.get(0, x);
        double x1 = vec[0],
                y1 = vec[1],
                x2 = vec[2],
                y2 = vec[3];
        Point start = new Point(x1, y1);
        Point end = new Point(x2, y2);

        Core.line(mRgba, start, end, new Scalar(255, 0, 0), 3);

    }

    Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);

    Utils.matToBitmap(mRgba, bmp);
        bitmap=bmp;

编辑:通过删除高斯模糊和阈值方法已经解决了问题
    mat = new Mat();
    edges = new Mat();
    Mat mRgba = new Mat(612,816, CvType.CV_8UC1);
    Mat lines = new Mat();

    Utils.bitmapToMat(bitmap, mat);

    Imgproc.Canny(mat, edges, 50, 90);


    int threshold = 50;
    int minLineSize = 20;
    int lineGap = 20;

    Imgproc.HoughLinesP(edges, lines, 1, Math.PI / 180,threshold,minLineSize,lineGap);



    for (int x = 0; x < lines.cols(); x++) {
        double[] vec = lines.get(0, x);
        double x1 = vec[0],
                y1 = vec[1],
                x2 = vec[2],
                y2 = vec[3];
        Point start = new Point(x1, y1);
        Point end = new Point(x2, y2);

        Core.line(mRgba, start, end, new Scalar(255, 0, 0), 3);

    }

    Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);

    Utils.matToBitmap(mRgba, bmp);
        bitmap=bmp;

最佳答案

  • 创建mRgba时,未指定其大小,它的大小应与尝试查找行的图像大小相同。
  • 您应该检查lines对象,以查看是否能够找到行或
    不。您可以通过检查mRgba来理解它。
  • 这似乎不是完整的代码,这里没有图像加载。您最好共享整个代码和一个示例图像。
  • 10-08 14:12