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.similaritymatrix.modes;
031
032import java.io.BufferedWriter;
033import java.io.File;
034import java.io.FileWriter;
035import java.util.List;
036
037import org.kohsuke.args4j.Option;
038import org.openimaj.image.FImage;
039import org.openimaj.image.ImageUtilities;
040import org.openimaj.image.renderer.FImageRenderer;
041import org.openimaj.image.renderer.RenderHints;
042import org.openimaj.image.typography.hershey.HersheyFont;
043import org.openimaj.math.geometry.point.Point2d;
044import org.openimaj.math.geometry.shape.Circle;
045import org.openimaj.math.matrix.similarity.SimilarityMatrix;
046import org.openimaj.math.matrix.similarity.processor.MultidimensionalScaling;
047import org.openimaj.util.pair.IndependentPair;
048
049public class MDS implements ToolMode {
050        @Option(name="--num-iterations", required=false, usage="number of iterations")
051        int numIterations = 1000;
052
053        @Option(name="--rate", required=false, usage="learning rate")
054        double rate = 0.01;
055
056        @Option(name="--output-image", aliases="-im", required=false, usage="output as image rather than text")
057        boolean imageOutputMode = false;
058        
059        @Option(name="--output-image-size", aliases="-s", required=false, usage="set the size of the output image")
060        int imageSize = 1000;
061
062        @Override
063        public void process(SimilarityMatrix matrix, File output) throws Exception {
064                MultidimensionalScaling mds = new MultidimensionalScaling(numIterations, rate);
065                matrix.processInplace(mds);
066
067                if (output == null) {
068                        if (imageOutputMode) { 
069                                ImageUtilities.write(render(mds.getPoints(), imageSize), "png", System.out);
070                        } else {
071                                System.out.println(mds);
072                        }
073                } else {
074                        if (imageOutputMode) {
075                                ImageUtilities.write(render(mds.getPoints(), imageSize), output);
076                        } else {
077                                BufferedWriter bw = new BufferedWriter(new FileWriter(output));
078                                bw.write(mds.toString());
079                                bw.close();
080                        }
081                }
082        }
083
084        protected FImage render(List<IndependentPair<String, Point2d>> pts, int sz) {
085                FImage image = new FImage(sz, sz);
086                FImageRenderer renderer = image.createRenderer(RenderHints.ANTI_ALIASED);
087
088
089                for (IndependentPair<String, Point2d> pair : pts) {
090                        double x = pair.secondObject().getX();
091                        double y = pair.secondObject().getY();
092
093                        int ix = (int) Math.round((x + 0.5) * sz/2);
094                        int iy = (int) Math.round((y + 0.5) * sz/2);
095
096                        renderer.drawShapeFilled(new Circle(ix, iy, 2), 1f);
097                        renderer.drawText(pair.firstObject(), ix+5, iy, HersheyFont.TIMES_MEDIUM, 20, 1f);
098                }
099                
100                return image;
101        }
102}