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;
}

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

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 What OpenCV's version do you have? I've updated my post with a 2nd method, can you give it a try?
I have version 2.3 now Im downloading 3.2. and one more thing Im using opencv in android..ur code looks like c++
that was not helpful
@San Do you mind explaining what your problem is? Any errors?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.