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 gnu.trove.map.hash.TObjectFloatHashMap;
33  import gnu.trove.procedure.TObjectFloatProcedure;
34  
35  import org.openimaj.citation.annotation.Reference;
36  import org.openimaj.citation.annotation.ReferenceType;
37  import org.openimaj.feature.DoubleFV;
38  import org.openimaj.feature.FeatureVectorProvider;
39  import org.openimaj.image.FImage;
40  import org.openimaj.image.MBFImage;
41  import org.openimaj.image.analyser.ImageAnalyser;
42  import org.openimaj.image.pixel.ConnectedComponent;
43  import org.openimaj.image.processor.connectedcomponent.render.BoundingBoxRenderer;
44  import org.openimaj.image.saliency.AchantaSaliency;
45  import org.openimaj.image.saliency.YehSaliency;
46  import org.openimaj.image.segmentation.FelzenszwalbHuttenlocherSegmenter;
47  import org.openimaj.util.array.ArrayUtils;
48  
49  /**
50   * Implementation of the region of interest based image simplicity measure
51   * described by Yeh et al.
52   * <p>
53   * Basically returns the proportion of the image that can be considered
54   * interesting.
55   *
56   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
57   */
58  @Reference(
59  		type = ReferenceType.Inproceedings,
60  		author = { "Che-Hua Yeh", "Yuan-Chen Ho", "Brian A. Barsky", "Ming Ouhyoung" },
61  		title = "Personalized Photograph Ranking and Selection System",
62  		year = "2010",
63  		booktitle = "Proceedings of ACM Multimedia",
64  		pages = { "211", "220" },
65  		month = "October",
66  		customData = { "location", "Florence, Italy" })
67  public class ROIProportion implements ImageAnalyser<MBFImage>, FeatureVectorProvider<DoubleFV> {
68  	protected YehSaliency saliencyGenerator;
69  	protected float alpha = 0.67f;
70  
71  	protected double roiProportion;
72  
73  	/**
74  	 * Construct with the default values
75  	 */
76  	public ROIProportion() {
77  		saliencyGenerator = new YehSaliency();
78  	}
79  
80  	/**
81  	 * Construct with the given alpha value, but use the defaults for the
82  	 * {@link YehSaliency} estimator.
83  	 *
84  	 * @param alpha
85  	 *            the alpha value for determining the threshold
86  	 */
87  	public ROIProportion(float alpha) {
88  		this();
89  		this.alpha = alpha;
90  	}
91  
92  	/**
93  	 * Construct with the given parameters.
94  	 *
95  	 * @param saliencySigma
96  	 *            smoothing for the {@link AchantaSaliency} class
97  	 * @param segmenterSigma
98  	 *            smoothing for {@link FelzenszwalbHuttenlocherSegmenter}.
99  	 * @param k
100 	 *            k value for {@link FelzenszwalbHuttenlocherSegmenter}.
101 	 * @param minSize
102 	 *            minimum region size for
103 	 *            {@link FelzenszwalbHuttenlocherSegmenter}.
104 	 * @param alpha
105 	 *            the alpha value for determining the threshold
106 	 */
107 	public ROIProportion(float saliencySigma, float segmenterSigma, float k, int minSize, float alpha) {
108 		saliencyGenerator = new YehSaliency(saliencySigma, segmenterSigma, k, minSize);
109 		this.alpha = alpha;
110 	}
111 
112 	@Override
113 	public DoubleFV getFeatureVector() {
114 		return new DoubleFV(new double[] { roiProportion });
115 	}
116 
117 	/*
118 	 * (non-Javadoc)
119 	 *
120 	 * @see
121 	 * org.openimaj.image.analyser.ImageAnalyser#analyseImage(org.openimaj.image
122 	 * .Image)
123 	 */
124 	@Override
125 	public void analyseImage(MBFImage image) {
126 		image.analyseWith(saliencyGenerator);
127 		final TObjectFloatHashMap<ConnectedComponent> componentMap = saliencyGenerator.getSaliencyComponents();
128 
129 		final float max = ArrayUtils.maxValue(componentMap.values());
130 
131 		final FImage map = new FImage(image.getWidth(), image.getHeight());
132 		final float thresh = max * alpha;
133 		final BoundingBoxRenderer<Float> renderer = new BoundingBoxRenderer<Float>(map, 1F, true);
134 
135 		componentMap.forEachEntry(new TObjectFloatProcedure<ConnectedComponent>() {
136 			@Override
137 			public boolean execute(ConnectedComponent cc, float sal) {
138 				if (sal >= thresh) { // note that this is reversed from the
139 					// paper, which doesn't seem to make
140 					// sense.
141 					renderer.process(cc);
142 				}
143 
144 				return true;
145 			}
146 		});
147 
148 		roiProportion = 0;
149 		for (int y = 0; y < map.height; y++)
150 			for (int x = 0; x < map.width; x++)
151 				roiProportion += map.pixels[y][x];
152 
153 		roiProportion /= (map.width * map.height); // smaller simplicity means
154 		// smaller ROI
155 	}
156 }