package org.doube.bone.geometry; /* Volume fraction of Bone * Plugin to calculate volume fraction in stacks and single slices * (C) Michael Doube 2008 * Imperial College London * GPL v3 or any earlier version. */ 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; boolean showMask = true; public int setup(String arg, ImagePlus imp) { stack = imp.getStack(); this.imp = imp; return DOES_8G + DOES_16 + DOES_32; } 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(); 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(); ImageProcessor mask = ip.getMask(); boolean hasMask = (mask != null); if (hasMask && showMask) { (new ImagePlus("The Mask", mask)).show(); } int rLeft = r.x; int rTop = r.y; int rRight = rLeft + r.width; int rBottom = rTop + r.height; int volTotal = 0; int volBone = 0; for (int s = startSlice; s <= endSlice; s++) { ImageProcessor ipSlice = stack.getProcessor(s); for (int v = rTop; v < rBottom; v++){ for (int u = rLeft; u < rRight; u++){ if (!hasMask || mask.getPixel(u-rLeft, v-rTop)>0){ volTotal++; if (ipSlice.getPixel(u,v) >= minT && ipSlice.getPixel(u,v) <= maxT){ volBone++; } } } } } double p = (double) volBone / volTotal; rt.incrementCounter(); rt.addLabel("Label", title); rt.addValue("VfB", p); rt.show("Results"); } }