例:根據(jù)滑動條參數(shù)檢測輪廓,在滑動條變化時候重新檢測
效果圖:

-
-
-
-
-
-
-
-
-
- #include "stdafx.h"
- #include "cv.h"
- #include "highgui.h"
-
- IplImage* g_img = NULL;
- IplImage* g_gray = NULL;
- int g_thresh = 100;
- CvMemStorage* g_storage = NULL;
-
-
- void on_trackbar(int pos)
- {
- if (g_storage == NULL)
- {
- g_gray = cvCreateImage(cvGetSize(g_img), 8, 1);
- g_storage = cvCreateMemStorage();
- }
- else
- {
- cvClearMemStorage(g_storage);
- }
-
- cvCvtColor(g_img, g_gray, CV_BGR2GRAY);
-
- cvThreshold(g_gray, g_gray, g_thresh, 255, CV_THRESH_BINARY);
-
-
-
- CvSeq* contours = 0;
-
- 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()
- {
- cvNamedWindow("g_img", CV_WINDOW_AUTOSIZE);
- g_img = cvLoadImage("lena.jpg");
- cvShowImage("g_img", g_img);
-
- cvNamedWindow("Contours", CV_WINDOW_AUTOSIZE);
-
- cvCreateTrackbar(
- "Threshold",
- "Contours",
- &g_thresh,
- 255,
- on_trackbar
-
-
- );
-
- on_trackbar(0);
- cvWaitKey();
-
- cvDestroyAllWindows();
- cvReleaseImage(&g_img);
- cvReleaseImage(&g_gray);
- return 0;
- }
/** Our Example 8-2 is drawn from the OpenCV package. Here we create a window with an image in it. A trackbar sets a simple threshold, and the contours in the thresholded im-age are drawn. The image is updated whenever the trackbar is adjusted.Example 8-2. Finding contours based on a trackbar’s location; the contours are updated whenever the trackbar is moved */#include "stdafx.h"#include "cv.h"#include "highgui.h"IplImage* g_img = NULL;IplImage* g_gray = NULL;int g_thresh = 100;CvMemStorage* g_storage = NULL; //內(nèi)存存儲器是一個可用來存儲諸如序列,輪廓,圖形,子劃分等 //動態(tài)增長數(shù)據(jù)結(jié)構(gòu)的底層結(jié)構(gòu)。void on_trackbar(int pos){ if (g_storage == NULL) { g_gray = cvCreateImage(cvGetSize(g_img), 8, 1); g_storage = cvCreateMemStorage();//創(chuàng)建內(nèi)存塊默認值為64K } else { cvClearMemStorage(g_storage); //清除儲存塊內(nèi)容,并不釋放內(nèi)存 } cvCvtColor(g_img, g_gray, CV_BGR2GRAY); //色彩空間轉(zhuǎn)換, BGR到GRAY cvThreshold(g_gray, g_gray, g_thresh, 255, CV_THRESH_BINARY); //對數(shù)組元素進行固定閾值操作 //該函數(shù)的典型應(yīng)用是對灰度圖像進行閾值操作得到二值圖像。 CvSeq* contours = 0; //可動態(tài)增長元素序列 cvFindContours( //在二值圖像中尋找輪廓 g_gray, //輸入二值圖像 g_storage, //得到的輪廓的存儲容器 &contours //指向第一個輪廓的指針 ); //coutours序列指針指向儲存在g_storage 內(nèi)存塊中的輪廓 cvZero(g_gray); //等于cvSetZero,清空圖像 if (contours) //如果序列非空 { cvDrawContours( //在圖像中繪制外部和內(nèi)部的輪廓 g_gray, contours, //指針指向第一個輪廓 cvScalarAll(255), //g_gray 為單通道顏色,只有一種顏色 cvScalarAll(255), //賦為白色 100 //繪制輪廓的最大等級,如果為0 單獨繪制輪廓 ); cvShowImage("Contours", g_gray); }}int main(){ cvNamedWindow("g_img", CV_WINDOW_AUTOSIZE); g_img = cvLoadImage("lena.jpg"); cvShowImage("g_img", g_img); cvNamedWindow("Contours", CV_WINDOW_AUTOSIZE); cvCreateTrackbar( "Threshold", //滑塊名字 "Contours", //滑塊所在窗口名字 &g_thresh, //指定創(chuàng)建時的滑塊位置 255, //滑塊位置的最大值 on_trackbar //每次滑塊位置被改變的時候,被調(diào)用函數(shù)的指針。 //這個函數(shù)應(yīng)該被聲明為void Foo(int); //如果沒有回調(diào)函數(shù),這個值可以設(shè)為NULL。 ); on_trackbar(0); //初始先調(diào)用一次,否者滑塊變動時才顯示 cvWaitKey(); cvDestroyAllWindows(); cvReleaseImage(&g_img); cvReleaseImage(&g_gray); return 0;}
(2) 效果圖
改進單獨加顏色繪制輪廓
-
-
-
-
-
- #include "stdafx.h"
- #include "cv.h"
- #include "highgui.h"
-
- IplImage* g_img = NULL;
- IplImage* g_gray = NULL;
- int g_thresh = 100;
- CvMemStorage* g_storage = NULL;
-
-
- void on_trackbar(int pos)
- {
- if (g_storage == NULL)
- {
- g_gray = cvCreateImage(cvGetSize(g_img), 8, 1);
- g_storage = cvCreateMemStorage();
- }
- else
- {
- cvClearMemStorage(g_storage);
- }
-
- cvCvtColor(g_img, g_gray, CV_BGR2GRAY);
-
- cvThreshold(g_gray, g_gray, g_thresh, 255, CV_THRESH_BINARY);
-
-
-
- CvSeq* contours = 0;
-
- cvFindContours(
- g_gray,
- g_storage,
- &contours
- );
-
-
- cvZero(g_gray);
-
- IplImage* g_temp = cvCreateImage(cvGetSize(g_gray), 8, 3);
-
- cvZero(g_temp);
-
- for(; contours != 0; contours = contours->h_next)
- {
-
- CvScalar color = CV_RGB(rand()&255, rand()&255, rand()&255);
-
- cvDrawContours(
- g_temp,
- contours,
- color,
- color,
- 0
- );
- }
- cvShowImage("Contours", g_temp);
- }
-
- int main()
- {
- cvNamedWindow("g_img", CV_WINDOW_AUTOSIZE);
- g_img = cvLoadImage("lena.jpg");
- cvShowImage("g_img", g_img);
-
- cvNamedWindow("Contours", CV_WINDOW_AUTOSIZE);
-
- cvCreateTrackbar(
- "Threshold",
- "Contours",
- &g_thresh,
- 255,
- on_trackbar
-
-
- );
-
- on_trackbar(0);
- cvWaitKey();
-
- cvDestroyAllWindows();
- cvReleaseImage(&g_img);
- cvReleaseImage(&g_gray);
- return 0;
- }