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 org.openimaj.citation.annotation.Reference;
033import org.openimaj.citation.annotation.ReferenceType;
034import org.openimaj.image.FImage;
035import org.openimaj.image.MBFImage;
036import org.openimaj.image.colour.ColourSpace;
037import org.openimaj.image.processing.convolution.FGaussianConvolve;
038
039/**
040 * Implementation of the saliency map algorithm described in:
041 * 
042 * R. Achanta, S. Hemami, F. Estrada and S. Susstrunk, Frequency-tuned Salient 
043 * Region Detection, IEEE International Conference on Computer Vision and 
044 * Pattern Recognition (CVPR), 2009.
045 * 
046 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
047 */
048@Reference(
049                type = ReferenceType.Inproceedings,
050                author = { "Achanta, Radhakrishna", "Hemami, Sheila", "Estrada, Francisco", "S{\"u}sstrunk, Sabine" },
051                title = "Frequency-tuned {S}alient {R}egion {D}etection",
052                year = "2009",
053                booktitle = "{IEEE} {I}nternational {C}onference on {C}omputer {V}ision and {P}attern {R}ecognition ({CVPR})",
054                url = "http://infoscience.epfl.ch/record/135217/files/1708.pdf",
055                customData = { "Affiliation", "EPFL", "Details", "http://infoscience.epfl.ch/record/135217", "Keywords", "IVRG; NCCR-MICS; NCCR-MICS/CL4; K-Space; PHAROS; Saliency; Segmentation; Frequency-domain analysis", "Location", "Miami Beach, Florida" }
056        )
057public class AchantaSaliency implements SaliencyMapGenerator<MBFImage> {
058        protected float sigma;
059        protected FImage map;
060        
061        /**
062         * Construct with the given amount of smoothing.
063         * @param sigma standard deviation of Gaussian kernel smoothing
064         */
065        public AchantaSaliency(float sigma) {
066                this.sigma = sigma;
067        }
068        
069        /**
070         * Construct with a smoothing of 1 pixel standard deviation.
071         */
072        public AchantaSaliency() {
073                this.sigma = 1;
074        }
075        
076        /* (non-Javadoc)
077         * @see org.openimaj.image.analyser.ImageAnalyser#analyseImage(org.openimaj.image.Image)
078         */
079        @Override
080        public void analyseImage(MBFImage image) {
081                int width = image.getWidth();
082                int height = image.getHeight();
083                
084                MBFImage lab = ColourSpace.convert(image, ColourSpace.CIE_Lab);
085                
086                float[][] Lb = lab.getBand(0).pixels;
087                float[][] ab = lab.getBand(1).pixels;
088                float[][] bb = lab.getBand(2).pixels;
089                float mL = 0, ma = 0, mb = 0;
090                
091                for (int y=0; y<height; y++) {
092                        for (int x=0; x<width; x++) {
093                                mL += Lb[y][x];
094                                ma += ab[y][x];
095                                mb += bb[y][x];
096                        }
097                }
098                
099                mL /= (height*width);
100                ma /= (height*width);
101                mb /= (height*width);
102                
103                //blur
104                MBFImage blur = lab.process(new FGaussianConvolve(sigma));
105                Lb = blur.getBand(0).pixels;
106                ab = blur.getBand(1).pixels;
107                bb = blur.getBand(2).pixels;
108                
109                //create map
110                map = new FImage(width, height);
111                for (int y=0; y<height; y++) {
112                        for (int x=0; x<width; x++) {
113                                float dL = (Lb[y][x]-mL);
114                                float da = (ab[y][x]-ma);
115                                float db = (bb[y][x]-mb);
116                                
117                                map.pixels[y][x] = dL*dL + da*da + db*db;
118                        }
119                }
120                map.normalise();
121        }
122        
123        @Override
124        public FImage getSaliencyMap() {
125                return map;
126        }
127}