using FileOpener on 16-bit grayscale in java app ?

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

using FileOpener on 16-bit grayscale in java app ?

Bill Christens-Barry
I have a 16-bit grayscale TIFF that I'd like to open, display, and work
with in a Java app that imports and draws on various classes in ij.jar.
I ran into trouble when I tried to use BufferedImage's TYPE_USHORT_GRAY
to ensure that I can get at the 16-bit data.

So far I haven't been successful and wondered if I might be able to do
this using ij. A quick look at the ImageJ source and API left me a bit
confused.

I'm trying to use the FileOpener, FileInfo, ImageReader, and
ShortProcessor classes to get at the 16-bit sample values of the image
data. In my toy program to work this out, based on the source from
FileOpener, the compiler gags with these errors:

Rastic8r.java:45: cannot find symbol
symbol  : method createColorModel(ij.io.FileInfo)
location: class ExtractRaster
       cm = createColorModel(fi);
                                         ^
Rastic8r.java:50: cannot find symbol
symbol  : method readPixels(ij.io.FileInfo)
location: class ij.io.ImageReader
        pixels = ir.readPixels(fi);
                     ^

In trying to understand the cause of these errors, I'm left wondering
whose createColorModel() and readPixels() methods are used in FileOpener
because I've imported java.awt.image.ColorModel, ij.io.FileOpener,
ij.io.ImageReader, and ij.io.FileInfo. Also, in my code I declared a new
ImageReader ir because I didn't see where Wayne had done this in the
FileOpener source. As I see it, readPixels() is a method of ImageReader,
so I thought I needed one. What am I misunderstanding here?

Can anyone help me understand this by explanation or example?

Thanks.

Bill Christens-Barry
Reply | Threaded
Open this post in threaded view
|

Re: using FileOpener on 16-bit grayscale in java app ?

Wayne Rasband
Use the getPixels() method of the ImageProcessor class to get the  
pixel data of an image. This example

    ImagePlus img = IJ.openImage(path);
    ImageProcessor ip = img.getProcessor();
    short[] data = (short[])ip.getPixels();

works for 16-bit images. Replace short[] with byte[] for 8-bits  
images, with float[] for 32-bits images and with int[] for RGB images.

-wayne

On Apr 10, 2006, at 5:38 PM, Bill Christens-Barry wrote:

> I have a 16-bit grayscale TIFF that I'd like to open, display, and  
> work with in a Java app that imports and draws on various classes  
> in ij.jar. I ran into trouble when I tried to use BufferedImage's  
> TYPE_USHORT_GRAY to ensure that I can get at the 16-bit data.
>
> So far I haven't been successful and wondered if I might be able to  
> do this using ij. A quick look at the ImageJ source and API left me  
> a bit confused.
>
> I'm trying to use the FileOpener, FileInfo, ImageReader, and  
> ShortProcessor classes to get at the 16-bit sample values of the  
> image data. In my toy program to work this out, based on the source  
> from FileOpener, the compiler gags with these errors:
>
> Rastic8r.java:45: cannot find symbol
> symbol  : method createColorModel(ij.io.FileInfo)
> location: class ExtractRaster
>       cm = createColorModel(fi);
>                                         ^
> Rastic8r.java:50: cannot find symbol
> symbol  : method readPixels(ij.io.FileInfo)
> location: class ij.io.ImageReader
>        pixels = ir.readPixels(fi);
>                     ^
>
> In trying to understand the cause of these errors, I'm left  
> wondering whose createColorModel() and readPixels() methods are  
> used in FileOpener because I've imported java.awt.image.ColorModel,  
> ij.io.FileOpener, ij.io.ImageReader, and ij.io.FileInfo. Also, in  
> my code I declared a new ImageReader ir because I didn't see where  
> Wayne had done this in the FileOpener source. As I see it,  
> readPixels() is a method of ImageReader, so I thought I needed one.  
> What am I misunderstanding here?
>
> Can anyone help me understand this by explanation or example?
>
> Thanks.
>
> Bill Christens-Barry
Reply | Threaded
Open this post in threaded view
|

Re: using FileOpener on 16-bit grayscale in java app ?

Bill Christens-Barry
In reply to this post by Bill Christens-Barry
Thanks for your feedback on this, Wayne. My difficulty is that I am
trying to avoid using an ImagePlus. I guess my question really is:

How can I get at the pixel data in a 16-bit unsigned grayscale image
without using an ImagePlus?

In my case, the image was created in ImageJ and saved as a TIFF. The
questions about FileOpener, readPixels, etc. came up while trying to
understand how ImageJ does it.

Bill Christens-Barry

> Use the getPixels() method of the ImageProcessor class to get the  
> pixel data of an image. This example
>
>     ImagePlus img = IJ.openImage(path);
>     ImageProcessor ip = img.getProcessor();
>     short[] data = (short[])ip.getPixels();
>
> works for 16-bit images. Replace short[] with byte[] for 8-bits  
> images, with float[] for 32-bits images and with int[] for RGB images.
>
> -wayne

On Apr 10, 2006, at 5:38 PM, Bill Christens-Barry wrote:

 > I have a 16-bit grayscale TIFF that I'd like to open, display, and
 > work with in a Java app that imports and draws on various classes
 > in ij.jar. I ran into trouble when I tried to use BufferedImage's
 > TYPE_USHORT_GRAY to ensure that I can get at the 16-bit data.
 >
 > So far I haven't been successful and wondered if I might be able to
 > do this using ij. A quick look at the ImageJ source and API left me
 > a bit confused.
 >
 > I'm trying to use the FileOpener, FileInfo, ImageReader, and
 > ShortProcessor classes to get at the 16-bit sample values of the
 > image data. In my toy program to work this out, based on the source
 > from FileOpener, the compiler gags with these errors:
 >
 > Rastic8r.java:45: cannot find symbol
 > symbol  : method createColorModel(ij.io.FileInfo)
 > location: class ExtractRaster
 >       cm = createColorModel(fi);
 >                                         ^
 > Rastic8r.java:50: cannot find symbol
 > symbol  : method readPixels(ij.io.FileInfo)
 > location: class ij.io.ImageReader
 >        pixels = ir.readPixels(fi);
 >                     ^
 >
 > In trying to understand the cause of these errors, I'm left
 > wondering whose createColorModel() and readPixels() methods are
 > used in FileOpener because I've imported java.awt.image.ColorModel,
 > ij.io.FileOpener, ij.io.ImageReader, and ij.io.FileInfo. Also, in
 > my code I declared a new ImageReader ir because I didn't see where
 > Wayne had done this in the FileOpener source. As I see it,
 > readPixels() is a method of ImageReader, so I thought I needed one.
 > What am I misunderstanding here?
 >
 > Can anyone help me understand this by explanation or example?
 >
 > Thanks.
 >
 > Bill Christens-Barry
Reply | Threaded
Open this post in threaded view
|

Re: using FileOpener on 16-bit grayscale in java app ?

Jeff Brandenburg
In reply to this post by Bill Christens-Barry
On Apr 10, 2006, at 5:38 PM, Bill Christens-Barry wrote:

> I have a 16-bit grayscale TIFF that I'd like to open, display, and
> work with in a Java app that imports and draws on various classes in
> ij.jar. I ran into trouble when I tried to use BufferedImage's
> TYPE_USHORT_GRAY to ensure that I can get at the 16-bit data.
>
> So far I haven't been successful and wondered if I might be able to do
> this using ij. A quick look at the ImageJ source and API left me a bit
> confused.
>
> I'm trying to use the FileOpener, FileInfo, ImageReader, and
> ShortProcessor classes to get at the 16-bit sample values of the image
> data. In my toy program to work this out, based on the source from
> FileOpener, the compiler gags with these errors:
>
> Rastic8r.java:45: cannot find symbol
> symbol  : method createColorModel(ij.io.FileInfo)
> location: class ExtractRaster
>       cm = createColorModel(fi);

I can't explain this, as the createColorModel method is clearly there
in FileOpener.  Or is ExtractRaster not based on FileOpener?

> Rastic8r.java:50: cannot find symbol
> symbol  : method readPixels(ij.io.FileInfo)
> location: class ij.io.ImageReader
>        pixels = ir.readPixels(fi);

It looks like you've skipped a step in this second part:

                        InputStream is = createInputStream(fi);
                        if (is==null)
                                return null;
                        ImageReader reader = new ImageReader(fi);
                        pixels = reader.readPixels(is);
                        is.close();

In other words, ImageReader.readPixels() wants an InputStream, not a
FileInfo.  You create the InputStream using FileOpener's
createInputStream() method.
--
        -jeffB (Jeff Brandenburg, Duke Center for In-Vivo Microscopy)