Ask Your Question
1

Draw line along countours path (moving object)

asked 2017-04-10 06:38:42 -0500

rabi gravatar image

I have code for moving object . It finds contours and draw rectangle. Now i want to detect the path traversed by moving object i.e. contours. And finally drew a line along with contours movement . Here is function for drawing of rectangle

static void refineSegments(  const Mat& img, Mat& mask, Mat& dst)
         {
 int niters = 3;

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
    Mat temp;

GaussianBlur(mask, temp, Size(11, 11), 3.5, 3.5);
threshold(temp, temp, 100, 255, THRESH_BINARY); //for web camera
dilate(temp, temp, Mat(), Point(-1,-1), niters);
erode(temp, temp, Mat(), Point(-1,-1), niters*2);
dilate(temp, temp, Mat(), Point(-1,-1), niters*2);

imshow("dilated", temp);


findContours( temp, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE );

vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );

for( int i = 0; i < contours.size(); i++ )
{
    vector<Point>contour = contours[i];
    double area0 = contourArea(contour);
    if( area0 > 5000)   
        {
    approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
    boundRect[i] = boundingRect( Mat(contours_poly[i]) );
        }

}

dst = Mat::zeros(img.size(), CV_8UC3);
if( contours.size() == 0 )
    return;

int idx = 0, largestComp = 0;
double maxArea = 0;

cout<<"Size of contours after removal:"<<contours.size()<<endl;  ///SIZE OF CONTOUR AFTER REMOVAL OF SMALL CONTOURS
for( int i = 0; i< contours.size(); i++ )
{
    Scalar color(0,0,255 );
    vector<Point>test_contour=contours[i];

    rectangle( img, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
            //line(img, contours[i], contours[i+1], color, 2, 8, 0 );
           //printf(contours[i]);
}}

Could you change it so that it draw a line along moving contour . Thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-04-10 06:48:54 -0500

Could you change it so that it draw a line along moving contour

No we can not. This forum is a forum where you get suggestions on what to do, but we will not code your problem for you.

As to how to tackle this, when looping over your contours, you can retrieve the center of mass of the contour, by using this partial code.

Moments mu = moments(contour[i]);
Point centroid = cv::Point (mu.m10/mu.m00 , mu.m01/mu.m00);

Do this for each contour and push all the points retrieve into a vector<Point>. Now just draw a line between each subsequent point using the line function.

edit flag offensive delete link more

Comments

Ya, you are write . Now , when I try your way , Still I am getting wrong output . Reason I am not able to uniquely identify each contour and hence it draws line from one contour to other one . Here is code I try

   vector<Point>centroid_points;
    int largest;
  for( int i = 0; i < contours.size(); i++ ){
for( int i = 0; i < contours.size(); i++ ){
Moments mu = moments(contours[i]);
  Point centroid = cv::Point (mu.m10/mu.m00 , mu.m01/mu.m00);
  centroid_points.push_back(centroid);
  largest =i;
  } 
    line(img,centroid_points[0],centroid_points[largest],Scalar(255, 0, 0), 2, 8, 0);
}
rabi gravatar imagerabi ( 2017-04-11 04:53:07 -0500 )edit

I am not able to uniquely identify each contour --> how are you matching contours over consecutive frames? What I would suggest is to do a euclidean distance between centers as a starter and match the closest ones. If that does not work, you will need to apply a tracker on top of your data like a Kalman filter.

StevenPuttemans gravatar imageStevenPuttemans ( 2017-04-11 06:02:32 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-04-10 06:38:42 -0500

Seen: 788 times

Last updated: Apr 10 '17