About color tracking
in
Core Library Questions
•
1 year ago
Hi, everyone.
I'm begginer of processing and I post my question.
Hope you can give me some tips.
I've tried a motion tracking by a color tracking code.
I'll use this for a modern ballet performence but tracking speed is way too slow.
When it tracks color, it draws a cluster of circles.
(actually I want to draw a line but it's harder than circle...)
So the circles will be displayed like a line.
However, when a performer moves fast(like turnning or jumping), those circles are chopped and looked ugly.
Therefore I want to make it more speed up which can detect and draw circles faster than now.(no chopped)
I tried to change framerate but it won't help.
Please help me out!
Below is the code.( I applied tracking code from Daniel Shiffman)
<sketch>
import processing.video.*;
Capture video;
color trackColor;
Snake snake;
Snake2 snake2;
void setup(){
size(1024, 768);
video = new Capture(this, width, height,40);
//video = new Capture(this, width, height, 15);
trackColor = color(0, 0,255);
smooth();
snake = new Snake(80);
snake2 = new Snake2(70);
//snake = new Snake(50);
}
void draw(){
if (video.available()){
video.read();
}
video.loadPixels();
image(video, 0, 0, width, height);
float worldRecord = 500;
int closestX = 0;
int closestY = 0;
for(int x = 0; x < video.width; x++){
for(int y = 0; y < video.height; y++){
int loc = x + y * video.width;
color currentColor = video.pixels[loc];
float r1 = red(currentColor);
float g1 = green(currentColor);
float b1 = blue(currentColor);
float r2 = red(trackColor);
float g2 = green(trackColor);
float b2 = blue(trackColor);
float d = dist(r1, g1, b1, r2, g2, b2);
if(d < worldRecord){
worldRecord = d;
closestX = x;
closestY = y;
}
}
}
//adjust the difference b/t color contrast
if(worldRecord < 200){
/* fill(255, 0, 0);
strokeWeight(4.0);
stroke(0);
ellipse(closestX, closestY, 16, 16);*/
snake.update(closestX, closestY);
snake2.update(closestX, closestY);
}
snake.display();
snake2.display();
}
<Tap of snake>
class Snake {
// x and y positions
int[] xpos;
int[] ypos;
// The constructor determines the length of the snake
Snake(int n) {
xpos = new int[n];
ypos = new int[n];
}
void update(int newX, int newY) {
// Shift all elements down one spot.
// xpos[0] = xpos[1], xpos[1] = xpos = [2], and so on. Stop at the second to last element.
for (int i = 0; i < xpos.length-1; i ++ ) {
xpos[i] = xpos[i+1];
ypos[i] = ypos[i+1];
}
// Update the last spot in the array with the mouse location.
xpos[xpos.length-1] = newX;
ypos[ypos.length-1] = newY;
}
void display() {
// Draw everything
//background(255);
//background(0);
for (int i = 0; i < xpos.length; i ++ ) {
// Draw an ellipse for each element in the arrays.
// Color and size are tied to the loop's counter: i.
noStroke();
fill(xpos[i], 255-i*1.5, ypos[i],i*2); // BK to WH
//fill(100+i*1.5);//WH to BK
//fill(random(100));
ellipse(xpos[i], ypos[i], i*0.3, i*0.3);
fill(xpos[i], 255-i*1.5, ypos[i],i*1.5); // BK to WH
ellipse(xpos[i], ypos[i], i*0.35, i*0.35);
fill(xpos[i], 255-i*1.5, ypos[i],i*1); // BK to WH
ellipse(xpos[i], ypos[i], i*0.4, i*0.4);
}
if (keyPressed){
background(0);
//background(255);
}
}
}
I'm begginer of processing and I post my question.
Hope you can give me some tips.
I've tried a motion tracking by a color tracking code.
I'll use this for a modern ballet performence but tracking speed is way too slow.
When it tracks color, it draws a cluster of circles.
(actually I want to draw a line but it's harder than circle...)
So the circles will be displayed like a line.
However, when a performer moves fast(like turnning or jumping), those circles are chopped and looked ugly.
Therefore I want to make it more speed up which can detect and draw circles faster than now.(no chopped)
I tried to change framerate but it won't help.
Please help me out!
Below is the code.( I applied tracking code from Daniel Shiffman)
<sketch>
import processing.video.*;
Capture video;
color trackColor;
Snake snake;
Snake2 snake2;
void setup(){
size(1024, 768);
video = new Capture(this, width, height,40);
//video = new Capture(this, width, height, 15);
trackColor = color(0, 0,255);
smooth();
snake = new Snake(80);
snake2 = new Snake2(70);
//snake = new Snake(50);
}
void draw(){
if (video.available()){
video.read();
}
video.loadPixels();
image(video, 0, 0, width, height);
float worldRecord = 500;
int closestX = 0;
int closestY = 0;
for(int x = 0; x < video.width; x++){
for(int y = 0; y < video.height; y++){
int loc = x + y * video.width;
color currentColor = video.pixels[loc];
float r1 = red(currentColor);
float g1 = green(currentColor);
float b1 = blue(currentColor);
float r2 = red(trackColor);
float g2 = green(trackColor);
float b2 = blue(trackColor);
float d = dist(r1, g1, b1, r2, g2, b2);
if(d < worldRecord){
worldRecord = d;
closestX = x;
closestY = y;
}
}
}
//adjust the difference b/t color contrast
if(worldRecord < 200){
/* fill(255, 0, 0);
strokeWeight(4.0);
stroke(0);
ellipse(closestX, closestY, 16, 16);*/
snake.update(closestX, closestY);
snake2.update(closestX, closestY);
}
snake.display();
snake2.display();
}
<Tap of snake>
class Snake {
// x and y positions
int[] xpos;
int[] ypos;
// The constructor determines the length of the snake
Snake(int n) {
xpos = new int[n];
ypos = new int[n];
}
void update(int newX, int newY) {
// Shift all elements down one spot.
// xpos[0] = xpos[1], xpos[1] = xpos = [2], and so on. Stop at the second to last element.
for (int i = 0; i < xpos.length-1; i ++ ) {
xpos[i] = xpos[i+1];
ypos[i] = ypos[i+1];
}
// Update the last spot in the array with the mouse location.
xpos[xpos.length-1] = newX;
ypos[ypos.length-1] = newY;
}
void display() {
// Draw everything
//background(255);
//background(0);
for (int i = 0; i < xpos.length; i ++ ) {
// Draw an ellipse for each element in the arrays.
// Color and size are tied to the loop's counter: i.
noStroke();
fill(xpos[i], 255-i*1.5, ypos[i],i*2); // BK to WH
//fill(100+i*1.5);//WH to BK
//fill(random(100));
ellipse(xpos[i], ypos[i], i*0.3, i*0.3);
fill(xpos[i], 255-i*1.5, ypos[i],i*1.5); // BK to WH
ellipse(xpos[i], ypos[i], i*0.35, i*0.35);
fill(xpos[i], 255-i*1.5, ypos[i],i*1); // BK to WH
ellipse(xpos[i], ypos[i], i*0.4, i*0.4);
}
if (keyPressed){
background(0);
//background(255);
}
}
}
1