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.util.ArrayList; 033import java.util.List; 034import java.util.Random; 035 036import org.openimaj.image.DisplayUtilities; 037import org.openimaj.image.FImage; 038import org.openimaj.image.MBFImage; 039import org.openimaj.image.colour.RGBColour; 040import org.openimaj.image.model.asm.ActiveShapeModel.IterationResult; 041import org.openimaj.image.model.asm.MultiResolutionActiveShapeModel; 042import org.openimaj.image.model.landmark.FNormalLandmarkModel; 043import org.openimaj.image.pixel.sampling.FLineSampler; 044import org.openimaj.math.geometry.point.Point2dImpl; 045import org.openimaj.math.geometry.point.PointList; 046import org.openimaj.math.geometry.point.PointListConnections; 047import org.openimaj.math.geometry.shape.PointDistributionModel; 048import org.openimaj.math.geometry.shape.Triangle; 049import org.openimaj.math.geometry.transforms.TransformUtilities; 050import org.openimaj.math.matrix.algorithm.pca.PrincipalComponentAnalysis.NumberComponentSelector; 051import org.openimaj.util.pair.IndependentPair; 052 053import Jama.Matrix; 054 055public class ASMTester { 056 public static List<IndependentPair<PointList, FImage>> generateData(int number) { 057 final List<IndependentPair<PointList, FImage>> data = new ArrayList<IndependentPair<PointList, FImage>>(number); 058 059 final Random rnd = new Random(); 060 061 for (int i = 0; i < number; i++) { 062 final float c1 = rnd.nextFloat(); 063 float c2 = rnd.nextFloat(); 064 065 while (Math.abs(c1 - c2) < 0.1) { 066 c2 = rnd.nextFloat(); 067 } 068 069 final Triangle t = new Triangle( 070 new Point2dImpl(((float) i / (number - 1)) * 80 + 10f, 10), 071 new Point2dImpl(90, 90), 072 new Point2dImpl(10, 90) 073 ); 074 075 final FImage img = new FImage(100, 100); 076 img.fill(Math.min(c1, c2)); 077 img.drawShapeFilled(t, Math.max(c1, c2)); 078 079 data.add(new IndependentPair<PointList, FImage>(t.asPolygon(), img)); 080 } 081 082 return data; 083 } 084 085 public static void main(String[] args) { 086 final List<IndependentPair<PointList, FImage>> data = generateData(10); 087 final PointListConnections connections = new PointListConnections(); 088 connections.addConnection(0, 1); 089 connections.addConnection(1, 2); 090 connections.addConnection(2, 0); 091 092 final float scale = 0.4f; 093 final FNormalLandmarkModel.Factory factory = new FNormalLandmarkModel.Factory(connections, 094 FLineSampler.INTERPOLATED_DERIVATIVE, 5, 13, scale); 095 final MultiResolutionActiveShapeModel<FImage> asm = MultiResolutionActiveShapeModel.trainModel(1, 096 new NumberComponentSelector(10), data, new PointDistributionModel.BoxConstraint(3), factory); 097 098 for (final IndependentPair<PointList, FImage> inst : generateData(10)) { 099 final MBFImage rgb = inst.secondObject().toRGB(); 100 101 // Matrix pose = TransformUtilities.translateMatrix(50, 102 // 50).times(TransformUtilities.scaleMatrix(50, 50)); 103 // PointList shape = asm.getPDM().getMean().transform(pose); 104 // Matrix pose = Matrix.identity(3, 3); 105 final Matrix pose = TransformUtilities.scaleMatrixAboutPoint(0.8, 0.8, 50, 50); 106 final PointList shape = inst.firstObject().transform(pose); 107 rgb.drawLines(connections.getLines(shape), 1, RGBColour.RED); 108 109 final IterationResult res = asm.fit(inst.secondObject(), shape); 110 111 rgb.drawLines(connections.getLines(res.shape), 1, RGBColour.GREEN); 112 113 DisplayUtilities.display(rgb); 114 } 115 } 116}