Where are we getting the value of this parameter?

主宰稳场 提交于 2019-12-24 10:31:25

问题


In a program in the Learning OpenCV book:

void onTrackbarSlide(int pos)
{
cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos);
}

And, in another location:

if(frames!=0)
{
cvCreateTrackbar("Position","Example3",&g_slider_position,frames,onTrackbarSlide);
}

Where are we retrieving the value of pos in the onTrackSlide(int pos) function from? What value will be passed to it from cvCreateTrackbar()?


回答1:


Check the docs and you'll see the function signature is:

cvCreateTrackbar(const char* trackbarName, const char* windowName, int* value, int count, CvTrackbarCallback onChange)

Notice that the last parameter has the type CvTrackbarCallback. Obviously, this is not a built-in type, but a type defined by OpenCV. So we go back to the documentation to find more about it and interestingly enough, this information shows up:

The function cvCreateTrackbar() creates a trackbar (a.k.a. slider or range control) with the specified name and range, assigns a variable to be syncronized with trackbar position and specifies a callback function to be called on trackbar position change.

And right below this paragraph, you can see how a CvTrackbarCallback should be declared:

CV_EXTERN_C_FUNCPTR( void (*CvTrackbarCallback)(int pos) );

To summarize, in order to call cvCreateTrackbar() you need to declared a function with the signature void some_fun(int pos), to be able to be notified by OpenCV when the slider of the trackbar is updated. The argument int pos informs the new position of the slider.




回答2:


I don't know OpenCV - but it looks like onTrackbarSlide is some sort of event handler, so it is passed a value of pos from the trackbar UI that you are creating.

There is no value of pos directly passed from cvCreateTrackbar() - that looks like it creates your trackbar UI, and then if interacted with by the user will call onTrackBarSlide.



来源:https://stackoverflow.com/questions/6808902/where-are-we-getting-the-value-of-this-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!