Skip to content

Instantly share code, notes, and snippets.

@abhamra
Created February 25, 2021 21:42
Show Gist options
  • Save abhamra/5de0cb85d1eda18e16855482a0eb7374 to your computer and use it in GitHub Desktop.
Save abhamra/5de0cb85d1eda18e16855482a0eb7374 to your computer and use it in GitHub Desktop.
import processing.video.*;
Capture video;
int x, y;
color cc;
color trackedColor;
int size = 1;
int threshold_val = 30;
boolean drawing = false;
//int[][] colors;
ArrayList<PVector> colors = new ArrayList<PVector>(0);
color[] penColor = new color[3];
int colorChoice = 0;
boolean erase = false;
void setup(){
//frameRate(30);
//background(255);
size(640, 360);
video = new Capture(this, 320, 240);
video.start();
//captureEvent(video);
penColor[0] = color(255, 0, 0);
penColor[1] = color(0, 255, 0);
penColor[2] = color(0, 0, 255);
}
void captureEvent(Capture video){
video.read();
}
void draw(){
image(video, 0, 0);
loadPixels();
video.loadPixels();
//background(255);
noStroke();
fill(trackedColor);
//write code that finds the distance between the ideal value
if(drawing){
int avgX = 1;
int avgY = 1;
int count = 0;
for(int r = 0;r<video.height;r++){
for(int c = 0;c<video.width;c++){
cc = video.pixels[c+(r*video.width)];
if(threshold(trackedColor, cc, threshold_val)){
//fill(0);
//stroke(0);
//ellipse(c, r, size, size);
count +=1;
avgX +=c;
avgY +=r;
colors.add(new PVector(avgX/count,avgY/count));
}
}
}
avgX = avgX / count;
avgY = avgY / count;
//noFill();
//circle(avgX, avgY, 25);
for(int i = 0;i<colors.size();i++){
fill(penColor[colorChoice]);
ellipse(colors.get(i).x, colors.get(i).y, 10, 10);
}//end ellipse for
}//end of drawing boolean
//fill(color(255, 0, 0));
//ellipse(avgX, avgY, 10, 10);
//print("drawing apparently");
//animationCount+=1;
}//end draw
public boolean threshold(color trackedColor, color cc, int thresh){
if(abs(red(trackedColor)-red(cc))<=thresh && abs(green(trackedColor)-green(cc))<=thresh && abs(blue(trackedColor)-blue(cc))<=thresh){
return true;
}//end if
return false;
}//end function
void keyPressed(){
if(keyCode==37){
if(colorChoice==0){
colorChoice=2;
} else {
colorChoice-=1;
}
}
if(keyCode==39){
if(colorChoice==2){
colorChoice=0;
} else {
colorChoice +=1;
}
}
if(keyCode==32){
if(!erase){
erase=true;
colors = new ArrayList<PVector>();
drawing=false;
}else if(erase){
erase=false;
drawing=true;
}
}
}
void mousePressed(){
x = mouseX;
y = mouseY;
drawing=true;
int position = (y*video.width) + x;
color c_selected = video.pixels[position];
print(red(c_selected) + ": " + green(c_selected) + ": " + blue(c_selected));
trackedColor = color(red(c_selected), green(c_selected), blue(c_selected));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment