Re: Handle multiple bit depths
Posted by
Reinhard Mayr aka Czerwinski on
Oct 10, 2008; 4:20pm
URL: http://imagej.273.s1.nabble.com/Handle-multiple-bit-depths-tp3694774p3694778.html
Hi Mike,
I once did some extentions to the AVI Reader plugin, and I remember that
in that case there are also different depths are handled. Maybe you find a
hint for your problem in the source. see
http://rsbweb.nih.gov/ij/plugins/avi-reader.htmlhth,
Reinhard.
On Fri, 10 Oct 2008 18:11:35 +0200, Michael Doube <
[hidden email]>
wrote:
> Hi all
>
> How do I handle multiple bit depths in a plugin? I am writing a
> super-simple plugin to calculate volume fraction, which is just the
> ratio of thresholded pixels to all pixels in a selected volume. I've
> tried a few things, but always get caught out by declaring the pixels[]
> array in an if statement like:
>
> if (imp.getBitDepth == 8){byte[] pixels = (byte[])stack.getPixels(s)};
>
> or by casting in a wrong way. I've looked around for examples but
> haven't come up with much.
>
> Cheers
>
> Mike
> ---------------
> /*Volume fraction of Bone
> * Plugin to calculate volume fraction in stacks
> */
>
> import java.awt.Rectangle;
> import ij.IJ;
> import ij.ImagePlus;
> import ij.ImageStack;
> import ij.process.ImageProcessor;
> import ij.plugin.filter.PlugInFilter;
> import ij.measure.ResultsTable;
> import ij.gui.*;
>
> public class Volume_Fraction implements PlugInFilter {
> ImagePlus imp;
> protected ImageStack stack;
>
> public int setup(String arg, ImagePlus imp) {
> stack = imp.getStack();
> this.imp = imp;
> return DOES_8G + DOES_16 + DOES_32 + STACK_REQUIRED;
> }
> public void run(ImageProcessor ip) {
> ResultsTable rt = ResultsTable.getResultsTable();
> rt.reset();
> String title = imp.getTitle();
> IJ.run("Threshold...");
> new WaitForUserDialog("Set the threshold, then click OK.").show();
> short minT = (short)ip.getMinThreshold();
> short maxT = (short)ip.getMaxThreshold();
> int startSlice = 1;
> int endSlice = stack.getSize();
> int w = stack.getWidth();
> GenericDialog gd = new GenericDialog("Limit Slices");
> gd.addNumericField("Start Slice:",startSlice,0);
> gd.addNumericField("End Slice:",endSlice,0);
> gd.showDialog();
> if (gd.wasCanceled()) {
> IJ.error("PlugIn canceled!");
> return;
> }
> startSlice = (int)gd.getNextNumber();
> endSlice = (int)gd.getNextNumber();
> Rectangle r = ip.getRoi();
> int volTotal = r.height * r.width * (endSlice - startSlice + 1);
> int offset, i;
> int volBone = 0;
> for (int s=startSlice;s<=endSlice;s++) {
> double[] pixels = (double[])stack.getPixels(s);
> for (int y=r.y; y<(r.y+r.height); y++) {
> offset = y*w;
> for (int x=r.x; x<(r.x+r.width); x++) {
> i = offset + x;
> if (pixels[i] >= minT && pixels[i] <= maxT){
> volBone++;
> }
> }
> }
> }
> double p = (double) volBone / volTotal;
> rt.incrementCounter();
> rt.addLabel("Label", title);
> rt.addValue("VfB", p);
> rt.show("Results");
> }
> }
--
Using Opera's revolutionary e-mail client:
http://www.opera.com/mail/