Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

color drawing with real time camera

$
0
0

Hi there This code shows that drawing a line with a color I want(by using mousepressed). But I want to change it. First, detect colors automatically(Red, Blue, Green) Second, If something with these colors moves on the screen, then draw a colored line automatically.

import processing.video.*; 
Capture video; 
PGraphics topLayer; 
color trackColor; 
int xprev=-1,yprev=-1;
void setup() { 
  size(640, 480); 
  smooth();
  video = new Capture(this, width, height);
  video.start(); 
  trackColor = color(255, 0, 0);
  topLayer = createGraphics(width, height, g.getClass().getName());
} 
void draw() { 
  if (video.available()) { 
    video.read();
  } 
  video.loadPixels(); 
  image(video, 0, 0); 
  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;
      }
    }
  }
  topLayer.beginDraw();
  topLayer.stroke(trackColor);
  if (worldRecord < 10) { 
   topLayer.strokeWeight(3);  
   if(!(xprev==-1||yprev==-1))
    topLayer.line(xprev,yprev,closestX, closestY);
    xprev = closestX;
    yprev = closestY;
  }
  topLayer.endDraw();
  image(topLayer,0,0);
} 
void mousePressed() { 
  int loc = mouseX + mouseY*video.width; 
  trackColor = video.pixels[loc];
}

I think that changing some codes below make it work easily, But I don't know how to do that.

could anyone give me some tips or changed codes, please?


Viewing all articles
Browse latest Browse all 2896

Trending Articles