RGB Split setColor(Color.BLUE)

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

RGB Split setColor(Color.BLUE)

Boizeau marielaure
Hi,
 
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.
 
I had try to use :
imagetosave.setColor(Color.BLUE);
instead. But it doen't work.
 
Have you some way out to save time.
Thank you
 
Marie
 
 
Reply | Threaded
Open this post in threaded view
|

Re: RGB Split setColor(Color.BLUE)

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

Re: RGB Split setColor(Color.BLUE)

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

Re: RGB Split setColor(Color.BLUE)

Wayne Rasband
Here is a faster version that uses a single for loop.

    public class Split_Blue implements PlugIn {
       public void run(String arg) {
          ImagePlus imp = IJ.getImage();
          if (imp.getBitDepth()!=24)
             {IJ.error("RGB image required"); return;}
          int[] pixels = (int[])imp.getProcessor().getPixels();
          int w=imp.getWidth(), h=imp.getHeight();
          int size = w*h;
          ImageProcessor ip2 = new ByteProcessor(w, h);
          byte[] bytes = (byte[])ip2.getPixels();
          for (int i=0; i<size; i++)
             bytes[i] = (byte)(pixels[i]&0xff);
          new ImagePlus("Blue Channel", ip2).show();
       }
    }

-wayne

On Jan 22, 2008, at 11:31 AM, 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
>
Reply | Threaded
Open this post in threaded view
|

Re: RGB Split setColor(Color.BLUE)

Michael Schmid
In reply to this post by Boizeau marielaure
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
Reply | Threaded
Open this post in threaded view
|

IJ 1.38/OSX macro crash

Mark J. Chopping
Greetings,

I have a macro that refuses to finish without IJ 1.38 crashing (on a G5
Mac running Mac OS X 10.4.9). The macro and crash log are here:
http://pages.csam.montclair.edu/~chopping/chopp/ijcrash.html Does anyone
have any idea why it cannot get through the (several hundreds of)
iterations OK?  Or perhaps pointers on how to debug (the memory monitor
would disappear when IJ crashes)? The machine has 4 GB of additional RAM.
I suspect a memory leak in the MultiThresholder/Max Entropy routine but
cannot be sure.

  -- Mark

  Mark J. Chopping, Ph.D.
  Associate Professor, Earth & Environmental Studies
  Montclair State University, Montclair, NJ 07043
  NASA EOS/MISR ST/LCLUC ST/North American Carbon Program
  Tel: (973) 655-7384  Fax: (973) 655-4072
  -------------------------------------------------------
  http://csam.montclair.edu/~chopping/wood
Reply | Threaded
Open this post in threaded view
|

Re: RGB Split setColor(Color.BLUE)

Boizeau marielaure
In reply to this post by Michael Schmid
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