Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;硬盘抖动原因_C++_Linux_Opencv_X11 - Fatal编程技术网

C++ c++;硬盘抖动原因

C++ c++;硬盘抖动原因,c++,linux,opencv,x11,C++,Linux,Opencv,X11,我正在做一个项目,它使用opencv检测屏幕上的一些图像并获取它们的坐标。我在stackoverflow上发现了一个功能,它可以非常快速地截图,因此我可以非常快速地扫描屏幕。代码如下: #include <X11/Xlib.h> #include <X11/Xutil.h> #include <cstdint> #include <cstring> #include <vector> #include "/usr/local/inclu

我正在做一个项目,它使用opencv检测屏幕上的一些图像并获取它们的坐标。我在stackoverflow上发现了一个功能,它可以非常快速地截图,因此我可以非常快速地扫描屏幕。代码如下:

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <cstdint>
#include <cstring>
#include <vector>
#include "/usr/local/include/opencv2/opencv.hpp"

using namespace cv;

//Function to take screenshots, this is the function that seems to be causing the HDD thrashing

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());

    XFree(img);
    XCloseDisplay(display);
}

int main(){
    int Width = 0;
    int Height = 0;
    int Bpp = 0;
    std::vector<std::uint8_t> Pixels;

    while (1){
        //Func to take image (this is what is causing the thrashing)
        ImageFromDisplay(Pixels, Width, Height, Bpp);

        if (Width && Height){
            //Create a matrix
            Mat img = Mat(Height, Width, Bpp > 24 ? CV_8UC4 : CV_8UC3, &Pixels[0]);

            //Display matrix
            imshow("Display window", img);

            waitKey(1);
        }
    }
}
#包括
#包括
#包括
#包括
#包括
#include“/usr/local/include/opencv2/opencv.hpp”
使用名称空间cv;
//功能拍摄屏幕截图,这似乎是导致硬盘抖动的功能
void ImageFromDisplay(标准::矢量和像素、整数和宽度、整数和高度、整数和比特像素)
{
Display*Display=XOpenDisplay(nullptr);
窗口根=默认根窗口(显示);
XWindowAttributes={0};
XGetWindowAttributes(显示、根和属性);
宽度=属性。宽度;
高度=属性。高度;
XImage*img=XGetImage(显示、根、0、0、宽度、高度、所有平面、ZPixmap);
BitsPerPixel=img->bits\u/u像素;
像素。调整大小(宽度*高度*4);
memcpy(&Pixels[0],img->data,Pixels.size());
XFree(img);
XCloseDisplay(显示);
}
int main(){
整数宽度=0;
整数高度=0;
int-Bpp=0;
std::矢量像素;
而(1){
//Func拍摄图像(这是导致抖动的原因)
ImageFromDisplay(像素、宽度、高度、Bpp);
if(宽度和高度){
//创建一个矩阵
Mat img=Mat(高度、宽度、Bpp>24?CV_8UC4:CV_8UC3和像素[0]);
//显示矩阵
imshow(“显示窗口”,img);
等待键(1);
}
}
}

我没有使用X11的经验,所以我不知道如何解决这个问题。有人能告诉我到底是什么原因导致了抖动,我如何修复它吗?

为了澄清,您的硬盘坏了=您可以扔掉它?你的代码不太可能是原因。它可能使用swap
Pixels.resize(宽*高*4)
您可能应该调用
XDestroyImage(img)而不是
XFree(img)以防止图像内存泄漏。我对X API也不是很熟悉,但是如果您要循环使用它,我认为您不应该无缘无故地一直打开和关闭显示连接。哦,就像@berak说的,尺寸应该再计算一点carefully@abc如果你想更快,就不应该在循环中分配、复制、取消分配。您应该获取初始图像,保持连接打开,使用
XGetSubImage
更新图像,如果完成,则清除单个
XImage*
并显示连接。