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.apache.commons.math.stat.descriptive.SummaryStatistics;
33  import org.openimaj.citation.annotation.Reference;
34  import org.openimaj.citation.annotation.ReferenceType;
35  import org.openimaj.feature.DoubleFV;
36  import org.openimaj.feature.FeatureVectorProvider;
37  import org.openimaj.image.FImage;
38  import org.openimaj.image.analyser.ImageAnalyser;
39  import org.openimaj.image.mask.AbstractMaskedObject;
40  import org.openimaj.image.processing.convolution.AverageBoxFilter;
41  import org.openimaj.image.processing.convolution.Laplacian3x3;
42  
43  /**
44   * Sharpness measures the clarity and level of detail of an image. This class
45   * measures the variation in sharpness of an image as a function of its
46   * Laplacian, normalized by the local average luminance in the surroundings of
47   * each pixel.
48   * 
49   * @author Jonathon Hare
50   */
51  @Reference(
52  		type = ReferenceType.Inproceedings,
53  		author = { "Jose San Pedro", "Stefan Siersdorfer" },
54  		title = "Ranking and Classifying Attractiveness of Photos in Folksonomies",
55  		year = "2009",
56  		booktitle = "18th International World Wide Web Conference",
57  		pages = { "771", "", "771" },
58  		url = "http://www2009.eprints.org/78/",
59  		month = "April")
60  public class SharpnessVariation extends AbstractMaskedObject<FImage>
61  		implements
62  		ImageAnalyser<FImage>,
63  		FeatureVectorProvider<DoubleFV>
64  {
65  	private final Laplacian3x3 laplacian = new Laplacian3x3();
66  	private final AverageBoxFilter average = new AverageBoxFilter(3, 3);
67  
68  	protected double sharpnessVariation;
69  
70  	/**
71  	 * Construct with no mask set
72  	 */
73  	public SharpnessVariation() {
74  		super();
75  	}
76  
77  	/**
78  	 * Construct with a mask.
79  	 * 
80  	 * @param mask
81  	 *            the mask.
82  	 */
83  	public SharpnessVariation(FImage mask) {
84  		super(mask);
85  	}
86  
87  	@Override
88  	public DoubleFV getFeatureVector() {
89  		return new DoubleFV(new double[] { sharpnessVariation });
90  	}
91  
92  	@Override
93  	public void analyseImage(FImage image) {
94  		final FImage limg = image.process(laplacian);
95  		final FImage aimg = image.process(average);
96  
97  		final SummaryStatistics stats = new SummaryStatistics();
98  		for (int r = 0; r < limg.height; r++) {
99  			for (int c = 0; c < limg.width; c++) {
100 				if (mask != null && mask.pixels[r][c] == 0)
101 					continue;
102 
103 				if (aimg.pixels[r][c] != 0) {
104 					stats.addValue(Math.abs(limg.pixels[r][c] / aimg.pixels[r][c]));
105 				}
106 			}
107 		}
108 
109 		sharpnessVariation = stats.getStandardDeviation();
110 	}
111 
112 	/**
113 	 * Get the variation in sharpness of the last image processed with
114 	 * {@link #analyseImage(FImage)}.
115 	 * 
116 	 * @return the sharpness variation value
117 	 */
118 	public double getSharpnessVariation() {
119 		return sharpnessVariation;
120 	}
121 }