001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.tools.globalfeature;
031
032import java.io.File;
033import java.io.IOException;
034import java.io.OutputStream;
035
036import org.kohsuke.args4j.CmdLineException;
037import org.kohsuke.args4j.CmdLineParser;
038import org.kohsuke.args4j.FileOutputStreamOptionHandler;
039import org.kohsuke.args4j.MBFImageOptionHandler;
040import org.kohsuke.args4j.Option;
041import org.kohsuke.args4j.ProxyOptionHandler;
042import org.openimaj.feature.FeatureVector;
043import org.openimaj.image.FImage;
044import org.openimaj.image.ImageUtilities;
045import org.openimaj.image.MBFImage;
046import org.openimaj.image.analysis.algorithm.FloodFill;
047import org.openimaj.io.IOUtils;
048import org.openimaj.tools.globalfeature.ShapeFeatures.ShapeFeaturesOp;
049
050
051/**
052 * A tool that computes features from the foreground
053 * object in an image. The flood-fill algorithm is
054 * used to segment the foreground/background based 
055 * on a seed pixel and distance threshold.
056 * 
057 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
058 */
059public class SegmentingGlobalFeaturesTool {
060        @Option(name="--feature-type", aliases="-f", handler=ProxyOptionHandler.class, usage="Feature type", required=false)
061        private ShapeFeatures feature;
062        private ShapeFeaturesOp featureOp;
063        
064        @Option(name = "--input", aliases="-i", required=true, usage="Set the input image", handler=MBFImageOptionHandler.class)
065        private MBFImage input;
066        
067        @Option(name = "--output", aliases="-o", required=false, usage="Set the output file; if not set then output is to stdout", handler=FileOutputStreamOptionHandler.class)
068        private OutputStream output = System.out;
069        
070        @Option(name = "--binary", aliases="-b", required=false, usage="Set output mode to binary")
071        private boolean binary = false;
072        
073        @Option(name = "--mask-output", aliases="-mo", required=false, usage="Save the generated segmentation mask")
074        private File maskoutput;
075        
076        @Option(name = "--px", aliases="-px", required=false, usage="x-position of the starting pixel")
077        private int px = 0;
078        
079        @Option(name = "--py", aliases="-py", required=false, usage="y-position of the starting pixel")
080        private int py = 0;
081        
082        @Option(name = "--thresh", aliases="-thresh", required=false, usage="threshold for flood-fill algorithm")
083        private float thresh = 25F/255F;
084        
085        void execute() throws IOException {
086                FImage mask = FloodFill.floodFill(input, px, py, thresh);
087                
088                if (maskoutput != null)
089                        ImageUtilities.write(mask, maskoutput.getName().substring(maskoutput.getName().lastIndexOf(".") + 1), maskoutput);
090                
091                if (feature != null) { 
092                        FeatureVector fv = featureOp.execute(input, mask);
093                
094                        if (binary)
095                                IOUtils.writeBinary(output, fv);
096                        else
097                                IOUtils.writeASCII(output, fv);
098                }
099        }
100        
101        /**
102         * The main method for the tool.
103         * @param args
104         * @throws IOException
105         */
106        public static void main(String [] args) throws IOException {
107                SegmentingGlobalFeaturesTool tool = new SegmentingGlobalFeaturesTool();
108                
109                CmdLineParser parser = new CmdLineParser(tool);
110
111                try {
112                        parser.parseArgument(args);
113                } catch (CmdLineException e) {
114                        System.err.println(e.getMessage());
115                        System.err.println("Usage: java -cp GlobalFeaturesTool.jar uk.ac.soton.ecs.jsh2.SegmentingGlobalFeaturesTool [options...]");
116                        parser.printUsage(System.err);
117                        
118                        if (tool.feature == null) {
119                                for (GlobalFeatureType m : GlobalFeatureType.values()) {
120                                        System.err.println();
121                                        System.err.println(m + " options: ");
122                                        new CmdLineParser(m.getOptions()).printUsage(System.err);
123                                }
124                        }
125                        return;
126                }
127                
128                tool.execute();
129        }
130}