Skip to content

Instantly share code, notes, and snippets.

@abhamra
Created February 18, 2021 17:45
Show Gist options
  • Save abhamra/9f3c37954659cfd3c5003228ab7c52cd to your computer and use it in GitHub Desktop.
Save abhamra/9f3c37954659cfd3c5003228ab7c52cd 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 = 40;
//int[] colors[][];
void setup(){
size(640, 360);
video = new Capture(this, 320, 240);
video.start();
}
void captureEvent(Capture video){
video.read();
}
void draw(){
video.loadPixels();
//print(video.pixels);
image(video, 0, 0);
noStroke();
fill(trackedColor);
//write code that finds the distance between the ideal value
int avg_x = 1;
int avg_y = 1;
int count = 1;
rect(0, 0, size, size);
String s1 = red(trackedColor) + " " + green(trackedColor) + " " + blue(trackedColor);
String s2 = "Avg X: " + avg_x + " Avg Y: " + avg_y;
text(s2, 15, 10);
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, 255, 0);
count+=1;
ellipse(c, r, size, size);
avg_x +=c;
avg_y +=r;
}
}
}
}//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 mousePressed(){
x = mouseX;
y = mouseY;
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