Page 1 of 1
How to read raw image data?
Posted: 2008-09-25T17:16:19-07:00
by simba.buffalo
Hi guys, I have a question about how to read raw image data to MagickWand.
I have got a raw image data (char *data) by other methods ( from OpenCV ), now I want to put these data into my MagickWand and do some more processing and save it.
I tried to figure out how to do it but no good result.
Is there any example for this?
Thanks
simba
Re: How to read raw image data?
Posted: 2008-09-25T17:49:51-07:00
by el_supremo
I know of two ways. The one I am more sure of though is to first read the file in to memory (e.g. unsigned char *data).
Something like this:
Code: Select all
// ... read the data in to memory and then do this
magick_wand = NewMagickWand();
MagickConstituteImage(magick_wand,width,height,"RGB",CharPixel,data);
The "RGB" could be "BGR" if the data is in the reverse order, or if you also have transparency it could be something like "RGBA".
The other way is to read the file directly. I think this will work but haven't tried it:
Code: Select all
magick_wand = NewMagickWand();
MagickSetSize(magick_wand,width,height);
MagickReadImage(magick_wand,"rgb:filename.raw");
Pete
Re: How to read raw image data?
Posted: 2008-09-25T19:33:19-07:00
by simba.buffalo
Thanks Pete, it works:)
Another question is when I destroy magickwand by using DestroyMagickWand(), it seems delete those data also.
Is there any way un-combine data and my magickwand?
I have tried MagickRemoveImage() but seems no good for me.
Re: How to read raw image data?
Posted: 2008-09-26T07:25:08-07:00
by el_supremo
I presume that you now want to extract the image from the magickwand. In that case you use this function:
MagickBooleanType MagickGetImagePixels(MagickWand *wand,
const long x,const long y,const unsigned long columns,
const unsigned long rows,const char *map,const StorageType storage,
void *pixels)
for example:
Code: Select all
MagickGetImagePixels(magick_wand,0,0,width,height,"RGB",CharPixel,data);
Pete
Re: How to read raw image data?
Posted: 2008-09-26T12:46:18-07:00
by simba.buffalo
Actually what I want to do is to read/write a image file by using MagickWand, but process those image data using other library.
So what I did is, load image file and get image pixels to my structure "img" in one routine:
Code: Select all
MagickWandGenesis();
img_wand = NewMagickWand();
status = MagickReadImage(img_wand, filename);
status = MagickGetImagePixels ( img_wand, 0, 0, width, height,
map, CharPixel, img->data.ptr );
DestroyMagickWand( img_wand );
MagickWandTerminus();
return img;
Then do some image processing stuff, and save this image in another routine:
Code: Select all
MagickWandGenesis();
img_wand = NewMagickWand();
data = (char*)malloc(sizeof(char) * img->step * img->rows);
memcpy( data, img->data.ptr, img->step * img->rows );
MagickConstituteImage(img_wand, img->cols, img->rows,
map, CharPixel, data);
status = MagickWriteImages(img_wand, filename, MagickTrue);
if (status == MagickFalse) {
#ifdef NDEBUG
ThrowWandException(img_wand);
#endif
return -1;
}
DestroyMagickWand( img_wand );
MagickWandTerminus();
Now the problem I have met are:
If loaded image is RGB format(32bits for each pixel), how can I get pixels as grayscale level(8 bits for each pixel), and how to do it reversely?
In the save image routine, when I destroy magickwand, it cause memory leak, so I am doubt that I misunderstood paramter "map" and "StorageType".
Thanks for reply.
Re: How to read raw image data?
Posted: 2008-09-26T14:01:18-07:00
by el_supremo
The types you can use when you call MagickConstituteImage or MagickGetImagePixels are:
Code: Select all
CharPixel, DoublePixel, FloatPixel, IntegerPixel, LongPixel, QuantumPixel, or ShortPixel
so I presume that instead of using CharPixel your 32 bit images would need IntegerPixel, which is a 32-bit unsigned integer.
If loaded image is RGB format(32bits for each pixel), how can I get pixels as grayscale level(8 bits for each pixel)
You might be able to do this by just extracting an intensity. I haven't tried it but this might work:
Code: Select all
MagickGetImagePixels(magick_wand,0,0,width,height,"I",CharPixel,data);
This should create one byte per pixel containing the grayscale intensity.
and how to do it reversely?
Just use "I",CharPixel in the call to constitute the image.
Pete
Re: How to read raw image data?
Posted: 2008-09-26T22:36:29-07:00
by simba.buffalo
Thanks Pete, it is almost done.
The remain problem is when I save my grayscale image (MagickWriteImages), the file is still 24-bits for each pixels not 8bits for each.
Do I need to set something before saving?
sim
Re: How to read raw image data?
Posted: 2008-09-28T08:12:02-07:00
by el_supremo
If your image is already grayscale then I think all it needs is:
Code: Select all
MagickSetImageDepth(magick_wand,8);
but you might also need
Code: Select all
MagicketImageColorspace(magick_wand,GRAYColorspace);
Pete
Re: How to read raw image data?
Posted: 2008-09-28T13:02:53-07:00
by simba.buffalo
Now I have a array char * pixels = (char*)malloc(256*256); which corresponds to an gray level image 256*256.
Code: Select all
img_wand = NewMagickWand();
status = MagickConstituteImage(img_wand, 256, 256,
"I", CharPixel, pixels);
status = MagickWriteImages(img_wand, filename, MagickTrue);
img_wand = DestroyMagickWand( img_wand );
No matter I am using MagickSetImageType, MagickSetImageDepth, MagickSetImageColorspace or any combination of them, it dose not work. The saved image is still 24-bits for each pixel.
The odd thing is that using MagickGetImageDepth, it returns 1, not 8. Or using MagickGetImageType, the return value is "BilevelType", not "GrayscaleType" instead.
Re: How to read raw image data?
Posted: 2008-09-28T15:15:45-07:00
by el_supremo
What is filename? If you write the image as a JPG, for example, it will be 24 bit.
When I create a grayscale image in memory, use constitute and then write it as a PNG file (with no depth or colorspace setting) the resulting PNG file is 8-bit.
Pete
Re: How to read raw image data?
Posted: 2008-09-28T16:16:02-07:00
by simba.buffalo
what I saved is a bmp file.
I just read a grayscale bmp file whose size is 937426 bytes and save it, then the size becoms 2800306 bytes, almost 3 time of original one.
Re: How to read raw image data?
Posted: 2008-09-28T19:17:06-07:00
by el_supremo
Add this after the MagickConstituteImage:
Code: Select all
MagickQuantizeImage(magick_wand,256,GRAYColorspace,0,MagickFalse,MagickFalse);
Pete