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.video.utils;
31  
32  import java.awt.event.MouseEvent;
33  import java.awt.event.MouseListener;
34  
35  import javax.swing.JFrame;
36  
37  import org.openimaj.feature.local.list.LocalFeatureList;
38  import org.openimaj.image.DisplayUtilities;
39  import org.openimaj.image.FImage;
40  import org.openimaj.image.Image;
41  import org.openimaj.image.feature.local.engine.ipd.InterestPointImageExtractorProperties;
42  import org.openimaj.image.feature.local.interest.InterestPointData;
43  import org.openimaj.image.feature.local.keypoints.InterestPointKeypoint;
44  import org.openimaj.image.processing.convolution.FGaussianConvolve;
45  import org.openimaj.image.processing.resize.ResizeProcessor;
46  import org.openimaj.image.processor.SinglebandImageProcessor;
47  import org.openimaj.math.geometry.point.Point2dImpl;
48  import org.openimaj.math.geometry.shape.Circle;
49  
50  /**
51   * Click on features and draw them
52   * @author Sina Samangooei (ss@ecs.soton.ac.uk)
53   *
54   * @param <S>
55   * @param <T>
56   */
57  public class FeatureClickListener<S,T extends Image<S,T> & SinglebandImageProcessor.Processable<Float,FImage,T> > implements MouseListener {
58  
59  	private LocalFeatureList<InterestPointKeypoint<InterestPointData>> points = null;
60  	private T image;
61  	private JFrame frame = null;
62  	private ResizeProcessor r = new ResizeProcessor(100,100);
63  	
64  	@Override
65  	public synchronized void mouseClicked(MouseEvent e) {
66  		if(this.points == null) return;
67  		double dist = Double.MAX_VALUE;
68  		Circle foundShape = null;
69  		InterestPointKeypoint<InterestPointData> foundPoint = null;
70  		Point2dImpl clickPoint = new Point2dImpl(e.getPoint().x,e.getPoint().y);
71  		for(InterestPointKeypoint<InterestPointData> point : points){
72  			Circle ellipse = new Circle(point.location.x, point.location.y, point.scale);
73  			if(ellipse.isInside(clickPoint)){
74  //				double pdist = Math.sqrt(clickPoint.x * clickPoint.x + clickPoint.y * clickPoint.y);
75  				double pdist = point.scale;
76  				if(pdist < dist){
77  					foundShape = ellipse;
78  					foundPoint = point;
79  					dist = pdist;
80  				}
81  			}
82  		}
83  		if(foundShape!=null){
84  //			PolygonExtractionProcessor<S, T> ext = new PolygonExtractionProcessor<S,T>(foundShape, image.newInstance(1, 1).getPixel(0,0));
85  			FGaussianConvolve blur = new FGaussianConvolve (foundPoint.scale);
86  			InterestPointImageExtractorProperties<S, T> extract = new InterestPointImageExtractorProperties<S,T>(image.process(blur),foundPoint.location);
87  			if(frame== null){
88  				frame = DisplayUtilities.display(extract.image.process(r));
89  			}
90  			else{
91  				frame.dispose();
92  				frame = DisplayUtilities.display(extract.image.process(r));
93  			}
94  		}
95  	}
96  
97  	@Override
98  	public void mousePressed(MouseEvent e) {
99  		// TODO Auto-generated method stub
100 		
101 	}
102 
103 	@Override
104 	public void mouseReleased(MouseEvent e) {
105 		// TODO Auto-generated method stub
106 		
107 	}
108 
109 	@Override
110 	public void mouseEntered(MouseEvent e) {
111 		// TODO Auto-generated method stub
112 		
113 	}
114 
115 	@Override
116 	public void mouseExited(MouseEvent e) {
117 		// TODO Auto-generated method stub
118 		
119 	}
120 
121 
122 	/**
123 	 * @return the clicked on
124 	 */
125 	public LocalFeatureList<InterestPointKeypoint<InterestPointData>> getPoints() {
126 		return points;
127 	}
128 
129 	/**
130 	 * the image and keypoints to draw
131 	 * @param kpl
132 	 * @param image
133 	 */
134 	public synchronized void setImage(LocalFeatureList<InterestPointKeypoint<InterestPointData>> kpl,T image) {
135 		this.image = image;
136 		this.points = kpl;
137 	}
138 
139 	/**
140 	 * @return the underlying image being clicked on
141 	 */
142 	public T getImage() {
143 		return image;
144 	}
145 
146 }