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.io.File;
033import java.io.IOException;
034import java.util.ArrayList;
035import java.util.List;
036
037import org.openimaj.image.DisplayUtilities;
038import org.openimaj.image.FImage;
039import org.openimaj.image.ImageUtilities;
040import org.openimaj.image.MBFImage;
041import org.openimaj.image.colour.RGBColour;
042import org.openimaj.image.processing.resize.ResizeProcessor;
043import org.openimaj.math.geometry.line.Line2d;
044import org.openimaj.math.geometry.point.Point2d;
045import org.openimaj.math.geometry.point.Point2dImpl;
046import org.openimaj.math.geometry.point.PointList;
047import org.openimaj.math.geometry.point.PointListConnections;
048
049public class PDMTest {
050        static List<PointList> loadData() {
051                final float[][] rawData = new float[][] {
052                                { 576, 303, 579, 418, 570, 466, 457, 415, 408, 355, 495, 216, 420, 99, 696, 81, 677, 206, 792, 382, 772,
053                                        438 },
054                                        { 442, 289, 444, 392, 442, 433, 417, 428, 326, 401, 393, 215, 268, 169, 514, 156, 550, 218, 626, 412,
055                                                527, 437 },
056                                                { 548, 317, 543, 423, 543, 464, 512, 463, 403, 434, 493, 254, 388, 216, 581, 187, 643, 243, 746, 447,
057                                                        627, 472 },
058                                                        { 563, 302, 561, 404, 537, 450, 536, 464, 425, 421, 476, 231, 349, 191, 616, 160, 656, 223, 753, 435,
059                                                                631, 462 },
060                                                                { 550, 267, 555, 375, 532, 413, 488, 442, 407, 392, 470, 193, 349, 164, 617, 124, 654, 189, 742, 405,
061                                                                        650, 449 },
062                                                                        { 565, 318, 578, 441, 566, 477, 513, 489, 409, 445, 490, 231, 356, 112, 739, 88, 673, 222, 787, 461, 670,
063                                                                                498 },
064                                // { 557, 312, 572, 434, 548, 472, 556, 464, 512, 481, 395, 438,
065                                // 477, 229, 352, 118, 715, 88, 671, 219, 783,
066                                // 455 },
067                                                                                { 574, 308, 560, 423, 566, 482, 526, 470, 405, 435, 496, 234, 374, 194, 636, 170, 682, 229, 800, 444,
068                                                                                        665, 476 },
069                                                                                        { 580, 308, 578, 424, 571, 468, 524, 513, 437, 453, 509, 228, 372, 173, 643, 154, 693, 227, 758, 469,
070                                                                                                668, 512 },
071                                                                                                { 582, 305, 583, 419, 583, 467, 422, 505, 411, 438, 496, 227, 349, 111, 766, 86, 698, 212, 775, 458, 762,
072                                                                                                        527 }
073                };
074
075                final List<PointList> ptsL = new ArrayList<PointList>();
076                for (int i = 0; i < rawData.length; i++) {
077                        final PointList pl = new PointList();
078
079                        for (int j = 0; j < rawData[i].length; j += 2) {
080                                final float x = rawData[i][j + 1];
081                                final float y = rawData[i][j + 0];
082
083                                pl.points.add(new Point2dImpl(x, y));
084                        }
085
086                        ptsL.add(pl);
087                }
088
089                return ptsL;
090        }
091
092        static List<FImage> loadImages() throws IOException {
093                final ArrayList<FImage> list = new ArrayList<FImage>();
094
095                for (int i = 1; i <= 10; i++) {
096                        if (i == 7)
097                                continue;
098                        FImage img = ImageUtilities.readF(new File(
099                                        "/Users/jon/Work/students/merlin/individual_project/Source/Data/Pictures/bp" + i + ".JPG"));
100
101                        img = img.processInplace(new ResizeProcessor(600, 900, false));
102
103                        list.add(img);
104                }
105                return list;
106        }
107
108        static PointListConnections loadConnections() {
109                final PointListConnections plc = new PointListConnections();
110                plc.addConnection(0, 1);
111                plc.addConnection(1, 2);
112                plc.addConnection(1, 4);
113                plc.addConnection(1, 9);
114                plc.addConnection(4, 3);
115                plc.addConnection(9, 10);
116                plc.addConnection(0, 5);
117                plc.addConnection(0, 8);
118                plc.addConnection(5, 6);
119                plc.addConnection(8, 7);
120                return plc;
121        }
122
123        // public static void main(String[] args) {
124        // final int width = 900, height = 600;
125        //
126        // final List<PointList> pointData = loadData();
127        // final PointListConnections plc = loadConnections();
128        //
129        // final Float[][] cols = new Float[pointData.get(0).size()][];
130        // for (int i = 0; i < cols.length; i++)
131        // cols[i] = RGBColour.randomColour();
132        //
133        // // for (final PointList pl : pointData) {
134        // // final MBFImage img = new MBFImage(width, height, 3);
135        // // final List<Line2d> lines = plc.getLines(pl);
136        // // img.drawLines(lines, 1, RGBColour.RED);
137        // //
138        // // for (int i = 0; i < pl.size(); i++) {
139        // // final Point2d pt = pl.get(i);
140        // // img.drawPoint(pt, cols[i], 3);
141        // // }
142        // // DisplayUtilities.display(img);
143        // // }
144        //
145        // final PointDistributionModel pdm = new PointDistributionModel(pointData);
146        // pdm.setNumComponents(2);
147        //
148        // final double sd = pdm.getStandardDeviations(3.0)[0];
149        // VideoDisplay.createVideoDisplay(new AnimatedVideo<MBFImage>(new
150        // MBFImage(width, height, 3))
151        // {
152        // ValueAnimator<Double> a = ForwardBackwardLoopingValueAnimator
153        // .loop(new LinearDoubleValueAnimator(-sd, sd, 60));
154        //
155        // @Override
156        // protected void updateNextFrame(MBFImage frame) {
157        // frame.fill(RGBColour.BLACK);
158        // final Double val = a.nextValue();
159        //
160        // System.out.println(val);
161        // final PointList newShape = pdm.generateNewShape(new double[] {
162        // val });
163        //
164        // final PointList tpts =
165        // newShape.transform(TransformUtilities.translateMatrix(width / 2,
166        // height / 2).times(
167        // TransformUtilities.scaleMatrix(100, 100)));
168        //
169        // for (int i = 0; i < tpts.size(); i++) {
170        // final Point2d pt = tpts.get(i);
171        // frame.drawPoint(pt, cols[i], 10);
172        // }
173        //
174        // final List<Line2d> lines = plc.getLines(tpts);
175        // frame.drawLines(lines, 5, RGBColour.RED);
176        // }
177        // });
178        // }
179
180        public static void main(String[] args) throws IOException {
181                final List<PointList> pointData = loadData();
182                final PointListConnections plc = loadConnections();
183                final List<FImage> images = loadImages();
184
185                System.out.println(pointData.size());
186                System.out.println(images.size());
187
188                final Float[][] cols = new Float[pointData.get(0).size()][];
189                for (int i = 0; i < cols.length; i++)
190                        cols[i] = RGBColour.randomColour();
191
192                for (int j = 0; j < pointData.size(); j++) {
193                        final PointList pl = pointData.get(j);
194                        final MBFImage img = images.get(j).toRGB();
195
196                        final List<Line2d> lines = plc.getLines(pl);
197                        img.drawLines(lines, 1, RGBColour.RED);
198
199                        for (int i = 0; i < pl.size(); i++) {
200                                final Point2d pt = pl.get(i);
201                                img.drawPoint(pt, cols[i], 3);
202                        }
203                        DisplayUtilities.display(img);
204                }
205        }
206}