Last active
October 27, 2018 01:40
-
-
Save n1ckfg/c256b074349ba355794b627522cc7e36 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.*; | |
PImage src; | |
OpenCV opencv; | |
int thresh = 70; | |
float approx = 0.02; | |
float minArea = 1.0; | |
ArrayList<Contour> contours; | |
ArrayList<Contour> polygons; | |
void setup() { | |
src = loadImage("shark_4.png"); | |
size(1080, 720); | |
opencv = new OpenCV(this, src); | |
doContour(); | |
} | |
void doContour() { | |
opencv.gray(); | |
opencv.threshold(thresh); | |
contours = opencv.findContours(); | |
for (Contour contour : contours) { | |
contour.setPolygonApproximationFactor(contour.getPolygonApproximationFactor() * approx); | |
} | |
println("found " + contours.size() + " contours"); | |
} | |
void draw() { | |
blendMode(NORMAL); | |
image(src, 0, 0); | |
if (keyPressed) { | |
if (key == 'w') { | |
thresh++; | |
} else if (key == 's') { | |
thresh--; | |
} | |
if (key == 'i') { | |
approx -= 0.01; | |
} else if (key == 'k') { | |
approx += 0.01; | |
} | |
if (key == 'o') { | |
minArea++; | |
} else if (key == 'l') { | |
minArea--; | |
} | |
thresh = constrain(thresh, 0, 255); | |
approx = constrain(approx, 0.0, 1.0); | |
minArea = constrain(minArea, 1.0, 1000.0); | |
if (key == 'w' || key == 's' || key == 'i' || key == 'k' || key == 'o' || key == 'l') { | |
doContour(); | |
println(thresh + " " + approx + " " + minArea); | |
} | |
} | |
noFill(); | |
blendMode(ADD); | |
for (Contour contour : contours) { | |
if (contour.area() >= minArea) { | |
strokeWeight(1); | |
stroke(0, 255, 0); | |
beginShape(); | |
for (PVector point : contour.getPoints()) { | |
vertex(point.x, point.y); | |
} | |
endShape(); | |
strokeWeight(3); | |
stroke(255, 0, 0); | |
beginShape(); | |
for (PVector point : contour.getPolygonApproximation().getPoints()) { | |
vertex(point.x, point.y); | |
} | |
endShape(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment