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.citation.annotation.References;
035import org.openimaj.feature.DoubleFV;
036import org.openimaj.feature.FeatureVectorProvider;
037import org.openimaj.image.DisplayUtilities;
038import org.openimaj.image.FImage;
039import org.openimaj.image.analyser.ImageAnalyser;
040import org.openimaj.image.processing.algorithm.FourierTransform;
041
042/**
043 * Implementation of the blur estimation feature described by Ke, Tang and Jing,
044 * and Yeh et al.
045 * <p>
046 * Basically, this technique estimates the proportion of blurred pixels by
047 * thresholding the power-spectrum (magnitude) of the FFT of the image. Results
048 * are in the range 0-1. A higher number implies a sharper image.
049 * 
050 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
051 * 
052 */
053@References(
054                references = {
055                                @Reference(
056                                                type = ReferenceType.Inproceedings,
057                                                author = { "Ke, Yan", "Tang, Xiaoou", "Jing, Feng" },
058                                                title = "The Design of High-Level Features for Photo Quality Assessment",
059                                                year = "2006",
060                                                booktitle = "Proceedings of the 2006 IEEE Computer Society Conference on Computer Vision and Pattern Recognition - Volume 1",
061                                                pages = { "419", "", "426" },
062                                                url = "http://dx.doi.org/10.1109/CVPR.2006.303",
063                                                publisher = "IEEE Computer Society",
064                                                series = "CVPR '06",
065                                                customData = {
066                                                                "isbn", "0-7695-2597-0",
067                                                                "numpages", "8",
068                                                                "doi", "10.1109/CVPR.2006.303",
069                                                                "acmid", "1153495",
070                                                                "address", "Washington, DC, USA"
071                                }
072                                ),
073                                @Reference(
074                                                type = ReferenceType.Inproceedings,
075                                                author = { "Che-Hua Yeh", "Yuan-Chen Ho", "Brian A. Barsky", "Ming Ouhyoung" },
076                                                title = "Personalized Photograph Ranking and Selection System",
077                                                year = "2010",
078                                                booktitle = "Proceedings of ACM Multimedia",
079                                                pages = { "211", "220" },
080                                                month = "October",
081                                                customData = { "location", "Florence, Italy" }
082                                )
083})
084public class SharpPixelProportion implements ImageAnalyser<FImage>, FeatureVectorProvider<DoubleFV> {
085        double bpp = 0;
086        private float threshold = 2f;
087
088        /**
089         * Construct with a default threshold on Fourier magnitude of 2.0.
090         */
091        public SharpPixelProportion() {
092        }
093
094        /**
095         * Construct with the given threshold on Fourier magnitude.
096         * 
097         * @param threshold
098         *            the threshold
099         */
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}