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.image.saliency;
031
032import gnu.trove.map.hash.TObjectFloatHashMap;
033
034import java.util.List;
035
036import org.openimaj.citation.annotation.Reference;
037import org.openimaj.citation.annotation.ReferenceType;
038import org.openimaj.image.FImage;
039import org.openimaj.image.MBFImage;
040import org.openimaj.image.pixel.ConnectedComponent;
041import org.openimaj.image.pixel.Pixel;
042import org.openimaj.image.segmentation.FelzenszwalbHuttenlocherSegmenter;
043
044/**
045 * Implementation of the region-based saliency algorithm described in:
046 * 
047 * Che-Hua Yeh, Yuan-Chen Ho, Brian A. Barsky, Ming Ouhyoung.
048 * Personalized photograph ranking and selection system.
049 * In Proceedings of ACM Multimedia'2010. pp.211~220
050 * 
051 * This algorithm is used to create a Rule-of-Thirds feature for images.
052 * 
053 * The algorithm uses the {@link AchantaSaliency} approach to get the saliency
054 * values for individual pixels. Regions are segmented from the image 
055 * using a {@link FelzenszwalbHuttenlocherSegmenter}. Saliency values are
056 * generated for each region by averaging the saliency values of the
057 * pixels within the region.
058 * 
059 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
060 */
061@Reference(
062                type = ReferenceType.Inproceedings,
063                author = { "Che-Hua Yeh, Yuan-Chen Ho, Brian A. Barsky, Ming Ouhyoung" },
064                title = "Personalized Photograph Ranking and Selection System",
065                year = "2010",
066                booktitle = "Proceedings of ACM Multimedia",
067                pages = { "211", "220" },
068                month = "October",
069                customData = { "location", "Florence, Italy" }
070        )
071public class YehSaliency implements SaliencyMapGenerator<MBFImage> {
072        AchantaSaliency saliencyGenerator;
073        FelzenszwalbHuttenlocherSegmenter<MBFImage> segmenter;
074        protected FImage map;
075        protected TObjectFloatHashMap<ConnectedComponent> componentMap;
076        
077        /**
078         * Construct with default settings for the {@link AchantaSaliency} 
079         * and {@link FelzenszwalbHuttenlocherSegmenter}.
080         */
081        public YehSaliency() {
082                saliencyGenerator = new AchantaSaliency();
083                segmenter = new FelzenszwalbHuttenlocherSegmenter<MBFImage>();
084        }
085        
086        /**
087         * Construct with custom parameters.
088         * @param saliencySigma smoothing for the {@link AchantaSaliency} class
089         * @param segmenterSigma smoothing for {@link FelzenszwalbHuttenlocherSegmenter}.
090         * @param k k value for {@link FelzenszwalbHuttenlocherSegmenter}.
091         * @param minSize minimum region size for {@link FelzenszwalbHuttenlocherSegmenter}.
092         */
093        public YehSaliency(float saliencySigma, float segmenterSigma, float k, int minSize) {
094                saliencyGenerator = new AchantaSaliency(saliencySigma);
095                segmenter = new FelzenszwalbHuttenlocherSegmenter<MBFImage>(segmenterSigma, k, minSize);
096        }
097
098        /* (non-Javadoc)
099         * @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}