0

How to count exactly the lines between the numbers, total count must be 6.

I'm using this method to count the number of lines using openCV and I got the grayscale image.

Is lines.cols() the right way to count the number of lines in an image?

image with lines to count

public String Func() {

    Bitmap bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.park);
    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);


    int count = lines.cols();
    System.out.println("count = " + count);
    String cou = String.valueOf(count);

    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;

    Drawable d = new BitmapDrawable(Resources.getSystem(), bitmap);
    img2.setImageDrawable(d);

    return cou;
}
Elouarn Laine
  • 1,665
  • 15
  • 27
San
  • 11
  • 8

1 Answers1

0

According to this SO's answer and this one, your code sample seems correct.

To get better results, prior to applying canny filter, you should do a color threshold on the image to only keep the white painted strips.

Other than that, I think you should increase your minLineSize in order to discard too short lines.

Community
  • 1
  • 1
Elouarn Laine
  • 1,665
  • 15
  • 27
  • I have the opencv sdk of lower version.Im getting error in the first line,can u provide me the link to download new version of opencv sdk – San May 11 '17 at 08:43
  • @San What OpenCV's version do you have? I've updated my post with a 2nd method, can you give it a try? – Elouarn Laine May 11 '17 at 08:51
  • I have version 2.3 now Im downloading 3.2. and one more thing Im using opencv in android..ur code looks like c++ – San May 11 '17 at 09:22
  • that was not helpful – San May 11 '17 at 10:21
  • @San Do you mind explaining what your problem is? Any errors? – Elouarn Laine May 11 '17 at 10:32
  • @San Can you print `lines.rows()` and `lines.cols()` and tell me the result? – Elouarn Laine May 11 '17 at 12:37
  • ya I did lines.cols() is 64 and lines.rows() is 3. lines.cols() considers everying as a line – San May 11 '17 at 12:47
  • @San Are you running OpenCV 3.2 now? Can you try to print x1, y1, x2, y2 for the first columns of the lines mat? – Elouarn Laine May 11 '17 at 13:32
  • when i run the above code I could not get even the greyscale image – San May 12 '17 at 06:15
  • @San, `Canny` needs a greyscale image, but in your code I don't see any color conversions, add this line before `Canny` : `Imgproc.cvtColor(mat, mat, Imgproc.COLOR_BGR2GRAY);` As far as I understand, your code should display an image with blue lines on a black background, is it the case? – Elouarn Laine May 12 '17 at 07:43