001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.demos.image; 031 032import java.awt.event.MouseEvent; 033import java.awt.event.MouseMotionListener; 034import java.io.IOException; 035import java.util.ArrayList; 036import java.util.List; 037 038import javax.swing.JFrame; 039 040import org.openimaj.demos.Demo; 041import org.openimaj.image.DisplayUtilities; 042import org.openimaj.image.ImageUtilities; 043import org.openimaj.image.MBFImage; 044import org.openimaj.image.pixel.Pixel; 045import org.openimaj.image.processing.transform.PiecewiseMeshWarp; 046import org.openimaj.math.geometry.shape.Shape; 047import org.openimaj.math.geometry.shape.Triangle; 048import org.openimaj.util.pair.Pair; 049 050/** 051 * Demonstrate the {@link PiecewiseMeshWarp}. 052 * 053 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 054 * 055 */ 056@Demo( 057 author = "Jonathon Hare", 058 description = "Demonstrates the OpenIMAJ piecewise mesh image warp processor. " + 059 "On the displayed image, drag the mouse to move the warp point.", 060 keywords = { "image", "distortion", "warp", "non-linear" }, 061 title = "Non-Linear Image Warp", 062 icon = "/org/openimaj/demos/icons/image/bird-icon.png") 063public class PiecewiseMeshWarpDemo implements MouseMotionListener { 064 private JFrame frame; 065 private MBFImage img; 066 067 /** 068 * Construct the demo 069 * 070 * @throws IOException 071 */ 072 public PiecewiseMeshWarpDemo() throws IOException { 073 img = ImageUtilities.readMBF(getClass().getResource("/org/openimaj/demos/image/bird.png")); 074 frame = DisplayUtilities.displaySimple(img); 075 076 frame.addMouseMotionListener(this); 077 } 078 079 protected void updateImage(Pixel newCentre) { 080 final Pixel p1 = new Pixel(0, 0); 081 final Pixel p2 = new Pixel(img.getWidth(), 0); 082 final Pixel p3 = new Pixel(img.getWidth(), img.getHeight()); 083 final Pixel p4 = new Pixel(0, img.getHeight()); 084 final Pixel p5 = new Pixel(img.getWidth() / 2, img.getHeight() / 2); 085 086 final Pixel np1 = new Pixel(0, 0); 087 final Pixel np2 = new Pixel(img.getWidth(), 0); 088 final Pixel np3 = new Pixel(img.getWidth(), img.getHeight()); 089 final Pixel np4 = new Pixel(0, img.getHeight()); 090 final Pixel np5 = newCentre; 091 092 final List<Pair<Shape>> matchingRegions = new ArrayList<Pair<Shape>>(); 093 matchingRegions.add(new Pair<Shape>(new Triangle(p1, p2, p5), new Triangle(np1, np2, np5))); 094 matchingRegions.add(new Pair<Shape>(new Triangle(p2, p3, p5), new Triangle(np2, np3, np5))); 095 matchingRegions.add(new Pair<Shape>(new Triangle(p3, p4, p5), new Triangle(np3, np4, np5))); 096 matchingRegions.add(new Pair<Shape>(new Triangle(p4, p1, p5), new Triangle(np4, np1, np5))); 097 098 DisplayUtilities.display(img.process(new PiecewiseMeshWarp<Float[], MBFImage>(matchingRegions)), frame); 099 } 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}