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.model.patch;
031
032import org.openimaj.feature.DoubleFVComparison;
033import org.openimaj.image.MBFImage;
034import org.openimaj.image.pixel.statistics.HistogramModel;
035
036/**
037 * A {@link MBFPatchClassificationModel} that performs classification
038 * based on the joint (colour) histogram of the patch by comparing the
039 * patch histogram to a model histogram with a given comparison measure. 
040 * 
041 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
042 */
043public class HistogramPatchModel extends MBFPatchClassificationModel {
044        private static final long serialVersionUID = 1L;
045        
046        /**
047         * The model histogram; public for speed. 
048         */
049        public HistogramModel model;
050        
051        protected DoubleFVComparison compare = DoubleFVComparison.BHATTACHARYYA;
052        
053        /**
054         * Construct with the given patch size and number of histogram bins
055         * per dimension. Uses {@link DoubleFVComparison#BHATTACHARYYA} as the
056         * comparison measure.
057         * @param patchWidth patch width.
058         * @param patchHeight patch height.
059         * @param nbins number of bins per dimension.
060         */
061        public HistogramPatchModel(int patchWidth, int patchHeight, int... nbins) {
062                super(nbins.length, patchWidth, patchHeight);
063
064                model = new HistogramModel(nbins);
065        }
066
067        /**
068         * Construct with the given patch size, comparison measure and ]
069         * number of histogram bins per dimension.
070         *  
071         * @param patchWidth patch width.
072         * @param patchHeight patch height.
073         * @param compare comparison measure.
074         * @param nbins number of bins per dimension.
075         */
076        public HistogramPatchModel(int patchWidth, int patchHeight, DoubleFVComparison compare, int... nbins) {
077                this(patchWidth, patchHeight, nbins);
078                this.compare = compare;
079                model = new HistogramModel(nbins);
080        }
081        
082        /**
083         * @return the comparison measure
084         */
085        public DoubleFVComparison getComparisonMeasure() {
086                return compare;
087        }
088
089        /**
090         * Set the comparison measure used.
091         * @param compare the new comparison measure.
092         */
093        public void setComparisonMeasure(DoubleFVComparison compare) {
094                this.compare = compare;
095        }
096
097        @Override
098        public float classifyPatch(MBFImage patch) {
099                HistogramModel h = new HistogramModel(model.histogram.nbins);
100                h.estimateModel(patch);
101                return (float) model.histogram.compare(h.histogram, compare);
102        }
103
104        @Override
105        public HistogramPatchModel clone() {
106                HistogramPatchModel newmodel = new HistogramPatchModel(patchWidth, patchHeight, model.histogram.nbins);
107                newmodel.model = model.clone();
108                
109                return newmodel;
110        }
111
112        @Override
113        public void learnModel(MBFImage... images) {
114                model.estimateModel(images);
115        }       
116}