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.feature.DoubleFV; 33 import org.openimaj.feature.FeatureVectorProvider; 34 import org.openimaj.image.FImage; 35 import org.openimaj.image.MBFImage; 36 import org.openimaj.image.analyser.ImageAnalyser; 37 import org.openimaj.image.mask.AbstractMaskedObject; 38 39 /** 40 * Extract the average brightness of an image. Brightness can be computed in a 41 * number of different ways, depending on the chosen {@link Mode}. 42 * 43 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 44 */ 45 public class AvgBrightness extends AbstractMaskedObject<FImage> 46 implements 47 ImageAnalyser<MBFImage>, 48 FeatureVectorProvider<DoubleFV> 49 { 50 /** 51 * Modes for computing brightness. 52 * 53 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 54 * 55 */ 56 public enum Mode { 57 /** 58 * Luminance using the NTSC weighting scheme (equivalent to the Y in the 59 * YUV colour space) 60 */ 61 NTSC_LUMINANCE { 62 @Override 63 public double computeBrightness(MBFImage image, FImage mask) { 64 final FImage R = image.getBand(0); 65 final FImage G = image.getBand(1); 66 final FImage B = image.getBand(2); 67 68 double brightness = 0; 69 70 if (mask != null) { 71 for (int y = 0; y < R.height; y++) { 72 for (int x = 0; x < R.width; x++) { 73 if (mask.pixels[y][x] == 1) 74 brightness += (0.299f * R.pixels[y][x] + 0.587f * G.pixels[y][x] + 0.114f * B.pixels[y][x]); 75 } 76 } 77 } else { 78 for (int y = 0; y < R.height; y++) 79 for (int x = 0; x < R.width; x++) 80 brightness += (0.299f * R.pixels[y][x] + 0.587f * G.pixels[y][x] + 0.114f * B.pixels[y][x]); 81 } 82 83 return brightness / (R.height * R.width); 84 } 85 }; 86 87 /** 88 * Compute the average brightness of the given image (applying the mask 89 * if it's not <code>null</code>). 90 * 91 * @param img 92 * the image to extract the average brightness from 93 * @param mask 94 * the mask 95 * @return the average brightness 96 */ 97 public abstract double computeBrightness(MBFImage img, FImage mask); 98 } 99 100 private Mode mode; 101 private double brightness; 102 103 /** 104 * Construct with the NTSC_LUMINANCE mode and no mask set 105 */ 106 public AvgBrightness() { 107 this(Mode.NTSC_LUMINANCE, null); 108 } 109 110 /** 111 * Construct with the given mode and no mask set 112 * 113 * @param mode 114 * the {@link Mode} 115 */ 116 public AvgBrightness(Mode mode) { 117 this(mode, null); 118 } 119 120 /** 121 * Construct with the given mode and a mask. 122 * 123 * @param mode 124 * the {@link Mode} 125 * @param mask 126 * the mask. 127 */ 128 public AvgBrightness(Mode mode, FImage mask) { 129 super(mask); 130 131 this.mode = mode; 132 } 133 134 @Override 135 public void analyseImage(MBFImage image) { 136 brightness = mode.computeBrightness(image, mask); 137 } 138 139 /** 140 * Get the brightness. 141 * 142 * @return the brightness 143 */ 144 public double getBrightness() { 145 return brightness; 146 } 147 148 @Override 149 public DoubleFV getFeatureVector() { 150 return new DoubleFV(new double[] { brightness }); 151 } 152 }