C++ opencv 从缓冲区读取 jpeg 图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14727267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
opencv read jpeg image from buffer
提问by Matekk
I have an unsigned char*
buffer containing data of a jpeg image. I would like to display that image using c++ and opencv. If i do:
我有一个unsigned char*
包含 jpeg 图像数据的缓冲区。我想使用 c++ 和 opencv 显示该图像。如果我做:
Mat img(Size(640, 480), CV_8UC3, data);
namedWindow("image", 1);
imShow("image", img);
I get a noisy mess of pixels.
我得到了一堆嘈杂的像素。
I suppose it's because the data is jpeg (with a header). Because this works:
我想这是因为数据是 jpeg(带有标题)。因为这有效:
Mat imgbuf(Size(640, 480), CV_8UC3, data);
Mat img = imdecode(imgbuf, CV_LOAD_IMAGE_COLOR);
BUT I cannot use the imdecode function as it is from highgui.h which is based upon GTK 2, and in my project I use GTK 3.
但是我不能使用 imdecode 函数,因为它来自基于 GTK 2 的 highgui.h,在我的项目中我使用 GTK 3。
So, how can I display the buffer data? Is there a way to decode the jpeg image other than imdecode in opencv, if that's the problem. I don't really want to have to rebuild opencv with Qt...
那么,如何显示缓冲区数据呢?如果这是问题的话,有没有办法在 opencv 中解码除 imdecode 之外的 jpeg 图像。我真的不想用 Qt 重建 opencv ......
Any other suggestions?
还有其他建议吗?
(Using Linux)
(使用 Linux)
采纳答案by Matekk
I have decompressed the JPEG image using libjpeg
using the standard procedure described in the libjpeg API documentationunder 'Decompression details'.
我已使用libjpeg API 文档中“解压缩详细信息”下libjpeg
描述的标准程序对 JPEG 图像进行解压缩。
After having decompressed the data you can use it to construct the cv::Mat
. Mind you, the decompressed image is in RGB format, whereas openCV uses a BGR format so a cvtColor()
operation with format CV_RGB2BGR
is needed.
解压缩数据后,您可以使用它来构建cv::Mat
. 请注意,解压后的图像是 RGB 格式,而 openCV 使用 BGR 格式,因此需要使用 format 进行cvtColor()
操作CV_RGB2BGR
。
回答by Jeff Reeder
I have seen many responses to this question around on the net saying that you should call libjpeg directly and bypass OpenCV's imread() routine.
我在网上看到很多对这个问题的回答说你应该直接调用 libjpeg 并绕过 OpenCV 的 imread() 例程。
This is NOT necessary! You can use imdecode() to decode a raw image buffer from memory. The way to do it is NOT intuitive, and isn't documented enough to help people trying to do this for the first time.
这不是必需的!您可以使用 imdecode() 从内存中解码原始图像缓冲区。这样做的方法并不直观,并且没有足够的文档来帮助人们第一次尝试这样做。
If you have a pointer/size for your raw file data (fread() directly from the .jpg, .png, .tif, files, etc...
如果您有原始文件数据的指针/大小(直接来自 .jpg、.png、.tif、文件等的 fread()...
int nSize = ... // Size of buffer
uchar* pcBuffer = ... // Raw buffer data
// Create a Size(1, nSize) Mat object of 8-bit, single-byte elements
Mat rawData( 1, nSize, CV_8UC1, (void*)pcBuffer );
Mat decodedImage = imdecode( rawData /*, flags */ );
if ( decodedImage.data == NULL )
{
// Error reading raw image data
}
That's IT!
就是这样!
Hope this helps someone in the future.
希望这对将来的人有所帮助。