Opening and closing VideoCapture takes 2-3 seconds, why?

Getting OpenCV Matrix from Macbook/Windows Desktop Using Huawei/Ubuntu and Ezcap 331 and HDMI cable

C++ chunk to get CV::Mat is below:

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

It gets matrix, but it takes 2-3 seconds to get one frame

Before I used other code for Windows, please find description below:

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


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,, (BITMAPINFO*)&bi, DIB_RGB_COLORS);  //copy from hwindowCompatibleDC to hbwindow

    ReleaseDC(hwnd, hwindowDC);


If I save matrix to file, file’s size within 500 kb

Also, there is also a bug, which considered not important in some web resources

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

It can cause the freeze too

Please advise

please, not 3 (or even 6 !) different problems at the same time !

restrict it to 1 os and 1 capture attempt, then come back, thank you…


yes, if not initialize cv::VideoCapture each time it’s fine

Can you please advise how to declare global with c++
cv::VideoCapture cap(2);

Extern does not work