如何从docker中的程序ubuntu中的c ++ opencv记录主机的桌面?

问题描述

我已经从Internet复制了c ++中的opencv代码以记录桌面”

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

using namespace std;
using namespace cv;
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,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);
}
int main()
{
    int Width = 0;
    int Height = 0;
    int Bpp = 0;
    std::vector<std::uint8_t> Pixels;
    while(true)
    {
        namedWindow("Display window",WINDOW_NORMAL);
        ImageFromDisplay(Pixels,Bpp);
        
        if (Width && Height)
        {
            Mat img = Mat(Height,Bpp > 24 ? CV_8UC4 : CV_8UC3,&Pixels[0]); //Mat(Size(Height,Width),&Pixels[0]);
            
            namedWindow("WindowTitle",WINDOW_AUTOSIZE);
            imshow("Display window",img);
            
            waitKey(1);
        }
    }
    return 0;
}

一切正常。

enter image description here

imshow窗口完全显示我的桌面。

但是,我的老板说每个程序都应该在docker中。 我写了一个dockerfile并将程序放入其中

FROM x11docker/xfce
ENV TZ=Asia/Taibei
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update && apt-get install -y \ 
    build-essential \
    cmake \
    git \
    libatlas-base-dev \
    libgtk2.0-dev \
    libjpeg-dev \
    libpng-dev \
    libtiff-dev \
    libvtk6-dev \
    pkg-config \
    qt5-default \
    wget \
    zlib1g-dev 

RUN apt-get install cmake-qt-gui
   
RUN git clone https://github.com/opencv/opencv.git && \
    cd ./opencv && \
    mkdir build && \
    cd build && \
    cmake  \
        -D BUILD_SHARED_LIBS=ON \ 
        -D WITH_QT=ON \
        -D WITH_OPENGL=ON \ 
        -D FORCE_VTK=ON \
        -D WITH_TBB=ON \ 
        -D WITH_GDAL=ON \ 
        -D WITH_V4L=ON \
        -D WITH_XINE=ON \ 
        -D BUILD_EXAMPLES=OFF \
        -D ENABLE_PRECOMPILED_HEADERS=OFF \
        -D BUILD_DOCS=OFF \
        -D BUILD_PERF_TESTS=OFF \
        -D BUILD_TESTS=OFF \
        -D BUILD_opencv_apps=OFF \
        -D CMAKE_INSTALL_PREFIX=/home/gino/opencv_install \
        .. && \
    make -j8 && \
    make install && \
    ldconfig

ADD ./ /home/gino/cvtest/
WORKDIR /home/gino/cvtest/
RUN make

ENV HOME /home/developer 

为了显示GUI,我使用x11docker运行图像。 但是泊坞窗中的程序只能获取映像的桌面。

enter image description here

如何通过Docker中的程序记录主机的桌面?

解决方法

找到另一个人的blog

的答案

首先,使用命令使x11可以被其他用户访问:

xhost +

第二次运行图像(图像名称为“ catchdesktop”)

sudo docker run --net=host --env="DISPLAY" --volume="$HOME/.Xauthority:/root/.Xauthority:rw" catchdesktop

现在我可以看到我的主机桌面出现在docker的opencv程序中。

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...