我可以将cv :: Mat作为JPEG数据写入命名管道吗?
问题描述:
我需要将JPEG格式的图像数据写入Linux中的命名管道(使用mkfifo
创建).
I need to write my image data in JPEG form to a named pipe (created with mkfifo
) in Linux.
但是我找不到使它正常工作的方法.我可以用imwrite
写入一个普通文件,但不能写入此FIFO.
But I couldn't find a way to get this working. I can write with imwrite
to a plain file, but not to this FIFO.
代码:
img = cv::Mat(cv::Size(videoWidth, videoHeight), CV_8UC3, videoBuffer);
cv::namedWindow("Display window", cv::WINDOW_NORMAL);
cv::imshow("Display window", img); // Show our image inside it.
cv::imwrite("image.jpg", img);
如何使用命名管道代替文件?
How can I use a named pipe instead of a file?
答
我找到了解决方案.
//using namespace cv; // do not use opencv namespace, because of the write method
// open named pipe
if ((fifo = open(outPipe.c_str(), O_WRONLY)) < 0) {
printf("Fifo error: %s\n", strerror(errno));
return 1;
}
cv::Mat img = cv::Mat(cv::Size(videoWidth, videoHeight), CV_8UC3, videoBuffer); // videoBuffer directly from libav transcode to memory
cv::namedWindow("Display window", cv::WINDOW_NORMAL); // Create a window for display.
cv::imshow("Display window", img); // Show our image inside it.
// encode image to jpeg
std::vector<uchar> buff; //buffer for coding
std::vector<int> param(2);
param[0] = cv::IMWRITE_JPEG_QUALITY;
param[1] = 95; //default(95) 0-100
cv::imencode(".jpg", img, buff, param);
printf("Image data size: %lu bytes (%d kB)\n", buff.size(), (int) (buff.size() / 1024));
// write encoded image to pipe/fifo
if (write(fifo, buff.data(), buff.size()) < 0) {
printf("Error write image data to fifo!\n");
}
close(fifo);
-
打开命名管道(等待直到出现读取器)
Open the named pipe (waits until a reader is present)
在opencv mat中读取数据
Read the data in opencv mat
以jpeg格式编码opencv mat图像
Encode the opencv mat image in jpeg format
将编码后的数据写入管道
Write the encoded data to pipe