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?

Recommended Answers

All 3 Replies

I'm sure there's a simple solution, but I wish someone would shed some light on the subject.

I've never really been the kind to do weird casting and such as is normal for C programmers.

Thanks for trying.

Anyone could tell me why this gives me an access violation??
I really have no clue why.

#include "cv.h" 
#include "highgui.h" 
#include <cstdio>
#include <iostream>
#include <fstream>
#include <vector>
std::vector<char> GetImageData(IplImage *img)
{
	std::vector<char> ret;
	for(int i = 0; i < img->imageSize; i++)
		ret.push_back(img->imageData[i]);
	return ret;
}
 // A Simple Camera Capture Framework 
 int main() 
 {
	// Create a window in which the captured images will be presented
	cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
	// Show the image captured from the camera in the window and repeat
	while ( 1 ) 
	{
		// Get one frame
		//IplImage* frame = cvQueryFrame( capture );
		IplImage *frame = cvLoadImage("img.jpg", 1);
		
		if ( !frame ) 
		{
			fprintf( stderr, "ERROR: frame is null...\n" );
			getchar();
			break;
		}
		
		//Get Image Data and create new image, testing.
		std::vector<char> dat = GetImageData(frame);
		IplImage image(*frame);
		image.imageData = new char[dat.size()];
		for(size_t i = 0; i < dat.size(); i++)
			image.imageData[i] = dat[i];
		
		CvFont font;
		double hScale=1.0;
		double vScale=1.0;
		int    lineWidth=1;
		std::string imageSizeString = " " + dat.size();
		cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale,vScale,0,lineWidth);
		cvPutText (&image,imageSizeString.c_str(),cvPoint(0,0), &font, cvScalar(255,255,0));
		//cvPutText (&image,"Image Size",cvPoint(100,100), &font, cvScalar(0,0,0));

		cvShowImage("mywindow", &image);
		cvReleaseImage(&frame);
		delete [] image.imageData;
		//cvShowImage( "mywindow", frame );
		// Do not release the frame!
		//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
		//remove higher bits using AND operator
		if ( (cvWaitKey(10) & 255) == 27 ) break;
	}
   // Release the capture device housekeeping
//   cvReleaseCapture( &capture );
   cvDestroyWindow( "mywindow" );
   return 0;
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.