通过X11获取屏幕截图并转为opencv Mat

#include
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/videoio.hpp"
#include //-lX11
#include
#include
#include

using namespace std;
using namespace cv;


Mat getScreenShot()
{
    Display *dis=XOpenDisplay((char *)0);
    Screen *scr = XDefaultScreenOfDisplay(dis);
    Drawable drawable = XDefaultRootWindow(dis);

    XImage *image = XGetImage(dis, drawable, 0, 0, scr->width, scr->height, AllPlanes, ZPixmap);
    int size=image->bytes_per_line*image->height;

    unsigned char imagedata[size];
    memcpy(imagedata,image->data,size);
    cv::Mat ret=cv::Mat(scr->height,scr->width,CV_8UC4,imagedata);

    XDestroyImage(image);

    XCloseDisplay(dis);
    return ret;
}

你可能感兴趣的:(opencv,人工智能,计算机视觉)