001/*
002 * Copyright 2010, 2011 Institut Pasteur.
003 * 
004 * This file is part of ICY.
005 * 
006 * ICY is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * ICY is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with ICY. If not, see <http://www.gnu.org/licenses/>.
018 */
019package plugins.tutorial.roi;
020
021import icy.gui.dialog.MessageDialog;
022import icy.gui.frame.progress.AnnounceFrame;
023import icy.image.IcyBufferedImage;
024import icy.plugin.abstract_.PluginActionable;
025import icy.sequence.Sequence;
026import icy.type.collection.array.Array1DUtil;
027import plugins.kernel.roi.roi2d.ROI2DArea;
028
029/**
030 * This tutorial creates an Area ROI containing pixel of values greater than the mean of the band 0
031 * of the image (t=0, z=0). It creates a ROI2DArea
032 * 
033 * @author Fabrice de Chaumont
034 * @author Stephane Dallongeville
035 */
036public class CreateAreaROI extends PluginActionable
037{
038
039    @Override
040    public void run()
041    {
042        // Display what this tutorial perform.
043        new AnnounceFrame(
044                "This tutorial creates an Area ROI containing pixel of values greater than the mean of the band 0 of the image (t=0, z=0).");
045
046        // Get the current active sequence
047        Sequence sequence = getActiveSequence();
048
049        // Check if sequence exists.
050        if (sequence == null)
051        {
052            MessageDialog.showDialog("Please open a sequence to use this plugin.", MessageDialog.WARNING_MESSAGE);
053            return;
054        }
055
056        // Get the image at t=0 and z=0
057        IcyBufferedImage image = sequence.getImage(0, 0);
058
059        // Check if the image exists
060        if (image == null)
061        {
062            MessageDialog.showDialog("No image is present at t=0 and z=0.", MessageDialog.WARNING_MESSAGE);
063            return;
064        }
065
066        // Get the data of the image for band 0 as a linear buffer, regardless of the type.
067        Object imageData = image.getDataXY(0);
068
069        // Get a copy of the data in double.
070        double[] dataBuffer = Array1DUtil.arrayToDoubleArray(imageData, image.isSignedDataType());
071
072        // Compute mean
073        double total = 0;
074        for (int i = 0; i < dataBuffer.length; i++)
075            total += dataBuffer[i];
076        double mean = total / dataBuffer.length;
077
078        // Compute mask
079        boolean[] mask = new boolean[dataBuffer.length];
080        for (int i = 0; i < dataBuffer.length; i++)
081            // equivalent to if ( dataBuffer[i] > mean ) mask[i]=true; else mask[i]=false;
082            mask[i] = (dataBuffer[i] > mean);
083
084        // create ROI
085        ROI2DArea roi = new ROI2DArea();
086
087        // fill the roi
088        roi.setAsBooleanMask(0, 0, sequence.getWidth(), sequence.getHeight(), mask);
089
090        // add the roi to the sequence
091        sequence.addROI(roi);
092    }
093
094}