Skip to content

Commit 54dd9ea

Browse files
committed
Examples by @jorditost
Examples that use the ControlP5 lib for adjust filtering, blob persistence and color tracking (of multiple colors): - WhichFace.pde (adapted from algorithm by @shiffman) - ImageFiltering.pde - ImageFilteringWithBlobPersistence.pde - HSVColorTracking.pde (from the examples of the OpenCV book by @atduskgreg) - MultipleColorTracking.pde
1 parent 6807ab8 commit 54dd9ea

14 files changed

Lines changed: 1374 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* HSVColorTracking
3+
* Greg Borenstein
4+
* https://github.com/atduskgreg/opencv-processing-book/blob/master/code/hsv_color_tracking/HSVColorTracking/HSVColorTracking.pde
5+
*
6+
* Modified by Jordi Tost @jorditost (color selection)
7+
*
8+
* University of Applied Sciences Potsdam, 2014
9+
*/
10+
11+
import gab.opencv.*;
12+
import processing.video.*;
13+
import java.awt.Rectangle;
14+
15+
Capture video;
16+
OpenCV opencv;
17+
PImage src, colorFilteredImage;
18+
ArrayList<Contour> contours;
19+
20+
// <1> Set the range of Hue values for our filter
21+
int rangeLow = 20;
22+
int rangeHigh = 35;
23+
24+
void setup() {
25+
video = new Capture(this, 640, 480);
26+
video.start();
27+
28+
opencv = new OpenCV(this, video.width, video.height);
29+
contours = new ArrayList<Contour>();
30+
31+
size(2*opencv.width, opencv.height, P2D);
32+
}
33+
34+
void draw() {
35+
36+
// Read last captured frame
37+
if (video.available()) {
38+
video.read();
39+
}
40+
41+
// <2> Load the new frame of our movie in to OpenCV
42+
opencv.loadImage(video);
43+
44+
// Tell OpenCV to use color information
45+
opencv.useColor();
46+
src = opencv.getSnapshot();
47+
48+
// <3> Tell OpenCV to work in HSV color space.
49+
opencv.useColor(HSB);
50+
51+
// <4> Copy the Hue channel of our image into
52+
// the gray channel, which we process.
53+
opencv.setGray(opencv.getH().clone());
54+
55+
// <5> Filter the image based on the range of
56+
// hue values that match the object we want to track.
57+
opencv.inRange(rangeLow, rangeHigh);
58+
59+
// <6> Get the processed image for reference.
60+
colorFilteredImage = opencv.getSnapshot();
61+
62+
///////////////////////////////////////////
63+
// We could process our image here!
64+
// See ImageFiltering.pde
65+
///////////////////////////////////////////
66+
67+
// <7> Find contours in our range image.
68+
// Passing 'true' sorts them by descending area.
69+
contours = opencv.findContours(true, true);
70+
71+
// <8> Display background images
72+
image(src, 0, 0);
73+
image(colorFilteredImage, src.width, 0);
74+
75+
// <9> Check to make sure we've found any contours
76+
if (contours.size() > 0) {
77+
// <9> Get the first contour, which will be the largest one
78+
Contour biggestContour = contours.get(0);
79+
80+
// <10> Find the bounding box of the largest contour,
81+
// and hence our object.
82+
Rectangle r = biggestContour.getBoundingBox();
83+
84+
// <11> Draw the bounding box of our object
85+
noFill();
86+
strokeWeight(2);
87+
stroke(255, 0, 0);
88+
rect(r.x, r.y, r.width, r.height);
89+
90+
// <12> Draw a dot in the middle of the bounding box, on the object.
91+
noStroke();
92+
fill(255, 0, 0);
93+
ellipse(r.x + r.width/2, r.y + r.height/2, 30, 30);
94+
}
95+
}
96+
97+
void mousePressed() {
98+
99+
color c = get(mouseX, mouseY);
100+
println("r: " + red(c) + " g: " + green(c) + " b: " + blue(c));
101+
102+
int hue = int(map(hue(c), 0, 255, 0, 180));
103+
println("hue to detect: " + hue);
104+
105+
rangeLow = hue - 5;
106+
rangeHigh = hue + 5;
107+
}
596 KB
Loading

0 commit comments

Comments
 (0)