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.citation.annotation.References;
35 import org.openimaj.feature.DoubleFV;
36 import org.openimaj.feature.FeatureVectorProvider;
37 import org.openimaj.image.DisplayUtilities;
38 import org.openimaj.image.FImage;
39 import org.openimaj.image.analyser.ImageAnalyser;
40 import org.openimaj.image.processing.algorithm.FourierTransform;
41
42 /**
43 * Implementation of the blur estimation feature described by Ke, Tang and Jing,
44 * and Yeh et al.
45 * <p>
46 * Basically, this technique estimates the proportion of blurred pixels by
47 * thresholding the power-spectrum (magnitude) of the FFT of the image. Results
48 * are in the range 0-1. A higher number implies a sharper image.
49 *
50 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
51 *
52 */
53 @References(
54 references = {
55 @Reference(
56 type = ReferenceType.Inproceedings,
57 author = { "Ke, Yan", "Tang, Xiaoou", "Jing, Feng" },
58 title = "The Design of High-Level Features for Photo Quality Assessment",
59 year = "2006",
60 booktitle = "Proceedings of the 2006 IEEE Computer Society Conference on Computer Vision and Pattern Recognition - Volume 1",
61 pages = { "419", "", "426" },
62 url = "http://dx.doi.org/10.1109/CVPR.2006.303",
63 publisher = "IEEE Computer Society",
64 series = "CVPR '06",
65 customData = {
66 "isbn", "0-7695-2597-0",
67 "numpages", "8",
68 "doi", "10.1109/CVPR.2006.303",
69 "acmid", "1153495",
70 "address", "Washington, DC, USA"
71 }
72 ),
73 @Reference(
74 type = ReferenceType.Inproceedings,
75 author = { "Che-Hua Yeh", "Yuan-Chen Ho", "Brian A. Barsky", "Ming Ouhyoung" },
76 title = "Personalized Photograph Ranking and Selection System",
77 year = "2010",
78 booktitle = "Proceedings of ACM Multimedia",
79 pages = { "211", "220" },
80 month = "October",
81 customData = { "location", "Florence, Italy" }
82 )
83 })
84 public class SharpPixelProportion implements ImageAnalyser<FImage>, FeatureVectorProvider<DoubleFV> {
85 double bpp = 0;
86 private float threshold = 2f;
87
88 /**
89 * Construct with a default threshold on Fourier magnitude of 2.0.
90 */
91 public SharpPixelProportion() {
92 }
93
94 /**
95 * Construct with the given threshold on Fourier magnitude.
96 *
97 * @param threshold
98 * the threshold
99 */
100 public SharpPixelProportion(float threshold) {
101 this.threshold = threshold;
102 }
103
104 @Override
105 public DoubleFV getFeatureVector() {
106 return new DoubleFV(new double[] { bpp });
107 }
108
109 /*
110 * (non-Javadoc)
111 *
112 * @see
113 * org.openimaj.image.analyser.ImageAnalyser#analyseImage(org.openimaj.image
114 * .Image)
115 */
116 @Override
117 public void analyseImage(FImage image) {
118 final FourierTransform ft = new FourierTransform(image, false);
119 final FImage mag = ft.getMagnitude();
120
121 int count = 0;
122 for (int y = 0; y < mag.height; y++) {
123 for (int x = 0; x < mag.width; x++) {
124 if (Math.abs(mag.pixels[y][x]) > threshold)
125 count++;
126 }
127 }
128 bpp = (double) count / (double) (mag.height * mag.width);
129
130 DisplayUtilities.display(image, "" + bpp);
131 }
132
133 /**
134 * @return the proportion of blurred pixels (those with a Fourier magnitude
135 * above the threshold)
136 */
137 public double getBlurredPixelProportion() {
138 return bpp;
139 }
140 }