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.demos.image;
31
32 import java.awt.event.MouseEvent;
33 import java.awt.event.MouseMotionListener;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.List;
37
38 import javax.swing.JFrame;
39
40 import org.openimaj.demos.Demo;
41 import org.openimaj.image.DisplayUtilities;
42 import org.openimaj.image.ImageUtilities;
43 import org.openimaj.image.MBFImage;
44 import org.openimaj.image.pixel.Pixel;
45 import org.openimaj.image.processing.transform.PiecewiseMeshWarp;
46 import org.openimaj.math.geometry.shape.Shape;
47 import org.openimaj.math.geometry.shape.Triangle;
48 import org.openimaj.util.pair.Pair;
49
50
51
52
53
54
55
56 @Demo(
57 author = "Jonathon Hare",
58 description = "Demonstrates the OpenIMAJ piecewise mesh image warp processor. " +
59 "On the displayed image, drag the mouse to move the warp point.",
60 keywords = { "image", "distortion", "warp", "non-linear" },
61 title = "Non-Linear Image Warp",
62 icon = "/org/openimaj/demos/icons/image/bird-icon.png")
63 public class PiecewiseMeshWarpDemo implements MouseMotionListener {
64 private JFrame frame;
65 private MBFImage img;
66
67
68
69
70
71
72 public PiecewiseMeshWarpDemo() throws IOException {
73 img = ImageUtilities.readMBF(getClass().getResource("/org/openimaj/demos/image/bird.png"));
74 frame = DisplayUtilities.displaySimple(img);
75
76 frame.addMouseMotionListener(this);
77 }
78
79 protected void updateImage(Pixel newCentre) {
80 final Pixel p1 = new Pixel(0, 0);
81 final Pixel p2 = new Pixel(img.getWidth(), 0);
82 final Pixel p3 = new Pixel(img.getWidth(), img.getHeight());
83 final Pixel p4 = new Pixel(0, img.getHeight());
84 final Pixel p5 = new Pixel(img.getWidth() / 2, img.getHeight() / 2);
85
86 final Pixel np1 = new Pixel(0, 0);
87 final Pixel np2 = new Pixel(img.getWidth(), 0);
88 final Pixel np3 = new Pixel(img.getWidth(), img.getHeight());
89 final Pixel np4 = new Pixel(0, img.getHeight());
90 final Pixel np5 = newCentre;
91
92 final List<Pair<Shape>> matchingRegions = new ArrayList<Pair<Shape>>();
93 matchingRegions.add(new Pair<Shape>(new Triangle(p1, p2, p5), new Triangle(np1, np2, np5)));
94 matchingRegions.add(new Pair<Shape>(new Triangle(p2, p3, p5), new Triangle(np2, np3, np5)));
95 matchingRegions.add(new Pair<Shape>(new Triangle(p3, p4, p5), new Triangle(np3, np4, np5)));
96 matchingRegions.add(new Pair<Shape>(new Triangle(p4, p1, p5), new Triangle(np4, np1, np5)));
97
98 DisplayUtilities.display(img.process(new PiecewiseMeshWarp<Float[], MBFImage>(matchingRegions)), frame);
99 }
100
101 @Override
102 public void mouseDragged(MouseEvent e) {
103 final Pixel p = new Pixel(e.getX(), e.getY());
104 updateImage(p);
105 }
106
107 @Override
108 public void mouseMoved(MouseEvent e) {
109
110 }
111
112
113
114
115
116
117
118
119 public static void main(String[] args) throws IOException {
120 new PiecewiseMeshWarpDemo();
121 }
122 }