Login  Register

Re: RGB Split setColor(Color.BLUE)

Posted by Boizeau marielaure on Jan 23, 2008; 9:00am
URL: http://imagej.273.s1.nabble.com/RGB-Split-setColor-Color-BLUE-tp3697482p3697486.html

Thank you very much that works very well.


void SaveImage() {
                ImageWindow win55 = IJ.getImage().getWindow();
                ImagePlus imagetosave =win55.getImagePlus();

int w = imagetosave.getWidth(), h = imagetosave.getHeight();
     ImageProcessor ip = imagetosave.getProcessor();
     int[] cPixels = (int[])ip.getPixels();   //works with RGB images
only
     byte[] bPixels = new byte[w * h];        //Java initializes  values
to zero
     for (int i=0; i<w*h; i++)                //for all pixels
         if ((cPixels[i]&0xff) >= 128)
             bPixels[i] = (byte)255;
     new ImagePlus("mask", new ByteProcessor(w, h, bPixels,
null)).show();

    String pathsave =dir+"/imagesretraitees/"+listTIF[i];
                IJ.saveAs("Tiff", pathsave);

                //close
                ImageWindow win5 = IJ.getImage().getWindow();
                win5.close();
                WindowManager.closeAllWindows();
                }

Have a good day

Marie



-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of
Michael Schmid
Sent: Tuesday, January 22, 2008 6:30 PM
To: [hidden email]
Subject: Re: RGB Split setColor(Color.BLUE)

Hi Marie-Laure,

if you need only the blue channel, you might do it even faster, see
below.
But also the version of Dscho should finish in a fraction of a second
unless you have a multi-Megapixel image or Java needs to free memory
filled with a lot of small objects before creating the new image.

     int w = imagetosave.getWidth(), h = imagetosave.getHeight();
     ImageProcessor ip = imagetosave.getProcessor();
     int[] cPixels = (int[])ip.getPixels();   //works with RGB images  
only
     byte[] bPixels = new byte[w * h];        //Java initializes  
values to zero
     for (int i=0; i<w*h; i++)                //for all pixels
         if ((cPixels[i]&0xff) >= 128)
             bPixels[i] = (byte)255;
     new ImagePlus("mask", new ByteProcessor(w, h, bPixels,
null)).show();


For the green component you would need
     if ((cPixels[i]&0xff00) >= (128<<8)) and for red
     if ((cPixels[i]&0xff0000) >= (128<<16))

This should be faster because it has no method call in the inner loop.
In Java, the overhead of a method call is comparable to maybe a dozen
arithmetic operations.

Michael
________________________________________________________________
On 22 Jan 2008, at 17:31, Marie-Laure Boizeau wrote:

> I had try to develop this double "for loop", but it's still slow.
> Is there anybody have an other way out ?
>
> Thank you for your help
>
> Marie
>
>
> -----Original Message-----
> From: Johannes Schindelin [mailto:[hidden email]]
> Sent: Monday, January 21, 2008 1:05 PM
> To: Boizeau, Marie-Laure SMA/FR
> Cc: [hidden email]
> Subject: Re: RGB Split setColor(Color.BLUE)
>
> Hi,
>
> On Mon, 21 Jan 2008, Marie-Laure Boizeau wrote:
>
>> I have develop a plugin with a step of splitting an RGB image and
>> then
>
>> I have to threshold it.
>>
>> part of the plugin :
>>
>>     ImagePlus imagetosave =win55.getImagePlus();
>>     IJ.run("RGB Split");
>>     IJ.setThreshold(128, 255);
>>     IJ.run("Convert to Mask");
>>     String pathsave =dir+"/imagesretraitees/"+listTIF[i];
>>     IJ.saveAs("Tiff", pathsave);
>>
>> the IJ.run("RGB Split") take a lot of time.
>
> Why don't you do the split yourself?
>
> Something like
>
> int w = imagetosave.getWidth(), h = imagetosave.getHeight();
> byte[] pixels = new byte[w * h];
> for (int y = 0; y < h; y++)
> for (int x = 0; x < w; x++)
> byte[x + y * w] =
> (imagetosave.getPixel(x, y)[0] >= 128 ?
> 255 : 0);
> new ImagePlus("mask", new ByteProcessor(w, h, pixels, null));
>
> If that is still too slow, you will have to work on a lower level,
> such as getting the int[] array of the ColorProcessor directly, and
> get at the red value (or the blue value) with boolean operations.
>
> Hth,
> Dscho