Possible to access pixels without creating java.awt.image

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

Possible to access pixels without creating java.awt.image

Brian Willkie-2
Hello,

Is it possible to create an ImageProcessor (one that accesses the  
pixels in a given tif file) without creating an instance of  
java.awt.Image?


        public ColorProcessor(Image img) {
                width = img.getWidth(null);
                height = img.getHeight(null);
                pixels = new int[width * height];
                PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixels,  
0, width);
                try {
                        pg.grabPixels();
                } catch (InterruptedException e){};
                createColorModel();
                fgColor = 0xff000000; //black
                resetRoi();
        }

Thanks,
Brian
Reply | Threaded
Open this post in threaded view
|

Re: Possible to access pixels without creating java.awt.image

Sami Badawi-2
Hi Brian,

ImageProcessor has its own very simple data structure that is
independent of java.awt.Image.
The constructors for ColorProcessor that you are showing the code for
is taking an java.awt.Image as input, but it is only used to take the
pixels and move them over to ColorProcessor's own field int[] pixels.
There are 2 other constructors that does not take an java.awt.Image.

        public ColorProcessor(int width, int height)
        public ColorProcessor(int width, int height, int[] pixels)

ImageProcessor contains the following fields based on java.awt.Image:

        protected Image img;
        protected BufferedImage image;

But they are only used for exporting to Image when you call:

        public Image createImage() {

You can use FileOpener to open your image. Here is a couple of links:
http://rsb.info.nih.gov/ij/developer/api/ij/io/FileOpener.html
http://www.mcdb.ucla.edu/research/hartenstein/acardona/imagej_programming_tutorials.html

FileOpener.open() will give you an ImagePlus object that you can get
your ImageProcessor from.

-Sami Badawi
http://www.shapelogic.org

On Mon, May 5, 2008 at 8:11 PM, Brian Willkie <[hidden email]> wrote:

> Hello,
>
>  Is it possible to create an ImageProcessor (one that accesses the pixels in
> a given tif file) without creating an instance of java.awt.Image?
>
>
>         public ColorProcessor(Image img) {
>                 width = img.getWidth(null);
>                 height = img.getHeight(null);
>                 pixels = new int[width * height];
>                 PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height,
> pixels, 0, width);
>                 try {
>                         pg.grabPixels();
>                 } catch (InterruptedException e){};
>                 createColorModel();
>                 fgColor = 0xff000000; //black
>                 resetRoi();
>         }
>
>  Thanks,
>  Brian
>
Reply | Threaded
Open this post in threaded view
|

Re: Possible to access pixels without creating java.awt.image

Brian Willkie-2
In reply to this post by Brian Willkie-2
Hi Wayne,

Ah, great! I'll take a look at ImageReader. Also off-list, Johannes  
Schindelin mentioned this:

> On X11 (Linux & Unix) it does not matter, because it is not heeded,  
> but on MacOSX, it could be the solution:
>
>        $ java -Djava.awt.headless=true -jar ij.jar -batch Arrays.txt
>
> I _think_ that the java.awt.headless property is respected on MacOSX.

A quick check on my laptop confirmed that this works. Unless some  
other obstacle comes up, this is likely a better solution. It might be  
worth adding to the wiki:

http://imagejdocu.tudor.lu/imagej-documentation-wiki/faq/how-do-i-run-imagej-without-a-graphics-environment-headless

I could set up an account to add it, unless someone who already has an  
account would like to. :)

Thanks to Johannes, Wayne, and Alberto for your help.

Cheers,
Brian


Quoting Wayne Rasband:

>
> On May 6, 2008, at 8:15 AM, [hidden email] wrote:
>
>> Hi Wayne,
>>
>> Thanks for the reply.
>>
>>> public ColorProcessor(int width, int height, int[] pixels) {
>> But that presupposes that I have the pixels. How do I get the  
>> pixels without creating an instance java.awt.Image?
>
> The ij.io.ImageReader class returns pixel arrays.
>
>> I'm trying to use the ImageJ API to build a command-line  
>> application. The problem that I'm running into is that core ImageJ  
>> classes (like ImagePlus, ImageProcessor, ImageStack) instantiate  
>> java.awt components (like ColorModel and Image). These cause my app  
>>  to create a GUI, even if they aren't visible, and even if I use  
>> headless ImageJA. Creating a GUI generates a window server error on  
>>  the Mac cluster I'm using.
>
> One way to avoid window server errors is to use Xvfb (X virtual framebuffer).
>
> -wayne
>
>> So I've been hacking up custom versions of the above classes,  
>> trying to strip out calls to java.awt classes, but I've reached a  
>> point where I have to instantiate a ColorProcessor. In order to  
>> make this work, I need to get rid of calls to  
>> ImageProcessor:getImage(). In order to do that, I need some  
>> alternative method to read in the pixels. java.awt.Image does it,  
>> so I assume it's possible. The question is "How?" (and the  
>> follow-up question is "How much code do I really want to re-write?").
>>
>> So I'm looking for suggestions to either question.
>>
>> Thanks,
>> Brian
>>
>>
>> Quoting Rasband Wayne:
>>
>>>> Is it possible to create an ImageProcessor (one that accesses the  
>>>>   pixels in a given tif file) without creating an instance of    
>>>> java.awt.Image?
>>>>
>>>>
>>>> public ColorProcessor(Image img) {
>>>> width = img.getWidth(null);
>>>> height = img.getHeight(null);
>>>> pixels = new int[width * height];
>>>> PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height,    
>>>> pixels, 0, width);
>>>> try {
>>>> pg.grabPixels();
>>>> } catch (InterruptedException e){};
>>>> createColorModel();
>>>> fgColor = 0xff000000; //black
>>>> resetRoi();
>>>> }
>>>>
>>>> Thanks,
>>>> Brian
>>>
>>> Brian,
>>>
>>> You can use this constructor
>>>
>>> /**Creates a ColorProcessor from a pixel array. */
>>> public ColorProcessor(int width, int height, int[] pixels) {
>>> if (pixels!=null && width*height!=pixels.length)
>>> throw new IllegalArgumentException(WRONG_LENGTH);
>>> this.width = width;
>>> this.height = height;
>>> createColorModel();
>>> fgColor = 0xff000000; //black
>>> resetRoi();
>>> this.pixels = pixels;
>>> }
>>>
>>> to create an ImageProcessor from an array of RGB pixels. What are you
>>> trying to accomplish?
>>>
>>> -wayne
>>
>>
>>