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.feature.global;
31
32 import org.openimaj.citation.annotation.Reference;
33 import org.openimaj.citation.annotation.ReferenceType;
34 import org.openimaj.feature.DoubleFV;
35 import org.openimaj.feature.FeatureVectorProvider;
36 import org.openimaj.image.FImage;
37 import org.openimaj.image.MBFImage;
38 import org.openimaj.image.analyser.ImageAnalyser;
39 import org.openimaj.image.colour.Transforms;
40 import org.openimaj.image.pixel.statistics.MaskingHistogramModel;
41 import org.openimaj.image.saliency.DepthOfFieldEstimator;
42 import org.openimaj.image.saliency.LuoTangSubjectRegion;
43 import org.openimaj.math.statistics.distribution.MultidimensionalHistogram;
44
45 /**
46 * Estimate the simplicity of an image by looking at the colour distribution of
47 * the background using the algorithm defined by Yiwen Luo and Xiaoou Tang.
48 *
49 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
50 */
51 @Reference(
52 type = ReferenceType.Inproceedings,
53 author = { "Luo, Yiwen", "Tang, Xiaoou" },
54 title = "Photo and Video Quality Evaluation: Focusing on the Subject",
55 year = "2008",
56 booktitle = "Proceedings of the 10th European Conference on Computer Vision: Part III",
57 pages = { "386", "399" },
58 url = "http://dx.doi.org/10.1007/978-3-540-88690-7_29",
59 publisher = "Springer-Verlag",
60 series = "ECCV '08",
61 customData = {
62 "isbn", "978-3-540-88689-1",
63 "location", "Marseille, France",
64 "numpages", "14",
65 "doi", "10.1007/978-3-540-88690-7_29",
66 "acmid", "1478204",
67 "address", "Berlin, Heidelberg"
68 })
69 public class LuoSimplicity implements ImageAnalyser<MBFImage>, FeatureVectorProvider<DoubleFV> {
70 LuoTangSubjectRegion extractor;
71 int binsPerBand = 16;
72 float gamma = 0.01f;
73 double simplicity;
74
75 /**
76 * Construct with the defaults of 16 histograms per image band and a gamma
77 * value of 0.01. The defaults are used for the {@link LuoTangSubjectRegion}
78 * extractor.
79 */
80 public LuoSimplicity() {
81 extractor = new LuoTangSubjectRegion();
82 }
83
84 /**
85 * Construct with the given parameters.
86 *
87 * @param binsPerBand
88 * the number of histogram bins per colour band
89 * @param gamma
90 * the gamma value for determining the threshold
91 * @param alpha
92 * the alpha value.
93 * @param maxKernelSize
94 * Maximum kernel size for the {@link DepthOfFieldEstimator}.
95 * @param kernelSizeStep
96 * Kernel step size for the {@link DepthOfFieldEstimator}.
97 * @param nbins
98 * Number of bins for the {@link DepthOfFieldEstimator}.
99 * @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 }