对多个图像的内存缓冲区 [英] A Memory buffer for multiple images
问题描述
我是从两个摄像头在电脑上接收图像。该流是不连续的,而是在事件检测发生。因此,我必须获得约8-10图像对一个特定的时间之后。因为我要处理图像,我想先保持在一个大的缓冲区中的所有图像,后来在一个批次处理它们。另外,也可以保持图像中两个大缓冲器(每个相机)。如何保持这些对图像中的一个(或两个)大的缓冲(S)?我实施这个方案在C
I receive images on the PC from two cameras. The stream is not continuous but takes place at event detection. Therefore, I have to receive around 8-10 image pairs after a particular time. Since I have to process the images, I want to first keep all the images in a large buffer and later process them in a batch. It is also acceptable to keep images in two large buffers (one for each camera). How do I keep these pair of images in one (or two) large buffer(s)? I am implementing this program in C.
推荐答案
Assumeing以下API来获取图像:
Assumeing the following API to get the image:
int ImageGet(
char * pBuffer,
size_t * pSize); /* returns 0 on success or any other value if no image is available or on error */
你可以做到以下几点:
you could do the following:
#include <stdlib.h>
#include <string.h>
...
#define IMAGESIZE_MAX (1024*1024)
char * pBuffer = NULL;
char * pBufferCurrent = pBuffer;
int iResult = 0;
size_t size = IMAGESIZE_MAX;
size_t sizeTotal = 0;
do
{
char * pBufferCurrent = realloc (
pBuffer,
sizeTotal + sizeof(size) + size);
if (!pBufferCurrent)
{
break;
}
pBuffer = pBufferCurrent;
pBufferCurrent += sizeTotal;
if ((iResult = ImageGet (
pBufferCurrent + sizeof(size),
&size))
{
break;
}
memcpy (
pBufferCurrent,
&size,
sizeof(size));
sizeTotal += (sizeof(size) + size);
} while (1);
...
此存储你的数据顺序:
[ size 1 ][ image data 1 ][ size 2 ][ image data 2 ]...
<-sizeof(size_t)-><- size 1 -><-sizeof(size_t)-><- size 2 ->
在缓冲区指向的pbuffer
。
要检索从缓冲区描述图像N数据的引用的pbuffer
你可以使用下面的方法:
To retrieve a reference to the data describing image N from the buffer pBuffer
you could use the following method:
const char * BufferImageGetByIndex(
const char * pBuffer,
unsigned indexImage)
{
for (unsigned indexImageCnt = 0;
indexImageCnt < indexImage;
++ indexImageCnt)
{
pBuffer += sizeof(size_t) + *((size_t *) pBuffer);
}
return pBuffer;
}
然后真正得到你可以做像这样的图片:
Then to actually get an image you could do like so:
...
/* Get a reference to the data decribing **fourth** image in the buffer filled by the example above. */
const char * pBufferImage4 = *BufferImageGetByIndex(pBuffer, 3);
/* Get the size ... */
size_t sizeImage4 = *((size_t *) pBufferImage4);
/* ... and a reference to the image data. */
const char * pImage4 = pBufferImage4 + sizeof(size_t);
...
这篇关于对多个图像的内存缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!