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