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 javax.swing.JFrame;
33
34 import org.openimaj.demos.Demo;
35 import org.openimaj.image.DisplayUtilities;
36 import org.openimaj.image.MBFImage;
37 import org.openimaj.image.colour.ColourSpace;
38 import org.openimaj.image.colour.RGBColour;
39 import org.openimaj.math.geometry.shape.Ellipse;
40 import org.openimaj.math.geometry.transforms.TransformUtilities;
41
42 import Jama.Matrix;
43
44
45
46
47
48
49
50
51 @Demo(
52 author = "Sina Samangooei",
53 description = "Demonstrates affine transforms for shapes.",
54 keywords = { "shape", "affine", "transform" },
55 title = "Affine Shape Transforms",
56 icon = "/org/openimaj/demos/icons/image/affine-icon.png"
57 )
58 public class TestShapeTransforms {
59 private static Runnable displayUpdater;
60 private static JFrame frame;
61 private static double rotation = Math.PI*2/4;
62 private static Ellipse ellipse;
63 private static MBFImage image;
64
65
66
67
68
69 public static void main(String args[]){
70 ellipse = new Ellipse(400,400,100,50,0);
71 image = new MBFImage(800,800,ColourSpace.RGB);
72 frame = DisplayUtilities.display(image);
73 displayUpdater = new Runnable(){
74 @Override
75 public void run() {
76 while(true){
77 DisplayUtilities.display(image,frame);
78 update();
79 try {
80 Thread.sleep(1000/30);
81 } catch (InterruptedException e) {
82 }
83 }
84 }
85
86 };
87 Thread t = new Thread(displayUpdater);
88 t.start();
89 }
90
91 private static void update() {
92 rotation += Math.PI/30;
93
94
95
96
97 Matrix rotMat = TransformUtilities.rotationMatrixAboutPoint(rotation, ellipse.calculateCentroid().getX(), ellipse.calculateCentroid().getY());
98
99 Matrix scaleMat = TransformUtilities.scaleMatrix(Math.abs(0.5 * Math.cos(rotation)) + 1, Math.abs(0.5 * Math.sin(rotation))+ 1);
100 Matrix scaledTrans = scaleMat.times(TransformUtilities.translateMatrix(-ellipse.calculateCentroid().getX(), -ellipse.calculateCentroid().getY()));
101 scaledTrans = TransformUtilities.translateMatrix(ellipse.calculateCentroid().getX(), ellipse.calculateCentroid().getY()).times(scaledTrans);
102 Matrix transform = Matrix.identity(3, 3);
103 transform = rotMat.times(transform);
104
105
106 image.fill(RGBColour.BLACK);
107 image.createRenderer().drawShapeFilled(ellipse.transformAffine(transform), RGBColour.RED);
108 }
109
110 }