在 OpenCV 中不使用 CvSaveImage 将 IplImage 转换为 JPEG
Posted
技术标签:
【中文标题】在 OpenCV 中不使用 CvSaveImage 将 IplImage 转换为 JPEG【英文标题】:Convert IplImage into a JPEG without using CvSaveImage in OpenCV 【发布时间】:2010-10-16 09:59:53 【问题描述】:我希望将 IplImage 转换为内存中的 JPEG 图像(以便将其作为 M-JPEG 帧通过套接字传输)。 我知道我可以为此使用 CvSaveImage,它创建一个 jpeg 文件,我再次读取它,然后通过网络传输它。 我希望避免这种额外的磁盘读写操作以加快操作。有什么见解吗?
【问题讨论】:
这真的是一个 C# 问题吗? OpenCV 是一个 C/C++ 库,不是吗? 【参考方案1】:查看this question。我不确定如何在 C# 中使用该解决方案,但也许它可以提供帮助。
【讨论】:
【参考方案2】:如果你的标签是正确的,并且这是在 C# 中,那么你应该检查一下 OpenCVSharp。
http://code.google.com/p/opencvsharp/
有了它,你可以做...
IplImage ipl = new IplImage("foo.png", LoadMode.Color);
Bitmap bitmap = ipl.ToBitmap();
我还发现了一个使用 VC++.NET 的示例
//IplImage -> Bitmap
void Fill_Bitmap(Bitmap* bitmap, IplImage* image)
int nl= image->height;
int nc= image->width * image->nChannels;
int step= image->widthStep;
unsigned char* data=reinterpret_cast<unsigned char*>(image->imageData);
for(int i=0; i<nl; i++)
for(int j=0; j<nc; j+= image->nChannels)
bitmap->SetPixel(j/3,i,Color::FromArgb(data[j],data[j+1],data[j+2]));
data+= step;
;
Assume that in your main function:
void main()
...
imRGB=cvCreateImage( cvSize(col,row), 8, 3 );
Tbitmap=new Bitmap(col,row,PixelFormat::Format24bppRgb);
...
Fill_Bitmap(Tbitmap,imRGB);
祝你好运!
【讨论】:
【参考方案3】:很简单
从内存缓冲区加载文件所需的只是一个不同的 src 管理器 (libjpeg)。我已经在 Ubuntu 8.10 中测试了以下代码。
/******************************** First define mem buffer function bodies **************/
/*
* memsrc.c
*
* Copyright (C) 1994-1996, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains decompression data source routines for the case of
* reading JPEG data from a memory buffer that is preloaded with the entire
* JPEG file. This would not seem especially useful at first sight, but
* a number of people have asked for it.
* This is really just a stripped-down version of jdatasrc.c. Comparison
* of this code with jdatasrc.c may be helpful in seeing how to make
* custom source managers for other purposes.
*/
/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
include "jinclude.h"
include "jpeglib.h"
include "jerror.h"
/* Expanded data source object for memory input */
typedef struct
struct jpeg_source_mgr pub; /* public fields */
JOCTET eoi_buffer[2]; /* a place to put a dummy EOI */
my_source_mgr;
typedef my_source_mgr * my_src_ptr;
/*
* Initialize source --- called by jpeg_read_header
* before any data is actually read.
*/
METHODDEF(void)
init_source (j_decompress_ptr cinfo)
/* No work, since jpeg_memory_src set up the buffer pointer and count.
* Indeed, if we want to read multiple JPEG images from one buffer,
* this *must* not do anything to the pointer.
*/
/*
* Fill the input buffer --- called whenever buffer is emptied.
*
* In this application, this routine should never be called; if it is called,
* the decompressor has overrun the end of the input buffer, implying we
* supplied an incomplete or corrupt JPEG datastream. A simple error exit
* might be the most appropriate response.
*
* But what we choose to do in this code is to supply dummy EOI markers
* in order to force the decompressor to finish processing and supply
* some sort of output image, no matter how corrupted.
*/
METHODDEF(boolean)
fill_input_buffer (j_decompress_ptr cinfo)
my_src_ptr src = (my_src_ptr) cinfo->src;
WARNMS(cinfo, JWRN_JPEG_EOF);
/* Create a fake EOI marker */
src->eoi_buffer[0] = (JOCTET) 0xFF;
src->eoi_buffer[1] = (JOCTET) JPEG_EOI;
src->pub.next_input_byte = src->eoi_buffer;
src->pub.bytes_in_buffer = 2;
return TRUE;
/*
* Skip data --- used to skip over a potentially large amount of
* uninteresting data (such as an APPn marker).
*
* If we overrun the end of the buffer, we let fill_input_buffer deal with
* it. An extremely large skip could cause some time-wasting here, but
* it really isn't supposed to happen ... and the decompressor will never
* skip more than 64K anyway.
*/
METHODDEF(void)
skip_input_data (j_decompress_ptr cinfo, long num_bytes)
my_src_ptr src = (my_src_ptr) cinfo->src;
if (num_bytes > 0)
while (num_bytes > (long) src->pub.bytes_in_buffer)
num_bytes -= (long) src->pub.bytes_in_buffer;
(void) fill_input_buffer(cinfo);
/* note we assume that fill_input_buffer will never return FALSE,
* so suspension need not be handled.
*/
src->pub.next_input_byte += (size_t) num_bytes;
src->pub.bytes_in_buffer -= (size_t) num_bytes;
/*
* An additional method that can be provided by data source modules is the
* resync_to_restart method for error recovery in the presence of RST markers.
* For the moment, this source module just uses the default resync method
* provided by the JPEG library. That method assumes that no backtracking
* is possible.
*/
/*
* Terminate source --- called by jpeg_finish_decompress
* after all data has been read. Often a no-op.
*
* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
* application must deal with any cleanup that should happen even
* for error exit.
*/
METHODDEF(void)
term_source (j_decompress_ptr cinfo)
/* no work necessary here */
/*
* Prepare for input from a memory buffer.
*/
GLOBAL(void)
jpeg_memory_src (j_decompress_ptr cinfo, const JOCTET * buffer, size_t bufsize)
my_src_ptr src;
/* The source object is made permanent so that a series of JPEG images
* can be read from a single buffer by calling jpeg_memory_src
* only before the first one.
* This makes it unsafe to use this manager and a different source
* manager serially with the same JPEG object. Caveat programmer.
*/
if (cinfo->src == NULL) /* first time for this JPEG object? */
cinfo->src = (struct jpeg_source_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
SIZEOF(my_source_mgr));
src = (my_src_ptr) cinfo->src;
src->pub.init_source = init_source;
src->pub.fill_input_buffer = fill_input_buffer;
src->pub.skip_input_data = skip_input_data;
src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
src->pub.term_source = term_source;
src->pub.next_input_byte = buffer;
src->pub.bytes_in_buffer = bufsize;
那么用法就很简单了。您可能需要将 SIZEOF() 替换为 sizeof()。找一个标准的解压例子。只需将“jpeg_stdio_src”替换为“jpeg_memory_src”即可。希望对您有所帮助!
【讨论】:
【参考方案4】:使用 CxImage http://www.codeproject.com/KB/graphics/cximage.aspx
【讨论】:
以上是关于在 OpenCV 中不使用 CvSaveImage 将 IplImage 转换为 JPEG的主要内容,如果未能解决你的问题,请参考以下文章
opencv中16位的矩阵保存成图像,最大灰度值只有32768