Reputation: 111
I want to do a car park lot detection program for my school assignment but I am rookie on openCV and image process.
What i plan to do is using houghLine to detect the white line on the car park lot and draw a box. However, the line of the car park lot is not a complete rectangle.
Example ::
The output i need ::
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. I also try the convex hull method but i didnot manage to do it. Any opencv function can overcome this porlbem ??
I really have no idea and hope anyone can give me some idea to solve the problem. Thank you.
Upvotes: 6
Views: 5643
Reputation: 1398
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. The HoughLines Detection has as result parameter: vector lines; Vec4i is a Vector of 4 integers (x1,y1,x2,y2) representing the two points of a line (start point and end point).
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:
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);
Upvotes: 0
Reputation: 8607
Have you checked out the example in the OpenCV doc? If you use the function HoughLinesP
you get the 4 coordinates of the lines, so that drawing the lines is quite easy. I copy the example from the 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. 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:
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 );
Upvotes: 2