Login  Register

Re: normalize stack of pictures

Posted by Zian_Fanti on Feb 18, 2010; 8:12am
URL: http://imagej.273.s1.nabble.com/normalize-stack-of-pictures-tp3689335p3689338.html

if you still need here is an implementation of one normalization method

You must compile the plugin before use.

To use the plugin:
1.- Open a stack or sequence
2.- select a good ilumination frame to use as reference
3.- run the plugin

The plugin

import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.plugin.filter.PlugInFilter;
import ij.process.FloatStatistics;
import ij.process.ImageProcessor;
import utils.IOUtils;

/**
 *
 * @author <a ref ="[hidden email]"> Zian Fanti Gutierrez<a/>
 * @version 0.1
 */
public class Normalize_Ilumination_1 implements PlugInFilter {

    private ImagePlus imp;
    private int currentFrame;

    public int setup(String arg0, ImagePlus imp) {
        if (imp == null)
            IJ.noImage();
        this.imp = imp;
        currentFrame = imp.getCurrentSlice();
        return DOES_8G;
    }


    public void run(ImageProcessor ip) {
        ImagePlus out = new ImagePlus(IOUtils.windowName("Normalized
Ilumination"),
                normalizeIlumination(imp.getImageStack(), currentFrame));
        out.show("Normalize Ilumination Done");
    }

    /**
     * Return a new <code>ImageStack</code> with the normalized
ilumination. The
     * return stack is generated from original <code>stack</code>
     *
     * @param referenceIndex, the index of the frame in the original
<code>stack</code>
     * which is used as reference frame.
     * @return Return a new <code>ImageStack</code> with the normalized
ilumination.
     *
     * @Article RICHARD, J., RADKE, SRINIVAS, A., OMAR, A.K. and
BADRINATH, R., 2005,
     * Image Change Detection Algorithms: A Systematic Survey, IEEE
Transactions
     * on Image Processing, 14(3), pp.294-307.
     */
    public ImageStack normalizeIlumination(ImageStack stack, int
referenceIndex) {
        int size = stack.getSize();
        int width = stack.getWidth();
        int height = stack.getHeight();
        if (size == 1) {
            return stack;
        }

        ImageStack resultStack = new ImageStack(width, height);
        double factor = 0;

        if (referenceIndex < 0 || referenceIndex > size) {
            throw new NullPointerException();
        } else {
            ImageProcessor tmpImage = null;
            ImageProcessor referenceImage =
stack.getProcessor(referenceIndex).convertToFloat();

            FloatStatistics stats = new FloatStatistics(referenceImage);
            double RIstdv = stats.stdDev;
            double RIMean = stats.mean;

            double tmpSTDV = 0;
            double tmpMean = 0;

            for (int i = 1; i <= size; i++) {
                tmpImage = stack.getProcessor(i).convertToFloat();
                stats = new FloatStatistics(tmpImage);

                tmpSTDV = stats.stdDev;
                tmpMean = stats.mean;

                //calculate normalizated image
                factor = ((RIMean * tmpSTDV) - (tmpMean * RIstdv)) / tmpSTDV;
                double f1 = RIstdv / tmpSTDV;
                tmpImage.multiply(f1);
                tmpImage.add(factor);
                resultStack.addSlice("normalized" + i,
tmpImage.convertToByte(false));

                IJ.showProgress(i, size);
            }
        }
        return resultStack;
    }
}