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.pixel;
031
032import org.openimaj.image.MBFImage;
033
034/**
035 * Model of an orthotope/hyperrectangle/box in space. Everything inside classifies 
036 * as positive, everything outside as negative.
037 * 
038 * @author Jonathon Hare
039 */
040public class OrthotopePixelModel extends MBFPixelClassificationModel {
041        private static final long serialVersionUID = 1L;
042        protected float [] min;
043        protected float [] max;
044        
045        /**
046         * Construct with the given number of dimensions. This 
047         * should be equal to the number of bands in the {@link MBFImage}s
048         * you wish to classify.
049         * @param ndims number of dimensions
050         */
051        public OrthotopePixelModel(int ndims) {
052                super(ndims);
053                
054                min = new float[ndims];
055                max = new float[ndims];
056        }
057
058        /**
059         * Construct with the given number box. The number of dimensions for
060         * each coordinate should be equal to the number of bands in the {@link MBFImage}s
061         * you wish to classify.
062         * 
063         * @param minCoords coordinates of the corner of the box with the smallest coordinates.
064         * @param maxCoords coordinates of the corner of the box with the largest coordinates.
065         */
066        public OrthotopePixelModel(float [] minCoords, float [] maxCoords) {
067                super(minCoords.length);
068                
069                if (minCoords.length != maxCoords.length)
070                        throw new IllegalArgumentException("minimum and maximum coordinates must have the same number of dimensions.");
071                
072                min = minCoords;
073                max = maxCoords;
074        }
075        
076        @Override
077        protected float classifyPixel(Float[] pix) {
078                
079                for (int i=0; i<ndims; i++) {
080                        if (pix[i] > max[i] || pix[i] < min[i])
081                                return 0;
082                }
083                
084                return 1;
085        }
086
087        @Override
088        public OrthotopePixelModel clone() {
089                OrthotopePixelModel newModel = new OrthotopePixelModel(ndims);
090                
091                newModel.min = min.clone();
092                newModel.max = max.clone();
093
094                return newModel;
095        }
096
097        @Override
098        public void learnModel(MBFImage... images) {
099                //Iterate through all the pixels and learn the bounding box
100                
101                for (int i=0; i<ndims; i++) {
102                        min[i] = Float.MAX_VALUE;
103                        max[i] = Float.MIN_VALUE;
104                }
105                
106                for (MBFImage image : images) {
107                        for (int y=0; y<image.getHeight(); y++) {
108                                for (int x=0; x<image.getWidth(); x++) {
109                                        Float [] pixel = image.getPixel(x, y);
110                                        
111                                        for (int i=0; i<ndims; i++) {
112                                                if (pixel[i] > max[i]) max[i] = pixel[i];
113                                                if (pixel[i] < min[i]) min[i] = pixel[i];
114                                        }
115                                }
116                        }
117                }
118        }
119}