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.feature.global; 031 032import gnu.trove.map.hash.TObjectFloatHashMap; 033import gnu.trove.procedure.TObjectFloatProcedure; 034 035import org.openimaj.citation.annotation.Reference; 036import org.openimaj.citation.annotation.ReferenceType; 037import org.openimaj.feature.DoubleFV; 038import org.openimaj.feature.FeatureVectorProvider; 039import org.openimaj.image.FImage; 040import org.openimaj.image.MBFImage; 041import org.openimaj.image.analyser.ImageAnalyser; 042import org.openimaj.image.pixel.ConnectedComponent; 043import org.openimaj.image.processor.connectedcomponent.render.BoundingBoxRenderer; 044import org.openimaj.image.saliency.AchantaSaliency; 045import org.openimaj.image.saliency.YehSaliency; 046import org.openimaj.image.segmentation.FelzenszwalbHuttenlocherSegmenter; 047import org.openimaj.util.array.ArrayUtils; 048 049/** 050 * Implementation of the region of interest based image simplicity measure 051 * described by Yeh et al. 052 * <p> 053 * Basically returns the proportion of the image that can be considered 054 * interesting. 055 * 056 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 057 */ 058@Reference( 059 type = ReferenceType.Inproceedings, 060 author = { "Che-Hua Yeh", "Yuan-Chen Ho", "Brian A. Barsky", "Ming Ouhyoung" }, 061 title = "Personalized Photograph Ranking and Selection System", 062 year = "2010", 063 booktitle = "Proceedings of ACM Multimedia", 064 pages = { "211", "220" }, 065 month = "October", 066 customData = { "location", "Florence, Italy" }) 067public class ROIProportion implements ImageAnalyser<MBFImage>, FeatureVectorProvider<DoubleFV> { 068 protected YehSaliency saliencyGenerator; 069 protected float alpha = 0.67f; 070 071 protected double roiProportion; 072 073 /** 074 * Construct with the default values 075 */ 076 public ROIProportion() { 077 saliencyGenerator = new YehSaliency(); 078 } 079 080 /** 081 * Construct with the given alpha value, but use the defaults for the 082 * {@link YehSaliency} estimator. 083 * 084 * @param alpha 085 * the alpha value for determining the threshold 086 */ 087 public ROIProportion(float alpha) { 088 this(); 089 this.alpha = alpha; 090 } 091 092 /** 093 * Construct with the given parameters. 094 * 095 * @param saliencySigma 096 * smoothing for the {@link AchantaSaliency} class 097 * @param segmenterSigma 098 * smoothing for {@link FelzenszwalbHuttenlocherSegmenter}. 099 * @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}