简体   繁体   English

OpenCV C ++绘制矩形基于两行

[英]OpenCV C++ Draw Rectangle Based on two line

I want to do a car park lot detection program for my school assignment but I am rookie on openCV and image process. 我想为我的学校作业做一个停车场检测计划,但我是openCV和图像处理的新手。

What i plan to do is using houghLine to detect the white line on the car park lot and draw a box. 我打算做的是使用houghLine来检测停车场上的白线并画一个盒子。 However, the line of the car park lot is not a complete rectangle. 但是,停车场的线路不是一个完整的矩形。

Example :: 示例::

4nFaLkI.jpg

The output i need :: 我需要的输出::

YMDuuHr.jpg

I able to use houghLine to draw the vertical line ( Red Line ) but i have no idea how to join the line ( green line ) to form a box since houghLine detect multiple point of the line, it will not detect the start point and end point of the straight line. 我能够使用houghLine绘制垂直线( 红线 )但我不知道如何加入线( 绿线 )以形成一个盒子,因为houghLine检测到多个线的点,它不会检测到起点和终点直线点。 I also try the convex hull method but i didnot manage to do it. 我也尝试凸壳方法,但我没有设法做到这一点。 Any opencv function can overcome this porlbem ?? 任何opencv函数都可以克服这个问题?

I really have no idea and hope anyone can give me some idea to solve the problem. 我真的不知道,希望有人能给我一些解决问题的想法。 Thank you. 谢谢。

Have you checked out the example in the OpenCV doc ? 你有没有在OpenCV doc中查看过这个例子? If you use the function HoughLinesP you get the 4 coordinates of the lines, so that drawing the lines is quite easy. 如果使用HoughLinesP函数, HoughLinesP可以获得线条的4个坐标,这样绘制线条非常容易。 I copy the example from the doc: 我从doc中复制了这个例子:

vector<Vec4i> lines;
HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );
for( size_t i = 0; i < lines.size(); i++ )
{
    line( color_dst, Point(lines[i][0], lines[i][1]),
        Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );
}

In vector lines you get the coordinates of all the lines in the image. 在矢量lines您可以获得图像中所有线条的坐标。 Once you have selected the two lines of the parking lot, you just need to use their coordinates to draw the new lines. 一旦选择了停车场的两条线,您只需使用它们的坐标绘制新线。 For example, if you first line is in index k1 and the second one in k2 , the code will probably be something like this: 例如,如果您的第一行是索引k1而第二行是k2 ,那么代码可能是这样的:

line( color_dst, Point(lines[k1][0], lines[k1][1]),
  Point(lines[k2][0], lines[k2][1]), Scalar(0,0,255), 3, 8 );
line( color_dst, Point(lines[k1][2], lines[k1][3]),
  Point(lines[k2][2], lines[k2][3]), Scalar(0,0,255), 3, 8 );

Regarding your question which point is e end point of a line: A line is a connection between two points. 关于你的问题哪一点是一条线的终点:一条线是两点之间的连接。 A point is described through its x,y coordinates. 通过其x,y坐标描述点。 The HoughLines Detection has as result parameter: vector lines; HoughLines Detection有结果参数:矢量线; Vec4i is a Vector of 4 integers (x1,y1,x2,y2) representing the two points of a line (start point and end point). Vec4i是4个整数的向量(x1,y1,x2,y2),表示一条线的两个点(起点和终点)。

Point pointA(lines[i][0],lines[i][1]);
Point pointB(lines[i][2],lines[i][3]);
    i represents the index of one of your lines

If you want to know which Point is where, you only have to check the coordinates between the points, for example: 如果你想知道哪个点在哪里,你只需要检查点之间的坐标,例如:

pointA.x > pointB.x or pointA.y > pointB.y

If you need a rectangle, which consists of your four lines, you can do this now. 如果你需要一个由四行组成的矩形,你现在可以这样做。 There are, as usual in image processing, many ways to get to your rectangle. 像往常一样,在图像处理中,有许多方法可以进入矩形。 One idea would be this one: 一个想法是这个:

vector<Point> RoiPoints;
RoiPoints.push_back(pointA);
RoiPoints.push_back(pointB);
 ... push all start and end points of your lines into this vector

RotatedRect rotRect = minAreaRect(Mat(RoiPoints));
 ... the RotatedRect fits around all points in your vector

If you would like to draw your RotatedRect you can use this function of myself: 如果你想绘制你的RotatedRect,你可以使用我自己的这个功能:

void drawRotRect(Mat& img, RotatedRect& rect) 
{
    Point2f rect_points[4];
    rect.points(rect_points);

    //draw rotated rect
    for (int j = 0; j < 4; j++)
    line(img, rect_points[j], rect_points[(j + 1) % 4],Scalar(0,0,255),1, 8);
}

call this function with: 调用此函数:

drawRotRect(img,rotRect);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM