Re: Specific color channel to retrieve from an image in a plugin

Posted by Rob van 't Hof-2 on
URL: http://imagej.273.s1.nabble.com/Specific-color-channel-to-retrieve-from-an-image-in-a-plugin-tp5004950p5005067.html

Hi,
getPixelValue is not particularly fast. The fastest way of going through
an image pixel by pixel is using the 1-dimensional array access methods.

Here is a code snippet:

int[] colPixels = (int[]) ip.getPixels();
PixelCount = ip.getPixelCount();

      for (int x = 0; x < PixelCount; x++) {
            //get brightness from shading image
            int  pixVal = colPixels [x];
             //get red , green and blue values from pixVal
            int  redVal = (pixVal & 0xff0000) >> 16;
            int  greenVal = (pixVal & 0x00ff00) >> 8;
            int  blueVal = (pixVal & 0x0000ff);

                 if  (blueVal <redVal &&blueVal <greenVal )
                      blueVal =0;
                  }
         }

Hope this helps,

Rob



On 07/10/2013 11:26, Herbie wrote:

> Philippe,
>
> you say you need to inspect every pixel of your image and that this is
> slow.
>
> Sorry, but I don't think it is slow but it simply takes time,
> especially if your images are big. Don't you agree?
>
> If you do the loop over all pixels in Java, I see little chance to
> speed up the desired process.
>
> Best
>
> Herbie
> _________________________________________
> On 07.10.13 10:16, Philippe GENDRE wrote:
>> Dear List,
>>
>> Recently, I posted a question to this list to retrieve a specific color
>> channel (the red one for example) from a RGB image. The answer
>> (ImageProcessor red = ((ColorProcessor)ip).getChannel(1, null);) is
>> perfect
>> but I have realized that it is not what I need. In fact I need to
>> retrieve
>> a specific color of pixel for example the blue ones and not the
>> intensity
>> of the channel. According to that I wrote the following code:
>> ImageProcessor ipR=((ColorProcessor)ip).getChannel(1,null);
>> ImageProcessor ipG=((ColorProcessor)ip).getChannel(2,null);
>> ImageProcessor ipB=((ColorProcessor)ip).getChannel(3,null);
>>
>>      for (int x=0; x<frame_width; x++){
>>          for (int y=0; y<frame_height; y++){
>>              if
>> (ipB.getPixelValue(x,y)<ipR.getPixelValue(x,y)&&ipB.getPixelValue(x,y)<ipG.getPixelValue(x,y))
>>
>>                  ipB.set(x,y,0);
>>          }
>>      }
>> That sounds good but slow. How to speed up this ?
>>
>> Best regards,
>>
>> Philippe
>>
>>
>> 2013/9/26 Philippe GENDRE <[hidden email]>
>>
>>> ImageProcessor red = ((ColorProcessor)ip).getChannel(1, null) is
>>> exactly
>>> what I needed.
>>>
>>> This list is a marvel. Thanks a lot.
>>>
>>> Philippe
>>>
>>>
>>> 2013/9/26 Rasband, Wayne (NIH/NIMH) [E] <[hidden email]>
>>>
>>> On Sep 26, 2013, at 1:02 PM, Philippe GENDRE wrote:
>>>>
>>>>> Dear List,
>>>>>
>>>>> I need to retrieve the red channel of an RGB image plus object as an
>>>> image
>>>>> processor in a plugin.
>>>>>
>>>>> What is the best solution ?
>>>>
>>>> Use the ImageProcessor.getChannel() method. Here is a JavaScript
>>>> example:
>>>>
>>>>     imp = IJ.openImage("http://imagej.nih.gov/ij/images/clown.jpg");
>>>>     ip = imp.getProcessor();
>>>>     red = ip.getChannel(1, null);
>>>>     new ImagePlus("Red Channel", red).show();
>>>>
>>>> In a plugin, it would look like this:
>>>>
>>>>     ImagePlus imp = IJ.openImage("
>>>> http://imagej.nih.gov/ij/images/clown.jpg");
>>>>     ImageProcessor ip = imp.getProcessor();
>>>>     ImageProcessor red = ((ColorProcessor)ip).getChannel(1, null);
>>>>     new ImagePlus("Red Channel", red).show();
>>>>
>>>> -wayne
>>>>
>>>> --
>>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>>>
>>>
>>>
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
_____________________________
Dr. Rob van 't Hof
Reader

Centre for Molecular Medicine
MRC IGMM
University of Edinburgh
Western General Hospital
Crewe Road, Edinburgh EH4 2XU
United Kingdom

Phone: (+44)-131-6511031
email: [hidden email]
_____________________________

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html