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.image.processing.face.util;
031
032import org.openimaj.image.MBFImage;
033import org.openimaj.image.colour.RGBColour;
034import org.openimaj.image.processing.face.detection.CLMDetectedFace;
035import org.openimaj.image.processing.face.tracking.clm.MultiTracker;
036import org.openimaj.image.processing.face.tracking.clm.MultiTracker.TrackedFace;
037import org.openimaj.math.geometry.point.Point2dImpl;
038import org.openimaj.math.geometry.shape.Rectangle;
039import org.openimaj.math.geometry.shape.Triangle;
040
041import Jama.Matrix;
042
043import com.jsaragih.IO;
044import com.jsaragih.Tracker;
045
046/**
047 * Renderer for drawing {@link CLMDetectedFace}s
048 *
049 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
050 *
051 */
052public class CLMDetectedFaceRenderer implements DetectedFaceRenderer<CLMDetectedFace> {
053        private int[][] triangles;
054        private boolean drawTriangles = true;
055        private boolean drawConnections = true;
056        private boolean drawPoints = true;
057        private boolean drawBounds = true;
058        private int[][] connections;
059        private float scale = 1f;
060        private Float[] boundingBoxColour = RGBColour.RED;
061        private Float[] pointColour = RGBColour.BLUE;
062        private Float[] meshColour = RGBColour.GREEN;
063        private Float[] connectionColour = RGBColour.YELLOW;
064        private int thickness;
065
066        /**
067         * Loads the triangles and connections used to render
068         */
069        public CLMDetectedFaceRenderer() {
070                this.triangles = IO.loadTri(Tracker.class.getResourceAsStream("face.tri"));
071                connections = IO.loadCon(Tracker.class.getResourceAsStream("face.con"));
072        }
073
074        @Override
075        public void drawDetectedFace(MBFImage image, int thickness, CLMDetectedFace f) {
076                this.thickness = thickness;
077                drawFaceModel(image, f.getShapeMatrix(), f.getVisibility(), f.getBounds());
078        }
079
080        /**
081         * Helper function, does the same as
082         * {@link #drawDetectedFace(MBFImage,int, CLMDetectedFace)} but with the
083         * insides of a {@link TrackedFace}.
084         *
085         * @param image
086         * @param f
087         */
088        public void drawDetectedFace(MBFImage image, MultiTracker.TrackedFace f) {
089                drawFaceModel(image, f.shape, f.clm._visi[f.clm.getViewIdx()], f.lastMatchBounds);
090        }
091
092        private void drawFaceModel(MBFImage image, Matrix shape, Matrix visi, Rectangle bounds)
093        {
094                final int n = shape.getRowDimension() / 2;
095
096                if (drawBounds && bounds != null)
097                        image.createRenderer().drawShape(bounds,
098                                        boundingBoxColour);
099
100                if (drawTriangles) {
101                        // Draw triangulation
102                        for (int i = 0; i < triangles.length; i++) {
103                                if (visi.get(triangles[i][0], 0) == 0 ||
104                                                visi.get(triangles[i][1], 0) == 0 ||
105                                                visi.get(triangles[i][2], 0) == 0)
106                                        continue;
107
108                                final Triangle t = new Triangle(
109                                                new Point2dImpl(
110                                                                (float) (shape.get(triangles[i][0], 0) + bounds.x) / scale,
111                                                                (float) (shape.get(triangles[i][0] + n, 0) + bounds.y) / scale),
112                                                                new Point2dImpl(
113                                                                                (float) (shape.get(triangles[i][1], 0) + bounds.x) / scale,
114                                                                                (float) (shape.get(triangles[i][1] + n, 0) + bounds.y) / scale),
115                                                                                new Point2dImpl(
116                                                                                                (float) (shape.get(triangles[i][2], 0) + bounds.x) / scale,
117                                                                                                (float) (shape.get(triangles[i][2] + n, 0) + bounds.y) / scale)
118                                                );
119                                image.drawShape(t, thickness, meshColour);
120                        }
121                }
122
123                if (drawConnections) {
124                        // draw connections
125                        for (int i = 0; i < connections[0].length; i++) {
126                                if (visi.get(connections[0][i], 0) == 0
127                                                || visi.get(connections[1][i], 0) == 0)
128                                        continue;
129
130                                image.drawLine(
131                                                new Point2dImpl(
132                                                                (float) (shape.get(connections[0][i], 0) + bounds.x) / scale,
133                                                                (float) (shape.get(connections[0][i] + n, 0) + bounds.y) / scale),
134                                                                new Point2dImpl(
135                                                                                (float) (shape.get(connections[1][i], 0) + bounds.x) / scale,
136                                                                                (float) (shape.get(connections[1][i] + n, 0) + bounds.y) / scale),
137                                                                                thickness, connectionColour);
138                        }
139                }
140
141                if (drawPoints) {
142                        // draw points
143                        for (int i = 0; i < n; i++) {
144                                if (visi.get(i, 0) == 0)
145                                        continue;
146
147                                image.drawPoint(
148                                                new Point2dImpl(
149                                                                ((float) shape.get(i, 0) + bounds.x) / scale,
150                                                                ((float) shape.get(i + n, 0) + bounds.y) / scale),
151                                                                pointColour, thickness);
152                        }
153                }
154        }
155
156        /**
157         * Static helper function for quick and dirty rendering
158         *
159         * @param mbf
160         *            image to draw on to
161         * @param thickness
162         *            line thickness
163         * @param face
164         *            face to draw
165         */
166        public static void render(MBFImage mbf, int thickness, CLMDetectedFace face) {
167                final CLMDetectedFaceRenderer render = new CLMDetectedFaceRenderer();
168                render.drawDetectedFace(mbf, thickness, face);
169        }
170
171}