contour.cpp
上传用户:banwdc
上传日期:2016-06-25
资源大小:2871k
文件大小:1k
- #include "cv.h"
- #include "highgui.h"
- int main( int argc, char** argv )
- {
- IplImage* src;
- // 第一条命令行参数确定了图像的文件名。
- if( argc == 2 && (src=cvLoadImage(argv[1], 0))!= 0)
- {
- IplImage* dst = cvCreateImage( cvGetSize(src), 8, 3 );
- CvMemStorage* storage = cvCreateMemStorage(0);
- CvSeq* contour = 0;
- cvThreshold( src, src, 1, 255, CV_THRESH_BINARY );
- cvNamedWindow( "Source", 1 );
- cvShowImage( "Source", src );
- cvFindContours( src, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
- cvZero( dst );
- for( ; contour != 0; contour = contour->h_next )
- {
- CvScalar color = CV_RGB( rand()&255, rand()&255, rand()&255 );
- /* 用1替代 CV_FILLED 所指示的轮廓外形 */
- cvDrawContours( dst, contour, color, color, -1, CV_FILLED, 8 );
- }
- cvNamedWindow( "Components", 1 );
- cvShowImage( "Components", dst );
- cvWaitKey(0);
- }
- }