Using OpenCV to detect parking spots

I am trying to use opencv to automatically search and search for all parking spaces in an empty parking lot.

I currently have code that spawns an image, applies canny edge detection, and then uses hough probabilistic strings to find the lines that mark each parking spot.

The program then draws the lines and points that make up the lines.

Here is the code:

#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> using namespace cv; using namespace std; int threshold_value = 150; int threshold_type = 0;; int const max_value = 255; int const max_type = 4; int const max_BINARY_value = 255; int houghthresh = 50; char* trackbar_value = "Value"; char* window_name = "Find Lines"; int main(int argc, char** argv) { const char* filename = argc >= 2 ? argv[1] : "pic1.jpg"; VideoCapture cap(0); Mat src, dst, cdst, tdst, bgrdst; namedWindow( window_name, CV_WINDOW_AUTOSIZE ); createTrackbar( trackbar_value, window_name, &threshold_value, max_value); while(1) { cap >> src; cvtColor(src, dst, CV_RGB2GRAY); threshold( dst, tdst, threshold_value, max_BINARY_value,threshold_type ); Canny(tdst, cdst, 50, 200, 3); cvtColor(tdst, bgrdst, CV_GRAY2BGR); vector<Vec4i> lines; HoughLinesP(cdst, lines, 1, CV_PI/180, houghthresh, 50, 10 ); for( size_t i = 0; i < lines.size(); i++ ) { Vec4i l = lines[i]; line( bgrdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,255,0), 2, CV_AA); circle( bgrdst, Point(l[0], l[1]), 5, Scalar( 0, 0, 255 ), -1, 8 ); circle( bgrdst, Point(l[2], l[3]), 5, Scalar( 0, 0, 255 ), -1, 8 ); } imshow("source", src); imshow(window_name, bgrdst); waitKey(1); } return 0; } 

My main problem right now is figuring out how to extrapolate the line data to find the locations of each parking spot. My goal is to have opencv find parking spots and draw rectangles on each parking spot with a number designating spots.

I think there are some serious problems with the method I use, because, as shown in the output images, opencv detects multiple points on a line other than the two endpoints. This can make it difficult to use opencv to connect 2 neighboring endpoints.

I read something about using a convex hull, but I'm not quite sure what it does and how it works.

Any help would be appreciated. Here are the output images from my program: http://imageshack.us/photo/my-images/22/test1hl.png/

http://imageshack.us/photo/my-images/822/test2lw.png/

+7
source share
1 answer

Consider thinning your binary image, and then determine the endpoints and branch points. Here is one such result based on the images provided; endpoints are in red, and branch points are in blue.

enter image description here

Now you can find parking spots. A pair of blue dots is always connected by one edge. Each blue dot is connected to two or three red dots. Then there are several ways to find a parking space formed by two blue dots and two red dots, the simplest is the lines: find the nearest pair of red dots, where one dot is connected to a specific blue dot and the other red dot is connected to another blue dot. This step can also be supplemented by checking how close the edges are to the parallel lines.

+2
source

All Articles