#include<cv.h> #include<highgui.h> #include<iostream> using namespace std; int main() { int i = 0; int mode = CV_RETR_EXTERNAL; int contours_num = 0; CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* contour = 0; IplImage* bin_img = cvLoadImage ("test.jpg", 1); cvThreshold (bin_img, bin_img, 128, 255, CV_THRESH_BINARY); IplImage* pContourImg = cvCreateImage (cvGetSize(bin_img), IPL_DEPTH_8U, 3); cvNamedWindow ("bin_img", 1); cvShowImage ("bin_img", bin_img); mode = CV_RETR_EXTERNAL; contours_num = cvFindContours (bin_img, storage, &contour, sizeof(CvContour), mode, CV_CHAIN_APPROX_NONE); cout << "检测出的轮廓数目:" << contours_num << " " << endl; CvSeqReader reader; int count = 0; if (contour != 0) { count = contour->total; cout << count << endl; } cvStartReadSeq (contour, &reader, 0); CvPoint pt1; CvScalar color = CV_RGB(255,0,0); cvNamedWindow ("contour", 1); cvShowImage ("contour", pContourImg); for (i = 0; i < count; i++) { CV_READ_SEQ_ELEM(pt1, reader); cvCircle (pContourImg, pt1, 1, color); } cvNamedWindow ("contours", 1); cvShowImage ("contours", pContourImg); cvWaitKey (0); cvReleaseImage (&bin_img); cvReleaseImage (&pContourImg); cvReleaseMemStorage (&storage); return 0; }