如何在OpenCV(Java)中使用HoughLines检测线? [英] How to detect lines using HoughLines in OpenCV (Java)?

查看:150
本文介绍了如何在OpenCV(Java)中使用HoughLines检测线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用OpenCV中的HoughLines检测图像上是否已部署了跨界屏障.我以为只要障碍出现在代码中,代码就会在图像上画一条线-但是,我收到一条错误消息,说"Mat数据类型不兼容".可以告诉我如何使用OpenCV检测Java中的行吗?

I'm currently trying to detect whether a level crossing barrier has been deployed on an image using HoughLines in OpenCV. I thought my code would draw a line on my image, so long as the barrier appears in it - but instead I get an error message saying "Mat data type is not compatible". Can show me how to detect lines in Java with OpenCV?

public class DetectLines {

public static void main(String args[]) {

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    Mat eventless = Highgui.imread("files/eventless.png");
    Mat barrier = Highgui.imread("files/barrier/lc-00201.png");
    Mat difference = new Mat();
    Mat lines = new Mat();

    Core.absdiff(eventless, barrier, difference);

    Mat grey = new Mat();
    Imgproc.cvtColor(difference, grey, Imgproc.COLOR_BGR2GRAY);

    Imgproc.HoughLines(grey, lines, 5.0, 4.0, 7);

    Imshow ims1 = new Imshow("Lines");
    ims1.showImage(lines);

}

}

推荐答案

在行矩阵中返回行,该行矩阵有两列,每行中都返回极坐标的rho和theta参数.这些坐标分别是指图像左上角与直线弧度之间的距离.因此,为了使用show方法,您必须创建一个Mat,该Mat将使用线坐标表示线.

The lines are returned in the lines matrix, which has two columns in which each line returns the rho and theta parameters of the polar coordinates. These coordinates refer to the distance between the top-left corner of the image and the line rotation in radians, respectively. So, in order to use the show method you must create a Mat that will represent the lines using the line coordinates.

  public Mat getHoughTransform(Mat image, double rho, double theta, int threshold) {
    Mat result = Image.getInstance().getImage().clone();
    Mat lines = new Mat();
    Imgproc.HoughLines(image, lines, rho, theta, threshold);

    for (int i = 0; i < lines.cols(); i++) {
        double data[] = lines.get(0, i);
        double rho1 = data[0];
        double theta1 = data[1];
        double cosTheta = Math.cos(theta1);
        double sinTheta = Math.sin(theta1);
        double x0 = cosTheta * rho1;
        double y0 = sinTheta * rho1;
        Point pt1 = new Point(x0 + 10000 * (-sinTheta), y0 + 10000 * cosTheta);
        Point pt2 = new Point(x0 - 10000 * (-sinTheta), y0 - 10000 * cosTheta);
        Imgproc.line(result, pt1, pt2, new Scalar(0, 0, 255), 2);
    }
    return result;
}

这是一种更简单的方法.它涉及Imgproc.HoughLinesP方法.

And this is a easier way to do it. It involve the Imgproc.HoughLinesP method.

public Mat getHoughPTransform(Mat image, double rho, double theta, int threshold) {
    Mat result = Image.getInstance().getImage().clone();
    Mat lines = new Mat();
    Imgproc.HoughLinesP(image, lines, rho, theta, threshold);

    for (int i = 0; i < lines.cols(); i++) {
        double[] val = lines.get(0, i);
        Imgproc.line(result, new Point(val[0], val[1]), new Point(val[2], val[3]), new Scalar(0, 0, 255), 2);
    }
    return result;
}

这篇关于如何在OpenCV(Java)中使用HoughLines检测线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆