1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
51
52
53
54
55
56
57
58
59
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
68
69
70
71
72
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
92
93
94
95
96
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
110
111
112
113
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
190
191
192
193
194
195
196
197
198
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 }