C++ 通过管道在进程之间发送和读取OpenCV Mat对象数据的正确方法

C++ 通过管道在进程之间发送和读取OpenCV Mat对象数据的正确方法,c++,image,opencv,image-processing,pipe,C++,Image,Opencv,Image Processing,Pipe,我尝试在父进程和子进程之间使用普通的管道s,使用OpenCVMat对象发送和接收图像 正在使用的算法是: 1.儿童阅读图像 2.将其存储在管道中 3.向父级发送图像已写入管道的信号 4.父级接收信号,并(在信号处理程序中)从管道读取图像并显示图像 这将被编译为:$g++simple_pipe.cpp-o sppkg配置--cflags--libs opencv 代码经过 #include <bits/stdc++.h> #include <unistd.h> #incl

我尝试在父进程和子进程之间使用普通的
管道
s,使用OpenCV
Mat
对象发送和接收图像

正在使用的算法是:
1.儿童阅读图像
2.将其存储在
管道中

3.向父级发送图像已写入管道的信号 4.父级接收信号,并(在
信号处理程序中
)从
管道
读取图像并显示图像

这将被编译为:
$g++simple_pipe.cpp-o sp
pkg配置--cflags--libs opencv

代码经过

#include <bits/stdc++.h>
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/wait.h>

// OpenCV 3.4 libraries
#include <opencv2/opencv.hpp>   // FOR OpenCV
#include <opencv2/core.hpp>     // Basic OpenCV structures (cv::Mat)
#include <opencv2/videoio.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>



// #defines...
#define IMG_HT 180      // == ROWS 
#define IMG_WD 180      // == COLS
#define BUFF_SIZE (3*IMG_HT*IMG_WD)
#define BFSZ 1024
#define SLEEP_FOR_SOME_TIME 10

using namespace std;

int fd1[2], fd2[2];
// fd1 => for parent ; 0 - read, 1 - write
// fd2 => for child ; 0 - read, 1 - write

pid_t child_pid;

void signal_handler(int signal_number) {

    if (child_pid == 0) {
        // the child part
    }
    else {
        // the parent part
        printf("Signalled, with signum = %d, and PID = %d\n", signal_number, getpid());
        // close unwanted ends
        close(fd1[1]);
        close(fd2[1]);
        close(fd1[0]);

        unsigned char buffer[BUFF_SIZE];
        read(fd2[0], buffer, BUFF_SIZE);
        close(fd2[0]);

        for (int i=0; i<100; i++) {
            printf("%u ", buffer[i]);
        }
        cout << "\n";

        cv::Mat img(IMG_HT, IMG_WD, CV_8UC3, cv::Scalar(0, 0, 0));
        img.data = (buffer);

        pid_t theProcess = getpid();
        string windowName = to_string(theProcess);

        cv::imshow(windowName, img);
        cv::waitKey(0); 
    }

}



int main() {

    int res1 = pipe(fd1);
    int res2 = pipe(fd2);

    if (res1 == -1 or res2 == -1) {
        printf("Pipe failed!\n");
        exit(EXIT_FAILURE);
    }

    signal(SIGABRT, signal_handler);

    child_pid = fork();

    if (child_pid < 0) {
        printf("Fork failed!\n");
        exit(EXIT_FAILURE);
    }

    if (child_pid == 0) {
        // child part
        printf("This is the child, with PID = %d, and parent PID = %d\n", getpid(), getppid());
        char *imgName = "Dbo2.jpg";
        // open image
        cv::Mat img = cv::imread(imgName);
        // cv::resize(img, img, cv::Size(IMG_WD, IMG_HT));

        // close unwanted ends
        close(fd1[1]);
        close(fd1[0]);
        close(fd2[0]);

        write(fd2[1], img.data, sizeof(img.data));
        close(fd2[1]);

        kill(getppid(), SIGABRT);

        while (1) {
            sleep(SLEEP_FOR_SOME_TIME);
        }
    }
    else {
        // parent part
        printf("The parent PID is: %d, with child PID = %d\n", getpid(), child_pid);
        while (1) {
            sleep(SLEEP_FOR_SOME_TIME);
        }
    }


    return 0;
}
#包括
#包括
#包括
#包括
//OpenCV 3.4库
#包含//用于OpenCV
#包括//基本OpenCV结构(cv::Mat)
#包括
#包括
#包括
//#定义。。。
#定义IMG_HT 180/==行
#定义IMG_WD 180/==COLS
#定义缓冲大小(3*IMG\U HT*IMG\U WD)
#定义BFSZ 1024
#定义睡眠时间10
使用名称空间std;
int-fd1[2],fd2[2];
//fd1=>对于父代;0-读取,1-写入
//fd2=>适用于儿童;0-读取,1-写入
pid_t child_pid;
无效信号处理器(内部信号编号){
如果(子项pid==0){
//儿童部分
}
否则{
//父部分
printf(“有信号,信号=%d,PID=%d\n”,信号号,getpid());
//收尾
关闭(fd1[1]);
关闭(fd2[1]);
关闭(fd1[0]);
无符号字符缓冲区[BUFF_SIZE];
读取(fd2[0],缓冲区,缓冲区大小);
关闭(fd2[0]);

对于(int i=0;i原始图像未显示,您能修复它吗?这与您读取的图像无关,即使您未添加任何图像,结果也是一样的。我搜索了,但找不到解决方案。您可以编写
sizeof(img.data)
bytes。这是指针的大小,为8个字节。您需要写入
width*height*channels*bits\u per\u channel
bytes。@CrisLuengo我在
write()
函数中进行了更改,但我的代码被卡在了write函数中。我使用此()帖子检查了图像大小,并给出了总大小(以字节为单位)是97200。是不是因为这个大小,
write()
函数速度严重减慢?嗯,您需要读取与写入相同的字节数。好吧,如果读取较少,则
write
调用将坐在那里等待其数据被获取。原始图像未显示,您能修复它吗?这与您读取的图像无关,即使您不添加任何图像,结果也是一样的。我搜索了bu找不到解决方案。您写入
sizeof(img.data)
字节。这是指针的大小,为8字节。您需要写入
width*height*channels*bits\u per\u channel
字节。@CrisLuengo我在
write()
函数中做了更改,但我的代码在写入函数中卡住了。我用这个()post,它给出的总大小(以字节为单位)为97200。正是由于这个大小,
write()
函数的速度大大减慢了吗?嗯,您需要读取与写入相同的字节数。好吧,如果读取的字节数较少,则
write
调用将坐在那里等待数据的获取。