View Javadoc

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.analyser.ImageAnalyser;
38  import org.openimaj.image.processor.GridProcessor;
39  import org.openimaj.math.util.FloatArrayStatsUtils;
40  
41  /**
42   * Implementation of the Bokeh estimation feature described by Yeh et al.
43   * 
44   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
45   * 
46   */
47  @Reference(
48  		type = ReferenceType.Inproceedings,
49  		author = { "Che-Hua Yeh", "Yuan-Chen Ho", "Brian A. Barsky", "Ming Ouhyoung" },
50  		title = "Personalized Photograph Ranking and Selection System",
51  		year = "2010",
52  		booktitle = "Proceedings of ACM Multimedia",
53  		pages = { "211", "220" },
54  		month = "October",
55  		customData = { "location", "Florence, Italy" })
56  public class YehBokehEstimator implements ImageAnalyser<FImage>, FeatureVectorProvider<DoubleFV> {
57  	class Sharpness implements GridProcessor<Float, FImage> {
58  		SharpPixelProportion bpp = new SharpPixelProportion();
59  
60  		@Override
61  		public int getHorizontalGridElements() {
62  			return nBlocksX;
63  		}
64  
65  		@Override
66  		public int getVerticalGridElements() {
67  			return nBlocksY;
68  		}
69  
70  		@Override
71  		public Float processGridElement(FImage patch) {
72  			patch.analyseWith(bpp);
73  			return (float) bpp.getBlurredPixelProportion();
74  		}
75  	}
76  
77  	class GreyLevelVariance implements GridProcessor<Float, FImage> {
78  		@Override
79  		public int getHorizontalGridElements() {
80  			return nBlocksX;
81  		}
82  
83  		@Override
84  		public int getVerticalGridElements() {
85  			return nBlocksY;
86  		}
87  
88  		@Override
89  		public Float processGridElement(FImage patch) {
90  			return FloatArrayStatsUtils.var(patch.pixels);
91  		}
92  	}
93  
94  	Sharpness sharpProcessor = new Sharpness();
95  	GreyLevelVariance varProcessor = new GreyLevelVariance();
96  
97  	int nBlocksX = 5;
98  	int nBlocksY = 5;
99  
100 	float varThreshold = 0.1f;
101 	float sharpnessThreshold = 0.5f;
102 	float lowerBound = 0.3f;
103 	float upperBound = 0.7f;
104 
105 	double bokeh;
106 
107 	/**
108 	 * Construct with defaults: 5x5 blocks, variance threshold of 0.1, sharpness
109 	 * threshold of 0.5, lower bound of 0.3, upper bound of 0.7
110 	 */
111 	public YehBokehEstimator() {
112 	}
113 
114 	/**
115 	 * Construct with the given parameters.
116 	 * 
117 	 * @param nBlocksX
118 	 *            number of blocks in the x-direction
119 	 * @param nBlocksY
120 	 *            number of blocks in the y-direction
121 	 * @param varThreshold
122 	 *            threshold for the variance
123 	 * @param sharpnessThreshold
124 	 *            threshold for the sharpness
125 	 * @param lowerBound
126 	 *            lower bound on Qbokeh for bokeh to be detected
127 	 * @param upperBound
128 	 *            upper bound on Qbokeh for bokeh to be detected
129 	 */
130 	public YehBokehEstimator(int nBlocksX, int nBlocksY, float varThreshold, float sharpnessThreshold, float lowerBound,
131 			float upperBound)
132 	{
133 		this.nBlocksX = nBlocksX;
134 		this.nBlocksY = nBlocksY;
135 		this.varThreshold = varThreshold;
136 		this.sharpnessThreshold = sharpnessThreshold;
137 		this.lowerBound = lowerBound;
138 		this.upperBound = upperBound;
139 	}
140 
141 	@Override
142 	public DoubleFV getFeatureVector() {
143 		return new DoubleFV(new double[] { bokeh });
144 	}
145 
146 	/*
147 	 * (non-Javadoc)
148 	 * 
149 	 * @see
150 	 * org.openimaj.image.processor.ImageProcessor#processImage(org.openimaj
151 	 * .image.Image)
152 	 */
153 	@Override
154 	public void analyseImage(FImage image) {
155 		final FImage sharpness = image.process(sharpProcessor);
156 		final FImage variance = image.process(varProcessor);
157 
158 		double Qbokeh = 0;
159 		int validBlocks = 0;
160 		for (int y = 0; y < sharpness.height; y++) {
161 			for (int x = 0; x < sharpness.width; x++) {
162 				if (variance.pixels[y][x] >= varThreshold) {
163 					Qbokeh += sharpness.pixels[y][x] > 0.5 ? 1 : 0;
164 					validBlocks++;
165 				}
166 			}
167 		}
168 		Qbokeh /= (validBlocks);
169 
170 		bokeh = (Qbokeh >= lowerBound && Qbokeh <= upperBound) ? 1 : 0;
171 	}
172 }