-1

I try to use OpenCV on an android phone to detect lines. I am only the beginner and i don't fully understand this code because i adapt it from internet. In the end I see only the one line, instead all of them. Can someone help me?

    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);
Oleg
  • 1

1 Answers1

-1

U sure the canny output or your input have enough line to detect? this code will print all the line. so i guess is your data issue.

See how many line at canny output first. if caused by canny change canny parameter

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

Then if canny can see many straight line and no output? dont worry, change houghline parameters

Dr Yuan Shenghai
  • 1,849
  • 1
  • 6
  • 19
  • Thanks for your answer! Canny give all of the line, so problem is Houghline's parameters. I change minLineLength from 20 to 0, maxLineGap from 20 to 50, threshold from 50 to 100, theta from Pi/180 to Pi/360. But result is same. P.S. This is the [image](https://www.maam.ru/upload/blogs/6df42868cd0d3c9d0d690247d7001127.png.jpg) which I use for test. Result is an upper line. – Oleg May 26 '19 at 19:35
  • Given your data, it seems the line is not staight enough. You might want to relax the detected canny edge by using morphological operation. The morphological is to incrase the line with of the canny, so that the angle-distance space of the hough is easier to get a maximum. Its bit diffilct to explain in word. ealiser to draw those concept yout. – Dr Yuan Shenghai May 27 '19 at 02:47