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.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   * Demonstrate the {@link PiecewiseMeshWarp}.
52   * 
53   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
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  	 * Construct the demo
69  	 * 
70  	 * @throws IOException
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 	 * The main method
114 	 * 
115 	 * @param args
116 	 *            ignored
117 	 * @throws IOException
118 	 */
119 	public static void main(String[] args) throws IOException {
120 		new PiecewiseMeshWarpDemo();
121 	}
122 }