OpenCV-Java-Android-Imgproc.HoughLinesP仅返回一行

时间:2018-07-18 14:45:20

标签: java android opencv

我正在尝试使用Android Studio上的Imgproc.HoughLinesP来检测以前处理过的图像(灰度-Canny)中的行,但是该功能仅返回一行。我尝试了几张图像,结果始终是最大的一行。这是代码:

        img_masked = null;

        try {

            img_masked = Utils.loadResource(this, R.drawable.paint3lines, CvType.CV_8UC3);
            Log.d(TAG,"Image loaded properly");
        } catch (IOException e) {
            e.printStackTrace();
        }

        img_bitmap = Bitmap.createBitmap(img_masked.cols(), img_masked.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(img_masked, img_bitmap);
        originalImageView.setImageBitmap(img_bitmap);


        lines = new Mat(img_masked.size(), CvType.CV_8UC3);

        System.out.printf("%n----------------> img_masked.cols() = %d  %n", img_masked.cols());
        System.out.printf("%n----------------> img_masked.rows() = %d  %n", img_masked.rows());

      Imgproc.HoughLinesP(img_masked, lines, 1, Math.PI/180, 20, 20, 300);
        Mat mRgba = new Mat(img_masked.size(), CvType.CV_8UC3, Scalar.all(0));

        System.out.printf("%n----------------> lines.cols() = %d  %n", lines.cols());
        System.out.printf("%n----------------> lines.rows() = %d  %n", lines.rows());

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

            Imgproc.line(mRgba, start, end, new Scalar(255,0,0), 4);
            System.out.printf("%n----------------> x = %d  %n", x);

        }

        img_bitmap = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(),Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(mRgba, img_bitmap);
        simpleImageView.setImageBitmap(img_bitmap);

执行代码后,“行”中的列数仅为1,因此for循环仅执行一次,结果只给出了一行。

请帮助我。

0 个答案:

没有答案