有没有更好的方法来命名管道`cv :: mat`变量

时间:2019-07-28 10:50:34

标签: c++ opencv named-pipes mat

我正在尝试将cv::mat变量从一个C程序传递到另一个C程序,它们彼此独立。

我已经创建了一个来自论坛和搜索的基本代码,我有两个程序,writer.creader.c

writer.c中有一个cv::mat img变量,我需要将其通过管道传递到reader.c cv::mat img,以便与imshow()一起显示;

我正在合并来自多个来源的代码,希望可以完成这项工作,因为我可以找到一个有效的示例

我的来源:

  

https://stackoverflow.com/a/2789967/11632453

     

https://stackoverflow.com/a/30274548/11632453

     

https://unix.stackexchange.com/questions/222075/pipe-named-fifo

     

https://stackoverflow.com/a/36080965/11632453

这是我到目前为止的发展:

文件writer.c中的代码

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"


using namespace cv;
using namespace std;

int main()
{
    Mat img;
    img = imread("/home/filipe/Documentos/QT_Projects/FIFO_Writer/download.jpeg", CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! img.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", img );
    //waitKey(0); // Wait for a keystroke in the window
    //return 0;




    int fd;
    char * myfifo = "/tmp/myfifo";

    // create the FIFO (named pipe)
    mkfifo(myfifo, 0666);

    // write "Hi" to the FIFO
    fd = open(myfifo, O_WRONLY);
    //write(fd, "Hi", sizeof("Hi"));
    write(fd, img.data, sizeof(img.data));
    close(fd);

    // remove the FIFO
    unlink(myfifo);

    return 0;
}

来自reader.c的代码

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;
using namespace std;

#define MAX_BUF 1024

int main()
{
    int fd;
    char * myfifo = "/tmp/myfifo";
    char buf[MAX_BUF];

    /* open, read, and display the message from the FIFO */
    fd = open(myfifo, O_RDONLY);
    read(fd, buf, MAX_BUF);
    printf("Received: %s\n", buf);
    close(fd);

    Mat img(177, 284, CV_8UC3, Scalar(0, 0, 0));

    img.data= ((unsigned char*) (buf));
    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", img );

    waitKey(0); // Wait for a keystroke in the window
    return 0;
}

此代码没有错误,但是没有图像以太 没有创建显示图像的窗口。

有帮助吗? 有什么可以帮助我度过的吗? 有方向吗?

谢谢

已解决https://answers.opencv.org/question/216274/is-there-a-better-way-to-named-pipe-a-cvmat-variable/

1 个答案:

答案 0 :(得分:1)

已解决Here

只需根据图像大小更改缓冲区大小设置器