Example: Detection of contours according to the slider parameters, re-detection when the sliding bar changes
Effect Diagram:
/** our Example 8-2 are 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 is drawn.
The image is updated whenever the trackbar is adjusted. Example 8-2. Finding contours based on a trackbar ' s location;
The contours is 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;
Memory storage is an underlying structure that can be used to store data structures such as sequences, contours, graphs, sub-divisions, etc.//dynamically growing.
void On_trackbar (int pos) {if (g_storage = = NULL) {G_gray = Cvcreateimage (Cvgetsize (g_img), 8, 1); G_storage = Cvcreatememstorage ();//create memory block default value is 64K} else {cvclearmemstorage (g_storage);//clear block contents, do not free memory} Cvcvtco
Lor (g_img, G_gray, Cv_bgr2gray); Color space conversion, BGR to Gray Cvthreshold (G_gray, G_gray, G_thresh, 255, Cv_thresh_biNARY);
Fixed threshold operation for array elements//The typical application of this function is to perform a threshold operation on a grayscale image to get a two value image.
cvseq* contours = 0; Dynamic growth element Sequence Cvfindcontours (//Search for contour G_gray in binary image,//input binary image g_storage,//The resulting contour of the storage container &contours
A pointer to the first contour); The coutours sequence pointer points to the contour Cvzero (g_gray) stored in the G_storage memory block; Equals Cvsetzero, empties the image if (contours)//If the sequence is not empty {cvdrawcontours (//Draw outer and inner contours in the image g_gray, contours,//pointer
Point to the first contour Cvscalarall (255),//g_gray is a single channel color, only one color Cvscalarall (255),//white 100//Draw the maximum level of the outline, if the outline is drawn separately for 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",//Slider name "contours",//slider in the window name &g_thresh,//Specify the slider position at Creation 255,/
/The maximum value of the slider position On_trackbar//each time the slider position is changed, a pointer to the function is called.
This function should be declared as void Foo (int);
If there is no callback function, this value can be set to null. ); On_trackbar (0);
Initial first call, no person slider changes to show Cvwaitkey ();
Cvdestroyallwindows ();
Cvreleaseimage (&G_IMG);
Cvreleaseimage (&g_gray);
return 0; }
(2) Effect diagram
Improved drawing outlines individually and color
The/** extension 8_2 the random color of each profile */#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;
Memory storage is an underlying structure that can be used to store data structures such as sequences, contours, graphs, sub-divisions, etc.//dynamically growing.
void On_trackbar (int pos) {if (g_storage = = NULL) {G_gray = Cvcreateimage (Cvgetsize (g_img), 8, 1); G_storage = Cvcreatememstorage ();//create memory block default value is 64K} else {cvclearmemstorage (g_storage);//clear block contents, do not free memory} Cvcvtco
Lor (g_img, G_gray, Cv_bgr2gray);
Color space conversion, BGR to Gray Cvthreshold (G_gray, G_gray, G_thresh, 255, cv_thresh_binary);
Fixed threshold operation for array elements//The typical application of this function is to perform a threshold operation on a grayscale image to get a two value image.
cvseq* contours = 0; Dynamic growth element Sequence Cvfindcontours (//Search for contour G_gray in binary image,//input binary image g_storage,//The resulting contour of the storage container &contours
A pointer to the first contour); The coutours sequence pointer points to the contour Cvzero (g_gray) stored in the G_storage memory block; Equals Cvsetzero, empties the image iplimage* G_temp = Cvcreateimage (Cvgetsize (G_graY), 8, 3);
Create a 3-channel image that shows the color of the contour Cvzero (g_temp); for (; contours! = 0; contours = contours->h_next) {//Because the contour is to be drawn separately, the loop cvscalar color = Cv_rgb (rand () &255, ran
D () &255, rand () &255); Random color cvdrawcontours (//Draw outer and inner contours in an image g_temp, contours,//pointer to first outline color, color, 0//individually painted
System profile);
} 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",//Slider name "contours",//slider in the window name &g_thresh,//Specify the slider position at Creation 255,/
/The maximum value of the slider position On_trackbar//each time the slider position is changed, a pointer to the function is called.
This function should be declared as void Foo (int);
If there is no callback function, this value can be set to null.
); On_trackbar (0);
Initial first call, no person slider changes to show Cvwaitkey ();
Cvdestroyallwindows ();
Cvreleaseimage (&G_IMG);
Cvreleaseimage (&g_gray);
return 0; }