Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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 ::

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. 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.

like image 556
user2649244 Avatar asked Nov 13 '22 00:11

user2649244


1 Answers

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 );
like image 135
ChronoTrigger Avatar answered Nov 15 '22 05:11

ChronoTrigger