cvWaitKey()显着减缓捕获进程 - 解决方法? [英] cvWaitKey() slowing capture process significantly - workaround?

查看:338
本文介绍了cvWaitKey()显着减缓捕获进程 - 解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将PS3 Eye Camera Video捕捉到的图像调整大小和显示以及将它们保存到磁盘。我有框架的麻烦,所以我测量时间与每个单独的过程。事实证明,真正减慢捕获的唯一进程是cvWaitKey(1),它为每个捕获循环增加了大约20毫秒。



有一个解决方法,以避免cvWaitKey )以某种方式?



测量的时间将是大约20ms没有waitkey和显示和60ms显示(需要waitkey)。调整大小不会增加大量的时间。



感谢您的帮助。

 code> //图像捕获循环
while(_running)
{
//激活cvWaitKey - 这显着降低帧率!
cvWaitKey(1);

//从捕获中获取帧并放入缓冲区
CLEyeCameraGetFrame(_cam,pCapBuffer);

//获取系统时间戳
GetSystemTime(& st);
GetLocalTime(& lt;)

//调整图像大小以适合屏幕大小
cvResize(pCapImage,resizedpCapImage,CV_INTER_NN);

//显示图片
cvShowImage(_windowName,resizedpCapImage);

//清除字符串
sstm.str(std :: string());

//完成文件名
sstm<< _folder<<前缀<< _participant<< std :: setfill('0')<< std :: setw(10)<< i<< - << st.wHour< st.wMinute< stswSecond<< st.wMilliseconds<<后缀;
image_name = sstm.str();
c = image_name.c_str();


//如果启用则记录
if(_logging){
//尝试保存图像
try {
cvSaveImage(c, pCapImage); // bmp = speed!
}
catch(runtime_error& ex){
fprintf(stderr,Exception converting image to bmp format:%s\\\
,ex.what());
}
}
}


解决方案>

我还没有找到cvWaitKey(1)的解决方法,但我想出了另一种方法来调整图像使用窗口处理程序的大小,这让我渲染图像比使用

大约20ms

  cvResize(pCapImage,resizedpCapImage,CV_INTER_NN); 

因此,这种插值可能会减慢流程,也会造成瓶颈。



这是我如何处理它:



而不是使用

  cvNamedWindow(_windowName,CV_WINDOW_AUTOSIZE); 

我现在使用

  cvNamedWindow(_windowName,CV_WINDOW_NORMAL); 

由于我还需要完成代表窗口在无边框全屏我添加了以下功能这样做:

  //以全屏显示窗口,具体取决于客户端camerawindow所在的位置以及此监视器具有的分辨率。 
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\\\
);
}

//获取显示器分辨率
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;

//调整大小
unsigned int flags =(SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER);
flags& =〜SWP_NOSIZE;

//设置位置
unsigned int x = 0;
unsigned int y = 0;
SetWindowPos(win_handle,HWND_NOTOPMOST,x,y,monitor_width,monitor_height,flags);

//无边框
SetWindowLong(win_handle,GWL_STYLE,0);
ShowWindow(win_handle,SW_SHOW);
}

这样做,我完成相当高的帧率,每次迭代大约20ms到30ms,约30-50hz(或fps)。这是对速度的改进,但它不是精确帧速率的确切解决方案。我也试图获得高达40hz的切分,但给cvWaitKey()像cvWaitKey(20)只是降低帧率,但不添加任何帧率稳定。更高的argment。


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());
            }
        }
    }

解决方案

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.

这篇关于cvWaitKey()显着减缓捕获进程 - 解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆