-1

我尝试在安卓手机上使用 OpenCV 来检测线路。我只是初学者,我不完全理解这段代码,因为我是从互联网上改编的。最后我只看到一行,而不是全部。有人能帮我吗?

    Bitmap bmp = ((BitmapDrawable)viewImage.getDrawable()).getBitmap();
    Mat grayMat = new Mat();
    Mat cannyEdges = new Mat();
    Mat lines = new Mat();
    Mat originalMat = new Mat();
    Utils.bitmapToMat(bmp, originalMat);

    //Converting the image to grayscale
    Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY);
    Imgproc.Canny(grayMat, cannyEdges, 10, 100);
    Imgproc.HoughLinesP(cannyEdges, lines, 1, Math.PI / 180, 50, 20, 20);

    Mat houghLines = new Mat();
    houghLines.create(cannyEdges.rows(), cannyEdges.cols(), CvType.CV_8UC1);

    //Drawing lines on the image
    for (int i = 0; i < lines.cols(); i++) {
        double[] points = lines.get(0, i);
        double x1, y1, x2, y2;
        x1 = points[0];
        y1 = points[1];
        x2 = points[2];
        y2 = points[3];
        Point pt1 = new Point(x1, y1);
        Point pt2 = new Point(x2, y2);

        //Drawing lines on an image
        Imgproc.line(houghLines, pt1, pt2, new Scalar(255, 0, 0), 1);
    }

    //Converting Mat back to Bitmap
    Utils.matToBitmap(houghLines, bmp);
    viewImage.setImageBitmap(bmp);
4

1 回答 1

-1

你确定精明的输出或你的输入有足够的线路来检测吗?此代码将打印所有行。所以我猜是你的数据问题。

先看看canny输出有多少行。如果由 canny 更改 canny 参数引起

Utils.matToBitmap(cannyEdges, bmp);
viewImage.setImageBitmap(bmp);

那么如果canny能看到很多直线而没有输出呢?不用担心,更改 houghline 参数

于 2019-05-25T19:18:24.920 回答