如何在HoughLinesp之后找出哪些坐标属于哪些线?

时间:2017-02-15 05:29:40

标签: c++ opencv image-processing computer-vision

我使用VS 15在OpenCV中实现了Houghlinesp。代码如下 -

#include "stdafx.h"


#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <math.h>
#include <iostream>

using namespace cv;
using namespace std;

static void help()
{
    cout << "\nThis program demonstrates line finding with the Hough transform.\n"
        "Usage:\n"
        "./houghlines <image_name>, Default is pic1.png\n" << endl;
}

int main(int argc, char** argv)
{
    const char* filename = argc >= 2 ? argv[1] : "Turbine.jpg";

    Mat src = imread(filename, 0);
    if (src.empty())
    {
        help();
        cout << "can not open " << filename << endl;
        return -1;
    }

    Mat dst, cdst;
    Canny(src, dst, 50, 200, 3);
    cvtColor(dst, cdst, CV_GRAY2BGR);
    vector<Vec4i> lines;
    HoughLinesP(dst, lines, 1, CV_PI / 180, 50, 110, 10);

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

        Point p1, p2;
        p1 = Point(l[0], l[1]);
        p2 = Point(l[2], l[3]);
        //calculate angle in radian,  if you need it in degrees just do angle * 180 / PI
        double angle = atan2(p1.y - p2.y, p1.x - p2.x);
        double angles = angle * 180 / 3.14159265358979323846;
        cout << "line coordinates are " << l << endl;
        cout << "Angles are " << angles << endl;
    }

    imshow("source", src);
    imshow("detected lines", cdst);


    waitKey();

    return 0;
}

我得到以下输出 - Image after Houghlinesp Coordinates and angles obtained

  

我怎么知道哪条线是哪条线?有可能这样做吗? 或许我可以将更近的行分组给我只有三个刀片行,是否可以使用它们的代码片段?

     

另外,我不了解已计算的角度。 [我想要相对于水平线的角度]。任何人都可以帮我理解这个吗?

     

有没有什么方法可以限制发现只有刀片线​​的线条(不限制找到的垂直线条,因为在另一个例子中刀片也可以处于垂直位置)

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

HugeLinesP()计算的线给出了定义线的两个点。从那里你可以计算函数f(x) = k*x + n,它是一条线的定义。 (只需用y代替f(x),用x代替x,你得到一个带有两个变量的两个方程组)。

cv :: Mat的坐标系在左上角以0,0开头。在通用坐标系中,我们使用x从左到右生长,y从下到上生长,这里我们有x仍然从左到右生长,但y反向生长,从上到下生长。这会影响角度计算。

对于你的上一个问题,我帮不了你。首先,在进行Huges变换之前,你必须以某种方式从图像中移除极点。如果你唯一感兴趣的是角度,你可以收集一些小间隔的所有角度并计算平均角度。如果你试图在一系列图像中跟踪刀片,你可以推测一个刀片的角度在两张图片之间不会发生很大的变化(这意味着你的相机必须至少拍摄两张图片,然后才能让刀片达到最快的60度旋转)。

希望这有帮助

相关问题