Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Draw line along countours path (moving object)

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