Problem converting HSB stack to separate images

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

Problem converting HSB stack to separate images

gibberfish
Hello,

I want to threshold an image based on peaks in the HSB histograms. For this I plan using the Color Thresholding plugin, feeding it the threshold values I calculated. To calculate these values, I want to convert the RGB image to an HSB stack, and then read the histogram values from each image in the stack.

I can convert to an HSB, stack fine, but converting the stack to images doesn't seem to work. When executing the command, I always get an error saying "Convert Stack to Images" requires a stack.

Here's my code:

        ImagePlus image = IJ.getImage();
        ImageConverter ic = new ImageConverter(image);
        ic.convertToHSB();
               
        StackEditor se = new StackEditor();
        se.convertStackToImages(image); //<---- problem line
       
        IJ.selectWindow("Hue");
        ImagePlus hue = IJ.getImage();
        int[] hueHist = hue.getStatistics().histogram;

        IJ.selectWindow("Saturation");
        ImagePlus sat = IJ.getImage();
        int[] satHist = sat.getStatistics().histogram;

 So it seems the ImagePlus object isn't the right one. Manually selecting the stack in ImageJ and running the plugin works fine. Any idea how I might fix this?

Reply | Threaded
Open this post in threaded view
|

Re: Problem converting HSB stack to separate images

Rasband, Wayne (NIH/NIMH) [E]
On Feb 19, 2011, at 10:25 AM, gibberfish wrote:

> Hello,
>
> I want to threshold an image based on peaks in the HSB histograms. For this
> I plan using the Color Thresholding plugin, feeding it the threshold values
> I calculated. To calculate these values, I want to convert the RGB image to
> an HSB stack, and then read the histogram values from each image in the
> stack.
>
> I can convert to an HSB, stack fine, but converting the stack to images
> doesn't seem to work. When executing the command, I always get an error
> saying "Convert Stack to Images" requires a stack.
>
> Here's my code:
>
> ImagePlus image = IJ.getImage();
> ImageConverter ic = new ImageConverter(image);
> ic.convertToHSB();
>
> StackEditor se = new StackEditor();
> se.convertStackToImages(image); //<---- problem line
>
> IJ.selectWindow("Hue");
> ImagePlus hue = IJ.getImage();
> int[] hueHist = hue.getStatistics().histogram;
>
> IJ.selectWindow("Saturation");
> ImagePlus sat = IJ.getImage();
> int[] satHist = sat.getStatistics().histogram;
>
> So it seems the ImagePlus object isn't the right one. Manually selecting
> the stack in ImageJ and running the plugin works fine. Any idea how I might
> fix this?

There is no need to convert the HSB stack to images. Instead, use the ImageProcessor.getStatistics() method to get the hue and saturation histograms from the stack slices. Here is a JavaScript example:

  imp = IJ.getImage();
  IJ.run(imp, "HSB Stack", "");
  stack = imp.getStack();
  hue = stack.getProcessor(1);
  hueHist = hue.getStatistics().histogram;
  sat = stack.getProcessor(2);
  satHist = sat.getStatistics().histogram;

-wayne
Reply | Threaded
Open this post in threaded view
|

Re: Problem converting HSB stack to separate images

gibberfish
That did it, thanks!