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 java.util.ArrayList;
33  import java.util.List;
34  
35  import org.openimaj.image.Image;
36  import org.openimaj.image.pixel.Pixel;
37  import org.openimaj.image.processor.ImageProcessor;
38  import org.openimaj.image.renderer.ScanRasteriser;
39  import org.openimaj.image.renderer.ScanRasteriser.ScanLineListener;
40  import org.openimaj.math.geometry.point.Point2d;
41  import org.openimaj.math.geometry.shape.Polygon;
42  import org.openimaj.math.geometry.shape.Rectangle;
43  import org.openimaj.math.geometry.shape.Shape;
44  import org.openimaj.math.geometry.transforms.TransformUtilities;
45  import org.openimaj.util.pair.Pair;
46  
47  import Jama.Matrix;
48  
49  /**
50   * Implementation of a piecewise warp. The warp can be piecewise affine,
51   * piecewise homographic or a mixture of the two. Basically this means you can
52   * warp images represented by triangles, quads or a mixture of the two.
53   * 
54   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
55   * 
56   * @param <T>
57   *            the pixel type
58   * @param <I>
59   *            the actual image of the concrete subclass
60   */
61  public class PiecewiseMeshWarp<T, I extends Image<T, I>> implements ImageProcessor<I> {
62  	List<Pair<Shape>> matchingRegions;
63  	List<Matrix> transforms = new ArrayList<Matrix>();
64  	Rectangle bounds;
65  
66  	/**
67  	 * Construct the warp with a list of matching shapes (which must be either
68  	 * quads or triangles). The pairs map from the measured/observed space to
69  	 * the canonical space.
70  	 * 
71  	 * @param matchingRegions
72  	 *            the matching shapes
73  	 */
74  	public PiecewiseMeshWarp(List<Pair<Shape>> matchingRegions) {
75  		this.matchingRegions = matchingRegions;
76  		initTransforms();
77  	}
78  
79  	protected final Matrix getTransform(Point2d p) {
80  		final int sz = matchingRegions.size();
81  
82  		for (int i = 0; i < sz; i++) {
83  			if (matchingRegions.get(i).secondObject().isInside(p)) {
84  				return transforms.get(i);
85  			}
86  		}
87  		return null;
88  	}
89  
90  	/**
91  	 * Get the shape in the observation space for a point in the canonical
92  	 * space.
93  	 * 
94  	 * @param p
95  	 *            point in the canonical space.
96  	 * @return the matching shape in observed space
97  	 */
98  	public Shape getMatchingShape(Point2d p) {
99  		for (int i = 0; i < matchingRegions.size(); i++) {
100 			final Pair<Shape> matching = matchingRegions.get(i);
101 			if (matching.secondObject().isInside(p)) {
102 				return matching.firstObject();
103 			}
104 		}
105 		return null;
106 	}
107 
108 	/**
109 	 * Get the shape pair index for a point in the canonical space.
110 	 * 
111 	 * @param p
112 	 *            the point in canonical space.
113 	 * @return the index of the matching point pair
114 	 */
115 	public int getMatchingShapeIndex(Point2d p) {
116 		for (int i = 0; i < matchingRegions.size(); i++) {
117 			final Pair<Shape> matching = matchingRegions.get(i);
118 			if (matching.secondObject().isInside(p)) {
119 				return i;
120 			}
121 		}
122 		return -1;
123 	}
124 
125 	protected void initTransforms() {
126 		bounds = new Rectangle(Float.MAX_VALUE, Float.MAX_VALUE, 0, 0);
127 
128 		for (final Pair<Shape> shape : matchingRegions) {
129 			final Polygon p1 = shape.firstObject().asPolygon();
130 			final Polygon p2 = shape.secondObject().asPolygon();
131 
132 			bounds.x = (float) Math.min(bounds.x, p2.minX());
133 			bounds.y = (float) Math.min(bounds.y, p2.minY());
134 			bounds.width = (float) Math.max(bounds.width, p2.maxX());
135 			bounds.height = (float) Math.max(bounds.height, p2.maxY());
136 
137 			if (p1.nVertices() == 3) {
138 				transforms.add(getTransform3(polyMatchToPointsMatch(p2, p1)));
139 			} else if (p1.nVertices() == 4) {
140 				transforms.add(getTransform4(polyMatchToPointsMatch(p2, p1)));
141 			} else {
142 				throw new RuntimeException("Only polygons with 3 or 4 vertices are supported!");
143 			}
144 		}
145 
146 		bounds.width -= bounds.x;
147 		bounds.height -= bounds.y;
148 	}
149 
150 	protected List<Pair<Point2d>> polyMatchToPointsMatch(Polygon pa, Polygon pb) {
151 		final List<Pair<Point2d>> pts = new ArrayList<Pair<Point2d>>();
152 		for (int i = 0; i < pa.nVertices(); i++) {
153 			final Point2d pta = pa.getVertices().get(i);
154 			final Point2d ptb = pb.getVertices().get(i);
155 
156 			pts.add(new Pair<Point2d>(pta, ptb));
157 		}
158 		return pts;
159 	}
160 
161 	protected Matrix getTransform4(List<Pair<Point2d>> pts) {
162 		return TransformUtilities.homographyMatrixNorm(pts);
163 	}
164 
165 	protected Matrix getTransform3(List<Pair<Point2d>> pts) {
166 		return TransformUtilities.affineMatrix(pts);
167 	}
168 
169 	@Override
170 	public void processImage(final I image) {
171 		final int width = image.getWidth();
172 		final int height = image.getHeight();
173 
174 		final I ret = image.newInstance(width, height);
175 
176 		final Scan scan = new Scan(width, height, image, ret);
177 
178 		for (int i = 0; i < matchingRegions.size(); i++) {
179 			final Polygon from = matchingRegions.get(i).secondObject().asPolygon();
180 			scan.tf = transforms.get(i);
181 
182 			ScanRasteriser.scanFill(from.points, scan);
183 		}
184 
185 		image.internalAssign(ret);
186 	}
187 
188 	/**
189 	 * Transform the content of the input image into an output image of the
190 	 * given dimensions.
191 	 * 
192 	 * @param image
193 	 *            the input image
194 	 * @param width
195 	 *            the output width
196 	 * @param height
197 	 *            the output height
198 	 * @return the output image
199 	 */
200 	public I transform(final I image, int width, int height) {
201 		final I ret = image.newInstance(width, height);
202 
203 		final Scan scan = new Scan(width, height, image, ret);
204 
205 		for (int i = 0; i < matchingRegions.size(); i++) {
206 			final Polygon from = matchingRegions.get(i).secondObject().asPolygon();
207 			scan.tf = transforms.get(i);
208 
209 			ScanRasteriser.scanFill(from.points, scan);
210 		}
211 
212 		return ret;
213 	}
214 
215 	private class Scan implements ScanLineListener {
216 		private final Pixel p = new Pixel();
217 		private final int xmin = (int) Math.max(0, bounds.x);
218 		private final int ymin = (int) Math.max(0, bounds.y);
219 		private final int xmax;
220 		private final int ymax;
221 		private final I image;
222 		private final I ret;
223 		Matrix tf;
224 
225 		Scan(int width, int height, I image, I ret) {
226 			xmax = (int) Math.min(width, bounds.x + bounds.width);
227 			ymax = (int) Math.min(height, bounds.y + bounds.height);
228 			this.image = image;
229 			this.ret = ret;
230 		}
231 
232 		@Override
233 		public void process(int x1, int x2, int y) {
234 			if (y < ymin || y > ymax)
235 				return;
236 
237 			final int startx = Math.max(xmin, Math.min(x1, x2));
238 			final int stopx = Math.min(xmax, Math.max(x1, x2));
239 
240 			for (int x = startx; x <= stopx; x++) {
241 				p.x = x;
242 				p.y = y;
243 
244 				p.transformInplace(tf);
245 
246 				ret.setPixel(x, y, image.getPixelInterp(p.x, p.y));
247 			}
248 		}
249 	}
250 }