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

标签 c++ opencv named-pipes mat

我正在尝试通过管道传输 cv::mat从一个 C 程序到另一个 C 程序的变量,它们彼此独立。

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

writer.c有一个cv::mat img其中的变量,我需要将其通过管道传输到 reader.c cv::mat imgimshow()一起出现;

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

我的来源:

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;
}

我没有从这段代码中得到任何错误,但是,我没有得到 Image ether 似乎没有要创建的窗口来显示图像。

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

谢谢

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

最佳答案

已解决 Here

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

关于c++ - 有没有更好的方法来命名管道 `cv::mat` 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57240143/

相关文章:

python - 如何准确测量流经命名管道的比特率?

c++ - VisualAssistX 弄乱了我的 C++ VS2008 配色方案

c++ - 用双花括号初始化 vector<string>

c++ - 在 ARM 上安装 OpenCV 2.4.9

c# - 如何增加命名管道的MaxStringContentLength?

cocoa - cocoa 中的命名管道

c++ - 如何从 LLVM 中的 CallInst 获取函数名称?

c++ - 避免堆分配的宏?在这种情况下有那么糟糕吗?

java - 如何在Android中使用Opencv在图像上有效应用Canny边缘检测器?

android - Opencv全屏使用CameraBridgeViewBase android