typedef void (CV_CDECL *CvTrackbarCallback)(int pos);
/* create trackbar and display it on top of given window, set callback */ CVAPI(int) cvCreateTrackbar( const char* trackbar_name, const char* window_name, int* value, int count, CvTrackbarCallback on_change CV_DEFAULT(NULL));
程序:首先创建一个窗口用于显示图像,滑动条(trackbar)用于设置阈值,然后对二值化后的图像提取轮廓并绘制轮廓。当控制参数的滑动条变化时,图像被更新
IplImage *g_image=NULL; IplImage *g_gray=NULL; int g_thresh=100; CvMemStorage *g_storage=NULL; void on_trackbar(int){ if(g_storage == NULL) { g_gray=cvCreateImage(cvGetSize(g_image), 8, 1); g_storage=cvCreateMemStorage(0); } else { cvClearMemStorage(g_storage); } CvSeq *contours=0; cvCvtColor(g_image, g_gray, CV_BGR2GRAY); cvThreshold(g_gray, g_gray, g_thresh, 255, CV_THRESH_BINARY); cvFindContours(g_gray, g_storage, &contours); cvZero(g_gray); if (contours) cvDrawContours(g_gray, contours, cvScalarAll(255), cvScalarAll(255), 100); cvShowImage("Contours", g_gray); } int main(void) { g_image = cvLoadImage("lena.jpg"); cvNamedWindow("Contours"); cvCreateTrackbar("Threshold", "Contours", &g_thresh, 255, on_trackbar); on_trackbar(0); cvWaitKey(0); return 0; }