Intersection of a contour and line in OpenCV c++?
Hi, I am trying to count the number of cars that passes through a line in opencv c++. I have drawn a line on my frame at a specific point and my idea is every time , a car passes through the line, the count variable will be incremented by 1. So, this is what i did.
Step1:- I created an image of zeros with the contour detected in the image.
Step2: - Then i created another image of zeros with just the line this time.
Step3:- Then i took the bitwise AND between these two image and i get the common pixel intersection between these two images.
Step4:- My idea was now if the pixel value in the new image is greater than 0 then i increment the count variable by 1.
So, if i do these then i get a huge value of count. This seems absurd and i would like to know how i correct the error. This is my code so far. Please help me out.
Mat drawing1 = Mat::zeros(resize_blur_Img.size(), CV_8UC1 );
Mat drawing2 = Mat::zeros(resize_blur_Img.size(),CV_8UC1);
Mat res;
for( int i = 0; i < contours.size(); i++ )
{
mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 );
drawContours( drawing1, contours, i, Scalar(255,0,0), 2, 8, hierarchy, 0, Point() );
line(drawing2,Point(0,10),Point(600,300),Scalar(255,0,0),3);
bitwise_and(drawing1,drawing2,res);
for(int i =0 ;i <res.rows;i++)
{
for(int j=0; j<res.cols;j++)
{
if(res.at<uchar>(i,j) > 0)
{
found = true;
count ++;
cout<<count<<endl;
}
else
{
found = false;
// cout<<found<<endl;
}
}
}
}