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.sandbox;
031
032import java.awt.event.ActionEvent;
033import java.awt.event.ActionListener;
034import java.awt.event.MouseAdapter;
035import java.awt.event.MouseEvent;
036import java.util.List;
037
038import javax.swing.JComboBox;
039import javax.swing.JFrame;
040import javax.swing.JPanel;
041import javax.swing.JSpinner;
042import javax.swing.SpinnerModel;
043import javax.swing.SpinnerNumberModel;
044import javax.swing.event.ChangeEvent;
045import javax.swing.event.ChangeListener;
046
047import org.openimaj.image.DisplayUtilities;
048import org.openimaj.image.DisplayUtilities.ImageComponent;
049import org.openimaj.image.FImage;
050import org.openimaj.image.ImageUtilities;
051import org.openimaj.image.MBFImage;
052import org.openimaj.image.colour.RGBColour;
053import org.openimaj.image.pixel.Pixel;
054
055/**
056 * Sample UI for building a PDM
057 *
058 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
059 *
060 */
061public class PDMBuilder implements ActionListener, ChangeListener {
062        private String[] pointLabels;
063        private List<FImage> images;
064        private ImageComponent ic;
065        private Pixel[][] pointData;
066        private JComboBox<String> labelsList;
067        private JSpinner imageSpinner;
068
069        public PDMBuilder(String[] pointLabels, List<FImage> images) {
070                this.pointLabels = pointLabels;
071                this.images = images;
072
073                this.pointData = new Pixel[images.size()][pointLabels.length];
074
075                createUI();
076        }
077
078        private void createUI() {
079                final JFrame frame = new JFrame("PDM Builder");
080
081                final JPanel panel = new JPanel();
082                frame.getContentPane().add(panel);
083
084                ic = new DisplayUtilities.ImageComponent(true, false);
085                ic.setAllowPanning(false);
086                ic.setAllowZoom(false);
087                ic.setImage(ImageUtilities.createBufferedImage(images.get(0)));
088                ic.setPreferredSize(ic.getSize());
089                ic.addMouseListener(new MouseAdapter() {
090                        @Override
091                        public void mouseClicked(MouseEvent e) {
092                                registerClick(e.getX(), e.getY());
093                        }
094                });
095                panel.add(ic);
096
097                final SpinnerModel model = new SpinnerNumberModel(0, 0, images.size(), 1);
098                imageSpinner = new JSpinner(model);
099                new JSpinner.NumberEditor(imageSpinner);
100                imageSpinner.addChangeListener(this);
101                panel.add(imageSpinner);
102
103                labelsList = new JComboBox<String>(pointLabels);
104                labelsList.addActionListener(this);
105                panel.add(labelsList);
106
107                frame.pack();
108                frame.setVisible(true);
109        }
110
111        private void registerClick(int x, int y) {
112                final int imageId = (int) imageSpinner.getModel().getValue();
113                final int labelId = labelsList.getSelectedIndex();
114
115                pointData[imageId][labelId] = new Pixel(x, y);
116
117                int nextLabel = -1;
118                int nextImage = nextImage(labelId);
119                if (nextLabel == -1) {
120                        for (int i = 0; i < pointLabels.length; i++) {
121                                nextImage = nextImage(i);
122                                if (nextImage != -1) {
123                                        nextLabel = i;
124                                        break;
125                                }
126                        }
127                }
128                if (nextLabel == -1)
129                        nextLabel = labelId;
130                if (nextImage == -1)
131                        nextImage = imageId;
132
133                imageSpinner.setValue(nextImage);
134                labelsList.setSelectedIndex(nextLabel);
135
136                updateImage();
137        }
138
139        private int nextImage(int labelId) {
140                for (int i = 0; i < images.size(); i++) {
141                        if (pointData[i][labelId] == null) {
142                                return i;
143                        }
144                }
145                return -1;
146        }
147
148        @Override
149        public void actionPerformed(ActionEvent e) {
150                updateImage();
151        }
152
153        private void updateImage() {
154                final int imageId = (int) imageSpinner.getModel().getValue();
155
156                final MBFImage img = images.get(imageId).toRGB();
157                for (final Pixel p : pointData[imageId]) {
158                        if (p != null) {
159                                img.drawPoint(p, RGBColour.RED, 9);
160                        }
161                }
162                ic.setImage(ImageUtilities.createBufferedImage(img));
163        }
164
165        @Override
166        public void stateChanged(ChangeEvent e) {
167                updateImage();
168        }
169
170        public static void main(String[] args) throws Exception {
171                final String[] pointLabels = { "Waist", "Neck", "Head", "Left hand", "Left elbow", "Left knee", "Left foot",
172                                "Right foot", "Right knee", "Right elbow", "Right hand" };
173                final List<FImage> images = PDMTest.loadImages();
174                System.out.println("images loaded");
175
176                new PDMBuilder(pointLabels, images);
177        }
178}