help with magick++ => opencv image convert

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
zorro_masitas

help with magick++ => opencv image convert

Post by zorro_masitas »

hello,

i am trying to convert an image from

Code: Select all

Image magicImage("image.jpg");
to

Code: Select all

IplImage* cvImage;
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
Last edited by zorro_masitas on 2010-09-10T15:27:18-07:00, edited 2 times in total.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: help with magick++ => opencv image convert

Post 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
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
zorro_masitas

Re: help with magick++ => opencv image convert

Post 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
zorro_masitas

Re: help with magick++ => opencv image convert

Post 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.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: help with magick++ => opencv image convert

Post by el_supremo »

After you've read the image, add a statement to your program so that it writes the image as a jpg:

Code: Select all

magicImage.write("test.jpg");
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
zorro_masitas

Re: help with magick++ => opencv image convert

Post 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 :D.

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);
Post Reply