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
#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