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
You can use this to get the screenshot into a structure of raw pixels. Pass that to OpenCV along with the Width & Height & BitsPerPixel and you should be good.
#include
#include
#include
#include
#include
void ImageFromDisplay(std::vector& Pixels, int& Width, int& Height, int& BitsPerPixel)
{
Display* display = XOpenDisplay(nullptr);
Window root = DefaultRootWindow(display);
XWindowAttributes attributes = {0};
XGetWindowAttributes(display, root, &attributes);
Width = attributes.width;
Height = attributes.height;
XImage* img = XGetImage(display, root, 0, 0 , Width, Height, AllPlanes, ZPixmap);
BitsPerPixel = img->bits_per_pixel;
Pixels.resize(Width * Height * 4);
memcpy(&Pixels[0], img->data, Pixels.size());
XDestroyImage(img);
XCloseDisplay(display);
}
Then to use it with OpenCV
, you can do:
int main()
{
int Width = 0;
int Height = 0;
int Bpp = 0;
std::vector Pixels;
ImageFromDisplay(Pixels, Width, Height, Bpp);
if (Width && Height)
{
Mat img = Mat(Height, Width, Bpp > 24 ? CV_8UC4 : CV_8UC3, &Pixels[0]); //Mat(Size(Height, Width), Bpp > 24 ? CV_8UC4 : CV_8UC3, &Pixels[0]);
namedWindow("WindowTitle", WINDOW_AUTOSIZE);
imshow("Display window", img);
waitKey(0);
}
return 0;
}