Magick++ get pixel intensities for an image

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
ese
Posts: 6
Joined: 2011-01-16T00:04:18-07:00
Authentication code: 8675308

Magick++ get pixel intensities for an image

Post 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++?
ese
Posts: 6
Joined: 2011-01-16T00:04:18-07:00
Authentication code: 8675308

Re: Magick++ get pixel intensities for an image

Post 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..)
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Magick++ get pixel intensities for an image

Post 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 );
ese
Posts: 6
Joined: 2011-01-16T00:04:18-07:00
Authentication code: 8675308

Re: Magick++ get pixel intensities for an image

Post 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.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Magick++ get pixel intensities for an image

Post 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.
ese
Posts: 6
Joined: 2011-01-16T00:04:18-07:00
Authentication code: 8675308

Re: Magick++ get pixel intensities for an image

Post 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
Last edited by ese on 2011-01-27T18:48:32-07:00, edited 1 time in total.
ese
Posts: 6
Joined: 2011-01-16T00:04:18-07:00
Authentication code: 8675308

Re: Magick++ get pixel intensities for an image

Post 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.
ese
Posts: 6
Joined: 2011-01-16T00:04:18-07:00
Authentication code: 8675308

Re: Magick++ get pixel intensities for an image

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