Page 1 of 1
help with magick++ => opencv image convert
Posted: 2010-09-08T13:37:53-07:00
by zorro_masitas
hello,
i am trying to convert an image from
to
which is used in openCV. I trying to eavde the extra step of saving to disk then loading again which works. so im using Blobs hoping that what i wanted will work:
-Write the magick++ Image object into a BLOB.
-Create the IplImage
-set data pointer to the BLOB data.
-show IplImage to check if its correct
but i get only random noise pixels on the converted image
Code: Select all
//magick++ part
Image magicImage("testImage.jpg");
int WIDTH = magicImage.columns();
int HEIGHT = magicImage.rows();
//Blob part
Blob myBlob;
magicImage.write(&myBlob);
//openCV part
IplImage *cvImage = cvCreateImage(cvSize(WIDTH, HEIGHT), IPL_DEPTH_8U, 3);
memcpy(cvImage->imageData, (char*)myBlob.data(), myBlob.length() );
cvShowImage("thumbnail",cvImage);
cvWaitKey(20);
this code executes but does not work how i wanted, i only get noise pixels on the IplImage* side.
any clue where could be the problem?
thanks in advance
edit: info on magick++ Blob class is here:
http://www.imagemagick.org/Magick++/Blob.html
Re: help with magick++ => opencv image convert
Posted: 2010-09-08T14:24:02-07:00
by el_supremo
I don't use Magick++, but I think what might be happening is that the Blob will contain not only the image data but also the JPG header, whereas it looks like cvCreateImage is expecting only the raw RGB data. Try creating your own block of memory for the image and then use this to write the image data into it:
Code: Select all
magickImage.write(0,0,WIDTH,HEIGHT,"RGB",CharPixel,your_block);
and then copy the data from your_block to cvImage.
Pete
Re: help with magick++ => opencv image convert
Posted: 2010-09-08T16:01:06-07:00
by zorro_masitas
supremo
many thanks!!!
your advice was correct, it works that way.
here is the final code
Code: Select all
//magick++ part
Image magicImage("testImage.jpg");
int WIDTH = magicImage.columns();
int HEIGHT = magicImage.rows();
//Blob part
char* block = (char*)malloc(WIDTH*HEIGHT*3);
magicImage.write(0,0, WIDTH, HEIGHT, "BGR", CharPixel, block);
//openCV part
IplImage *cvImage = cvCreateImage(cvSize(WIDTH, HEIGHT), IPL_DEPTH_8U, 3);
memcpy(cvImage->imageData, block, WIDTH*HEIGHT*3 );
cvShowImage("thumbnail",cvImage);
cvWaitKey(20);
There were 2 key aspects improtant to mention.
1) when doing malloc and memcpy, consider the size the triple because of each color channel "RGB".
2) when writing to the block, invert the channels, thats why i put "BGR" on the parameter.
thanks and best regards
Re: help with magick++ => opencv image convert
Posted: 2010-09-10T15:29:25-07:00
by zorro_masitas
i realized that some images are in CR2 format, which is one of the RAW ones.
when i launch my program with those images, i get a little diferent result on the video, some different colors.
is it possible to open the "CR2" file and convert it internally in my C++ program to JPG?? before sending the image data to the OpenCV part¿???, that way i would be sure that it works.
Re: help with magick++ => opencv image convert
Posted: 2010-09-10T15:55:33-07:00
by el_supremo
After you've read the image, add a statement to your program so that it writes the image as a jpg:
Pete
Re: help with magick++ => opencv image convert
Posted: 2010-09-12T18:36:41-07:00
by zorro_masitas
supremo,
you mean saving to disk as jpg , then read it back right?,this is indeed an option, but i was looking for an RAM only method if pssible.
with that as a goal, i found that i can write to a Blob specifiyng a format, this has worked

.
Code: Select all
Blob tmp;
imgOriginal.write(&tmp, "jpg");
Image img(tmp);
..
..
..
this worked. but maybe a better solution would be to know what channels does the CR2 format use???
so i could write the pixels appropiately without needing to convert to jpg
i would like to have this type of code, where CR2_NUMCHANNELS and CR2_CHANNEL_FORMAT would be known.
any idea?
Code: Select all
//magick++ part
Image magicImage("testImage.jpg");
int WIDTH = magicImage.columns();
int HEIGHT = magicImage.rows();
//Blob part
char* block = (char*)malloc(WIDTH*HEIGHT*CR2_NUMCHANNELS);
magicImage.write(0,0, WIDTH, HEIGHT, CR2_CHANNEL_FORMAT, CharPixel, block);
//openCV part
IplImage *cvImage = cvCreateImage(cvSize(WIDTH, HEIGHT), IPL_DEPTH_8U, 3);
memcpy(cvImage->imageData, block, WIDTH*HEIGHT*CR2_NUMCHANNELS );
cvShowImage("thumbnail",cvImage);
cvWaitKey(20);