将返回 void 但具有整数参数的函数作为 ARGUMENT 本身传递给另一个函数

发布于 2024-09-27 07:40:15 字数 1888 浏览 7 评论 0原文

我最近开始使用 c++ 开发 OpenCV。我在使用以下代码时遇到问题。

#include "cv.h"
#include "highgui.h"
int g_slider_position = 0;
CvCapture* g_capture = NULL;

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

int main( int argc, char** argv ) {
  cvNamedWindow( "Example3", CV_WINDOW_AUTOSIZE );
  g_capture = cvCreateFileCapture( "Aawaarapan.avi" );
  int frames = (int) cvGetCaptureProperty(
    g_capture,
    CV_CAP_PROP_FRAME_COUNT
  );
  if( frames!= 0 ) {
    cvCreateTrackbar(
      "Position",
      "Example3",
      &g_slider_position,
      frames,
      onTrackbarSlide //need to be call as onTrackbarSlide(g_slider_position) but gives error:invalid use of void expression 
    );
  }

  IplImage* frame;
  while(1) {
    frame = cvQueryFrame( g_capture );
    if( !frame ) break;

    cvShowImage( "Example3", frame );
    cvCreateTrackbar(
      "Position",
      "Example3",
      &g_slider_position,
      frames,
      onTrackbarSlide
    );

    char c = cvWaitKey(33);
    if( c == 27 ) break;

    ++g_slider_position;
  }

  cvReleaseCapture( &g_capture );
  cvDestroyWindow( "Example3" );
  return(0);
}

这里对函数 cvCreateTrackbar 的调用将函数

void onTrackbarSlide(int pos)

作为参数,但是当我像这样调用该函数时 cvCreateTrackbar( “位置”, “示例3”, &g_slider_position, 框架, onTrackbarSlide(g_slider_position) );`

报告错误 错误:无效使用 void 表达式

实际上,我需要将 g_slider_position 传递给函数,以便知道滑块的位置。

我的一个朋友告诉我调用该函数,因为

cvCreateTrackbar(
  "Position",
  "Example3",
  &g_slider_position,
  frames,
  onTrackbarSlide
);

这不会报告任何错误,但它没有执行预期的操作。滑块的位置是必要的。

所以,我的问题是如何使用 onTrackerSlide 参数(及其整数参数)调用 cvCreateTracker。我希望我没有感到困惑。

谢谢你!

I have recently started working on OpenCV using c++. I am having a problem with the following code.

#include "cv.h"
#include "highgui.h"
int g_slider_position = 0;
CvCapture* g_capture = NULL;

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

int main( int argc, char** argv ) {
  cvNamedWindow( "Example3", CV_WINDOW_AUTOSIZE );
  g_capture = cvCreateFileCapture( "Aawaarapan.avi" );
  int frames = (int) cvGetCaptureProperty(
    g_capture,
    CV_CAP_PROP_FRAME_COUNT
  );
  if( frames!= 0 ) {
    cvCreateTrackbar(
      "Position",
      "Example3",
      &g_slider_position,
      frames,
      onTrackbarSlide //need to be call as onTrackbarSlide(g_slider_position) but gives error:invalid use of void expression 
    );
  }

  IplImage* frame;
  while(1) {
    frame = cvQueryFrame( g_capture );
    if( !frame ) break;

    cvShowImage( "Example3", frame );
    cvCreateTrackbar(
      "Position",
      "Example3",
      &g_slider_position,
      frames,
      onTrackbarSlide
    );

    char c = cvWaitKey(33);
    if( c == 27 ) break;

    ++g_slider_position;
  }

  cvReleaseCapture( &g_capture );
  cvDestroyWindow( "Example3" );
  return(0);
}

here the call to the function cvCreateTrackbar takes as argument, the function

void onTrackbarSlide(int pos)

but when I call the function like this
cvCreateTrackbar(
"Position",
"Example3",
&g_slider_position,
frames,
onTrackbarSlide(g_slider_position)
);`

it is reporting an error
error: invalid use of void expression

Actually i need to pass the g_slider_position to the function so that the slider's position is known.

A friend of mine told me to call the function as

cvCreateTrackbar(
  "Position",
  "Example3",
  &g_slider_position,
  frames,
  onTrackbarSlide
);

this doesn't report any error but it doesn't do what it is supposed to.the slider's position is necessary.

So, my question is how do I call the cvCreateTracker with argument onTrackerSlide(with its integer argument). I hope I was not confusing.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆那么伤 2024-10-04 07:40:15

g_slider_position 可以被你的函数访问——你不能传递它。您应该使用您拥有的代码(来自您的朋友),但传递的 int 是轨迹栏的 id。

应该是这样的:

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

g_slider_position is accessible to your function -- you can't pass it. You should use the code you have (from your friend), but the int passed is the id of the trackbar.

Should be this:

void onTrackbarSlide(int id) {
  cvSetCaptureProperty(
    g_capture,
    CV_CAP_PROP_POS_FRAMES,
    g_slider_position
  );
}
傲影 2024-10-04 07:40:15

这是 C 函数指针使用的问题,我假设您从未使用过。

在创建轨迹栏时,您确实应该使用:

cvCreateTrackbar(
  "Position",
  "Example3",
  &g_slider_position,
  frames,
  onTrackbarSlide
);

当您进行该调用时,您将告诉 OpenCV 两条信息:

  1. g_slider_position 的地址
  2. onTrackbarSlide 的地址

现在,当移动轨迹栏时,OpenCV 将:

  1. 直接修改 g_slider_position (因此您实际上甚至不需要将其作为函数参数接收,但无论如何)
  2. 调用 onTrackbarSlide(g_slider_position)< /code>,它可以做到这一点,因为它知道这两条信息

如果您按照您尝试的方式编写代码:

cvCreateTrackbar(
  "Position",
  "Example3",
  &g_slider_position,
  frames,
  onTrackbarSlide(g_slider_position)
);

在您编写 onTrackbarSlide(g_slider_position) 的地方,它会变成 onTrackbarSlide(g_slider_position) 的函数调用代码>onTrackbarSlide。换句话说,在尝试找出要传递给 cvCreateTrackbar 的值时,它调用 onTrackbarSlide 并向其传递 g_slider_position 的当前值,并希望 onTrackbarSlide 能够返回一个函数指针,因为这就是它所需要的。好吧,它永远不会走到这一步,因为它可以在编译时知道 onTrackbarSlide 返回 void,并且 void 不是 void (*aCallbackFunction)(int) (这就是指定类型“接受 int 且不返回任何内容的函数”的方式),但我使用叙述风格...

至于 id< /code> 与 position 争论,我无法从经验中得出结论。我刚刚从 willowgarage 读到的文档指出,您的回调接收的是位置,而不是 id,这意味着您无法使用单个回调来区分滑块。无论如何,我个人不想这样做,因为这意味着我将拥有一个带有开关的功能,而不是多个功能。多个函数将(微观上)更加高效,并且绝对更具可读性。

另一方面,cvCreateTrackbar 确实返回一个 int,它可能是该跟踪栏的 id。但不确定你应该用它做什么。我刚刚阅读的文档页面上没有定义返回值的实际含义。鉴于这是一个 C API 并且缺少 bool 类型,int 可能只是一个 true/false 返回...

This is an issue of C function pointer usage, which I'm assuming you've never used.

At the point of creating the trackbar, you should indeed use:

cvCreateTrackbar(
  "Position",
  "Example3",
  &g_slider_position,
  frames,
  onTrackbarSlide
);

When you make that call, you are telling OpenCV two pieces of information:

  1. the address of g_slider_position
  2. the address of onTrackbarSlide

Now, when that trackbar is moved, OpenCV will:

  1. modify g_slider_position directly (so you don't actually even need to receive it as a function parameter, but anyway)
  2. call onTrackbarSlide(g_slider_position), which it can do because it knows both pieces of information

If you write the code the way you were trying to:

cvCreateTrackbar(
  "Position",
  "Example3",
  &g_slider_position,
  frames,
  onTrackbarSlide(g_slider_position)
);

Where you wrote onTrackbarSlide(g_slider_position), that turned into a function invocation of onTrackbarSlide. In other words, while trying to figure out what values you wanted to pass to cvCreateTrackbar, it called onTrackbarSlide and passed it the current value of g_slider_position, and hoped that onTrackbarSlide would return a function pointer, because that's what it needed. OK, it never got that far, because it could tell at compile time that onTrackbarSlide returned void, and that void was not void(*aCallbackFunction)(int) (which is how you specify the type "a function that takes an int and returns nothing"), but I'm using narrative style...

As for the id vs position argument, I can't speak from experience. The docs I just read from willowgarage state that your callback receives the position, not the id, which means you have no way to use a single callback to differentiate between sliders. I personally wouldn't want to do that anyway, 'coz it means I'd have one function with a switch instead of multiple functions. Multiple functions will be (microscopically) more efficient, and definitely more readable.

On the other hand, cvCreateTrackbar does return an int, which might be the id of that trackbar. Not sure what you're supposed to do with it, though. The actual meaning of the return value was not defined on the doc page I just read. Given that this is a C API and lacks a bool type, that int might just be a true/false return...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文