如何使用opencv

时间:2017-05-11 06:16:45

标签: android opencv

如何准确计算数字之间的线数,总计数必须 6

我使用这种方法来计算使用openCV的行数,我得到了灰度图像。

lines.cols()是计算图像中行数的正确方法吗?

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 个答案:

答案 0 :(得分:0)

根据此SO's answerthis,您的代码示例似乎是正确的。

为了获得更好的效果,在应用精确滤镜之前,您应该在图像上执行颜色阈值以仅保留白色条纹。

除此之外,我认为你应该增加你的minLineSize以便丢弃太短的行。