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.demos;
031
032import java.net.URL;
033
034import org.openimaj.feature.DoubleFV;
035import org.openimaj.feature.DoubleFVComparison;
036import org.openimaj.image.DisplayUtilities;
037import org.openimaj.image.ImageUtilities;
038import org.openimaj.image.MBFImage;
039import org.openimaj.image.colour.ColourSpace;
040import org.openimaj.image.dataset.FlickrImageDataset;
041import org.openimaj.image.pixel.statistics.HistogramModel;
042import org.openimaj.math.geometry.point.Point2d;
043import org.openimaj.math.geometry.transforms.TransformUtilities;
044import org.openimaj.math.matrix.similarity.SimilarityMatrix;
045import org.openimaj.math.matrix.similarity.processor.MultidimensionalScaling;
046import org.openimaj.util.api.auth.DefaultTokenFactory;
047import org.openimaj.util.api.auth.common.FlickrAPIToken;
048import org.openimaj.util.pair.IndependentPair;
049
050public class ImageFeatureMDS {
051        public static void main(String[] args) throws Exception {
052                final FlickrAPIToken token = DefaultTokenFactory.getInstance().getToken(FlickrAPIToken.class);
053                final int numImages = 20;
054
055                final FlickrImageDataset<MBFImage> dataset = FlickrImageDataset.create(ImageUtilities.MBFIMAGE_READER, token,
056                                "colorful", numImages);
057
058                dataset.getPhotos().set(1, dataset.getPhoto(0));
059
060                final DoubleFV[] features = new DoubleFV[numImages];
061                for (int i = 0; i < numImages; i++) {
062                        features[i] = extractFeature(dataset.get(i));
063                }
064
065                final SimilarityMatrix matrix = new SimilarityMatrix(numImages);
066                for (int i = 0; i < numImages; i++) {
067                        matrix.setIndexValue(i, dataset.getID(i));
068                        final DoubleFV fi = features[i];
069
070                        for (int j = 0; j < numImages; j++) {
071                                final DoubleFV fj = features[j];
072
073                                matrix.set(i, j, fi.compare(fj, DoubleFVComparison.COSINE_SIM));
074                        }
075                }
076
077                System.out.println(matrix);
078
079                final MultidimensionalScaling mds = new MultidimensionalScaling();
080                mds.process(matrix);
081                System.out.println(mds.getPoints());
082
083                final MBFImage img = new MBFImage(1000, 1000, ColourSpace.RGB);
084                for (final IndependentPair<String, Point2d> pt : mds.getPoints()) {
085                        // img.drawPoint(pt.getSecondObject(), RGBColour.RED, 3);
086
087                        final int idx = dataset.indexOfID(pt.firstObject());
088                        final MBFImage thumb = ImageUtilities.readMBF(new URL(dataset.getPhoto(idx).getThumbnailUrl()));
089                        img.drawImage(thumb, pt.getSecondObject().transform(TransformUtilities.scaleMatrix(1000, 1000)));
090                }
091                DisplayUtilities.display(img);
092        }
093
094        static DoubleFV extractFeature(MBFImage image) {
095                final HistogramModel model = new HistogramModel(4, 4, 4);
096
097                model.estimateModel(image);
098
099                return model.histogram.normaliseFV();
100        }
101}