简体   繁体   中英

manually stop a video in openCV

I have an OpenCV code that was given to me, and it displays a video using a linked list of pictures. I want to add to it the option of stoping the video in case of a loop, using a keyboard key. I did some searching and got to the 'WaitKey' feature, but I'm not sure where and how I'm supposed to use it in my code.

cvNamedWindow("Display window", CV_WINDOW_AUTOSIZE); //create a window
FrameNode* head = list;
int imgNum = 1, playCount = 0;
IplImage* image;
while (playCount < GIF_REPEAT)
{
    while (list != 0)
    {
        image = cvLoadImage(list->frame->path, 1);
        IplImage* pGrayImg = 0;
        pGrayImg = cvCreateImage(cvSize(image->width, image->height), image->depth, 1);
        if (!image) //The image is empty - shouldn't happen since we checked already.
        {
            printf("Could not open or find image number %d", imgNum);
        }
        else
        {
            cvShowImage("Display window", image); //display the image
            cvWaitKey(list->frame->duration); //wait
            list = list->next;
            cvReleaseImage(&image);
        }
        imgNum++;
    }
    list = head; // rewind
    playCount++;
}
cvDestroyWindow("Display window");
return;
cv::imshow("display", image);
char ch = cv::waitKey(duration);
if (ch == ' ')
    ch = cv::waitKey(0);  // waits until user presses a key
if cv2.waitKey(1) & 0xFF == ord("q"):
                break
  • put this inside 1st while loop
  • This will help you after pressing 'q' the program stop execution

waitKey(0 will display the window infinitely until any keypress (it is suitable for image display).

waitKey(25) will display a frame for 25 ms , after which display will be automatically closed. (If you put it in a loop to read videos, it will display the video frame-by-frame)

See https://docs.opencv.org/2.4/modules/highgui/doc/user_interface.html?highlight=waitkey

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM