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.classifier.citylandscape;
031
032import java.io.BufferedWriter;
033import java.io.File;
034import java.io.FileWriter;
035import java.io.IOException;
036import java.math.BigDecimal;
037
038import org.openimaj.image.FImage;
039import org.openimaj.image.ImageUtilities;
040import org.openimaj.image.analysis.algorithm.EdgeDirectionCoherenceVector;
041
042/**
043 * 
044 *  @author Ajay Mehta (am24g08@ecs.soton.ac.uk)
045 *      
046 *      @created 20 Jun 2011
047 */
048public class TrainClassifier {
049
050        
051        final static int OFFSET = 0;
052        /**
053         * First arg: Number of images in directory to go through
054         * Second arg: Directory of images
055         * Third arg: File to write to
056         * @param args
057         * @throws IOException
058         */
059        public static void main(String[] args) throws IOException {
060
061                if(args.length!=3){
062                        System.out.println("Invalid number of arguments entered");
063                        System.exit(1);
064                }
065                
066                System.out.println("Writing training data for classifier");
067                long init = System.currentTimeMillis();
068                TrainClassifier.writeHistograms(Integer.parseInt(args[0]), args[1],args[2]);
069                long runTime = System.currentTimeMillis() - init;
070                BigDecimal bd = new BigDecimal(runTime);
071                System.out.println("Program time to run: " + bd.movePointLeft(3)
072                                + " s");
073                System.out.println("All training data files written...\nEnd of program.");
074        }
075        
076        /**
077         * Write histograms to file
078         * 
079         * @param numberOfImages
080         * @param inputPath
081         * @param outfile
082         * @throws IOException
083         */
084        public static void writeHistograms(int numberOfImages, String inputPath, String outfile) throws IOException{
085                BufferedWriter bw = new BufferedWriter(new FileWriter(outfile));
086                System.out.println("Input file: "+inputPath);
087                System.out.println("Writing to: "+outfile);
088                File dir = new File(inputPath);
089                File [] array = dir.listFiles();
090                int skipped = 0;
091                for (int i = OFFSET; i-skipped<numberOfImages && i < array.length; i++){
092                        FImage image = null;
093                        try{
094                                System.out.println("Attempting write image" +array[i].getName()+ " Number: "+(i+skipped));
095                                image = ImageUtilities.readF(array[i].getAbsoluteFile());
096                                
097                        }
098                        catch(Exception e){
099                                System.out.println("Error reading image: " + array[i].getName()+". Number: "+(i+skipped));
100                                skipped++;
101                                continue;
102                        }
103                        EdgeDirectionCoherenceVector cldo = new EdgeDirectionCoherenceVector();
104                        image.analyseWith(cldo);
105                        
106                        double[][] vec = new double[][] {
107                                        cldo.getLastHistogram().incoherentHistogram.values,
108                                        cldo.getLastHistogram().coherentHistogram.values
109                        };
110                        int n = cldo.getNumberOfDirBins();
111
112                        double edgeCounter = 0;
113                        for (int j = 0; j < n; j++) {
114                                // Incoherent
115                                bw.write(vec[0][j] + ",");
116                                edgeCounter += vec[0][j];
117
118                        }
119                        for (int j = 0; j < n; j++) {
120                                // Coherent
121                                bw.write(vec[1][j] + ",");
122                                edgeCounter += vec[1][j];
123                        }
124                        //edgeCounter variable hold sum of all elements in vector, used in normalization
125                        bw.write(Double.toString(edgeCounter));
126                        bw.newLine();
127                        edgeCounter = 0;
128                }
129                
130                bw.flush();
131                bw.close();
132                System.out.println("Vector Write Completed");
133        }
134}