1

I'm capturing PS3 Eye Camera Video to images which i resize and display as well as saving them to disk. I had troubles with framerates so i measured time with each individual process. It turns out that the only process really slowing down the capturing is cvWaitKey(1) which adds some 20 milliseconds to each capture loop.

Is there a workaround to avoid cvWaitKey() in some way ?

The times measured will be about 20ms without the waitkey and display and 60ms with displaying (which waitkey is needed for). Resizing does not add a this significant amount of time.

Thanks for any help.

// image capturing loop
    while(_running)
    {
        //activate cvWaitKey - this drops framerate significantly!
        cvWaitKey(1);

        //get frame from capture and put into buffer
        CLEyeCameraGetFrame(_cam, pCapBuffer);

        //get system timestamps 
        GetSystemTime(&st);
        GetLocalTime(&lt);

        // Resize image to fit screen size
        cvResize(pCapImage,resizedpCapImage,CV_INTER_NN);

        //display image
        cvShowImage(_windowName, resizedpCapImage);

        //clear string
        sstm.str(std::string());

        //complete filname      
        sstm << _folder << prefix << _participant << std::setfill('0') << std::setw(10) << i  << "-" << st.wHour << st.wMinute << st.wSecond <<  st.wMilliseconds << suffix;
        image_name = sstm.str();
        c = image_name.c_str();


        //log if enabled
        if (_logging){
            //try to save image
            try {
                cvSaveImage(c, pCapImage); //bmp = speed!
            }
            catch (runtime_error& ex) {
                fprintf(stderr, "Exception converting image to bmp format: %s\n", ex.what());
            }
        }
    }

2 Answers 2

0

Firstly, it is not advisable to grab a frame and write it to disk immediately since it's a system call and thus the time taken is not predictable. For short videos, you can store the entire set of uncompressed frames in memory. If that does not work, you will need two threads : one to grab frames and write it to a buffer and another to save them to disk.

Secondly, if you're running windows, you can use _getch (with _kbhit) to replace cvWaitKey if it's faster.

if(_kbhit()) ch = _getch();

However, I have my reservations about blaming cvWaitKey. If you're using GetSystemTime to profile your code, it 's quite likely that your results are wrong since it is not very accurate.

I would recommend re-profiling your code with QueryPerformanceCounter (if you're using windows).

7
  • Hi Jacob, my Videos are not videos but captured frames and are about 5-10 minutes long so i doubt buffering is an option. multithreading this is no gain in speed as outsourcing the saving proces is not actually helping a lot as i get a speed gain of about 3-5 ms. If i do not display and only save i get a speed gain of about 40ms. As for measurement - I see the images that get saved in a period of time and also see the video in an objective way so difference is quite measureable to me. How would you exchange cvWaitKey for _getch ?
    – refuzee
    Sep 9, 2013 at 15:33
  • 5
    sorry, for the downvote @jakob, but it's either drop both waitkey & showImage, or keep both here. waitkey is doing much more than waiting for keypresses, it contains the window's message pump, and triggers the actual blitting ( which might explain, why it's taking time at all ). also getch blocks the main thread, which is totally bad.
    – berak
    Sep 9, 2013 at 15:43
  • @berak: No problem ; _getch() alone is a bad idea. It needs to be combined with _kbhit() as usual.
    – Jacob
    Sep 9, 2013 at 19:17
  • hey, true. but apart from being totally non-portable, it also waits for events in the console window, not the one showing the image.
    – berak
    Sep 9, 2013 at 19:48
  • 1
    hey, let's settle it here, we've both done our best to help ;)
    – berak
    Sep 9, 2013 at 19:57
0

I have not yet found a workaround for cvWaitKey(1), but i figured another way to resize the image using a window handler and this lets me render the image in about 20ms less than using

 cvResize(pCapImage,resizedpCapImage,CV_INTER_NN);

So it might be that this interpolation slows down the process and represents a bottleneck as well.

Here is how i dealt with it:

Instead of using

cvNamedWindow(_windowName, CV_WINDOW_AUTOSIZE);

I now use

cvNamedWindow(_windowName, CV_WINDOW_NORMAL);

As i still have to accomplish representing the window in borderless fullscreen i added the following function to do so:

//display Window in Fullscreen depending on where the client camerawindow is at and what resolution this monitor has.
void Class::setFullscreen(){    

    cvSetWindowProperty(_windowName, CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);

    HWND win_handle = FindWindow(0, (LPCTSTR)_windowName);
    if (!win_handle)
    {
        printf("Failed FindWindow\n");
    }

    // Get monitor resolution
    HMONITOR monitor = MonitorFromWindow(win_handle, MONITOR_DEFAULTTONEAREST);
    MONITORINFO info;
    info.cbSize = sizeof(MONITORINFO);
    GetMonitorInfo(monitor, &info);
    int monitor_width = info.rcMonitor.right - info.rcMonitor.left;
    int monitor_height = info.rcMonitor.bottom - info.rcMonitor.top;

    // Resize
    unsigned int flags = (SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER);
    flags &= ~SWP_NOSIZE;

    // Set position
    unsigned int x = 0;
    unsigned int y = 0;
    SetWindowPos(win_handle, HWND_NOTOPMOST, x, y, monitor_width, monitor_height, flags);

    // Borderless
    SetWindowLong(win_handle, GWL_STYLE, 0);
    ShowWindow(win_handle, SW_SHOW);
}

Doing so i accomplish quite high framerates between about 20ms and 30ms per iteration which equals about 30-50hz (or fps). This is an improvement to speed but it is not an exact solution for a precise framerate. I also tried to get the prescision up to 40hz but giving a higher argment to cvWaitKey() like cvWaitKey(20) simply lowers framerate but does not add any framerate stabilization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.