You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于OpenCV VideoCapture获取帧耗时2-3秒及相关告警的技术咨询

桌面/采集卡OpenCV矩阵获取优化及异常解决

场景概述

从MacBook/Windows桌面、华为设备/Ubuntu系统(搭配Ezcap 331采集卡与HDMI线缆)获取OpenCV的CV::Mat矩阵。

Ubuntu下当前实现及问题

用于获取CV::Mat的C++代码如下:

cv::VideoCapture cap(2);
cap.set(cv::CAP_PROP_FRAME_WIDTH, 1920);
cap.set(cv::CAP_PROP_FRAME_HEIGHT, 1080);
cv::Mat frame;
cap.read(frame);

代码可成功获取矩阵,但单帧获取耗时2-3秒,效率极低。

此前Windows下的高效实现

之前在Windows系统中通过窗口句柄转Mat的方式实现,代码如下:

hwnd2Mat::hwnd2Mat(HWND hwindow, float scale)
{
    hwnd = hwindow;
    hwindowDC = GetDC(hwnd);
    hwindowCompatibleDC = CreateCompatibleDC(hwindowDC);
    SetStretchBltMode(hwindowCompatibleDC, COLORONCOLOR);
    RECT windowsize;
    GetClientRect(hwnd, &windowsize);
    srcheight = windowsize.bottom;
    srcwidth = windowsize.right;
    height = (int)(windowsize.bottom * scale);
    width = (int)(windowsize.right * scale);
    image.create(height, width, CV_8UC4);
    hbwindow = CreateCompatibleBitmap(hwindowDC, width, height);
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = width;
    bi.biHeight = -height;
    bi.biPlanes = 1;
    bi.biBitCount = 32;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = 0;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrUsed = 0;
    bi.biClrImportant = 0;
    SelectObject(hwindowCompatibleDC, hbwindow);
    read();

}

void hwnd2Mat::read()
{
    StretchBlt(hwindowCompatibleDC, 0, 0, width, height, hwindowDC, 0, 0, srcwidth, srcheight, SRCCOPY); //change SRCCOPY to NOTSRCCOPY for wacky colors !
    GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, image.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS);  //copy from hwindowCompatibleDC to hbwindow
}

hwnd2Mat::~hwnd2Mat()
{
    DeleteObject(hbwindow);
    DeleteDC(hwindowCompatibleDC);
    ReleaseDC(hwnd, hwindowDC);

}

该方式获取的矩阵保存为文件后,大小在500KB以内,效率表现良好。

异常问题及解决方案请求

当前Ubuntu环境下运行代码时,出现以下GStreamer警告:

[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (935) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1

部分资源认为该警告无关紧要,但实际运行中可能导致程序冻结,恳请提供解决方案建议。


内容的提问来源于stack exchange,提问作者JamesQReader

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 07:04:53