Last active
September 29, 2018 13:29
-
-
Save atduskgreg/1c0447eb23ecea2f8065 to your computer and use it in GitHub Desktop.
opencv for processing convexity defects example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gab.opencv.*; | |
import org.opencv.imgproc.Imgproc; | |
import org.opencv.core.Mat; | |
import org.opencv.core.MatOfInt; | |
import org.opencv.core.MatOfInt4; | |
import org.opencv.core.MatOfPoint; | |
import java.awt.Rectangle; | |
import java.util.Arrays; | |
PImage src, dst; | |
OpenCV opencv; | |
ArrayList<Contour> contours; | |
ArrayList<Contour> polygons; | |
ArrayList<PVector> defectPoints; | |
ArrayList<Float> depths; | |
ArrayList<PVector> handPoints; | |
Contour convexHull; | |
PVector hullCenter; | |
void setup() { | |
src = loadImage("white_hand.jpg"); | |
size(src.width, src.height/2, P2D); | |
opencv = new OpenCV(this, src); | |
opencv.blur(5); | |
opencv.inRange(80, 180); | |
dst = opencv.getOutput(); | |
contours = opencv.findContours(false, true); | |
Contour contour = contours.get(0); | |
convexHull = contour.getPolygonApproximation().getConvexHull(); | |
hullCenter = new PVector(convexHull.getBoundingBox().x + convexHull.getBoundingBox().width/2, | |
convexHull.getBoundingBox().y + convexHull.getBoundingBox().height/2); | |
MatOfInt hull = new MatOfInt(); | |
MatOfPoint points = new MatOfPoint(contours.get(0).pointMat); | |
Imgproc.convexHull(points, hull); | |
MatOfInt4 defects = new MatOfInt4(); | |
Imgproc.convexityDefects(points, hull, defects ); | |
defectPoints = new ArrayList<PVector>(); | |
depths = new ArrayList<Float>(); | |
ArrayList<Integer> defectIndices = new ArrayList<Integer>(); | |
for (int i = 0; i < defects.height(); i++) { | |
int startIndex = (int)defects.get(i, 0)[0]; | |
int endIndex = (int)defects.get(i, 0)[1]; | |
int defectIndex = (int)defects.get(i, 0)[2]; | |
if (defects.get(i, 0)[3] > 10000) { | |
defectIndices.add( defectIndex ); | |
defectPoints.add(contour.getPoints().get(defectIndex)); | |
depths.add((float)defects.get(i, 0)[3]); | |
} | |
} | |
// | |
Integer[] handIndices = new Integer[defectIndices.size() + hull.height()]; | |
for (int i = 0 ; i < hull.height(); i++) { | |
handIndices[i] = (int)hull.get(i, 0)[0]; | |
} | |
for(int d = 0; d < defectIndices.size(); d++){ | |
handIndices[d + hull.height()] = defectIndices.get(d); | |
} | |
Arrays.sort(handIndices); | |
println(handIndices); | |
handPoints = new ArrayList<PVector>(); | |
for(int i = 0; i < handIndices.length; i++){ | |
handPoints.add(contour.getPoints().get(handIndices[i])); | |
} | |
} | |
void draw() { | |
scale(0.5); | |
image(src, 0, 0); | |
image(dst, src.width, 0); | |
noFill(); | |
strokeWeight(3); | |
stroke(0, 255, 0); | |
contours.get(0).draw(); | |
stroke(255, 0, 0); | |
convexHull.draw(); | |
stroke(0, 0, 255); | |
fill(255); | |
for (PVector p : defectPoints) { | |
ellipse(p.x, p.y, 20,20); | |
} | |
pushStyle(); | |
fill(255); | |
stroke(255, 0, 0); | |
ellipse(hullCenter.x, hullCenter.y, 30, 30); | |
popStyle(); | |
stroke(0,0,255); | |
noFill(); | |
beginShape(); | |
for(PVector p : handPoints){ | |
vertex(p.x,p.y); | |
} | |
endShape(CLOSE); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment