1
votes

I am trying to write a processed mat to STDOUT so that I can pipe it into ffmpeg, but I can't understand what the difference is exactly between

imencode and imread.

According to the opencv documentation

imencode : Encodes an image into a memory buffer.

imread : Loads an image from a file.

Isn't imread loading image data into a memory buffer as well? Can an opencv window be piped to another application?

Instead of using raspvid I would like to pipe ffmpeg my program's output, which would be the window that displays the processed window.

http://www.valmueller.net/stream-video-with-raspberry-pi-to-youtube/

2
Primary difference between imread and imencode is that the first is for input (encoded image, in this case in a file, read into a cv::Mat) and the second for output (cv::Mat written as an encoded image to a memory buffer). What exactly do you mean by "window be piped to another application"? - Dan MaĊĦek
valmueller.net/stream-video-with-raspberry-pi-to-youtube I want to stream the opencv window that has the processed output into ffmpeg, then stream that to youtube. Currently my opencv program shows the processed window using imshow("cctv", img); if I do ./main am I already piping to STDOUT? - Zypps987
@Zypps987, did you manage to pipe into ffmpeg? I want to do the same thing. - regina_fallangi

2 Answers

0
votes

imread is related to imdecode, while imwrite is related to imencode.

You can think of imread as:

  1. read as-is the raw data buffer from an image file (jpg, png, etc...)
  2. use imdecode to decode the raw data buffer to (usually, but depending on flags) a BGR image.

while imwrite is:

  1. use imencode to decode into jpg, png, etc... a (usually) BGR image to a raw data buffer.
  2. write the raw data buffer as-is to disk.

The raw data buffer is a vector<uchar>.

Depending on your needs, you can put on stdout:

  • the (usually) BGR buffer, which contains the a sequence of bytes BGRBGR...BGR. The byte array starts at mat.data and has length equals to: mat.rows * mat.cols * mat.channels(). This is valid for continuous matrices, but let's assume that this is the case for now.
  • or the decoded jpg, png, etc... raw data buffer
0
votes

The OpenCV tutorial on Operations with Images mentions that while imread/imwrite can be used for reading and writing to files, but to...

Use cv::imdecode and cv::imencode to read and write an image from/to memory rather than a file.

So since we want to push the result to standard out, we need to use imencode to save the matrix data in a buffer (here assuming JPEG file format):

cv::Mat fileToSend = # initialised somehow
std::vector<uchar> buffer;
cv::imencode(".jpg", fileToSend, buffer);

And then write the buffer data to standard out:

std::fwrite(buffer.data(), 1, buffer.size(), stdout);
std::fflush(stdout);
return 0;