Reputation: 47
I have made a blue line using OpenCV and Houghlines (in c++) and I was wondering what to include in an if statement that is supposed to detect if the blue line exists and if it does perform an action.
I was trying with the cv::inRange function to detect the blue, but I cannot use it in the if statement.
Here is the code to draw the blue lines:
vector<Vec2f> lines;
HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 );
for(size_t i = 0;i < lines.size();i++)
{
float rho = lines[i][0], theta = lines[i][1];
//scan only for horizontal line)
if(theta > CV_PI/180*80 && theta < CV_PI/180*100){
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
//draw the line in blue
cvLine(m_image, pt1, pt2, Scalar(255,0,0), 3, CV_AA);
}
}
}
And basically what I want to do is
if (blue line exists) {
do something
}
Here is the current image I am getting. (Ignore the green and red line, it is basically POV of car on the road) I am suppose to detect when blue line get in the lower part of the image (right in front of the picture) then perform the action (make the car stop)
Thank you for your time.
PS. Thanks Saransh Kejriwal, I am trying with the Flag now.
Upvotes: 2
Views: 129
Reputation: 2533
There are 2 ways you can follow:
1) set a flag when you draw a blue line:
Everytime you draw a blue line, set an int flag=1.
drawline();
flag=1;
This can be checked in the 'if' condition. So when flag is 1, that means you've drawn a blue line earlier. It is much neater, if it suits your requirement.
2) design a blue colour filter In case you've drawn the blue line before you're calling the 'if' statement, here's the algorithm you follow, -design a blue colour mask using inRange, and save the mask on the different Mat object (Mat mask;) - Apply findContours on mask. Check contour area of each contour that you obtained. - In case you have drawn a line, one of the contours will have a significant area, then you can apply if condition as:
if(contourArea(contours[i])>some_threshold_value)
{
//your code here
}
Upvotes: 1