001/**
002 * 
003 */
004package plugins.tutorial.training;
005
006import icy.gui.dialog.MessageDialog;
007import icy.plugin.abstract_.PluginActionable;
008import icy.roi.ROI2D;
009import icy.sequence.Sequence;
010import icy.sequence.SequenceDataIterator;
011
012import java.util.List;
013
014/**
015 * @author Stephane
016 */
017public class MeanIntensityPlugin3 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        // create an iterator to iterate through all pixels contained in the ROI
052        SequenceDataIterator iterator = new SequenceDataIterator(sequence, roi);
053        double mean = 0;
054        double sample = 0;
055
056        while (!iterator.done())
057        {
058            mean += iterator.get();
059            iterator.next();
060            sample++;
061        }
062
063        System.out.println("mean intensity over ROI: " + (mean / sample));
064    }
065}