Skip to content

Commit 0c75652

Browse files
committed
added red_tracking
1 parent 1897f4f commit 0c75652

File tree

2 files changed

+240
-0
lines changed

2 files changed

+240
-0
lines changed

red_tracking.pde

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* Brightness Tracking
3+
* by Daniel Shiffman.
4+
*
5+
* Tracks the brightest pixel in the video.
6+
* Click the mouse to select a different color.
7+
*
8+
* Created 2 May 2005
9+
* modified to track bright red
10+
*/
11+
12+
import processing.video.*;
13+
14+
// Variable for capture device
15+
Capture video;
16+
color trackColor;
17+
boolean imageFlag = false;
18+
boolean printImage = false;
19+
int w = 400;
20+
int h = 300;
21+
22+
23+
void setup()
24+
{
25+
background(255);
26+
size(w , h);
27+
frameRate(25);
28+
colorMode(RGB,255,255,255,100);
29+
// Using the default capture device
30+
video = new Capture(this, w, h, 12);
31+
trackColor = color(255,0,0); // Start off tracking for white
32+
//noFill();
33+
smooth();
34+
strokeWeight(10.0);
35+
stroke(0);
36+
}
37+
38+
void captureEvent(Capture camera)
39+
{
40+
camera.read();
41+
}
42+
43+
void draw()
44+
{
45+
//background(255);
46+
//loadPixels();
47+
48+
// Draw the video image on the background
49+
if (imageFlag) {
50+
image(video,0,0);
51+
}
52+
// Local variables to track the color
53+
float closestDiff = 500.0f;
54+
int closestX = 0;
55+
int closestY = 0;
56+
// Begin loop to walk through every pixel
57+
for ( int x = 0; x < video.width; x++) {
58+
for ( int y = 0; y < video.height; y++) {
59+
int loc = x + y*video.width;
60+
// What is current color
61+
color currentColor = video.pixels[loc];
62+
float r1 = red(currentColor);
63+
float g1 = green(currentColor);
64+
float b1 = blue(currentColor);
65+
float r2 = red(trackColor);
66+
float g2 = green(trackColor);
67+
float b2 = blue(trackColor);
68+
// Using euclidean distance to compare colors
69+
float d = dist(r1,g1,b1,r2,g2,b2);
70+
//float d = dist(r1,0,0,r2,0,0);
71+
// If current color is more similar to tracked color than
72+
// closest color, save current location and current difference
73+
if (d < closestDiff) {
74+
//println(closestDiff);
75+
closestDiff = d;
76+
closestX = x;
77+
closestY = y;
78+
}
79+
}
80+
}
81+
// Draw a circle at the tracked pixel
82+
if (closestDiff < 120) {
83+
if (imageFlag) {
84+
point(closestX,closestY);
85+
}
86+
else {
87+
point(400-closestX,closestY);
88+
}
89+
}
90+
91+
if (printImage) {
92+
saveFrame("image-######.jpg");
93+
printImage = false;
94+
}
95+
96+
97+
}
98+
99+
void mousePressed() {
100+
// Save color where the mouse is clicked in trackColor variable
101+
//int loc = mouseX + mouseY*video.width;
102+
//trackColor = video.pixels[loc];
103+
104+
}
105+
106+
void keyPressed() {
107+
if (keyCode == ENTER) {
108+
printImage = true;
109+
}
110+
if (key == 'e') {
111+
if (imageFlag) {
112+
imageFlag = false;
113+
background(255);
114+
}
115+
else {
116+
imageFlag = true;
117+
}
118+
}
119+
}
120+

red_tracking/red_tracking.pde

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* Brightness Tracking
3+
* by Daniel Shiffman.
4+
*
5+
* Tracks the brightest pixel in the video.
6+
* Click the mouse to select a different color.
7+
*
8+
* Created 2 May 2005
9+
* modified to track bright red
10+
*/
11+
12+
import processing.video.*;
13+
14+
// Variable for capture device
15+
Capture video;
16+
color trackColor;
17+
boolean imageFlag = false;
18+
boolean printImage = false;
19+
int w = 400;
20+
int h = 300;
21+
22+
23+
void setup()
24+
{
25+
background(255);
26+
size(w , h);
27+
frameRate(25);
28+
colorMode(RGB,255,255,255,100);
29+
// Using the default capture device
30+
video = new Capture(this, w, h, 12);
31+
trackColor = color(255,0,0); // Start off tracking for white
32+
//noFill();
33+
smooth();
34+
strokeWeight(10.0);
35+
stroke(0);
36+
}
37+
38+
void captureEvent(Capture camera)
39+
{
40+
camera.read();
41+
}
42+
43+
void draw()
44+
{
45+
//background(255);
46+
//loadPixels();
47+
48+
// Draw the video image on the background
49+
if (imageFlag) {
50+
image(video,0,0);
51+
}
52+
// Local variables to track the color
53+
float closestDiff = 500.0f;
54+
int closestX = 0;
55+
int closestY = 0;
56+
// Begin loop to walk through every pixel
57+
for ( int x = 0; x < video.width; x++) {
58+
for ( int y = 0; y < video.height; y++) {
59+
int loc = x + y*video.width;
60+
// What is current color
61+
color currentColor = video.pixels[loc];
62+
float r1 = red(currentColor);
63+
float g1 = green(currentColor);
64+
float b1 = blue(currentColor);
65+
float r2 = red(trackColor);
66+
float g2 = green(trackColor);
67+
float b2 = blue(trackColor);
68+
// Using euclidean distance to compare colors
69+
float d = dist(r1,g1,b1,r2,g2,b2);
70+
//float d = dist(r1,0,0,r2,0,0);
71+
// If current color is more similar to tracked color than
72+
// closest color, save current location and current difference
73+
if (d < closestDiff) {
74+
//println(closestDiff);
75+
closestDiff = d;
76+
closestX = x;
77+
closestY = y;
78+
}
79+
}
80+
}
81+
// Draw a circle at the tracked pixel
82+
if (closestDiff < 120) {
83+
if (imageFlag) {
84+
point(closestX,closestY);
85+
}
86+
else {
87+
point(400-closestX,closestY);
88+
}
89+
}
90+
91+
if (printImage) {
92+
saveFrame("image-######.jpg");
93+
printImage = false;
94+
}
95+
96+
97+
}
98+
99+
void mousePressed() {
100+
// Save color where the mouse is clicked in trackColor variable
101+
//int loc = mouseX + mouseY*video.width;
102+
//trackColor = video.pixels[loc];
103+
104+
}
105+
106+
void keyPressed() {
107+
if (keyCode == ENTER) {
108+
printImage = true;
109+
}
110+
if (key == 'e') {
111+
if (imageFlag) {
112+
imageFlag = false;
113+
background(255);
114+
}
115+
else {
116+
imageFlag = true;
117+
}
118+
}
119+
}
120+

0 commit comments

Comments
 (0)