Ask Your Question

Revision history [back]

You can use polylines or draw contour by lines.

int main( int argc, char** argv )
{
    Mat img = imread("c:\\sample.bmp",0);
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    imshow("org imgae",img);
    findContours( img, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_TC89_L1 );

    Mat newImg;
    img.copyTo(newImg);
    newImg.setTo(0);

    Scalar color( rand()&255, rand()&255, rand()&255 );

    for( size_t k = 0; k < contours.size(); k++ )
        approxPolyDP(Mat(contours[k]), contours[k], 3, true);

    cout << contours.size() << endl;
    for (size_t k=0;k < contours.size();k++)
        for (size_t i=1;i < contours[k].size();i++)
        cv::line(newImg, contours[k][i],contours[k][i-1],color,1);
        //cout << contours[k][i] << endl;


imshow("view",newImg);
waitKey(0);

    return 0;
}

image description

You can use polylines or draw contour by lines.

int main( int argc, char** argv )
{
    Mat img = imread("c:\\sample.bmp",0);
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    imshow("org imgae",img);
    findContours( img, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_TC89_L1 );

    Mat newImg;
    img.copyTo(newImg);
    newImg.setTo(0);

    Scalar color( rand()&255, rand()&255, rand()&255 );

    for( size_t k = 0; k < contours.size(); k++ )
        approxPolyDP(Mat(contours[k]), contours[k], 3, true);

    cout << contours.size() << endl;
    for (size_t k=0;k < contours.size();k++)
        for (size_t i=1;i < contours[k].size();i++)
        cv::line(newImg, contours[k][i],contours[k][i-1],color,1);
        //cout << contours[k][i] << endl;


imshow("view",newImg);
waitKey(0);

    return 0;
}

image descriptionimage description