c++ fast screenshots in linux for use with opencv

后端 未结 4 1473
面向向阳花
面向向阳花 2021-01-30 08:02

I\'m trying to find a fast way to take screenshots (as in 30 fps or over) for use with opencv in c++

All the information I\'ve found online either involved windows.h or

4条回答
  •  不思量自难忘°
    2021-01-30 08:20

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    using namespace cv;
    
    struct ScreenShot
    {
        ScreenShot(int x, int y, int width, int height):
            x(x),
            y(y),
            width(width),
            height(height)
        {
            display = XOpenDisplay(nullptr);
            root = DefaultRootWindow(display);
    
            init = true;
        }
    
        void operator() (Mat& cvImg)
        {
            if(init == true)
                init = false;
            else
                XDestroyImage(img);
    
            img = XGetImage(display, root, x, y, width, height, AllPlanes, ZPixmap);
    
            cvImg = Mat(height, width, CV_8UC4, img->data);
        }
    
        ~ScreenShot()
        {
            if(init == false)
                XDestroyImage(img);
    
            XCloseDisplay(display);
        }
    
        Display* display;
        Window root;
        int x,y,width,height;
        XImage* img;
    
        bool init;
    };
    
    int main(int, char**)
    {
        for(;;){
            ScreenShot screen(0,0,1366,768);
    
            Mat img;
            screen(img);
    
            imshow("img", img);
            if(waitKey(30) >= 0) break;
        }
        return 0;
    }
    

    I used the following code, based in the answer posted by @abc.

    In addition, I was using Visual Studio Code and g++ to compile:

    g ++ -std=c++0x main.cpp -lX11 'pkg-config --cflags opencv' 'pkg-config --libs opencv' -o main

    The path for Xlib.h and Xutil.h (ubuntu 14.04): /usr/include/X11

提交回复
热议问题