Question: I am capturing images in real time using OpenCV, and I want to show these images in the OGRE window as a background. So, for each frame the background will change.

I am trying to use `MemoryDataStream` along with `loadRawData` to load the images into an OGRE window, but I am getting the following error:

> 
> OGRE EXCEPTION(2:InvalidParametersException): Stream size does not
>  match calculated image size in Image::loadRawData at
>  ../../../../../OgreMain/src/OgreImage.cpp (line 283)
> 
> 
> 

An image comes from OpenCV with a size of 640x480 and `frame->buffer` is a type of `Mat` in OpenCV 2.3. Also, the pixel format that I used in OpenCV is `CV_8UC3` (i.e., each pixel is 8-bits and each pixel contains 3 channels ( B8G8R8 ) ).

```
Ogre::MemoryDataStream* videoStream = new Ogre::MemoryDataStream((void*)frame->buffer.data, 640*480*3, true);
Ogre::DataStreamPtr ptr(videoStream,Ogre::SPFM_DELETE);
ptr->seek(0);
Ogre::Image* image =  new Ogre::Image();
image->loadRawData(ptr,640, 480,Ogre::PF_B8G8R8 );
texture->unload();
texture->loadImage(*image)

```

Why I always getting this memory error?

Answer: 
Quick idea, maybe memory 4-byte alignment issues ?

see [Link 1](https://stackoverflow.com/questions/1712899/what-is-the-memory-structure-of-opencvs-cvmat) and 
[Link 2](http://chi3x10.wordpress.com/2008/05/07/be-aware-of-memory-alignment-of-iplimage-in-opencv/)
I had similar problems to get image data into OGRE, in my case the data came from ROS (see ros.org). The thing is that your data in frame->buffer is not RAW, but has a file header etc.
I think my solution was to search the data stream for the beginning of the image (by finding the appropriate indicator in the data block, e.g. 0x4D 0x00), and inserting the data from this point on.

You would have to find out were in your buffer the header ends and where your data begins.