How to read raw image data?
How to read raw image data?
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
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
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: How to read raw image data?
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:
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:
Pete
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 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");
Re: How to read raw image data?
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.
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.
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: How to read raw image data?
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:
Pete
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);
Re: How to read raw image data?
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:
Then do some image processing stuff, and save this image in another routine:
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.
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;
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();
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.
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: How to read raw image data?
The types you can use when you call MagickConstituteImage or MagickGetImagePixels are:
so I presume that instead of using CharPixel your 32 bit images would need IntegerPixel, which is a 32-bit unsigned integer.
This should create one byte per pixel containing the grayscale intensity.
Pete
Code: Select all
CharPixel, DoublePixel, FloatPixel, IntegerPixel, LongPixel, QuantumPixel, or ShortPixel
You might be able to do this by just extracting an intensity. I haven't tried it but this might work:If loaded image is RGB format(32bits for each pixel), how can I get pixels as grayscale level(8 bits for each pixel)
Code: Select all
MagickGetImagePixels(magick_wand,0,0,width,height,"I",CharPixel,data);
Just use "I",CharPixel in the call to constitute the image.and how to do it reversely?
Pete
Re: How to read raw image data?
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
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
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: How to read raw image data?
If your image is already grayscale then I think all it needs is:
but you might also need
Pete
Code: Select all
MagickSetImageDepth(magick_wand,8);
Code: Select all
MagicketImageColorspace(magick_wand,GRAYColorspace);
Re: How to read raw image data?
Now I have a array char * pixels = (char*)malloc(256*256); which corresponds to an gray level image 256*256.
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.
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 );
The odd thing is that using MagickGetImageDepth, it returns 1, not 8. Or using MagickGetImageType, the return value is "BilevelType", not "GrayscaleType" instead.
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: How to read raw image data?
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
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?
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.
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.
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: How to read raw image data?
Add this after the MagickConstituteImage:
Pete
Code: Select all
MagickQuantizeImage(magick_wand,256,GRAYColorspace,0,MagickFalse,MagickFalse);