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.video.utils;
031
032import java.awt.event.MouseEvent;
033import java.awt.event.MouseListener;
034
035import javax.swing.JFrame;
036
037import org.openimaj.feature.local.list.LocalFeatureList;
038import org.openimaj.image.DisplayUtilities;
039import org.openimaj.image.FImage;
040import org.openimaj.image.Image;
041import org.openimaj.image.feature.local.engine.ipd.InterestPointImageExtractorProperties;
042import org.openimaj.image.feature.local.interest.InterestPointData;
043import org.openimaj.image.feature.local.keypoints.InterestPointKeypoint;
044import org.openimaj.image.processing.convolution.FGaussianConvolve;
045import org.openimaj.image.processing.resize.ResizeProcessor;
046import org.openimaj.image.processor.SinglebandImageProcessor;
047import org.openimaj.math.geometry.point.Point2dImpl;
048import org.openimaj.math.geometry.shape.Circle;
049
050/**
051 * Click on features and draw them
052 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
053 *
054 * @param <S>
055 * @param <T>
056 */
057public class FeatureClickListener<S,T extends Image<S,T> & SinglebandImageProcessor.Processable<Float,FImage,T> > implements MouseListener {
058
059        private LocalFeatureList<InterestPointKeypoint<InterestPointData>> points = null;
060        private T image;
061        private JFrame frame = null;
062        private ResizeProcessor r = new ResizeProcessor(100,100);
063        
064        @Override
065        public synchronized void mouseClicked(MouseEvent e) {
066                if(this.points == null) return;
067                double dist = Double.MAX_VALUE;
068                Circle foundShape = null;
069                InterestPointKeypoint<InterestPointData> foundPoint = null;
070                Point2dImpl clickPoint = new Point2dImpl(e.getPoint().x,e.getPoint().y);
071                for(InterestPointKeypoint<InterestPointData> point : points){
072                        Circle ellipse = new Circle(point.location.x, point.location.y, point.scale);
073                        if(ellipse.isInside(clickPoint)){
074//                              double pdist = Math.sqrt(clickPoint.x * clickPoint.x + clickPoint.y * clickPoint.y);
075                                double pdist = point.scale;
076                                if(pdist < dist){
077                                        foundShape = ellipse;
078                                        foundPoint = point;
079                                        dist = pdist;
080                                }
081                        }
082                }
083                if(foundShape!=null){
084//                      PolygonExtractionProcessor<S, T> ext = new PolygonExtractionProcessor<S,T>(foundShape, image.newInstance(1, 1).getPixel(0,0));
085                        FGaussianConvolve blur = new FGaussianConvolve (foundPoint.scale);
086                        InterestPointImageExtractorProperties<S, T> extract = new InterestPointImageExtractorProperties<S,T>(image.process(blur),foundPoint.location);
087                        if(frame== null){
088                                frame = DisplayUtilities.display(extract.image.process(r));
089                        }
090                        else{
091                                frame.dispose();
092                                frame = DisplayUtilities.display(extract.image.process(r));
093                        }
094                }
095        }
096
097        @Override
098        public void mousePressed(MouseEvent e) {
099                // 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}