Ask Your Question

detecting car from moving vehicle

asked 2015-10-15 06:44:54 -0600

OpenCV_Learner gravatar image

updated 2015-10-16 03:40:34 -0600

I want to detect car from a moving vehicle by finding the rear light. I have extracted red color and using findContour detected them too, now how do I pair the lights? Is it by thresholding the distance between the red contours detected? Can anyone suggest the method for this thresholding?

My code is as follows:

            Mat src=imread("773.jpg");
Mat roi=src(Rect(5,400,1350,400));(same width but half the height)
Mat src1=roi.clone();
cv::Scalar   min(129, 150, 10);//min(220/2, 0, 0); Hue=140-179(red),
cv::Scalar   max(179,255, 255);//max(260/2,255, 255);
Mat dst;
Mat gray_roi;
dilate(gray_roi, gray_roi, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/// Find contours
findContours( gray_roi, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
vector<Rect> boundRect( contours.size() );
            for( int i = 0; i< contours.size(); i++ )
    boundRect[i] = boundingRect( Mat(contours[i]) );
                            if(area>40)//(3.0<=aspect_ratio || (
    rectangle( src1, boundRect[i].tl(), boundRect[i].br(), Scalar(255,0,0), 2, 8, 0 ); 
    imshow( "Contours1", src1 );
    std::cout<<"index values"<<boundRect[i].x<<"  "<<boundRect[i].y;

           for( int i = 0; i< boundRect.size()-1; i++ )
              if((boundRect[i+1].y==boundRect[i].y-1)||(boundRect[i+1].y==boundRect[i].y)||(boundRect[i+1].y==boundRect[i].y+1))//(3.0<=aspect_ratio || (
    rectangle( src1, boundRect[i+1].tl(), boundRect[i].br(), Scalar(255,0,0), 2, 8, 0 ); 
    imshow( "Contours1", src1 );

imshow( "Contours", src1 );

Kindly tell me how to pair the contours for rear lights and detect vehicle.

edit retag flag offensive close merge delete


I would suggest you to try something and show the results, like this you can also help us better understand your case, and suggest you the more appropriate approach.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-10-15 07:27:55 -0600 )edit

Can you post the code in the question, by editing it, it is hard to read it?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-10-16 02:27:59 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-10-16 03:59:41 -0600

Some suggestions for pairing

  • Try to define an average horizontal distance between back car lights for your setup over a range of cars.
  • Increase these dimensions to a lower and upper border by reducing and increasing it with lets say 20% of size.
  • Now loop over blobs, calculate their centers and store them somewhere.
  • Start with center 1, now look if there is a center of which the y coordinate is somewhere on the same height as the one of the center you are processing. Then look at the x distance between both points. Look if it falls in between your borders set at step 2.
  • If you find a pair, delete both blobs/centers from your search structure.
  • Continue this iteration untill all blobs are matched or not, but at least all processed.

I am pretty sure that this will work.

edit flag offensive delete link more


Thank you for these steps, I shall implement and shall share the output.

OpenCV_Learner gravatar imageOpenCV_Learner ( 2015-10-16 06:55:11 -0600 )edit

Question Tools

1 follower


Asked: 2015-10-15 06:44:54 -0600

Seen: 1,006 times

Last updated: Oct 16 '15