[ImageJ-devel] How to implement filters in ImgLib?

Albert Cardona sapristi at gmail.com
Thu Jan 20 14:35:26 CST 2011


Hi Wilhem,

There's also the script.imglib.* way. For example, to normalize an image:

import mpicbg.imglib.image.Image;
import mpicbg.imglib.type.numeric.RealType;
import mpicbg.imglib.type.numeric.real.FloatType;
import script.imglib.math.*;
import script.imglib.ImgLib;
import ij.IJ;
import ij.plugin.PlugIn;

public class Normalize<R extends RealType<R>> implements PlugIn {

    public void run(String arg) {
        try {
                run();
        } catch (Exception e) {
                e.printStackTrace();
        }
    }

    public void run() throws Exception {

        Image<R> image = ImgLib.wrap(IJ.getImage());

        double mean = 0;
        for (RealType<?> pixel : image) {
            mean += pixel.getRealFloat() / image.size();
        }

        double variance = 0;
        for (RealType<?> pixel : image) {
            variance += Math.pow(pixel.getRealFloat() - mean, 2) / image.size();
        }
        double stdDev = Math.sqrt(variance);

        Image<FloatType> normalized = Compute.inFloats(new Divide(new
Subtract(image, mean), stdDev));

        normalized.getDisplay().setMinMax();
        ImgLib.wrap(normalized).show();
    }

    static public final void main(String[] args) {
        try {
            new Normalize().run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



Internally, "Compute" uses cursors to do the right thing. Then JIT
removes all layers and the algorithm runs at the same speed as it
would when hand-coded with cursors. In addition, "Compute"
automatically parallelizes the operations by processing different
chunks of the image in parallel.

There are also constructors for extracting specific channels from
color images. See examples towards the end of this tutorial:

http://www.ini.uzh.ch/~acardona/fiji-tutorial/

Albert

-- 
http://albert.rierol.net




More information about the ImageJ-devel mailing list