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.connectedcomponent.proc;
031
032import org.openimaj.feature.DoubleFV;
033import org.openimaj.feature.FeatureVectorProvider;
034import org.openimaj.image.MBFImage;
035import org.openimaj.image.pixel.ConnectedComponent;
036import org.openimaj.image.pixel.statistics.BasicDescriptiveStatisticsModel;
037import org.openimaj.image.processor.connectedcomponent.ConnectedComponentProcessor;
038import org.openimaj.util.array.ArrayUtils;
039
040/**
041 * Descriptors based on the first-order statistics
042 * of the colour of pixel values in an image.
043 * 
044 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
045 */
046public class ColourDescriptor implements ConnectedComponentProcessor, FeatureVectorProvider<DoubleFV> {
047        /**
048         * The different types of statistic available. This provides
049         * a convenient way of getting a single statistic, as the {@link ColourDescriptor}
050         * computes them all.
051         * 
052         * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
053         *
054         */
055        public enum ColourDescriptorType {
056                /**
057                 * The mean colour.
058                 * 
059                 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
060                 */
061                MEAN {
062                        @Override
063                        public DoubleFV getFeatureVector(ColourDescriptor desc) {
064                                return new DoubleFV(desc.colmodel.mean);
065                        }
066                },
067                /**
068                 * The modal colour.
069                 * 
070                 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
071                 */
072                MODE {
073                        @Override
074                        public DoubleFV getFeatureVector(ColourDescriptor desc) {
075                                return new DoubleFV(desc.colmodel.mode);
076                        }
077                },
078                /**
079                 * The median colour.
080                 * 
081                 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
082                 */
083                MEDIAN {
084                        @Override
085                        public DoubleFV getFeatureVector(ColourDescriptor desc) {
086                                return new DoubleFV(desc.colmodel.median);
087                        }
088                },
089                /**
090                 * The range of the colours in the image.
091                 * 
092                 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
093                 */
094                RANGE {
095                        @Override
096                        public DoubleFV getFeatureVector(ColourDescriptor desc) {
097                                return new DoubleFV(desc.colmodel.range);
098                        }
099                },
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}