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.image.feature.global;
31  
32  import java.util.List;
33  
34  import org.openimaj.citation.annotation.Reference;
35  import org.openimaj.citation.annotation.ReferenceType;
36  import org.openimaj.feature.DoubleFV;
37  import org.openimaj.feature.FeatureVectorProvider;
38  import org.openimaj.image.MBFImage;
39  import org.openimaj.image.analyser.ImageAnalyser;
40  import org.openimaj.image.analysis.colour.CIEDE2000;
41  import org.openimaj.image.colour.ColourSpace;
42  import org.openimaj.image.pixel.ConnectedComponent;
43  import org.openimaj.image.pixel.Pixel;
44  import org.openimaj.image.pixel.PixelSet;
45  import org.openimaj.image.segmentation.FelzenszwalbHuttenlocherSegmenter;
46  
47  /**
48   * Implementation of a color contrast feature.
49   * <p>
50   * The feature is calculated by performing a weighted average of the average
51   * colour difference of all the segments in the image.
52   * 
53   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
54   */
55  @Reference(
56  		type = ReferenceType.Inproceedings,
57  		author = { "Che-Hua Yeh", "Yuan-Chen Ho", "Brian A. Barsky", "Ming Ouhyoung" },
58  		title = "Personalized Photograph Ranking and Selection System",
59  		year = "2010",
60  		booktitle = "Proceedings of ACM Multimedia",
61  		pages = { "211", "220" },
62  		month = "October",
63  		customData = { "location", "Florence, Italy" })
64  public class ColourContrast implements ImageAnalyser<MBFImage>, FeatureVectorProvider<DoubleFV> {
65  	FelzenszwalbHuttenlocherSegmenter<MBFImage> segmenter;
66  	double contrast;
67  
68  	/**
69  	 * Construct the {@link ColourContrast} feature extractor using the default
70  	 * settings for the {@link FelzenszwalbHuttenlocherSegmenter}.
71  	 */
72  	public ColourContrast() {
73  		segmenter = new FelzenszwalbHuttenlocherSegmenter<MBFImage>();
74  	}
75  
76  	/**
77  	 * Construct the {@link ColourContrast} feature extractor with the given
78  	 * parameters for the underlying {@link FelzenszwalbHuttenlocherSegmenter}.
79  	 * 
80  	 * @param sigma
81  	 *            amount of blurring
82  	 * @param k
83  	 *            threshold
84  	 * @param minSize
85  	 *            minimum allowed component size
86  	 */
87  	public ColourContrast(float sigma, float k, int minSize) {
88  		segmenter = new FelzenszwalbHuttenlocherSegmenter<MBFImage>(sigma, k, minSize);
89  	}
90  
91  	@Override
92  	public DoubleFV getFeatureVector() {
93  		return new DoubleFV(new double[] { contrast });
94  	}
95  
96  	/*
97  	 * (non-Javadoc)
98  	 * 
99  	 * @see
100 	 * org.openimaj.image.processor.ImageProcessor#processImage(org.openimaj
101 	 * .image.Image)
102 	 */
103 	@Override
104 	public void analyseImage(MBFImage image) {
105 		final List<ConnectedComponent> ccs = segmenter.segment(image);
106 		final MBFImage labImage = ColourSpace.convert(image, ColourSpace.CIE_Lab);
107 		final float[][] avgs = new float[ccs.size()][3];
108 		final int w = image.getWidth();
109 		final int h = image.getHeight();
110 
111 		// calculate patch average colours
112 		for (int i = 0; i < avgs.length; i++) {
113 			for (final Pixel p : ccs.get(i).pixels) {
114 				final Float[] v = labImage.getPixel(p);
115 
116 				avgs[i][0] += v[0];
117 				avgs[i][0] += v[1];
118 				avgs[i][0] += v[2];
119 			}
120 			final int sz = ccs.get(i).pixels.size();
121 			avgs[i][0] /= sz;
122 			avgs[i][1] /= sz;
123 			avgs[i][2] /= sz;
124 		}
125 
126 		for (int i = 0; i < avgs.length; i++) {
127 			for (int j = i + 1; j < avgs.length; j++) {
128 				final PixelSet ci = ccs.get(i);
129 				final PixelSet cj = ccs.get(i);
130 				final float C = CIEDE2000.calculateDeltaE(avgs[i], avgs[j]);
131 
132 				contrast += (1 - distance(ci, cj, w, h)) * (C / (ci.calculateArea() * cj.calculateArea()));
133 			}
134 		}
135 	}
136 
137 	float distance(PixelSet c1, PixelSet c2, int w, int h) {
138 		final double[] cen1 = c1.calculateCentroid();
139 		final double[] cen2 = c2.calculateCentroid();
140 
141 		final double dx = (cen1[0] - cen2[0]) / w;
142 		final double dy = (cen1[1] - cen2[1]) / h;
143 
144 		return (float) (Math.sqrt(dx * dx + dy * dy) / Math.sqrt(2));
145 	}
146 }