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 org.openimaj.citation.annotation.Reference; 033import org.openimaj.citation.annotation.ReferenceType; 034import org.openimaj.feature.DoubleFV; 035import org.openimaj.feature.FeatureVectorProvider; 036import org.openimaj.image.FImage; 037import org.openimaj.image.MBFImage; 038import org.openimaj.image.analyser.ImageAnalyser; 039import org.openimaj.image.colour.Transforms; 040import org.openimaj.image.pixel.statistics.MaskingHistogramModel; 041import org.openimaj.image.saliency.DepthOfFieldEstimator; 042import org.openimaj.image.saliency.LuoTangSubjectRegion; 043import org.openimaj.math.statistics.distribution.MultidimensionalHistogram; 044 045/** 046 * Estimate the simplicity of an image by looking at the colour distribution of 047 * the background using the algorithm defined by Yiwen Luo and Xiaoou Tang. 048 * 049 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 050 */ 051@Reference( 052 type = ReferenceType.Inproceedings, 053 author = { "Luo, Yiwen", "Tang, Xiaoou" }, 054 title = "Photo and Video Quality Evaluation: Focusing on the Subject", 055 year = "2008", 056 booktitle = "Proceedings of the 10th European Conference on Computer Vision: Part III", 057 pages = { "386", "399" }, 058 url = "http://dx.doi.org/10.1007/978-3-540-88690-7_29", 059 publisher = "Springer-Verlag", 060 series = "ECCV '08", 061 customData = { 062 "isbn", "978-3-540-88689-1", 063 "location", "Marseille, France", 064 "numpages", "14", 065 "doi", "10.1007/978-3-540-88690-7_29", 066 "acmid", "1478204", 067 "address", "Berlin, Heidelberg" 068 }) 069public class LuoSimplicity implements ImageAnalyser<MBFImage>, FeatureVectorProvider<DoubleFV> { 070 LuoTangSubjectRegion extractor; 071 int binsPerBand = 16; 072 float gamma = 0.01f; 073 double simplicity; 074 075 /** 076 * Construct with the defaults of 16 histograms per image band and a gamma 077 * value of 0.01. The defaults are used for the {@link LuoTangSubjectRegion} 078 * extractor. 079 */ 080 public LuoSimplicity() { 081 extractor = new LuoTangSubjectRegion(); 082 } 083 084 /** 085 * Construct with the given parameters. 086 * 087 * @param binsPerBand 088 * the number of histogram bins per colour band 089 * @param gamma 090 * the gamma value for determining the threshold 091 * @param alpha 092 * the alpha value. 093 * @param maxKernelSize 094 * Maximum kernel size for the {@link DepthOfFieldEstimator}. 095 * @param kernelSizeStep 096 * Kernel step size for the {@link DepthOfFieldEstimator}. 097 * @param nbins 098 * Number of bins for the {@link DepthOfFieldEstimator}. 099 * @param windowSize 100 * window size for the {@link DepthOfFieldEstimator}. 101 */ 102 public LuoSimplicity(int binsPerBand, float gamma, float alpha, int maxKernelSize, int kernelSizeStep, int nbins, 103 int windowSize) 104 { 105 extractor = new LuoTangSubjectRegion(alpha, maxKernelSize, kernelSizeStep, nbins, windowSize); 106 this.binsPerBand = binsPerBand; 107 this.gamma = gamma; 108 } 109 110 @Override 111 public void analyseImage(MBFImage image) { 112 Transforms.calculateIntensityNTSC(image).analyseWith(extractor); 113 final FImage mask = extractor.getROIMap().inverse(); 114 115 final MaskingHistogramModel hm = new MaskingHistogramModel(mask, binsPerBand, binsPerBand, binsPerBand); 116 hm.estimateModel(image); 117 118 final MultidimensionalHistogram fv = hm.getFeatureVector(); 119 final double thresh = gamma * fv.max(); 120 int count = 0; 121 for (final double f : fv.values) { 122 if (f >= thresh) 123 count++; 124 } 125 126 simplicity = (double) count / (double) fv.values.length; 127 } 128 129 @Override 130 public DoubleFV getFeatureVector() { 131 return new DoubleFV(new double[] { simplicity }); 132 } 133}