1

I'm trying to pipe a cv::mat variable from one C program to an other they are independent of each other.

I already create a basic code sourced from forums and search, I have 2 programs, writer.c and reader.c.

The writer.c have a cv::mat img variable in it and I need pipe it to reader.c cv::mat img to be showed up with imshow();

I'm merging code from multiple sources hoping this work, because I can find a working sample

My sources:

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

This is my evolution until now:

Code from file 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;
}

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

I got no errors from this code, but, I got no Image ether no window apear to be created to show the image.

Any help? Anything to help me passing through? Any direction?

thanks

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

Community
  • 1
  • 1
  • printf("Received: %s\n", buf); is undefined behavior IMHO, unless you are sure that buf is zero terminated. Secondly, do you see something inside the buffer, if yes then the problem is how you create the image otherwise the problem is in the transmission. Lasty (and probably not related to your problem), wouldn't the assignment img.data= ((unsigned char*) (buf)); create a leak? I am no expert of OpenCV but I would expect data to already point to a buffer before assignment. – CuriouslyRecurringThoughts Jul 28 '19 at 11:15
  • Hi, thanks for help, sorry but I'm no following you in all your explanation, you are talking too technical, I'm a dum noobee on C, opencv, and all linux programming... any way... I understand you say the problem is not on pipe but yes on how disintegrate the image and reassemble it so I create a copy of this post on https://answers.opencv.org/question/216274/is-there-a-better-way-to-named-pipe-a-cvmat-variable/ – Filipe Brígido Jul 28 '19 at 11:40
  • What I said is that you should try to do find out if the problem is the pipe or the image creation. A way to do it is to print the content of the buffer upon transmission and receipt. I would avoid using %s because the buffer might not have a 0 as last element. You can do a loop on the first, say, 100 elements and print them one after the other both on send and receive. – CuriouslyRecurringThoughts Jul 28 '19 at 11:47
  • You're right, I have a problem with the pipe, I add `printf("%u\n",img.data);` on both programs and run them, I got different outputs – Filipe Brígido Jul 28 '19 at 13:21
  • Solved in https://answers.opencv.org/question/216274/is-there-a-better-way-to-named-pipe-a-cvmat-variable/ The problem it's the buffer size – Filipe Brígido Jul 28 '19 at 19:45

1 Answers1

1

Solved Here

Just change the buffer sizer according with the image size