View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.image.segmentation;
31  
32  import org.openimaj.feature.FloatFVComparator;
33  import org.openimaj.image.MBFImage;
34  import org.openimaj.image.colour.ColourSpace;
35  
36  /**
37   * Simple image segmentation from grouping colours with k-means, and also
38   * incorporating a spatial aspect based on pixel location.
39   * 
40   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
41   * 
42   */
43  public class KMSpatialColourSegmenter extends KMColourSegmenter {
44  
45  	/**
46  	 * Construct using the given colour space and number of segments. Euclidean
47  	 * distance is used, and the elements of each colour band are unscaled. Up
48  	 * to 100 K-Means iterations will be performed.
49  	 * 
50  	 * @param colourSpace
51  	 *            the colour space
52  	 * @param K
53  	 *            the number of segments
54  	 */
55  	public KMSpatialColourSegmenter(ColourSpace colourSpace, int K) {
56  		super(colourSpace, K);
57  	}
58  
59  	/**
60  	 * Construct using the given colour space, number of segments, and distance
61  	 * measure. The elements of each colour band are unscaled. Up to 100 K-Means
62  	 * iterations will be performed.
63  	 * 
64  	 * @param colourSpace
65  	 *            the colour space
66  	 * @param K
67  	 *            the number of segments
68  	 * @param distance
69  	 *            the distance measure
70  	 */
71  	public KMSpatialColourSegmenter(ColourSpace colourSpace, int K, FloatFVComparator distance) {
72  		super(colourSpace, K, distance);
73  	}
74  
75  	/**
76  	 * Construct using the given colour space, number of segments, and distance
77  	 * measure. The elements of each colour band are by the corresponding
78  	 * elements in the given scaling vector; the scaling vector should be two
79  	 * elements longer than the number of colour bands of the target colour
80  	 * space such that the last two elements correspond to the scalings for the
81  	 * normalised x and y positions of the pixels. Up to 100 K-Means iterations
82  	 * will be performed.
83  	 * 
84  	 * @param colourSpace
85  	 *            the colour space
86  	 * @param scaling
87  	 *            the scaling vector
88  	 * @param K
89  	 *            the number of segments
90  	 * @param distance
91  	 *            the distance measure
92  	 */
93  	public KMSpatialColourSegmenter(ColourSpace colourSpace, float[] scaling, int K, FloatFVComparator distance) {
94  		super(colourSpace, scaling, K, distance);
95  	}
96  
97  	/**
98  	 * Construct using the given colour space, number of segments, and distance
99  	 * 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 }