OpenCV - 让滑块在视频播放期间更新其位置
我选择了“学习 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("The Tom 'n Jerry Show", CV_WINDOW_AUTOSIZE);
g_capture = cvCreateFileCapture(argv[1]);
int frames = (int) cvGetCaptureProperty(
g_capture,
CV_CAP_PROP_FRAME_COUNT
);
if (frames != 0)
{
cvCreateTrackbar(
"Position",
"The Tom 'n Jerry Show",
&g_slider_position,
frames,
onTrackbarSlide
);
}
IplImage* frame;
while (1)
{
frame = cvQueryFrame(g_capture);
if (!frame)
break;
cvSetTrackbarPos(
"Position",
"The Tom 'n Jerry Show",
++g_slider_position
);
cvShowImage("The Tom 'n Jerry Show", frame);
char c = cvWaitKey(33);
if (c == 27)
break;
}
cvReleaseCapture(&g_capture);
cvDestroyWindow("The Tom 'n Jerry Show");
return 0;
}
任何想法如何让滑块和视频按预期工作?
I've picked up 'Learning OpenCV' and have been trying some of the code examples/exercises. In this code snippet, I want to get the slider to update its position with each video frame change, but for some reason it won't work (the picture freezes 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("The Tom 'n Jerry Show", CV_WINDOW_AUTOSIZE);
g_capture = cvCreateFileCapture(argv[1]);
int frames = (int) cvGetCaptureProperty(
g_capture,
CV_CAP_PROP_FRAME_COUNT
);
if (frames != 0)
{
cvCreateTrackbar(
"Position",
"The Tom 'n Jerry Show",
&g_slider_position,
frames,
onTrackbarSlide
);
}
IplImage* frame;
while (1)
{
frame = cvQueryFrame(g_capture);
if (!frame)
break;
cvSetTrackbarPos(
"Position",
"The Tom 'n Jerry Show",
++g_slider_position
);
cvShowImage("The Tom 'n Jerry Show", frame);
char c = cvWaitKey(33);
if (c == 27)
break;
}
cvReleaseCapture(&g_capture);
cvDestroyWindow("The Tom 'n Jerry Show");
return 0;
}
Any idea how to get the slider and video to work as intended?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您在代码中将 g_slider_position 递增两次,因此它将超出其限制(在
cvCreateTrackbar
中设置为frames
)。这可能会导致您的图片冻结。要修复此问题,请将其更改
为
“会计”以编辑代码,我将检查 OpenCV 是否正确读取文件中的帧数。请参阅学习 OpenCV 的第 2 章示例 2.3,了解从 AVI 中检索帧数的一般方法(如果您正在使用该方法)。
在上面的代码中,如果帧数为 0,则不会创建轨迹栏,但代码仍会进入尝试更新轨迹栏位置的循环(如果找到帧)。我会用这个代替:
You are incrementing g_slider_position twice in the code, so it will increment beyond its limit (set in
cvCreateTrackbar
asframes
). This is likely causing your picture to freeze.To fix, change this
to
Accounting for the edited code, I would check that OpenCV is properly reading the number of frames from your file. Look at Learning OpenCV's Chapter 2, example 2.3 for a method of generically retrieving the number of frames from your AVI (if that is what you are using).
In your code above, if the number of frames is 0, the trackbar is not created but the code still enters a loop that attempts to update the trackbar position (if it finds a frame). I would use this instead:
这对我来说似乎有点复杂。我使用
cvGetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES)
调用来检索当前帧并使用它来更新滑块。然后回调函数仅用于更改 g_capture 中的位置。
所以回调是:
循环是:
其中
g_
变量是全局变量。This seems a bit complicated to me. I used the
cvGetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES)
call to retrieve the current frame and used this to update the slider.The callback function is then used just to change the position within g_capture.
So the call back is:
And the loop is:
Where the
g_
variables are global.您可以尝试下面的解决方案。
将其更改
为
并将其更改
为
You can try the solution below.
change this
to
and also change this
to
您好,我有类似的代码,我执行了以下操作:
......................
所以您可以将滑动条抓取到任何方向,我希望这可以有所帮助
Hi I have simlar code and I did the following:
.............
So you can grab the slide bar to any direction , I hope this can help
好吧我终于解决了更新滑块的问题
如果您想移动滑块,视频也会更新
现在不存在图片冻结的问题
了,现在我所做的是创建了一个全局计数器,
当我们使用鼠标滑块到不同位置时,它将与帧一起更新,然后在 onTrackbarSlider 例程中使用 if 循环将启动并将视频设置到新位置
OK I finally solved this problem of updating the slider
and also if you want to move the slider the video will update
there is no problem of picture freezing now
ok now what i have done is that i have created a global counter which will be updated alongside with the frames
now when we use the slider with the mouse to a different position then in onTrackbarSlider routine the if loop will be initiated and it will set the video to the new position