001/**
002 * 
003 */
004package plugins.tutorial.training;
005
006import icy.gui.dialog.MessageDialog;
007import icy.image.IcyBufferedImage;
008import icy.plugin.abstract_.PluginActionable;
009import icy.roi.BooleanMask2D;
010import icy.roi.ROI2D;
011import icy.sequence.Sequence;
012
013import java.util.List;
014
015/**
016 * @author Stephane
017 */
018public class MeanIntensityPlugin2 extends PluginActionable
019{
020
021    /*
022     * (non-Javadoc)
023     * 
024     * @see java.lang.Runnable#run()
025     */
026    @Override
027    public void run()
028    {
029        // get the current sequence having focus.
030        Sequence sequence = getActiveSequence();
031
032        // check if a sequence is opened
033        if (sequence == null)
034        {
035            MessageDialog.showDialog("Please open a sequence to use this plugin.", MessageDialog.WARNING_MESSAGE);
036            return;
037        }
038
039        // retrieve 2D rois attached to this sequence
040        List<ROI2D> rois = sequence.getROI2Ds();
041
042        if (rois.size() == 0)
043        {
044            MessageDialog.showDialog("The sequence should have a ROI to use this plugin.",
045                    MessageDialog.WARNING_MESSAGE);
046            return;
047        }
048
049        // get the first 2D roi
050        ROI2D roi = rois.get(0);
051
052        // consider first image only here
053        BooleanMask2D mask = roi.getBooleanMask(false);
054        // consider first image only here
055        IcyBufferedImage image = sequence.getFirstImage();
056        double mean = 0;
057        double sample = 0;
058
059        for (int x = 0; x < sequence.getSizeX(); x++)
060        {
061            for (int y = 0; y < sequence.getSizeY(); y++)
062            {
063                if (mask.contains(x, y))
064                {
065                    mean += image.getData(x, y, 0);
066                    sample++;
067                }
068            }
069        }
070
071        System.out.println("mean intensity over ROI: " + (mean / sample));
072    }
073}