...
Code Block |
---|
title | Beispiel |
---|
collapse | true |
---|
|
import processing.video.*;
Capture video;
PImage backgroundImage;
float threshold = 20;
void setup() {
size(640, 480);
// start video captire
video = new Capture(this, width, height, 30);
video.start();
// prepare image to save background
backgroundImage = createImage(video.width, video.height, RGB);
}
void draw() {
// read camera image if available
if (video.available()) {
video.read();
}
loadPixels();
video.// active pixel manipulation of canvas
loadPixels();
// get pixel data from video and background image
video.loadPixels();
backgroundImage.loadPixels();
// loop through video pixel by pixel
for (int x=0; x < video.width; x++) {
for (int y=0; y < video.height; y++) {
int// loc get pixel array location
int loc = x + y * video.width;
color fgColor = video.pixels[loc]; // get foreground color (video)
color bgColorfgColor = backgroundImagevideo.pixels[loc];
// floatget r1background =color red(fgColorimage);
floatcolor g1bgColor = green(fgColor);backgroundImage.pixels[loc];
float// b1get = blue(fgColor);individual colors
float r2r1 = red(bgColorfgColor);
float g2g1 = green(bgColorfgColor);
float b2b1 = blue(bgColorfgColor);
float diffr2 = dist(r1, g1, b1, r2, g2, b2red(bgColor);
float g2 if (diff > threshold) {= green(bgColor);
float b2 pixels[loc] = fgColorblue(bgColor);
}
else { // calculate spacial distance pixels[loc] = color(0); between the two colors
}float dist = dist(r1, g1, }b1, r2, g2, }b2);
updatePixels(); } void mousePressed()
{ backgroundImage.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
backgroundImage.updatePixels();
} |
Hellster Punkt
Image Removed
Für die direkte Steuerung eines Interfaces kann es hilfreich sein, zu wissen, wo sich der hellste Punkt in einem Bild befindet. Dazu wird ein PVector erstellt und eine Variable, welche den jeweils hellsten Wert für das aktuelle Frame beinhaltet. Durch das Vergleichen der Helligkeitswerte im ganzen Frame kann sehr schnell der Hellste Punkt bestimmt werden.
Code Block |
---|
title | Beispiel |
---|
collapse | true |
---|
|
import processing.video.*;
Capture video;
PVector brightestPoint = new PVector(0,0);
void setup() {
size(640, 480);
video = new Capture(this, width, height, 30);
video.start();
}
void draw() {
if (video.available()) {
video.read();
}
float brightness = 0;
for (int x=0; x < width; x++) {
for (int y=0; y < height; y++) {
int loc = x + y * width;
color c = video.pixels[loc];
if (brightness(c) > brightness) {
brightness = brightness(c); // check if distance is above threshold
if (dist > threshold) {
// write foreground pixel
pixels[loc] = fgColor;
} else {
// set pixel to black
pixels[loc] = color(0);
}
}
}
// write pixel back to canvas
updatePixels();
}
void mousePressed() {
// copy current video frame into background image
backgroundImage.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
backgroundImage.updatePixels();
} |
Hellster Punkt
Image Added
Für die direkte Steuerung eines Interfaces kann es hilfreich sein, zu wissen, wo sich der hellste Punkt in einem Bild befindet. Dazu wird ein PVector erstellt und eine Variable, welche den jeweils hellsten Wert für das aktuelle Frame beinhaltet. Durch das Vergleichen der Helligkeitswerte im ganzen Frame kann sehr schnell der Hellste Punkt bestimmt werden.
Code Block |
---|
language | java |
---|
title | Beispiel |
---|
collapse | true |
---|
|
import processing.video.*;
Capture video;
void setup() {
size(640, 480);
// start video capture
video = new Capture(this, width, height, 30);
video.start();
}
void draw() {
// read new video frame if available
if (video.available()) {
video.read();
}
// initially set brightness to zero
float brightness = 0;
// initially set point to center
PVector point = new PVector(width/2, height/2);
// go through video pisel by pixel
for (int x=0; x < width; x++) {
for (int y=0; y < height; y++) {
// get pixel location
int loc = x + y * width;
// get color of pixel
color c = video.pixels[loc];
// check if brightness is higher than current value
if (brightness(c) > brightness) {
// set new brightness
brightness = brightness(c);
// save location of brighter point
brightestPointpoint.x = x;
brightestPointpoint.y = y;
}
}
}
// draw video
image(video, 0, 0, 0); 0);
// draw circle
ellipse(brightestPointpoint.x, brightestPointpoint.y, 20, 20);
} |
Farbtracking
...
Code Block |
---|
title | Beispiel |
---|
collapse | true |
---|
|
import processing.video.*;
Capture video;
color trackColor;
void setup() {
size(640, 480);
video = new Capture(this, width, height, 15);
video.start();
trackColor = color(255, 0, 0);
smooth();
}
void draw() {
if (video.available()) {
() {
size(640, 480);
// start video capture
video = new Capture(this, width, height, 15);
video.readstart();
}
video.loadPixels();
image(video// initialize track color to red
trackColor = color(255, 0, 0);
}
void floatdraw() worldRecord{
= 500; // read video frame intif closestXavailable
= 0; if int closestY = 0;
PVector closestPoint = new PVector();
(video.available()) {
video.read();
}
for// (int x=0; x <load pixels
video.widthloadPixels();
x++) { // draw video
for image(intvideo, y=0; y < video.height; y++) {
, 0);
// intinitialize locrecord =to xnumber +greater ythan * video.width;
the diagonal of the screen
float colorrecord currentColor = video.pixels[loc]width+height;
// initialize variable to store closest point
PVector currColorVecclosestPoint = new PVector(red(currentColor), green(currentColor), blue(currentColor));
PVector();
// get track color as vector
PVector trackColorVec = new PVector(red(trackColor), green(trackColor), blue(trackColor));
// go through floatimage diffpixel = currColorVec.dist(trackColorVec);by pixel
for (int x=0; x
if (diff < worldRecord< video.width; x++) {
for worldRecord = diff(int y=0; y closestPoint.x = x;
< video.height; y++) {
closestPoint.y = y;
// get pixel location
} int loc = x }+ y }
* video.width;
if (worldRecord < 10) {
fill(trackColor); // get strokeWeight(4.0);pixel color
stroke(0); color currentColor = ellipse(closestPoint.x, closestPoint.y, 50, 50)video.pixels[loc];
} } void mousePressed() {// get current intcolor locas =vector
mouseX + mouseY * video.width; PVector trackColorcurrColorVec = video.pixels[loc];
} |
Blob Detection
Image Removed
Die Blob Detection is schon ein komplexere Art von Algorithm, wo ein gesamtes Objekt (Blop) zu erkennen versucht wird.
Code Block |
---|
title | Beispiel |
---|
collapse | true |
---|
|
import processing.video.*;
Capture video;
BlobDetector blobDetector;
color blobColor;
void setup() {
size(640, 480);
video = new Capture(this, width, height, 15);
video.start( new PVector(red(currentColor), green(currentColor), blue(currentColor));
// calculate distance between current color and track color
float dist = currColorVec.dist(trackColorVec);
blobDetector = new BlobDetector();
blobColor = color(255, 0, 0);// save point if closer than previous
smooth(); }if (dist void< draw(record) {
if (video.available()) { record = video.read()dist;
} video.loadPixels();closestPoint.x = x;
closestPoint.y = //y;
for black and white image }
// video.filter(THRESHOLD,0.1); }
image(video, 0, 0);}
// blobDetector.findBlob(blobColor, 20);
blobDetector.drawBlob();
blobDetector.drawBoundingBox();
blobDetector.drawCenterOfMass();
}
void mousePressed() {
int loc = mouseX + mouseY*video.width;
blobColor = video.pixels[loc];
}
class BlobDetector {
int blobPoints[][];
int blobWeight = 0;
PVector centerOfMass;
BlobDetectordraw point if we found a one that is less than 10 apart
if (record < 10) {
fill(trackColor);
strokeWeight(4.0);
stroke(0);
ellipse(closestPoint.x, closestPoint.y, 50, 50);
}
}
void mousePressed() {
// save blobPointscolor =of new int[width][height];
centerOfMass = new PVector(0, 0);
}
void findBlob(color blobColor, int threshold) {
blobWeight = 0;
for (int x = 0; x < width; x ++ ) {
for (int y = 0; y < height; y ++ ) {
int loc = x + y*width;
color currentColor = video.pixels[loc];
PVector currColorVec = new PVector(red(currentColor), green(currentColor), blue(currentColor));
PVector trackColorVec = new PVector(red(blobColor), green(blobColor), blue(blobColor));
float diff = currColorVec.dist(trackColorVec);
if (diff < threshold) {
blobPoints[x][y] = 1;
blobWeight++;
} else {
blobPoints[x][y] = 0;
}
}
}
text(blobWeight, 20, 20);
}
void drawBlob(current pixel under the mouse
int loc = mouseX + mouseY * video.width;
trackColor = video.pixels[loc];
} |
Blob Detection
Image Added
Die Blob Detection is schon ein komplexere Art von Algorithm, wo ein gesamtes Objekt (Blop) zu erkennen versucht wird.
Code Block |
---|
title | Beispiel |
---|
collapse | true |
---|
|
import processing.video.*;
Capture video;
// the color to track
color trackColor;
// a dimensional array to store marked pixels
boolean marks[][];
// the total marked pixels
int total = 0;
// the most top left pixel
PVector topLeft;
// the most bottom right pixel
PVector bottomRight;
void setup() {
size(640, 480);
// start video capture
video = new Capture(this, width, height, 15);
video.start();
// set initial track color to red
trackColor = color(255, 0, 0);
// initialize marks array
marks = new boolean[width][height];
}
void draw() {
// read video frame if available
if (video.available()) {
video.read();
}
// draw video image
image(video, 0, 0);
// find track color with treshold
findBlob(20);
// load canvas pixels
loadPixels();
// draw blob
for (int x = 0; x < width; x ++ ) {
for (int y = 0; y < height; y ++ ) {
if (blobWeight >// 200)get {pixel location
for (int xloc = 0; x <+ y*width; x ++ ) {
// make pixel red if marked
forif (int y = 0; y < height; y ++ ) {
marks[x][y]) {
pixels[loc] = color(255, 0, 0);
}
if (blobPoints[x][y] == 1) {}
}
// set canvas pixels
strokeWeightupdatePixels(5);
// draw bounding box
stroke(255, 0, 0);
noFill();
point(x, rect(topLeft.x, topLeft.y, bottomRight.x-topLeft.x, bottomRight.y-topLeft.y);
}
void mousePressed() {
// save current }pixel under mouse as track color
int }loc = mouseX + mouseY*video.width;
}trackColor }
}
= video.pixels[loc];
}
void drawBoundingBoxfindBlob(int threshold) {
// reset PVectortotal
A = new PVector(width, height)total = 0;
// PVectorprepare B = new PVector(0, 0);
for (int x = 0; x < width; x ++ ) {
for (int ypoint trackers
int lowestX = width;
int lowestY = height;
int highestX = 0;
int highestY = 0;
y
< height; y// ++prepare )track {color vector
PVector trackColorVec = new if (blobPoints[x][y] == 1) {
if (y < A.y)
PVector(red(trackColor), green(trackColor), blue(trackColor));
// go through image pixel by pixel
for (int x = 0; x < width; x ++ ) {
for (int A.y = y0; y if (y > B.y)< height; y ++ ) {
// get pixel location
B.y = y; int loc = x + y*width;
if (x < A.x) // get color of pixel
color A.xcurrentColor = xvideo.pixels[loc];
// get vector of pixel color
if (x > B.x) PVector currColorVec = new PVector(red(currentColor), green(currentColor), blue(currentColor));
B.x = x; // get distance to track color
} float dist }
= currColorVec.dist(trackColorVec);
}
strokeWeight(1); // reset noFill();mark
rect(A.x, A.y, B.x-A.x, B.y-A.y);
text("A "+A.x+","+A.y, A.x, A.y);
text("B "+B.x+","+B.y, B.x, B.y);
}
void drawCenterOfMass( marks[x][y] = false;
// check if distance is below threshold
if (dist < threshold) {
centerOfMass.set(0.0, 0.0, 0.0);
// mark pixel
for (int x = 0; marks[x][y] <= widthtrue;
x ++ ) { total++;
for (int y = 0; y < height;// yupdate ++point )trackers {
if (blobPoints[x][y] == 1) {
< lowestX) lowestX = x;
if centerOfMass.(x +> highestX) highestX = x;
if (y < lowestY) centerOfMass.ylowestY += y;
}if (y }
}
> highestY) highestY = y;
centerOfMass.x}
/= blobWeight; }
centerOfMass.y /=}
blobWeight;
// save locations
noStroke(); topLeft = new fill(255PVector(lowestX, lowestY);
bottomRight = new ellipsePVector(centerOfMass.xhighestX, centerOfMass.y, 10, 10highestY);
}
}
|
Weitere Informationen