Login  Register

Re: Exception on getPixels() ImageProcessor method

Posted by Wayne Rasband on Jul 05, 2006; 2:44pm
URL: http://imagej.273.s1.nabble.com/Exception-on-getPixels-ImageProcessor-method-tp3702293p3702295.html

On Jul 5, 2006, at 9:03 AM, [hidden email] wrote:

> Hi all,
> I'm developing a Java application that use ij libraries.
> I'm writing a class that process a variable number of JPG input images
> and calculate averagePixels image.
> Normally input images are like "blank" image (or enough bright..).
>
> I've see that procedure work, but on some images (especially when
> images are particular bright..) a "java.lang.ClassCastException: [B"
> occours.
>
> Here's a simplify parted of incriminated code:
> ------------------------------------------------------------------
> int dim = (imp.getWidth() * imp.getHeight());
>
> int[] pixelData  = new int[dim];
> pixelData = (int[]) imp.getProcessor().getPixels();
> ------------------------------------------------------------------
>
> After many trying, I've see that if in place of "int" I use "byte"
> error does not occour.
> On the whole I use jpg COLOR images, so I think to understand reading
> "writing ImageJ Plugins-A tutorial", I should use ColorProcessor that
> return the pixels array as an int[]....
>
> How can I avoid this problem??
> Is there a simple tip to  obtain getpixels() as an integer array for
> ALL passed images???

ImageJ opens grayscale JPEGs as 8-bit images so you need to convert
back to RGB to insure that getPixels() returns an int array.

     ImageProcessor ip = imp.getProcessor();
     ip = ip.convertToRGB()
     int[] pixelData = (int[]) imp.getPixels();

There is no need to allocate the int array using

     int dim = (imp.getWidth() * imp.getHeight());
     int[] pixelData  = new int[dim];

because getPixels() return a reference to the pixel data, not a copy.

-wayne