There is no button in the highgui of opencv, but we can make full use of the trackbar function to implement the button.
I. Main Ideas
Set a trackbar with only two values, that is, the position range of the trackbar is [0, 1]. When the position is 0, it corresponds to one State; when the position is 1, it corresponds to another State. To implement the button function.
Ii. Use instances
WriteProgramCreate a trackbar. When the position is 0, the original color image is displayed. When the position is 1, The grayscale image corresponding to the image is displayed.
// Switch. c <br/> # include "CV. H "<br/> # include" highgui. H "<br/> iplimage * src = NULL; <br/> iplimage * DST = NULL; <br/> const char * image_name =" lena.jpg "; <br/> const char * wnd_name = "Switch"; <br/> const char * trackbar_name = "on_or_off"; <br/> void on_or_off (int pos) <br/>{< br/> If (0 = POS) <br/>{< br/> cvshowimage (wnd_name, Src ); <br/>}< br/> if (1 = POS) <br/>{< br/> cvcvtcolor (SRC, DST, cv_bgr2gray ); <br/> cvshowimage (wnd_name, DST); <br/>}</P> <p> int main (INT argc, char ** argv) <br/>{< br/> int value = 0; <br/> src = cvloadimage (image_name, 1 ); <br/> DST = cvcreateimage (cvgetsize (SRC), ipl_depth_8u, 1); <br/> cvnamedwindow (wnd_name, cv_window_autosize); <br/> cvcreatetrackbar (trackbar_name, wnd_name, & Value, 1, on_or_off); <br/> on_or_off (0); <br/> cvwaitkey (0); <br/> cvreleaseimage (& SRC ); <br/> cvreleaseimage (& DST); <br/> cvdestroywindow (wnd_name); <br/> return 0; <br/>}
After compilation, the running result is as follows: