How to declare cv::VideoCapture cap(2) as global variable

Trying to declare with extern notation, but failed.
C++ chunk to get CV::Mat is below:

1
2
3
4
5
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);

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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);

}

please advise
Last edited on
Declaration (header file): extern cv::VideoCapture cap ;

Definition (cpp file): #include header and then cv::VideoCapture cap(2) ;
:-1: error: ReadCapture.o:/home/supernova/build-MPU-Desktop-Debug/../Desktop/MPU/Glob.h:4: multiple definition of `cap'; Glob.o:/home/supernova/build-MPU-Desktop-Debug/../Desktop/MPU/Glob.h:4:
first defined here

or

/home/supernova/Desktop/MPU/Glob.cpp:2: error: redefinition of ‘cv::VideoCapture cap’
../Desktop/MPU/Glob.cpp:2:19: error: redefinition of ‘cv::VideoCapture cap’
2 | cv::VideoCapture cap ;
Last edited on
Topic archived. No new replies allowed.