Exception on getPixels() ImageProcessor method

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Exception on getPixels() ImageProcessor method

nicola76b@libero.it
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???

Thanks,

    Nicola
Reply | Threaded
Open this post in threaded view
|

Re: Exception on getPixels() ImageProcessor method

Wayne Rasband
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
Reply | Threaded
Open this post in threaded view
|

Re: Exception on getPixels() ImageProcessor method

nicola76b@libero.it
In reply to this post by nicola76b@libero.it
great, works..

THANK YOU

    Nicola



---------- Initial Header -----------

From      : "ImageJ Interest Group" [hidden email]
To          : [hidden email]
Cc          :
Date      : Wed, 5 Jul 2006 09:44:17 -0400
Subject : Re: Exception on getPixels() ImageProcessor method







> 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
>