OpenCV - getting the slider to update its position during video playback

后端 未结 6 1936
-上瘾入骨i
-上瘾入骨i 2020-12-28 21:12

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

6条回答
  •  抹茶落季
    2020-12-28 21:36

    You are incrementing g_slider_position twice in the code, so it will increment beyond its limit (set in cvCreateTrackbar as frames). This is likely causing your picture to freeze.

    To fix, change this

        g_slider_position++;
        cvSetTrackbarPos(
            "Position", 
            "The Tom 'n Jerry Show",
            ++g_slider_position
            );
    

    to

        cvSetTrackbarPos(
            "Position", 
            "The Tom 'n Jerry Show",
            ++g_slider_position
            );
    

    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:

    if (frames != 0)
    {
        cvCreateTrackbar(
            "Position",
            "The Tom 'n Jerry Show",
            &g_slider_position,
            frames,
            onTrackbarSlide
            );
    } 
    else
    {
        exit(1);
    }
    

提交回复
热议问题