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.hand; 031 032import java.util.ArrayList; 033import java.util.List; 034 035import javax.swing.JFrame; 036 037import org.openimaj.image.DisplayUtilities; 038import org.openimaj.image.FImage; 039import org.openimaj.image.MBFImage; 040import org.openimaj.image.colour.RGBColour; 041import org.openimaj.image.connectedcomponent.ConnectedComponentLabeler; 042import org.openimaj.image.pixel.ConnectedComponent; 043import org.openimaj.image.pixel.ConnectedComponent.ConnectMode; 044import org.openimaj.image.processing.threshold.OtsuThreshold; 045import org.openimaj.math.geometry.point.Point2d; 046import org.openimaj.math.geometry.shape.Circle; 047import org.openimaj.math.geometry.shape.Polygon; 048import org.openimaj.math.geometry.shape.algorithm.ConvexityDefect; 049import org.openimaj.video.capture.VideoCapture; 050import org.openimaj.video.capture.VideoCaptureException; 051 052public class Fingers { 053 public static void main(String[] args) throws VideoCaptureException { 054 final VideoCapture vc = new VideoCapture(320, 240); 055 056 final JFrame frame = DisplayUtilities.displaySimple(vc.getNextFrame(), "capture"); 057 final ConnectedComponentLabeler ccl = new ConnectedComponentLabeler(ConnectMode.CONNECT_4); 058 059 while (true) { 060 final MBFImage cimg = vc.getNextFrame(); 061 final FImage gimg = cimg.flatten(); 062 gimg.processInplace(new OtsuThreshold()); 063 // gimg.threshold(0.4f); 064 065 ccl.analyseImage(gimg); 066 final ConnectedComponent hand = findBiggest(ccl.getComponents()); 067 068 cimg.drawPoints(hand, RGBColour.WHITE, 1); 069 070 if (hand != null) { 071 Polygon poly = hand.toPolygon(); 072 poly = poly.reduceVertices(3); 073 074 final Polygon chull = poly.calculateConvexHull(); 075 076 final List<ConvexityDefect> defects = ConvexityDefect.findDefects(poly, chull); 077 // for (final ConvexityDefect cd : defects) { 078 // cimg.drawShapeFilled(cd.getTriangle(), RGBColour.MAGENTA); 079 // } 080 081 final List<Point2d> tips = findTips(defects); 082 083 final Point2d centroid = poly.calculateCentroid(); 084 System.out.println(centroid); 085 086 for (final Point2d pt : tips) { 087 cimg.drawLine(centroid, pt, RGBColour.RED); 088 cimg.drawShape(new Circle(pt, 5), RGBColour.CYAN); 089 } 090 091 cimg.drawPolygon(poly, 1, RGBColour.RED); 092 cimg.drawPolygon(chull, 1, RGBColour.BLUE); 093 } 094 095 DisplayUtilities.display(cimg, frame); 096 } 097 } 098 099 private static final int MIN_FINGER_DEPTH = 20; 100 private static final int MAX_FINGER_ANGLE = 60; // degrees 101 102 private static List<Point2d> findTips(List<ConvexityDefect> defects) { 103 final ArrayList<Point2d> fingerTips = new ArrayList<Point2d>(); 104 105 for (int i = 0; i < defects.size(); i++) { 106 if (defects.get(i).depth < MIN_FINGER_DEPTH) // defect too shallow 107 continue; 108 109 // look at fold points on either side of a tip 110 final int prevIdx = (i == 0) ? (defects.size() - 1) : (i - 1); 111 final int nextIdx = (i == defects.size() - 1) ? 0 : (i + 1); 112 113 final int angle = angleBetween(defects.get(i).start, defects.get(prevIdx).deepestPoint, 114 defects.get(nextIdx).deepestPoint); 115 if (angle >= MAX_FINGER_ANGLE) 116 continue; // angle between finger and folds too wide 117 118 // this point is probably a fingertip, so add to list 119 fingerTips.add(defects.get(i).start); 120 } 121 122 return fingerTips; 123 } 124 125 // calculate the angle between the tip and its neighboring folds 126 // (in integer degrees) 127 private static int angleBetween(Point2d tip, Point2d next, Point2d prev) 128 { 129 return Math.abs((int) Math.round( 130 Math.toDegrees( 131 Math.atan2(next.getX() - tip.getX(), next.getY() - tip.getY()) - 132 Math.atan2(prev.getX() - tip.getX(), prev.getY() - tip.getY())))); 133 } 134 135 static ConnectedComponent findBiggest(List<ConnectedComponent> components) { 136 ConnectedComponent biggest = null; 137 int size = 0; 138 139 for (final ConnectedComponent cc : components) { 140 if (cc.pixels.size() > size) { 141 size = cc.pixels.size(); 142 biggest = cc; 143 } 144 } 145 146 return biggest; 147 } 148}