color drawing with real time camera

edited April 2018 in Library Questions

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.

Capture video; 
PGraphics topLayer; 
color trackColor; 
int xprev=-1,yprev=-1;
void setup() { 
size(640, 480); 
video = new Capture(this, width, height);
trackColor = color(255, 0, 0);
topLayer = createGraphics(width, height, g.getClass().getName());
void draw() { 
if (video.available()) {;
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;
if (worldRecord < 10) { 
topLayer.line(xprev,yprev,closestX, closestY);
xprev = closestX;
yprev = closestY;
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?



  • Total unclear description

    Missing code

    For detecting color use color colorGet = get(mouseX,mouseY);

    You want to draw a line automatically, but from where to where?

    Do you mean a change of color in the video?

  • I have no idea. Do you video analysis?

  • ??

    I wrote how to get a color!!

    See above.

    Then go on by saying:,

    int redPart = red( colorGet );

    ..... green (

    ..... blue (

  • In theory you would for loop over the image/screen and check the colors



    See reference

    Have you done the tutorial on images and colors??

  • And stop posting on my wall please that makes this discussion look totally nuts. ;-)

    I kind of understand. You have a video of moving red apple. While it moves the positions of it are connected by lines (same color as the apple). Remember we need to store the lines in an ArrayList because otherwise the image from the movie would overwrite them.

    For real image recognition look at openCV - google it.

  • Please don’t delete your posts.

    It makes it useless to read for other readers.

This discussion has been closed.