简体   繁体   English

OpenCV-创建PNG图像

[英]Opencv - create png image

As part of my project I wanted to send stream of images using websockets from embedded machine to client application and display them in img tag to achieve streaming. 作为项目的一部分,我想使用Websocket从嵌入式计算机向客户端应用程序发送图像流,并在img标签中显示它们以实现流传输。

Firstly I tried to send raw RGB data (752*480*3 - something about 1MB) but in the end I got some problems with encoding image to png in javascript based on my RGB image so I wanted to try to encode my data to PNG firstly and then sent it using websockets. 首先,我尝试发送原始RGB数据(752 * 480 * 3-大约1MB),但最后我在基于RGB图像的javascript中将图像编码为png时遇到了一些问题,因此我想尝试将数据编码为PNG首先,然后使用websocket发送。

The thing is, I am having some problems with encoding my data to PNG using OpenCV library that is already used in the project. 问题是,使用项目中已经使用的OpenCV库将数据编码为PNG时,我遇到了一些问题。

Firstly, some code: 首先,一些代码:

websocketBrokerStructure.matrix = cvEncodeImage(0, websocketBrokerStructure.bgrImageToSend, 0);
websocketBrokerStructure.imageDataLeft = websocketBrokerStructure.matrix->rows * websocketBrokerStructure.matrix->cols *  websocketBrokerStructure.matrix->step;
websocketBrokerStructure.imageDataSent = 0;

but I am getting strange error during execution of the second line: 但是我在执行第二行时遇到奇怪的错误:

terminate called after throwing an instance of 'std::logic_error'
what():  basic_string::_S_construct NULL not valid

and I am a bit confused why I am getting this error from my code. 我有点困惑为什么我从代码中得到这个错误。

Also I am wondering if I understand it right: after invoking cvEncodeImage (where bgrImage is IplImage* with 3 channels - BGR) I just need to iterate through data member of my CvMat to get all of the png encoded data? 我也想知道我是否理解正确:调用cvEncodeImage(其中bgrImage是具有3个通道的IplImage* )之后,我只需要遍历CvMat data成员即可获取所有png编码数据?

The cvEncodeImage function takes as its first parameter the extension of the image you want to encode. cvEncodeImage函数将要编码的图像扩展名作为第一个参数。 You are passing 0 , which is the same thing as NULL . 您正在传递0 ,这与NULL相同。 That's why you are getting the message NULL not valid . 这就是为什么您收到消息NULL not valid的原因。

You should probably use this: 您可能应该使用此:

websocketBrokerStructure.matrix = cvEncodeImage(".png", websocketBrokerStructure.bgrImageToSend, 0);

You can check out the documentation of cvEncodeImage here . 您可以在此处查看cvEncodeImage的文档。

You can check out some examples of cvEncodeImage , or its C++ brother imencode here: encode_decode_test.cpp . 您可以检查出的一些例子cvEncodeImage ,或它的C ++兄弟imencode这里: encode_decode_test.cpp They also show some parameters you can pass to cvEncodeImage in case you want to adjust them. 它们还显示了一些参数,您可以在传递给cvEncodeImage的情况下进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM