Page 1 of 1

Magick++ get pixel intensities for an image

Posted: 2011-01-16T00:15:21-07:00
by ese
Hi,

I'm trying to get an array of pixel intensities for an image with ImageMagick in C++ via Magick++.

I found out how to do it in Perl:

Code: Select all

@pixels = $image->GetPixels(map=>'I', height=>$height, width=>$width, normalize=>true);
How can I do the same in C++?

Re: Magick++ get pixel intensities for an image

Posted: 2011-01-16T10:50:37-07:00
by ese
I see this is possible in C, using MagickWand and/or MagickCore.

The closest method in Magick++ seems to be getPixel, however this returns a PixelPacket. How can I simple get a double of intensities as there is no way to pass the map to this function.

Would it be faster to just use libs to read images and access pixels via their interfaces? It looks like ImageMagick uses lib* for different formats (JPEG, GIF, PNG, etc..)

Re: Magick++ get pixel intensities for an image

Posted: 2011-01-16T12:14:32-07:00
by magick
getPixels() would work. To get normalized values simply divide the color components by QuantumRange (p.red / QuantumRange). Alternatively, write the intensity pixels to a pixel buffer one scanline at a time (for example):
  • image.write( 0, 0, 640, 1, "I", DoublePixel, pixels );

Re: Magick++ get pixel intensities for an image

Posted: 2011-01-16T13:20:12-07:00
by ese
Thanks magick.

Do you know which is faster? I'm writing a modified radon transform algorithm on a couple million images so speed is very important.

All I want to do is to read the image, run a Gaussian blur and then get pixel intensities into an array as fast as possible.

Re: Magick++ get pixel intensities for an image

Posted: 2011-01-16T13:28:51-07:00
by magick
The fastest times generally come from scanning over the image pixels with OpenMP enabled. See http://www.imagemagick.org/script/archi ... hp#threads for some example code in MagickCore and Magick++. ImageMagick includes a Radon implementation in magick/shear.c/DeskewImage(). It even includes a method to export the Radon cells to / from memory to / from disk if the cells exceed a thresholded memory limit.

Re: Magick++ get pixel intensities for an image

Posted: 2011-01-16T22:39:40-07:00
by ese
My current development machine is a a slightly old single core, so OpenMP wont help until I get a new system :)

I'm implementing a generalization of the radon transform, the trace transform so the included method wouldn't really help.

Here is what I am doing:
  1. Read input image (various formats)
  2. Resize/scale image
  3. Gaussian blur on image
  4. Extract pixel intensity into array
Therefore I think the best method would be image.write as you mentioned after modifying the image.

Here's a quick hack code snippet:

Code: Select all

Image img("test.jpg");
Geometry geo(640,480);
img.scale(geo);
img.blur(1,0.5);
double *pixels = new double[640*480];
image.write( 0, 0, 640, 480, "I", DoublePixel, pixels );
edit: Runtime ~0.74s on a 1.4 Ghz Athlon

Re: Magick++ get pixel intensities for an image

Posted: 2011-01-26T22:30:07-07:00
by ese
I'm still having issues with pixel input, is it possible to simply get 8-bit grayscale intensities for an image?
e.g. The value for any pixel is between [0 .. 255]

I have tried the following without success.

Code: Select all

char *pixels = new char[width*height];
image.write( 0, 0, width, height, "I", CharPixel, pixels );
With CharPixel I get numbers that are negative and positive.

Code: Select all

unsigned int *pixels = new unsigned int[width*height];
image.write( 0, 0, width, height, "I", IntegerPixel, pixels );
With IntegerPixel I get really large numbers.

Re: Magick++ get pixel intensities for an image

Posted: 2011-01-27T21:08:01-07:00
by ese
Fixed the issue by using 'unsigned char' for CharPixels.

Code: Select all

unsigned char *pixels = new unsigned char[width*height];
image.write( 0, 0, width, height, "I", CharPixel, pixels );