Login  Register

Get Calibration from DICOM stack

Posted by Michael Doube on Jul 04, 2008; 3:09pm
URL: http://imagej.273.s1.nabble.com/Get-Calibration-from-DICOM-stack-tp3695656.html

Hi everyone

I've just made the leap from macros to plugins, having got to a point
where my macros were getting a bit unmanageable.  The learning curve is
has been steep for the last couple of days!  Could someone please show
me how to get the calibration (voxel dimensions and greyscale->HU) from
my DICOM stacks to use in this simple plugin?

Many thanks

Mike

----------------------------------------------------
/** Work out the centroid of a whole stack
and each of its slices, restricted to thresholded
pixels within an ROI */
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.Calibration;

public class Stack_Centroids implements PlugInFilter {
    protected ImageStack stack;
   
    public int setup(String arg, ImagePlus imp) {
        stack = imp.getStack();
        return DOES_16 + STACK_REQUIRED + SUPPORTS_MASKING;
    }
   
    public void run(ImageProcessor ip) {
        int minT = (int)ip.getMinThreshold();
        int maxT = (int)ip.getMaxThreshold();
        //2D centroids
        double xc = 0; double yc = 0;
        //3D centroids
        double xcent = 0; double ycent = 0; double zcent = 0;
        //pixel counters
        double cstack = 0;
        Rectangle r = ip.getRoi();
        int offset, i;
        short[] pixels;  
        int w = stack.getWidth();
        for (int s=1;s<=stack.getSize();s++) {
            double sumx = 0;
            double sumy = 0;
            double cslice = 0;
            pixels = (short[])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){
                        cslice++;
                        sumx += x;
                        sumy += y;
                    }
                }
            }
            if (cslice > 0){
                xc = sumx/cslice;
                yc = sumy/cslice;
                xcent += xc*cslice;
                ycent += yc*cslice;
                zcent += cslice*s;
                cstack += cslice;
                IJ.write("Slice "+s+" centroid is at ("+xc+","+yc+"),
"+cslice+" pixels contributed.");  
            } else {
                IJ.write("Slice "+s+": No pixels available to make a
calculation");
            }
        }
        if (cstack > 0){
            IJ.write("3D centroid is at
("+xcent/cstack+","+ycent/cstack+","+zcent/cstack+")");
        } else {
            IJ.write("No voxels available to make a calculation");
        }
    }
}