#include #ifdef _CH_ #pragma package #endif #ifndef _EiC #include "cv.h" #include "highgui.h" #include #endif int sizeX, sizeY; int levels = 1; CvSeq* contours = 0; IplImage* output = 0; void on_change(int pos) { // config IplImage* img2 = cvCreateImage( cvSize(sizeX, sizeY), 8, 3 ); CvSeq* _contours = contours; int _levels = levels; // draw image printf("\tcontour level %i \t[ok]\n", pos); cvZero(img2); cvDrawContours(img2, _contours, CV_RGB(255,0,0), CV_RGB(0,255,0), _levels, 1, CV_AA, cvPoint(0,0) ); cvShowImage("img_contour", img2); output = cvCloneImage(img2); cvReleaseImage(&img2); } int main(int argc, char** argv) { int i, j; CvMemStorage* memStorage = cvCreateMemStorage(0); // open image IplImage* img = 0; char* filename = argc == 2 ? argv[1] : (char*)"contours-sample.bmp"; if( (img = cvLoadImage(filename,0)) == 0 ) { printf("open image \t\t\t[failed]\n"); exit(0); } else { printf("open image \t\t\t[ok]\n"); } sizeX = img->width; sizeY = img->height; // show orginal image printf("show orginal \t\t\t[ok]\n"); cvNamedWindow("img_base", 1); cvShowImage("img_base", img); cvWaitKey(0); // find contours printf("find contours \t\t\t[ok]\n"); cvFindContours(img, memStorage, &contours, sizeof(CvContour), CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) ); // show contours image cvNamedWindow("img_contour", 1); cvCreateTrackbar("levels", "img_contour", &levels, 15, on_change); on_change(levels); cvWaitKey(0); cvDestroyWindow("img_base"); cvDestroyWindow("img_contour"); // save image printf("save image \t\t\t[ok]\n"); cvSaveImage("contours-output.bmp", output); cvWaitKey(0); // ending program printf("exit program \t\t\t[ok]\n"); cvReleaseMemStorage(&memStorage); cvReleaseImage(&img); return 0; } #ifdef _EiC main(1,""); #endif