c ++快速截图在linux与opencv使用 [英] c++ fast screenshots in linux for use with opencv

查看:641
本文介绍了c ++快速截图在linux与opencv使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一种快速的方式来截取屏幕截图(如30 fps或以上),以便与c ++中的opencv一起使用

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

在网上找到的或者涉及windows.h或者太慢。

All the information I've found online either involved windows.h or were too slow.

有人可以为我提供一些代码来实现这一点,或者至少指向我的方向,所以我可以解决这个我的自我?

Could someone provide me with some code that achieves this or at least point me in the right direction so I can solve this my self?

推荐答案

你可以使用它来获得原始像素的结构截图。传递给OpenCV以及宽度&高度& BitsPerPixel,你应该很好。

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 <X11/Xlib.h>
#include <X11/Xutil.h>
#include <cstdint>
#include <cstring>
#include <vector>

void ImageFromDisplay(std::vector<uint8_t>& 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);
}

然后使用 OpenCV ,您可以:

int main()
{
    int Width = 0;
    int Height = 0;
    int Bpp = 0;
    std::vector<std::uint8_t> 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;
}

这篇关于c ++快速截图在linux与opencv使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆