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.saliency;
31  
32  import gnu.trove.map.hash.TObjectFloatHashMap;
33  
34  import java.util.List;
35  
36  import org.openimaj.citation.annotation.Reference;
37  import org.openimaj.citation.annotation.ReferenceType;
38  import org.openimaj.image.FImage;
39  import org.openimaj.image.MBFImage;
40  import org.openimaj.image.pixel.ConnectedComponent;
41  import org.openimaj.image.pixel.Pixel;
42  import org.openimaj.image.segmentation.FelzenszwalbHuttenlocherSegmenter;
43  
44  /**
45   * Implementation of the region-based saliency algorithm described in:
46   * 
47   * Che-Hua Yeh, Yuan-Chen Ho, Brian A. Barsky, Ming Ouhyoung.
48   * Personalized photograph ranking and selection system.
49   * In Proceedings of ACM Multimedia'2010. pp.211~220
50   * 
51   * This algorithm is used to create a Rule-of-Thirds feature for images.
52   * 
53   * The algorithm uses the {@link AchantaSaliency} approach to get the saliency
54   * values for individual pixels. Regions are segmented from the image 
55   * using a {@link FelzenszwalbHuttenlocherSegmenter}. Saliency values are
56   * generated for each region by averaging the saliency values of the
57   * pixels within the region.
58   * 
59   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
60   */
61  @Reference(
62  		type = ReferenceType.Inproceedings,
63  		author = { "Che-Hua Yeh, Yuan-Chen Ho, Brian A. Barsky, Ming Ouhyoung" },
64  		title = "Personalized Photograph Ranking and Selection System",
65  		year = "2010",
66  		booktitle = "Proceedings of ACM Multimedia",
67  		pages = { "211", "220" },
68  		month = "October",
69  		customData = { "location", "Florence, Italy" }
70  	)
71  public class YehSaliency implements SaliencyMapGenerator<MBFImage> {
72  	AchantaSaliency saliencyGenerator;
73  	FelzenszwalbHuttenlocherSegmenter<MBFImage> segmenter;
74  	protected FImage map;
75  	protected TObjectFloatHashMap<ConnectedComponent> componentMap;
76  	
77  	/**
78  	 * Construct with default settings for the {@link AchantaSaliency} 
79  	 * and {@link FelzenszwalbHuttenlocherSegmenter}.
80  	 */
81  	public YehSaliency() {
82  		saliencyGenerator = new AchantaSaliency();
83  		segmenter = new FelzenszwalbHuttenlocherSegmenter<MBFImage>();
84  	}
85  	
86  	/**
87  	 * Construct with custom parameters.
88  	 * @param saliencySigma smoothing for the {@link AchantaSaliency} class
89  	 * @param segmenterSigma smoothing for {@link FelzenszwalbHuttenlocherSegmenter}.
90  	 * @param k k value for {@link FelzenszwalbHuttenlocherSegmenter}.
91  	 * @param minSize minimum region size for {@link FelzenszwalbHuttenlocherSegmenter}.
92  	 */
93  	public YehSaliency(float saliencySigma, float segmenterSigma, float k, int minSize) {
94  		saliencyGenerator = new AchantaSaliency(saliencySigma);
95  		segmenter = new FelzenszwalbHuttenlocherSegmenter<MBFImage>(segmenterSigma, k, minSize);
96  	}
97  
98  	/* (non-Javadoc)
99  	 * @see org.openimaj.image.analyser.ImageAnalyser#analyseImage(org.openimaj.image.Image)
100 	 */
101 	@Override
102 	public void analyseImage(MBFImage image) {
103 		List<ConnectedComponent> ccs = segmenter.segment(image);
104 		
105 		image.analyseWith(saliencyGenerator);
106 		map = saliencyGenerator.getSaliencyMap();
107 		componentMap = new TObjectFloatHashMap<ConnectedComponent>();
108 		
109 		for (ConnectedComponent cc : ccs) {
110 			float mean = 0;
111 			
112 			for (Pixel p : cc.pixels) {
113 				mean += map.pixels[p.y][p.x];
114 			}
115 			
116 			mean /= cc.pixels.size();
117 			
118 			for (Pixel p : cc.pixels) {
119 				map.pixels[p.y][p.x] = mean;
120 			}
121 			
122 			componentMap.put(cc, mean);
123 		}
124 	}
125 
126 	@Override
127 	public FImage getSaliencyMap() {
128 		return map;
129 	}
130 	
131 	/**
132 	 * Get a map of component->saliency for all the components in
133 	 * the image
134 	 * @return component->saliency map
135 	 */
136 	public TObjectFloatHashMap<ConnectedComponent> getSaliencyComponents() {
137 		return componentMap;
138 	}
139 }