如何在Android上从HoughLinesP(openCV)获得多行?

问题描述 投票:0回答:2

我想从此过程中获取多行。

override fun onCameraFrame(inputFrame: CvCameraViewFrame): Mat? {
    mRgba = inputFrame.rgba()

    // generate gray scale and blur
    Imgproc.cvtColor(mRgba, mRgbaFiltered, Imgproc.COLOR_BGR2GRAY)
    Imgproc.blur(mRgbaFiltered, mRgbaFiltered, Size(3.0, 3.0))

    // detect the edges
    val mMat = Mat()
    val ratio = 4

    // Find Canny Edges
    Imgproc.Canny(mRgbaFiltered, mMat, threshold, threshold * ratio.toDouble())

    // Find horizontal lines
    val lines = Mat()
    Imgproc.HoughLinesP(mMat, lines, 1.0, Math.PI / 180, 80, 100.0, 10.0)

    // Change Color to RGB
    Imgproc.cvtColor(mMat, mMat, Imgproc.COLOR_GRAY2BGR)

    // draw lines
    for (i in 0 until lines.cols()) {
        val `val` = lines[0, i]
        Imgproc.line(
            mMat,
            Point(`val`[0], `val`[1]),
            Point(`val`[2], `val`[3]),
            Scalar(0.0, 255.0, 0.0),
            1
        )
    }

    num_lines = lines.cols()
    updateHandler.postDelayed(updateRunnable, 500)

    return mMat
}

但是,我只能得到一行,如下所示。我需要画所有的线。然后我该怎么做?

enter image description here

android opencv kotlin hough-transform
2个回答
0
投票

我刚刚更改了houghline和canny参数。

这是我的代码:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;

int main()
{
    Mat img = imread("/ur/image/directory/lines.png");

    imshow("Source",img);

    Mat dst, cdst;
    Canny(img, dst, 80, 250);
    cvtColor(dst, cdst, CV_GRAY2BGR);

    vector<Vec4i> lines;
    HoughLinesP(dst, lines, 1.0, CV_PI/180, 80, 100.0, 10.0 );
    for( size_t i = 0; i < lines.size(); i++ )
    {
        Vec4i l = lines[i];
        line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, CV_AA);
    }
    imshow("Output", cdst);

    waitKey(0);
    return 0;
}

结果:

enter image description here


0
投票

您需要将其更改为..

cols->行

lines [0,i]-> lines [i,0]

// draw lines
for (i in 0 until lines.rows()) {
    val `val` = lines[i, 0]
    Imgproc.line(
        mMat,
        Point(`val`[0], `val`[1]),
        Point(`val`[2], `val`[3]),
        Scalar(0.0, 255.0, 0.0),
        1
    )
}
© www.soinside.com 2019 - 2024. All rights reserved.