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 java.util.List;
033
034import org.openimaj.image.FImage;
035import org.openimaj.image.Image;
036import org.openimaj.image.model.ImageClassificationModel;
037import org.openimaj.util.pair.IndependentPair;
038
039/**
040 * An {@link ImageClassificationModel} based on the idea of determining the
041 * probability of a class of a pixel given the local patch of pixels surrounding
042 * the pixel in question. A sliding window of a given size is moved across the
043 * image (with overlap), and the contents of the window are analysed to
044 * determine the probability belonging to the pixel at the centre of the window.
045 * 
046 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
047 * 
048 * @param <Q>
049 *            Type of pixel
050 * @param <T>
051 *            Type of {@link Image}
052 */
053public abstract class PatchClassificationModel<Q, T extends Image<Q, T>> implements ImageClassificationModel<T> {
054        private static final long serialVersionUID = 1L;
055
056        protected int patchHeight, patchWidth;
057
058        /**
059         * Construct with the given dimensions for the sampling patch.
060         * 
061         * @param patchWidth
062         *            the width of the sampling patch
063         * @param patchHeight
064         *            the height of the sampling patch
065         */
066        public PatchClassificationModel(int patchWidth, int patchHeight) {
067                this.patchHeight = patchHeight;
068                this.patchWidth = patchWidth;
069        }
070
071        /**
072         * Classify a patch, returning the probability of the central pixel
073         * belonging to the class.
074         * 
075         * @param patch
076         *            the patch.
077         * @return the probability of the central pixel belonging to the class.
078         */
079        public abstract float classifyPatch(T patch);
080
081        @Override
082        public FImage classifyImage(T im) {
083                final FImage out = new FImage(im.getWidth(), im.getHeight());
084                final T roi = im.newInstance(patchWidth, patchHeight);
085
086                final int hh = patchHeight / 2;
087                final int hw = patchWidth / 2;
088
089                for (int y = hh; y < im.getHeight() - (patchHeight - hh); y++) {
090                        for (int x = hw; x < im.getWidth() - (patchWidth - hw); x++) {
091                                im.extractROI(x - hw, y - hh, roi);
092                                out.pixels[y][x] = this.classifyPatch(roi);
093                        }
094                }
095
096                return out;
097        }
098
099        @Override
100        public abstract PatchClassificationModel<Q, T> clone();
101
102        protected abstract T[] getArray(int length);
103
104        @Override
105        public boolean estimate(List<? extends IndependentPair<T, FImage>> data) {
106                final T[] samples = getArray(data.size());
107                for (int i = 0; i < data.size(); i++) {
108                        samples[i] = data.get(i).firstObject();
109                }
110                learnModel(samples);
111
112                return true;
113        }
114
115        @Override
116        public int numItemsToEstimate() {
117                return 1; // need a minimum of 1 sample
118        }
119
120        @Override
121        public FImage predict(T data) {
122                return classifyImage(data);
123        }
124}