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.connectedcomponent.proc; 31 32 import org.openimaj.feature.DoubleFV; 33 import org.openimaj.feature.FeatureVectorProvider; 34 import org.openimaj.image.MBFImage; 35 import org.openimaj.image.pixel.ConnectedComponent; 36 import org.openimaj.image.pixel.statistics.BasicDescriptiveStatisticsModel; 37 import org.openimaj.image.processor.connectedcomponent.ConnectedComponentProcessor; 38 import org.openimaj.util.array.ArrayUtils; 39 40 /** 41 * Descriptors based on the first-order statistics 42 * of the colour of pixel values in an image. 43 * 44 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 45 */ 46 public class ColourDescriptor implements ConnectedComponentProcessor, FeatureVectorProvider<DoubleFV> { 47 /** 48 * The different types of statistic available. This provides 49 * a convenient way of getting a single statistic, as the {@link ColourDescriptor} 50 * computes them all. 51 * 52 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 53 * 54 */ 55 public enum ColourDescriptorType { 56 /** 57 * The mean colour. 58 * 59 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 60 */ 61 MEAN { 62 @Override 63 public DoubleFV getFeatureVector(ColourDescriptor desc) { 64 return new DoubleFV(desc.colmodel.mean); 65 } 66 }, 67 /** 68 * The modal colour. 69 * 70 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 71 */ 72 MODE { 73 @Override 74 public DoubleFV getFeatureVector(ColourDescriptor desc) { 75 return new DoubleFV(desc.colmodel.mode); 76 } 77 }, 78 /** 79 * The median colour. 80 * 81 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 82 */ 83 MEDIAN { 84 @Override 85 public DoubleFV getFeatureVector(ColourDescriptor desc) { 86 return new DoubleFV(desc.colmodel.median); 87 } 88 }, 89 /** 90 * The range of the colours in the image. 91 * 92 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 93 */ 94 RANGE { 95 @Override 96 public DoubleFV getFeatureVector(ColourDescriptor desc) { 97 return new DoubleFV(desc.colmodel.range); 98 } 99 }, 100 /** 101 * The variance of the colours in the image. 102 * 103 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 104 */ 105 VARIANCE { 106 @Override 107 public DoubleFV getFeatureVector(ColourDescriptor desc) { 108 return new DoubleFV(desc.colmodel.variance); 109 } 110 }; 111 112 /** 113 * Extract the feature for the given descriptor. 114 * @param desc the descriptor to extract from 115 * @return the extracted feature. 116 */ 117 public abstract DoubleFV getFeatureVector(ColourDescriptor desc); 118 } 119 120 protected MBFImage image; 121 protected BasicDescriptiveStatisticsModel colmodel = new BasicDescriptiveStatisticsModel(3); 122 123 /** 124 * Construct with no image. The image must be set with 125 * {@link #setImage(MBFImage)} before processing. 126 */ 127 public ColourDescriptor() { 128 } 129 130 /** 131 * Construct with the given image. 132 * @param image the image to extract pixels from. 133 */ 134 public ColourDescriptor(MBFImage image) { 135 this.image = image; 136 } 137 138 @Override 139 public void process(ConnectedComponent cc) { 140 colmodel.estimateModel(cc.extractPixels1d(image)); 141 } 142 143 /** 144 * @return the extracted feature (containing all statistics) from the last call to {@link #process(ConnectedComponent)}. 145 */ 146 public double[] getFeatureVectorArray() { 147 return ArrayUtils.concatenate( 148 colmodel.mean, 149 colmodel.median, 150 colmodel.mode, 151 colmodel.range, 152 colmodel.variance 153 ); 154 } 155 156 /** 157 * Set the image to extract pixels from. 158 * @param img the image. 159 */ 160 public void setImage(MBFImage img) { 161 image = img; 162 } 163 164 /** 165 * @return the extracted colour model from the last call to {@link #process(ConnectedComponent)} 166 */ 167 public BasicDescriptiveStatisticsModel getModel() { 168 return colmodel; 169 } 170 171 @Override 172 public DoubleFV getFeatureVector() { 173 return new DoubleFV(getFeatureVectorArray()); 174 } 175 }