View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.demos;
31  
32  import java.net.URL;
33  
34  import org.openimaj.feature.DoubleFV;
35  import org.openimaj.feature.DoubleFVComparison;
36  import org.openimaj.image.DisplayUtilities;
37  import org.openimaj.image.ImageUtilities;
38  import org.openimaj.image.MBFImage;
39  import org.openimaj.image.colour.ColourSpace;
40  import org.openimaj.image.dataset.FlickrImageDataset;
41  import org.openimaj.image.pixel.statistics.HistogramModel;
42  import org.openimaj.math.geometry.point.Point2d;
43  import org.openimaj.math.geometry.transforms.TransformUtilities;
44  import org.openimaj.math.matrix.similarity.SimilarityMatrix;
45  import org.openimaj.math.matrix.similarity.processor.MultidimensionalScaling;
46  import org.openimaj.util.api.auth.DefaultTokenFactory;
47  import org.openimaj.util.api.auth.common.FlickrAPIToken;
48  import org.openimaj.util.pair.IndependentPair;
49  
50  public class ImageFeatureMDS {
51  	public static void main(String[] args) throws Exception {
52  		final FlickrAPIToken token = DefaultTokenFactory.getInstance().getToken(FlickrAPIToken.class);
53  		final int numImages = 20;
54  
55  		final FlickrImageDataset<MBFImage> dataset = FlickrImageDataset.create(ImageUtilities.MBFIMAGE_READER, token,
56  				"colorful", numImages);
57  
58  		dataset.getPhotos().set(1, dataset.getPhoto(0));
59  
60  		final DoubleFV[] features = new DoubleFV[numImages];
61  		for (int i = 0; i < numImages; i++) {
62  			features[i] = extractFeature(dataset.get(i));
63  		}
64  
65  		final SimilarityMatrix matrix = new SimilarityMatrix(numImages);
66  		for (int i = 0; i < numImages; i++) {
67  			matrix.setIndexValue(i, dataset.getID(i));
68  			final DoubleFV fi = features[i];
69  
70  			for (int j = 0; j < numImages; j++) {
71  				final DoubleFV fj = features[j];
72  
73  				matrix.set(i, j, fi.compare(fj, DoubleFVComparison.COSINE_SIM));
74  			}
75  		}
76  
77  		System.out.println(matrix);
78  
79  		final MultidimensionalScaling mds = new MultidimensionalScaling();
80  		mds.process(matrix);
81  		System.out.println(mds.getPoints());
82  
83  		final MBFImage img = new MBFImage(1000, 1000, ColourSpace.RGB);
84  		for (final IndependentPair<String, Point2d> pt : mds.getPoints()) {
85  			// img.drawPoint(pt.getSecondObject(), RGBColour.RED, 3);
86  
87  			final int idx = dataset.indexOfID(pt.firstObject());
88  			final MBFImage thumb = ImageUtilities.readMBF(new URL(dataset.getPhoto(idx).getThumbnailUrl()));
89  			img.drawImage(thumb, pt.getSecondObject().transform(TransformUtilities.scaleMatrix(1000, 1000)));
90  		}
91  		DisplayUtilities.display(img);
92  	}
93  
94  	static DoubleFV extractFeature(MBFImage image) {
95  		final HistogramModel model = new HistogramModel(4, 4, 4);
96  
97  		model.estimateModel(image);
98  
99  		return model.histogram.normaliseFV();
100 	}
101 }