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 ? Thank you. Philippe -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hi Philippe,
On Thu, 26 Sep 2013, Philippe GENDRE wrote: > I need to retrieve the red channel of an RGB image plus object as an image > processor in a plugin. I suspect that you need to use the static getChannel() method of the ChannelSplitter class: http://jenkins.imagej.net/job/ImageJ1-javadoc/javadoc/ij/plugin/ChannelSplitter.html#getChannel%28ij.ImagePlus,%20int%29 Ciao, Johannes -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by Philippe GENDRE
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 |
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 |
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 |
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 |
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 |
Sorry, just noticed that there is a line that is not needed: the // get
brightness etc comment. The perils of copy and paste. Also you can further speed up things using multithreading if you use a more modern processor with multiple cores. this only gives substantial benefits if you are using really largish images (mine can be up to 100 Mpixels). Rob On 07/10/2013 13:15, Rob van 't Hof wrote: > 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 |
Thanks to Herbie and Rob.
Herbie : yes you are right, it is not slow it takes time but what Rob proposes is considerably faster and it is just what I try to do. Rob : I use the following lines : int[] colPixels = (int[]) ip.getPixels(); int PixelCount = ip.getPixelCount(); int[] redVal=new int[PixelCount]; int[] greenVal=new int[PixelCount]; int[] blueVal=new int[PixelCount]; for (int x = 0; x < PixelCount; x++) { int pixVal = colPixels [x]; //get red , green and blue values from pixVal redVal[x] = (pixVal & 0xff0000) >> 16; greenVal[x] = (pixVal & 0x00ff00) >> 8; blueVal[x] = (pixVal & 0x0000ff); if (blueVal[x] <redVal[x] &&blueVal[x] <greenVal[x] ) blueVal[x] =0; } ImageProcessor ipB = new ColorProcessor(frame_width,frame_height,blueVal); In my case, this code is 3 times faster than the previous one. Another request is how to cast blueVal values into a ByteProcessor without consuming time. Adding the line ipB=ipB.convertToByte(false) is time consuming. Best regards, Philippe 2013/10/7 Rob van 't Hof <[hidden email]> > Sorry, just noticed that there is a line that is not needed: the // get > brightness etc comment. The perils of copy and paste. > Also you can further speed up things using multithreading if you use a > more modern processor with multiple cores. this only gives substantial > benefits if you are using really largish images (mine can be up to 100 > Mpixels). > Rob > > > On 07/10/2013 13:15, Rob van 't Hof wrote: > >> 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<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<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<http://imagej.nih.gov/ij/list.html> >>>>>> >>>>>> >>>>> >>>>> >>>> -- >>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.**html<http://imagej.nih.gov/ij/list.html> >>>> >>>> >>> -- >>> ImageJ mailing list: http://imagej.nih.gov/ij/list.**html<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<http://imagej.nih.gov/ij/list.html> > -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hi Philippe,
You could specify the blueVal array as byte[], and change the line to: blueVal[x] = (byte)(blueal); you can then create a byte processor straight from the array. Also do you really need the redVal and greenVal arrays for later use? because assigning these arrays takes time and filling them with the values (and reading them in the if statement) takes time. Having local int redval and greenval variables would be quite a bit faster. so you would have: for (int x = 0; x < PixelCount; x++) { 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[x] =0; } else{ blueVal[x] =(byte)(blueval); } If your images are big, you could contemplate multi threading. It's not too complicated for something like this. You assign as many chunks of your arrays as you have processors in your system, and then run the code in parallel. There is a code example how to do this somewhere on the Fiji web site. If you're interested I could dig out some of my own code. However, I'm moving house and lab at the moment so do not have direct access to all my stuff just now. bye, Rob On 08/10/2013 10:17, Philippe GENDRE wrote: > Thanks to Herbie and Rob. > > Herbie : yes you are right, it is not slow it takes time but what Rob > proposes is considerably faster and it is just what I try to do. > > Rob : I use the following lines : > int[] colPixels = (int[]) ip.getPixels(); > int PixelCount = ip.getPixelCount(); > int[] redVal=new int[PixelCount]; > int[] greenVal=new int[PixelCount]; > int[] blueVal=new int[PixelCount]; > > for (int x = 0; x < PixelCount; x++) { > > int pixVal = colPixels [x]; > //get red , green and blue values from pixVal > redVal[x] = (pixVal & 0xff0000) >> 16; > greenVal[x] = (pixVal & 0x00ff00) >> 8; > blueVal[x] = (pixVal & 0x0000ff); > > if (blueVal[x] <redVal[x] &&blueVal[x] > <greenVal[x] ) > blueVal[x] =0; > } > > ImageProcessor ipB = new > ColorProcessor(frame_width,frame_height,blueVal); > > In my case, this code is 3 times faster than the previous one. > Another request is how to cast blueVal values into a ByteProcessor without > consuming time. Adding the line ipB=ipB.convertToByte(false) is time > consuming. > > Best regards, > > Philippe > > > 2013/10/7 Rob van 't Hof <[hidden email]> > >> Sorry, just noticed that there is a line that is not needed: the // get >> brightness etc comment. The perils of copy and paste. >> Also you can further speed up things using multithreading if you use a >> more modern processor with multiple cores. this only gives substantial >> benefits if you are using really largish images (mine can be up to 100 >> Mpixels). >> Rob >> >> >> On 07/10/2013 13:15, Rob van 't Hof wrote: >> >>> 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<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<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<http://imagej.nih.gov/ij/list.html> >>>>>>> >>>>>>> >>>>>> >>>>> -- >>>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.**html<http://imagej.nih.gov/ij/list.html> >>>>> >>>>> >>>> -- >>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.**html<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<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 |
Hi Rob,
On Tue, 8 Oct 2013, Rob van 't Hof wrote: > If your images are big, you could contemplate multi threading. It's not > too complicated for something like this. You assign as many chunks of > your arrays as you have processors in your system, and then run the > code in parallel. There is a code example how to do this somewhere on > the Fiji web site. You are probably referring to multi-threading with Javascript? http://fiji.sc/Multithreaded_Image_Processing_in_Javascript It could be a bit simpler but demonstrates the technique... Ciao, Johannes -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by Rob van 't Hof-2
Thank you Rob.
I have tested the following lines : int[] colPixels = (int[]) ip.getPixels(); int PixelCount = ip.getPixelCount(); byte[] bluePixels=new byte[PixelCount]; for (int x = 0; x < PixelCount; x++) { 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 ) bluePixels[x]=0; else bluePixels[x]=(byte)blueVal; } ImageProcessor ipb = new ByteProcessor(frame_width,frame_height,bluePixels); It is three times slower than the previous code. I suspect that to cast int to byte is "slow" then I tested the same code without casting : int[] colPixels = (int[]) ip.getPixels(); int PixelCount = ip.getPixelCount(); int[] bluePixels=new int[PixelCount]; for (int x = 0; x < PixelCount; x++) { 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 ) bluePixels[x]=0; else bluePixels[x]=blueVal; } ImageProcessor ipb = new ColorProcessor(frame_width,frame_height,bluePixels); The speed is 3 times higher. If this behaviour is normal, I have no special need to convert RGB image to byte. The frames I have to process are not specially big (1 MO) but I need a good frame rate according to analyze what append in real time (from the camera live stream). Best regards, Philippe 2013/10/8 Rob van 't Hof <[hidden email]> > Hi Philippe, > > You could specify the blueVal array as byte[], and change the line to: > > blueVal[x] = (byte)(blueal); > > you can then create a byte processor straight from the array. > > Also do you really need the redVal and greenVal arrays for later use? > because assigning these arrays takes time and filling them with the values > (and reading them in the if statement) takes time. Having local int redval > and greenval variables would be quite a bit faster. so you would have: > > > for (int x = 0; x < PixelCount; x++) { > > 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[x] =0; > } > else{ > blueVal[x] =(byte)(blueval); > } > > If your images are big, you could contemplate multi threading. It's not > too complicated for something like this. You assign as many chunks of your > arrays as you have processors in your system, and then run the code in > parallel. There is a code example how to do this somewhere on the Fiji web > site. If you're interested I could dig out some of my own code. However, > I'm moving house and lab at the moment so do not have direct access to all > my stuff just now. > > bye, > Rob > > > > On 08/10/2013 10:17, Philippe GENDRE wrote: > >> Thanks to Herbie and Rob. >> >> Herbie : yes you are right, it is not slow it takes time but what Rob >> proposes is considerably faster and it is just what I try to do. >> >> Rob : I use the following lines : >> int[] colPixels = (int[]) ip.getPixels(); >> int PixelCount = ip.getPixelCount(); >> int[] redVal=new int[PixelCount]; >> int[] greenVal=new int[PixelCount]; >> int[] blueVal=new int[PixelCount]; >> >> for (int x = 0; x < PixelCount; x++) { >> >> int pixVal = colPixels [x]; >> //get red , green and blue values from pixVal >> redVal[x] = (pixVal & 0xff0000) >> 16; >> greenVal[x] = (pixVal & 0x00ff00) >> 8; >> blueVal[x] = (pixVal & 0x0000ff); >> >> if (blueVal[x] <redVal[x] &&blueVal[x] >> <greenVal[x] ) >> blueVal[x] =0; >> } >> >> ImageProcessor ipB = new >> ColorProcessor(frame_width,**frame_height,blueVal); >> >> In my case, this code is 3 times faster than the previous one. >> Another request is how to cast blueVal values into a ByteProcessor without >> consuming time. Adding the line ipB=ipB.convertToByte(false) is time >> consuming. >> >> Best regards, >> >> Philippe >> >> >> 2013/10/7 Rob van 't Hof <[hidden email]> >> >> Sorry, just noticed that there is a line that is not needed: the // get >>> brightness etc comment. The perils of copy and paste. >>> Also you can further speed up things using multithreading if you use a >>> more modern processor with multiple cores. this only gives substantial >>> benefits if you are using really largish images (mine can be up to 100 >>> Mpixels). >>> Rob >>> >>> >>> On 07/10/2013 13:15, Rob van 't Hof wrote: >>> >>> 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 <http://nih.gov/ij/images/clown.jpg><ht >>>>>>>> **tp://imagej.nih.gov/ij/images/**clown.jpg<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<http://imagej.nih.gov/ij/**images/clown.jpg> >>>>>>>> <http://**imagej.nih.gov/ij/images/**clown.jpg<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<http://imagej.nih.gov/ij/list.**html> >>>>>>>> <http://imagej.nih.gov/**ij/list.html<http://imagej.nih.gov/ij/list.html> >>>>>>>> > >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>> -- >>>>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.****html<http://imagej.nih.gov/ij/list.**html> >>>>>> <http://imagej.nih.gov/**ij/list.html<http://imagej.nih.gov/ij/list.html> >>>>>> > >>>>>> >>>>>> >>>>>> -- >>>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.****html<http://imagej.nih.gov/ij/list.**html> >>>>> <http://imagej.nih.gov/**ij/list.html<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<http://imagej.nih.gov/ij/list.**html> >>> <http://imagej.nih.gov/**ij/list.html<http://imagej.nih.gov/ij/list.html> >>> > >>> >>> -- >> ImageJ mailing list: http://imagej.nih.gov/ij/list.**html<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<http://imagej.nih.gov/ij/list.html> > -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by Philippe GENDRE
Dear specialists,
I'm curious if there is something like the "IJ-macro BatchMode" for IJ-plugins. The situation is that I'm calling a plugin(2) from a plugin(1). Plugin(2) generates a modified image(2) from an input image(1). Then plugin(1) does some evaluation on image(2) and closes it. Is there a way to avoid showing image(2) without modifying plugin(2)? Currently, I call plugin(1) from a macro in BatchMode and this works as desired. However, it would be nicer if the BatchMode-effect could be achieved by plugin(1). Any hints are highly appreciated! Regards Herbie -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
> The situation is that I'm calling a plugin(2) from a plugin(1).
> Plugin(2) generates a modified image(2) from an input image(1). Then > plugin(1) does some evaluation on image(2) and closes it. > > Is there a way to avoid showing image(2) without modifying plugin(2)? Hi Herbie I think this depends on the plugins, some you can and some you cannot. For example my Domes plugin calls the GreyscaleReconstruct plugin without the latter showing the result. If I recall correct it was Albert Cardona who suggested some guidelines to construct plugins that would allow to do this easily. You will probably have to modify plugin2 to call the exec method of plugin1 (if this exists) while making sure it returns an object (i.e. an image) without showing it. The image is showed elsewhere in plugin1 when running on its own, after the exec method is done. Here this is explained with an example: http://fiji.sc/wiki/index.php/PlugIn_Design_Guidelines That is the way I have written some of my plugins and I can say that indeed simplifies things when one starts calling one plugin from another. Cheers Gabriel -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Thanks Gabriel!
The link contains interesting hints. It seems as if a modification of the called plugin(2) is necessary. I really wonder how the macro-BatchMode does the trick. I didn't find the relevant Java source code yet... If it is possible with a macro, it should be possible with a plugin but perhaps I'm unaware of some crucial obstacles! Thanks again and have a nice weekend Herbie __________________________________________ On 11.10.13 21:54, Gabriel Landini wrote: >> The situation is that I'm calling a plugin(2) from a plugin(1). >> Plugin(2) generates a modified image(2) from an input image(1). Then >> plugin(1) does some evaluation on image(2) and closes it. >> >> Is there a way to avoid showing image(2) without modifying plugin(2)? > > Hi Herbie > I think this depends on the plugins, some you can and some you cannot. > For example my Domes plugin calls the GreyscaleReconstruct plugin without the > latter showing the result. > > If I recall correct it was Albert Cardona who suggested some guidelines to > construct plugins that would allow to do this easily. > > You will probably have to modify plugin2 to call the exec method of plugin1 > (if this exists) while making sure it returns an object (i.e. an image) > without showing it. The image is showed elsewhere in plugin1 when running on > its own, after the exec method is done. > > Here this is explained with an example: > http://fiji.sc/wiki/index.php/PlugIn_Design_Guidelines > > That is the way I have written some of my plugins and I can say that indeed > simplifies things when one starts calling one plugin from another. > > Cheers > > Gabriel > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |