cvWaitKey() slowing capture process significantly
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).
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 FindWindown");
}
// 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.
链接地址: http://www.djcxy.com/p/95062.html上一篇: 在Mac OS X 10.10.2上使用waitKey的opencv imshow太慢(使用c ++)
下一篇: cvWaitKey()显着减缓捕获过程