I am trying to pass a set of iplimage from OpenCV to another program (.net) that does not use iplimage but uses list of byte[] instead through c++/cli. Any idea how to add/convert iplimage to the list of byte[] ?
Thanks.
I am trying to pass a set of iplimage from OpenCV to another program (.net) that does not use iplimage but uses list of byte[] instead through c++/cli. Any idea how to add/convert iplimage to the list of byte[] ?
Thanks.
If you want to copy exact pixels from iplimage.imageData, you can use Marshal.Copy
to copy data from an IntPtr
(imageData) to a managed array (byte[]).
But since imageData is uncompressed, you might want to use imencode
or cvEncodeImage
to compress it to a intermediate buffer and then copy that to a byte[]
with above code.
Before doing anything else, take the time to learn how an IplImage
is created, and what kind of information it stores.
Then, notice that the pixels of the image can be accessed through the member imageData
, which is an unsigned char*
:
IplImage* image = cvLoadImage("starwars.jpg", 0);
int img_sz = image->width * image->height * image->nChannels;
unsigned char* data = (unsigned char*) malloc(img_sz);
memcopy(data, image->imageData, img_sz);
You'll have to do a similar procedure to convert it to byte[]
. By the way, this post]2 shows (among other things) how to convert an IplImage*
to a SDL_Surface*
.
Firstly, I would recommend that you switch to the C++ API of OpenCV if possible - it offers a lot more flexibility and features as compared to the old C interface.
In the C++ interface, a snippet to perform the above task would be:
cv::Mat image = cv::imread(imname);
uchar *ptr = (uchar*)image.data
You can use your ptr( an 8 bit unsigned byte data ) wherever you want - do remember that modifying this data will modify the actual image. If you do not want that to happen, take a copy of the data using memcpy.
If I understand you right you search something like this :
IplImage* image = cvLoadImage("image.bmp");
std::list<unsigned char*> bytes;
for (int i=0;i<image->height;i++)
{
bytes.push_back(image->imageData + image ->widthStep*i);
}
you could test that this works doing the following (I access here the first pixel of the second row):
std::list<unsigned char*>::iterator it = bytes.begin();
it++;
*(*it) = 22;
int a = *((unsigned char*)(image->imageData + image->widthStep*1));
I didn t find no way to do things like this with the new cv::Mat object. You can transform the image to std::vectors but there is no way to transform the image to a row std::vector/std::list.
But if you remain use the cv::Mat class you can try the cv::Ptr<Pixelformat>
that also gives access to the row pointers.