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.processing.transform;
31  
32  import org.openimaj.image.FImage;
33  import org.openimaj.image.analysis.algorithm.ImageInterpolation;
34  import org.openimaj.image.analysis.algorithm.ImageInterpolation.InterpolationType;
35  import org.openimaj.image.analysis.algorithm.ImageInterpolation.Interpolator;
36  import org.openimaj.image.processor.ImageProcessor;
37  import org.openimaj.image.processor.SinglebandImageProcessor;
38  
39  /**
40   * {@link ImageProcessor} and associated static utility methods for transforming
41   * an image based on the pixel positions given by a distortion map:
42   * <code>destination(x,y) = source(mapx(x,y), mapy(x,y))</code>. This allows
43   * efficient implementation of highly non-linear mappings.
44   *
45   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
46   */
47  public class RemapProcessor implements SinglebandImageProcessor<Float, FImage> {
48  	ImageInterpolation interpolation;
49  	FImage xords;
50  	FImage yords;
51  
52  	/**
53  	 * Construct with the given distortion map, and use
54  	 * {@link InterpolationType#BILINEAR} interpolation.
55  	 *
56  	 * @param xords
57  	 *            the x-ordinates
58  	 * @param yords
59  	 *            the y-ordinates
60  	 */
61  	public RemapProcessor(FImage xords, FImage yords) {
62  		this(xords, yords, ImageInterpolation.InterpolationType.BILINEAR);
63  	}
64  
65  	/**
66  	 * Construct with the given distortion map and interpolator.
67  	 *
68  	 * @param xords
69  	 *            the x-ordinates
70  	 * @param yords
71  	 *            the y-ordinates
72  	 * @param interpolator
73  	 *            the interpolator
74  	 */
75  	public RemapProcessor(FImage xords, FImage yords, Interpolator interpolator) {
76  		this.interpolation = new ImageInterpolation(interpolator);
77  		this.xords = xords;
78  		this.yords = yords;
79  	}
80  
81  	@Override
82  	public void processImage(FImage image) {
83  		final FImage out = remap(image, xords, yords, interpolation);
84  		image.internalAssign(out);
85  	}
86  
87  	/**
88  	 * Transform an image using the given parameters
89  	 *
90  	 * @param in
91  	 *            the image to transform
92  	 * @param xords
93  	 *            the x-ordinates
94  	 * @param yords
95  	 *            the y-ordinates
96  	 * @param interpolator
97  	 *            the interpolator
98  	 * @return the transformed image
99  	 */
100 	public static FImage remap(FImage in, FImage xords, FImage yords, Interpolator interpolator) {
101 		return remap(in, xords, yords, new ImageInterpolation(interpolator));
102 	}
103 
104 	/**
105 	 * Transform an image using the given parameters
106 	 *
107 	 * @param in
108 	 *            the image to transform
109 	 * @param xords
110 	 *            the x-ordinates
111 	 * @param yords
112 	 *            the y-ordinates
113 	 * @param interpolation
114 	 *            the interpolation
115 	 * @return the transformed image
116 	 */
117 	public static FImage remap(FImage in, FImage xords, FImage yords, ImageInterpolation interpolation) {
118 		return remap(in, new FImage(xords.width, xords.height), xords, yords, interpolation);
119 	}
120 
121 	/**
122 	 * Transform an image using the given parameters, and write te results into
123 	 * <code>out</code>
124 	 *
125 	 * @param in
126 	 *            the image to transform
127 	 * @param out
128 	 *            the output image
129 	 * @param xords
130 	 *            the x-ordinates
131 	 * @param yords
132 	 *            the y-ordinates
133 	 * @param interpolation
134 	 *            the interpolation
135 	 * @return out
136 	 */
137 	public static FImage remap(FImage in, FImage out, FImage xords, FImage yords, ImageInterpolation interpolation) {
138 		final int width = Math.min(xords.width, out.width);
139 		final int height = Math.min(xords.height, out.height);
140 
141 		interpolation.analyseImage(in);
142 
143 		for (int y = 0; y < height; y++) {
144 			for (int x = 0; x < width; x++) {
145 				out.pixels[y][x] = interpolation.getPixelInterpolated(xords.pixels[y][x], yords.pixels[y][x]);
146 			}
147 		}
148 		return out;
149 	}
150 
151 	/**
152 	 * Transform an image using the given parameters. Assume
153 	 * {@link InterpolationType#BILINEAR} interpolation.
154 	 *
155 	 * @param in
156 	 *            the image to transform
157 	 * @param xords
158 	 *            the x-ordinates
159 	 * @param yords
160 	 *            the y-ordinates
161 	 * @return the transformed image
162 	 */
163 	public static FImage remap(FImage in, FImage xords, FImage yords) {
164 		return remap(in, xords, yords, new ImageInterpolation(ImageInterpolation.InterpolationType.BILINEAR));
165 	}
166 
167 	/**
168 	 * Transform an image using the given parameters, and write the results into
169 	 * <code>out</code>. Assume {@link InterpolationType#BILINEAR}
170 	 * interpolation.
171 	 *
172 	 * @param in
173 	 *            the image to transform
174 	 * @param out
175 	 *            the output image
176 	 * @param xords
177 	 *            the x-ordinates
178 	 * @param yords
179 	 *            the y-ordinates
180 	 * @return out
181 	 */
182 	public static FImage remap(FImage in, FImage out, FImage xords, FImage yords) {
183 		return remap(in, out, xords, yords, new ImageInterpolation(ImageInterpolation.InterpolationType.BILINEAR));
184 	}
185 }