[Ubuntu + opencv] The trackbar of highgui acts as the switch button-learning notes [3]

Source: Internet
Author: User

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:

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.