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.segmentation;
031
032import org.openimaj.feature.FloatFVComparator;
033import org.openimaj.image.MBFImage;
034import org.openimaj.image.colour.ColourSpace;
035
036/**
037 * Simple image segmentation from grouping colours with k-means, and also
038 * incorporating a spatial aspect based on pixel location.
039 * 
040 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
041 * 
042 */
043public class KMSpatialColourSegmenter extends KMColourSegmenter {
044
045        /**
046         * Construct using the given colour space and number of segments. Euclidean
047         * distance is used, and the elements of each colour band are unscaled. Up
048         * to 100 K-Means iterations will be performed.
049         * 
050         * @param colourSpace
051         *            the colour space
052         * @param K
053         *            the number of segments
054         */
055        public KMSpatialColourSegmenter(ColourSpace colourSpace, int K) {
056                super(colourSpace, K);
057        }
058
059        /**
060         * Construct using the given colour space, number of segments, and distance
061         * measure. The elements of each colour band are unscaled. Up to 100 K-Means
062         * iterations will be performed.
063         * 
064         * @param colourSpace
065         *            the colour space
066         * @param K
067         *            the number of segments
068         * @param distance
069         *            the distance measure
070         */
071        public KMSpatialColourSegmenter(ColourSpace colourSpace, int K, FloatFVComparator distance) {
072                super(colourSpace, K, distance);
073        }
074
075        /**
076         * Construct using the given colour space, number of segments, and distance
077         * measure. The elements of each colour band are by the corresponding
078         * elements in the given scaling vector; the scaling vector should be two
079         * elements longer than the number of colour bands of the target colour
080         * space such that the last two elements correspond to the scalings for the
081         * normalised x and y positions of the pixels. Up to 100 K-Means iterations
082         * will be performed.
083         * 
084         * @param colourSpace
085         *            the colour space
086         * @param scaling
087         *            the scaling vector
088         * @param K
089         *            the number of segments
090         * @param distance
091         *            the distance measure
092         */
093        public KMSpatialColourSegmenter(ColourSpace colourSpace, float[] scaling, int K, FloatFVComparator distance) {
094                super(colourSpace, scaling, K, distance);
095        }
096
097        /**
098         * Construct using the given colour space, number of segments, and distance
099         * measure. The elements of each colour band are by the corresponding
100         * elements in the given scaling vector; the scaling vector should be two
101         * elements longer than the number of colour bands of the target colour
102         * space such that the last two elements correspond to the scalings for the
103         * normalised x and y positions of the pixels. The k-means algorithm will
104         * iterate at most <code>maxIters</code> times.
105         * 
106         * @param colourSpace
107         *            the colour space
108         * @param scaling
109         *            the scaling vector
110         * @param K
111         *            the number of segments
112         * @param distance
113         *            the distance measure
114         * @param maxIters
115         *            the maximum number of iterations to perform
116         */
117        public KMSpatialColourSegmenter(ColourSpace colourSpace, float[] scaling, int K, FloatFVComparator distance,
118                        int maxIters)
119        {
120                super(colourSpace, scaling, K, distance, maxIters);
121        }
122
123        @Override
124        protected float[][] imageToVector(MBFImage image) {
125                final int height = image.getHeight();
126                final int width = image.getWidth();
127                final int bands = image.numBands();
128
129                final float[][] f = new float[height * width][bands + 2];
130                for (int b = 0; b < bands; b++) {
131                        final float[][] band = image.getBand(b).pixels;
132                        final float w = scaling == null ? 1 : scaling[b];
133
134                        for (int y = 0; y < height; y++)
135                                for (int x = 0; x < width; x++)
136                                        f[x + y * width][b] = band[y][x] * w;
137                }
138                for (int y = 0; y < height; y++) {
139                        for (int x = 0; x < width; x++) {
140                                f[x + y * width][bands] = ((float) x / (float) width) * (scaling == null ? 1 : scaling[bands]);
141                                f[x + y * width][bands + 1] = ((float) y / (float) height) * (scaling == null ? 1 : scaling[bands + 1]);
142                        }
143                }
144
145                return f;
146        }
147}