|
ClassWork / ComputationalCamerasWeek3
Your Favorite Pixel
public void draw() {
if (video.available()) {
video.read();
float brightestSoFar = 0; //start out very far away
int winnerX = 0;
int winnerY = 0;
for(int row = 0; row < video.height; row++){
for(int col = 0; col < video.width; col++){
int offset = row*width + col;
int thisPixel = video.pixels[offset];
float brightness = brightness(thisPixel);
//check to see if this beats the world record for brightness
if ( brightness > brightestSoFar ){
winnerX = col;
winnerY = row;
brightestSoFar = brightness; //up the world record to this brightness
}
}
}
image(video,0,0);
fill(255,0,0);
ellipse(winnerX, winnerY,30,30); //draw a circle over the winner
}
}
Favorite Among Favorites
public void draw() {
if (video.available()) {
video.read();
int threshold = 20;
for(int row = 0; row < video.height; row++){
for(int col = 0; col < video.width; col++){
int offset = row*width + col;
int thisPixel = video.pixels[offset];
float brightness = brightness(thisPixel);
//check to see if it is over a threshold of brightness
if (brightness < threshold) {
video.pixels[offset] = 0; //to debug the qualifying pixesl
totalQualified++;
sumX = sumX + col;
sumY = sumY + row;
}
}
}
image(video,0,0);
int aveX = sumX/totalQualified;
int aveY = sumY/totalQualified;
ellipse(aveX-5,aveY-5,10,10);
}
Adjusting the Threshold
public void keyPressed() {
if (key == 's') {
video.settings();
} else if (key == '-') {
threshold--;
println("Threshold " + threshold);
} else if (key == '=') {
threshold++;
println("Threshold " + threshold);
} else if (key == 'a'){
video.read();
video.loadPixels();
float totalBrightness = 0;
for(int i = 0; i < video.pixels.length; i++){
totalBrightness += brightness(video.pixels[i]);
}
threshold = 2* totalBrightness/video.pixels.length;
//now pixels have to be twice as bright as average to qualify.
}
}
Put A Box Around an Area
public void draw() {
if (video.available()) {
video.read();
int leftMost = width; //starts out as the right most
int topMost = height; // starts out as the bottom most
int rightMost = 0; // starts out left most
int bottomMost = 0; //sarts out top most
for (int row = 0; row < video.height; row++) {
for (int col = 0; col < video.width; col++) {
int offset = row * width + col;
int thisPixel = video.pixels[offset];
float h = hue(thisPixel);
float s = saturation(thisPixel);
float b = brightness(thisPixel);
float closeness = dist(h, s, b, goalHue, goalSaturation, goalBrightness);
if (closeness < threshold) {
video.pixels[offset] = 0; //color it black for debuggin
if (col < leftMost) leftMost = col;
if (col > rightMost) rightMost = col;
if (row > bottomMost ) bottomMost = row;
if (row < topMost) topMost = row;
}
}
}
image(video, 0, 0);
stroke(255, 0, 255);
fill(0, 255, 0, 0);
rect(leftMost, topMost, rightMost-leftMost, bottomMost-topMost);
}
Find Multiple Areas
Rectangle[] myArray = new Rectangle[10]; //stuck with a size of 10 ArrayList myList = new ArrayList(); //start with an empty list and add them as needed myArray[0] = new Rectangle(10,10,50,50); /add something to the list myList.add(newRectangle(10,10,50,50); myArray.length; //learn the length myList.size(); // thisRect = myArray[0]; //pull something out of the array thisRect = (Rectangle) myList.get(0); //notice you have to "cast" it as a rectangle on the way out
ArrayList<Rectangle> myList = new ArrayList<Rectangle>(); myRect = myList.get(0); //look no casting! import java.awt.Rectangle; import java.util.ArrayList; import blobs.Blob; import processing.core.PApplet; import processing.video.Capture; public class TrackRects extends PApplet { Capture video;// regular processing libary
float goalRed = 200;
float goalGreen = 0;
float goalBlue = 0;
int threshold = 40;
int reach = 5;
long elapsedTime;
static public void main(String _args[]) {
PApplet.main(new String[] { "tracking.TrackRects" });
}
public void setup() {
size(640, 480);
video = new Capture(this, width, height);
}
public void draw() {
if (video.available()) {
video.read();
long startTime = millis();
//video.filter(BLUR, 2);
elapsedTime = millis() - startTime;
//println(elapsedTime);
//uncomment this line or press 't' to see how long the blur takes.
ArrayList<Rectangle> boxes = new ArrayList<Rectangle>();
for (int row = 0; row < video.height; row++) {
for (int col = 0; col < video.width; col++) {
int offset = row * width + col;
int thisPixel = video.pixels[offset];
float r = red(thisPixel);
float g = green(thisPixel);
float b = blue(thisPixel);
float closeness = dist(r, g, b, goalRed, goalGreen, goalBlue);
if (closeness < threshold) {
video.pixels[offset] = 0;
//be pessimistic
boolean foundAHome = false;
//look throught the existing
for (int i = 0; i < boxes.size(); i++) {
Rectangle existingBox = boxes.get(i);
//is this spot in an existing box
Rectangle inflatedBox = new Rectangle(existingBox); //copy the existing box
inflatedBox.grow(reach, reach); //widen it's reach
if (inflatedBox.contains(col, row)) {
existingBox.add(col,row);
foundAHome = true; //no need to make a new one
break; //no need to look through the rest of the boxes
}
}
//if this does not belong to one of the existing boxes make a new one at this place
if (foundAHome == false) boxes.add(new Rectangle(col, row,0,0));
}
}
}
//consolidate(boxes,0,0);
image(video, 0, 0);
fill(0, 0, 0, 0);
stroke(255, 0, 0);
for (int i = 0; i < boxes.size(); i++) {
Rectangle thisBox = boxes.get(i);
rect(thisBox.x, thisBox.y, thisBox.width, thisBox.height);
}
}
}
public void mousePressed() {
int thisPixel = video.pixels[mouseY * width + mouseX];
goalRed = red(thisPixel);
goalGreen = green(thisPixel);
goalBlue = blue(thisPixel);
println("Goal" + goalRed + " " + goalGreen + " " + goalBlue);
}
public void keyPressed() {
if (key == 's') {
video.settings();
} else if (key == '-') {
threshold--;
println("Threshold " + threshold);
} else if (key == '=') {
threshold++;
println("Threshold " + threshold);
} else if (key == '_') {
reach--;
println("reach " + reach);
} else if (key == '+') {
reach++;
println("reach " + reach);
} else if (key == 't') {
println("Elapsedtime " + elapsedTime);
}
}
} |