Login  Register

Re: obtaining the slice with the maximum intensity

Posted by John Dunsmuir on Jul 20, 2013; 5:24pm
URL: http://imagej.273.s1.nabble.com/obtaining-the-slice-with-the-maximum-intensity-tp5004051p5004056.html

Eri,

Below is the source for a simple plugin to find the the maximum brightness slice in a stack.  You will probably need to make some changes to it to make it do exactly what your want like finding multiple slices but it will get you started.  Use Compile and Run to create the .class file in your plugins folder.  When you restart imageJ the plugin should appear in the Plugins pull down menu.

John D.

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.filter.*;
import ij.measure.*;

public class Get_Max_Pixel implements PlugInFilter
{
        ImagePlus imp;

        public int setup(String arg, ImagePlus imp)
        {
                this.imp = imp;
                return DOES_ALL;
        }

        public void run(ImageProcessor ip)
        {
                int i,j,k;
                double maxVal = -70000.0;
                double minVal = 70000.0;
                double data;
                long count = 0;

                ImageStack stack = imp.getStack();

                int width = stack.getWidth();
  int height = stack.getHeight();
  int depth = stack.getSize();
                int iMax =0;
                int jMax =0;
                int kMax =0;
                int iMin =0;
                int jMin =0;
                int kMin =0;


                IJ.showStatus("Finding Max Pixel");
                for(k=0;k<depth;k++)
                {
                        IJ.showProgress((double)k/(double)depth);
                        for(j=0;j<height;j++)
                        {
                                for(i=0;i<width;i++)
                                {
                                        data = stack.getVoxel(i,j,k);
                                        if(data !=0) count++;
                                        if(data>maxVal )
                                        {
                                                maxVal = data; //find the value and location of the largest value
                                                iMax=i;
                                                jMax=j;
                                                kMax=k;
                                        }
                                        if(data<minVal  && data !=0)
                                        {
                                                minVal = data; //find the value and location of the smallest value
                                                iMin=i;
                                                jMin=j;
                                                kMin=k;
                                        }
                                }
                        }
                }

                StackWindow sw = new StackWindow(imp);
                sw.showSlice(kMax+1);

                IJ.run("Specify...", "width=1 height=1 x="+iMax+" y="+jMax+" slice="+kMax);

                ResultsTable rt = new ResultsTable();

                rt = rt.getResultsTable();
                rt.incrementCounter();
                rt.addValue("Max Radius pixels",maxVal);
                rt.addValue("iMax",iMax);
                rt.addValue("jMax Val",jMax);
                rt.addValue("kMax Val",kMax+1);
       
                //rt.addValue("Min Radius pixels",minVal);
                //rt.addValue("iMin",iMin);
                //rt.addValue("jMin Val",jMin);
                //rt.addValue("kMin Val",kMin+1);
                //rt.addValue("Flooded Pixels",count);
                //rt.show("Results");
        }
}

<quote author="Eri">
Every stack of images/slices that I have, always has an image (it may be two) that has the highest number of counts per pixel, not specifically a region of interest but the whole image. What I want is to somehow find this image and then extract it from the stack. I hope you understand now what I mean!