[Ubuntu + opencv] How to Create highgui trackbar (slide)-learning notes [2]

Source: Internet
Author: User

1. Add a slide to an image:

1. Create a window, which is the parent window to put trackba. That is, the trackbar belongs to that window.

2. Create a trackbar. The cvcreatetrackbar function is used.

In the PDF file that comes with opencv, you can see that the prototype of this function is as follows:

Creates a trackbar and attaches it to the specified window <br/> int cvcreatetrackbar (<br/> const char * trackbarname, <br/> const char * windowname, <br/> int * value, <br/> int count, <br/> cvtrackbarcallback onchange); <br/> trackbarname name of the created trackbar. <br/> windowname name of the window which will be used as a parent for created trackbar. <br/> value pointer to an integer variable, whose value will reflect the position of the slider. upon <br/> creation, the slider position is defined by this variable. <br/> count maximal position of the slider. minimal position is always 0. <br/> onchange pointer to the function to be called every time the slider changes position. this function <br/> shocould be prototyped as void Foo (INT); can be null if callback is not required. <br/> the function cvcreatetrackbar creates a trackbar (a.k. a. slider or range control) with the <br/> specified name and range, assigns a variable to be syncronized with trackbar position and speci-<br/> FIES a callback function to be called on trackbar position change. the created trackbar is displayed <br/> on the top of the given window.

Trackbarname -- the name of the trackbar.

Windowname -- used to specify the window to which the trackbar is attached. Note:

: This window must be created before calling cvcreatetrackbar. Otherwise, the trackbar is not displayed in the window.

Value -- customizes an integer pointer variable. When the position of the trackbar changes, * value changes accordingly. Experience:

At the same time, * the value determines the position at the very beginning of the trackbar! You can set value to. Check the initial position of the trackbar !! I know this experiment !! Haha ......

Count -- determines the trackbar range. The default value of the minimum value is 0, and the maximum value is count. Slider position value range: [0, Count]

Onchange --
The function called when the position of the trackbar is changed. The prototype of the function must conform to the following prototype: void XXXXX (INT); you can take the function name as needed.
: Onchange is just a function placeholder. You can change the name to any function name you have defined !!

3. In short:

1. This function creates a slide entry named trackbarname in the window named windowname. The sliding range of the slide bar is [0, Count]. As for whether the slide bar is at the top or bottom of the window, this is related to the system.

2. When the position of the slide bar changes, the values of (a) values change to the position value of the current slide bar. (B) The call name is



Ii. Practical Examples


A grayscale image is detected on the edge of the image. Use the scroll bar to determine threshold1 and threshold2

// 2011/06/14 <br/> # include "CV. H "<br/> # include" highgui. H "<br/> # include <stdio. h> <br/> iplimage * src = NULL; <br/> iplimage * DST = NULL; <br/> static const char * wnd_name = "kan "; <br/> static const char * file_name = "lena.jpg"; <br/> static const char * trackbar_name = "threshold"; <br/> void on_track (int pos) <br/>{< br/> If (SRC-> nchannels! = 1) <br/>{< br/> printf ("source image is not gray/N "); <br/>}</P> <p> If (Pos = 0) <br/>{< br/> cvshowimage (wnd_name, Src ); <br/>}< br/> else <br/> {<br/> cvkan (SRC, DST, POs, POS * 3, 3 ); <br/> cvshowimage (wnd_name, DST); <br/>}</P> <p >}< br/> int main (INT argc, char ** argv) <br/>{< br/> int value = 0; <br/> src = cvloadimage (file_name, 0); <br/> DST = cvcreateimage (cvgetsize (SRC ), ipl_depth_8u, 1); </P> <p> cvnamedwindow (wnd_name, cv_window_autosize); <br/> cvcreatetrackbar (<br/> trackbar_name, // const char * trackbarname, <br/> wnd_name, // const char * windowname, <br/> & Value, // int * value, <br/> 100, // int count, <br/> on_track // cvtrackbarcallback onchange <br/>); <br/> on_track (0); </P> <p> cvwaitkey (0 ); </P> <p> cvdestroyallwindows (); <br/> cvreleaseimage (& SRC); <br/> cvreleaseimage (& DST); <br/> return 0; <br/>}


Iii. Results

In the Ubuntu + opencv environment, for program compilation, see the ** learning notes of the monks [1]

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.