OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Sat, 27 Feb 2016 22:24:36 -0600Intersection of a contour and line in OpenCV c++?http://answers.opencv.org/question/88752/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;
}
}
}
}
Sat, 27 Feb 2016 07:47:06 -0600http://answers.opencv.org/question/88752/intersection-of-a-contour-and-line-in-opencv-c/Answer by Guyygarty for <p>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.</p>
<p><strong>Step1</strong>:- I created an image of zeros with the contour detected in the image.</p>
<p><strong>Step2</strong>: - Then i created another image of zeros with just the line this time.</p>
<p><strong>Step3</strong>:- Then i took the bitwise AND between these two image and i get the common pixel intersection between these two images.</p>
<p><strong>Step4</strong>:- My idea was now if the pixel value in the new image is greater than 0 then i increment the count variable by 1. </p>
<p>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.</p>
<pre><code>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;
}
}
}
</code></pre>
<p>}</p>
http://answers.opencv.org/question/88752/intersection-of-a-contour-and-line-in-opencv-c/?answer=88785#post-id-88785Your life may be easier if the line corresponded to a row of the image. That way you just do:
drawContours( drawing1, contours, i, Scalar(255,0,0), -2, 8, hierarchy, 0, Point() );//note that thickness is negative - that fills the contour.
if (countNonZero(Drawing1.row(300))>0)
{
//car #i is crossing row 300
}
The problem with this type of algorithm is that you would need to keep track that you don't count the same car in the next frame.
If only one car at a time can cross the line, you would just need some sort of flag that only allows the counter to increment if there weren't any lit pixels on the line in the previous frame. If multiple cars can cross the line simultaneously, you will need to correlate between car i in one frame and car j in the other. This is much harder
If the cars are relatively slow you could just look at the blob centroids and find the nearest one in the previous frame or maybe one that has moved in a predefined direction. If they are fast (i.e. motion between frames is larger than average distance between cars) you may need a more complex algorithm.
guy
Sat, 27 Feb 2016 22:24:36 -0600http://answers.opencv.org/question/88752/intersection-of-a-contour-and-line-in-opencv-c/?answer=88785#post-id-88785Answer by LBerger for <p>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.</p>
<p><strong>Step1</strong>:- I created an image of zeros with the contour detected in the image.</p>
<p><strong>Step2</strong>: - Then i created another image of zeros with just the line this time.</p>
<p><strong>Step3</strong>:- Then i took the bitwise AND between these two image and i get the common pixel intersection between these two images.</p>
<p><strong>Step4</strong>:- My idea was now if the pixel value in the new image is greater than 0 then i increment the count variable by 1. </p>
<p>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.</p>
<pre><code>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;
}
}
}
</code></pre>
<p>}</p>
http://answers.opencv.org/question/88752/intersection-of-a-contour-and-line-in-opencv-c/?answer=88755#post-id-88755I think you can use lineiterator (may be there is another way to count car)
for (int i=0;i <contour.size();i++)
drawContours( drawing1, contours, i, Scalar(i+1), 1 ); // contour.size() must be less than 254
cv::LineIterator it(drawing1, Point(0,10),Point(600,300), 4);
cv::LineIterator it2 = it;
vector <int> carCounter(contours.size()); //
for (int i=0;i <carCounter.size();i++)
carCounter[i]=0;
for(int nbPt = 0; nbPt < it.count; nbPt++, ++it)
{
if (drawing1.at<uchar>(it.pos()) != 0)
{
carCounter[i]++;
}
}
int nbCar=0;
for (int i=0;i<carCounter.size();i++)
if (carCounter[i]!=0)
nbCar++;
cout<<nbCar << "cross line\n";
in carCounter number nonzero values is equal to number of car which shapes crossed the red line...
PS I haven't check this programSat, 27 Feb 2016 10:02:30 -0600http://answers.opencv.org/question/88752/intersection-of-a-contour-and-line-in-opencv-c/?answer=88755#post-id-88755Comment by sam001 for <p>I think you can use lineiterator (may be there is another way to count car)</p>
<pre><code>for (int i=0;i <contour.size();i++)
drawContours( drawing1, contours, i, Scalar(i+1), 1 ); // contour.size() must be less than 254
cv::LineIterator it(drawing1, Point(0,10),Point(600,300), 4);
cv::LineIterator it2 = it;
vector <int> carCounter(contours.size()); //
for (int i=0;i <carCounter.size();i++)
carCounter[i]=0;
for(int nbPt = 0; nbPt < it.count; nbPt++, ++it)
{
if (drawing1.at<uchar>(it.pos()) != 0)
{
carCounter[i]++;
}
}
int nbCar=0;
for (int i=0;i<carCounter.size();i++)
if (carCounter[i]!=0)
nbCar++;
cout<<nbCar << "cross line\n";
</code></pre>
<p>in carCounter number nonzero values is equal to number of car which shapes crossed the red line...</p>
<p>PS I haven't check this program</p>
http://answers.opencv.org/question/88752/intersection-of-a-contour-and-line-in-opencv-c/?comment=88768#post-id-88768@Lberger- Thanks a lot for your reply. But this is what its happening so far. The carCounter vector gives me the total number of cars in the frame rather than the number of cars passing through the line. How do i fix it?Sat, 27 Feb 2016 14:02:32 -0600http://answers.opencv.org/question/88752/intersection-of-a-contour-and-line-in-opencv-c/?comment=88768#post-id-88768