我可以将cv :: Mat作为JPEG数据写入命名管道吗? [英] Can I write a cv::Mat as JPEG data to a named pipe?

查看:144
本文介绍了我可以将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);

  1. 打开命名管道(等待直到出现读取器)

  1. 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

这篇关于我可以将cv :: Mat作为JPEG数据写入命名管道吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆