I'm attempting to transfer some OpenCV images over the network, I've been trying to put them into a format I can easily encrypt and transfer, but I'm almost certain it's not going to work properly.
Essentially the IplImage is a structure but it has a pointer that points to some image data in it along with the data members.
Is there an easy way to copy the image data, cast the entire structure to a character array, and transfer them over the network, then reassemble?
Here's my first idea, lots of typing and I'm pretty sure it's already got problems.
#include "cv.h"
#include "highgui.h"
#include <cstdio>
#include <iostream>
#include <fstream>
#include <vector>
struct ImageBuffer
{
std::vector<int> imageInfo;
//int nSize; /* sizeof(IplImage) */
//int ID; /* version (=0)*/
//int nChannels; /* Most of OpenCV functions support 1,2,3 or 4 channels */
//int alphaChannel; /* Ignored by OpenCV */
//int depth; /* Pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,
// IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported. */
//char colorModel[4]; /* Ignored by OpenCV */
//char channelSeq[4]; /* ditto */
//int dataOrder; /* 0 - interleaved color channels, 1 - separate color channels.
// cvCreateImage can only create interleaved images */
//int origin; /* 0 - top-left origin,
// 1 - bottom-left origin (Windows bitmaps style). */
//int align; /* Alignment of image rows (4 or 8).
// OpenCV ignores it and uses widthStep instead. */
//int width; /* Image width in pixels. */
//int height; /* Image height in pixels. */
//struct _IplROI *roi; /* Image ROI. If NULL, the whole image is selected. */
//struct _IplImage *maskROI; /* Must be NULL. */
//void *imageId; /* " " */
//struct _IplTileInfo *tileInfo; /* " " */
//int imageSize; /* Image data size in bytes
// (==image->height*image->widthStep
// in case of interleaved data)*/
std::vector<char> imageData;
//char *imageData; /* Pointer to aligned image data. */
std::vector<int> imageExtraInfo;
//int widthStep; /* Size of aligned image row in bytes. */
//int BorderMode[4]; /* Ignored by OpenCV. */
//int BorderConst[4]; /* Ditto. */
//char *imageDataOrigin; /* Pointer to very origin of image data
// (not necessarily aligned) -
// needed for correct deallocation */
};
class CreateImageBuffer
{
public:
static ImageBuffer GetImageBuffer(IplImage *img)
{
ImageBuffer buf;
buf.imageInfo.push_back(img->nSize);
buf.imageInfo.push_back(img->ID);
buf.imageInfo.push_back(img->nChannels);
buf.imageInfo.push_back(img->alphaChannel);
buf.imageInfo.push_back(img->depth);
buf.imageInfo.push_back(0); //Color model
buf.imageInfo.push_back(0); //Channel seq.
buf.imageInfo.push_back(img->dataOrder);
buf.imageInfo.push_back(img->origin);
buf.imageInfo.push_back(img->align);
buf.imageInfo.push_back(img->width);
buf.imageInfo.push_back(img->height);
buf.imageInfo.push_back(NULL);
buf.imageInfo.push_back(NULL);
buf.imageInfo.push_back(NULL);
buf.imageInfo.push_back(NULL);
buf.imageInfo.push_back(img->imageSize);
for(int i = 0; i < img->imageSize; i++)
buf.imageData.push_back(img->imageData[i]);
buf.imageExtraInfo.push_back(img->widthStep);
buf.imageExtraInfo.push_back(0);
buf.imageExtraInfo.push_back(0);
buf.imageExtraInfo.push_back((int)img->imageDataOrigin);
return buf;
}
static IplImage GetImageFromBuffer(ImageBuffer buf)
{
IplImage img;
img.nSize = buf.imageInfo[0];
img.ID = buf.imageInfo[1];
img.nChannels = buf.imageInfo[2];
img.alphaChannel = buf.imageInfo[3];
img.depth = buf.imageInfo[4];
for(int i = 0; i < 4; i++)
img.colorModel[i] = ' ';
for(int i = 0; i < 4; i++)
img.channelSeq[i] = ' ';
img.dataOrder = buf.imageInfo[12];
img.origin = buf.imageInfo[13];
img.align = buf.imageInfo[14];
img.width = buf.imageInfo[15];
img.height = buf.imageInfo[16];
img.roi = NULL;
img.maskROI = NULL;
img.imageId = NULL;
img.tileInfo = NULL;
img.imageSize = buf.imageInfo[22];
img.imageData = "ehlo";//f'd
return img;
}
};
// A Simple Camera Capture Framework
int main()
{
IplImage *img = cvLoadImage("img.jpg");
ImageBuffer buf = CreateImageBuffer::GetImageBuffer(img);
IplImage newImage = CreateImageBuffer::GetImageFromBuffer(buf);
cvReleaseImage(&img);
}
What do?