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:
follows:
Mat src=imread("773.jpg");
Mat roi=src(Rect(5,400,1350,400));(same width but half the height)
Mat src1=roi.clone();
cvtColor(roi,roi,CV_BGR2HSV);
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;
inRange(roi,min,max,dst);
imshow("roi",dst);
Mat gray_roi;
cvtColor(roi,gray_roi,CV_BGR2GRAY);
bitwise_and(gray_roi,dst,gray_roi);
imshow("gray",gray_roi);
dilate(gray_roi, gray_roi, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
imshow("gray_dilate",gray_roi);
vector<vector<point> vector<vector<Point> > contours;
vector<vec4i> vector<Vec4i> hierarchy;
/// Find contours
findContours( gray_roi, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
vector<rect> vector<Rect> boundRect( contours.size() );
for( int i = 0; i< contours.size(); i++ )
{
boundRect[i] = boundingRect( Mat(contours[i]) );
area=boundRect[i].width*boundRect[i].height;
if(area>40)//(3.0<=aspect_ratio || (
{
count++;
rectangle( src1, boundRect[i].tl(), boundRect[i].br(), Scalar(255,0,0), 2, 8, 0 );
imshow( "Contours1", src1 );
waitKey(0);
std::cout<<"index values"<<boundrect[i].x<<" "<<boundrect[i].y;="" index.push_back(i);<="" p="">
values"<<boundRect[i].x<<" "<<boundRect[i].y;
index.push_back(i);
}
}
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.