Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Processing 2. Креативное программирование

.pdf
Скачиваний:
138
Добавлен:
06.03.2016
Размер:
16.65 Mб
Скачать

, - .

, , captureEvent().draw() webcam.available(), . , . -if,. webcam.read()Processing image().

, -loadPixels(). for., . .

for ( int i = 0; i < numPixels; i++ ) { float b = brightness( webcam.pixels[i] ); if ( b > threshold ) {

pixels[i] = color( 255 );

}else {

pixels[i] = color( 0 );

}

}

170

8

“ ” “ ” . , - ., .

, , ,. ., ,.

video .Capture ..

import processing.video.*;

Capture webcam; int threshold;

int topLeftX; int topLeftY;

int bottomRightX; int bottomRightY;

void setup()

{

size( 640, 480 );

webcam = new Capture( this, width, height, 30); webcam.start();

threshold = 127;

topLeftX = width; topLeftY = height;

bottomRightX = 0; bottomRightY = 0;

}

171

draw() , ,. for,. .

void draw()

{

if ( webcam.available() ) { webcam.read();

image( webcam, 0, 0 );

loadPixels();

int counter = 0;

for ( int j = 0; j < webcam.height; j++ ) { for ( int i = 0; i < webcam.width; i++ ) {

color c = webcam.pixels[counter]; float b = brightness( c );

if ( b > threshold ) { pixels[counter] = color( 255 ); if ( i < topLeftX ) {

topLeftX = i;

}

if ( j < topLeftY ) { topLeftY = j;

}

if ( i > bottomRightX ) { bottomRightX = i;

}

if ( j > bottomRightY ) { bottomRightY = j;

}

}else {

pixels[counter] = color( 0 );

}

counter++;

}

}

updatePixels();

noFill();

stroke( 255, 0, 0 ); strokeWeight( 2 );

rect( topLeftX, topLeftY, bottomRightX - topLeftX, bottomRightY - topLeftY );

// reset tracking points topLeftX = width; topLeftY = height;

172

8

bottomRightX = 0; bottomRightY = 0;

fill( 255, 0, 0 ); noStroke();

rect( 10, 10, 110, 20 ); fill( 255 );

text( "Threshold: " + threshold, 14, 24 );

}

}

void keyPressed()

{

if ( key == CODED ) {

if ( keyCode == UP ) { threshold++;

}

if ( keyCode == DOWN ) { threshold--;

}

}

}

, ., - .

173

. , .. ,.

topLeftX .topLeftY . .

topLeftX = width; topLeftY = height;

for i . - ,topLeftX. ,topLeftX.x . for j. y. min().:

topLeftX = min( i, topLeftX ); topLeftY = min( j, topLeftY );

i j bottomRightX bottomRightY. .max(). min() -. , :

rect( topLeftX, topLeftY, bottomRightX - topLeftX, bottomRightY - topLeftY );

, .

. . ,, . ,,- .

174

8

.color trackColor,: , . maxColorDifference.

import processing.video.*;

Capture webcam;

color trackColor; int trackR;

int trackG; int trackB;

int topLeftX; int topLeftY;

int bottomRightX; int bottomRightY;

int maxColorDifference;

void setup()

{

size( 640, 480 );

webcam = new Capture( this, width, height ); webcam.start();

trackColor = color( 255 );

trackR = (trackColor >> 16) & 0xff; trackG = (trackColor >> 8) & 0xff; trackB = trackColor & 0xff; maxColorDifference = 40;

topLeftX = width; topLeftY = height;

bottomRightX = 0; bottomRightY = 0;

}

175

draw() , .brightness() , dist().maxColorDifference,, .

void draw()

{

if ( webcam.available() ) { webcam.read();

image( webcam, 0, 0 );

loadPixels();

int counter = 0;

for ( int j = 0; j < webcam.height; j++ ) { for ( int i = 0; i < webcam.width; i++ ) {

color c = webcam.pixels[counter]; int r = (c >> 16) & 0xff;

int g = (c >> 8) & 0xff; int b = c & 0xff;

float colorDifference = dist( r, g, b, trackR, trackG, trackB );

if ( colorDifference < maxColorDifference ) { if ( i < topLeftX ) {

topLeftX = i;

}

if ( j < topLeftY ) { topLeftY = j;

}

if ( i > bottomRightX ) { bottomRightX = i;

}

if ( j > bottomRightY ) { bottomRightY = j;

}

}

counter++;

}

}

updatePixels();

// draw tracking color fill( trackColor ); noStroke();

176

8

rect( 0, 0, 20, 20 );

noFill(); stroke( 0 );

strokeWeight( 2 );

rect( topLeftX, topLeftY, bottomRightX - topLeftX, bottomRightY - topLeftY );

// reset tracking points topLeftX = width; topLeftY = height; bottomRightX = 0; bottomRightY = 0;

}

}

mousePressed() , ..

void mousePressed()

{

trackColor = webcam.get( mouseX, mouseY ); trackR = (trackColor >> 16) & 0xff;

trackG = (trackColor >> 8) & 0xff; trackB = trackColor & 0xff;

}

, - :

177

:

color c = webcam.pixels[counter]; int r = (c >> 16) & 0xff;

int g = (c >> 8) & 0xff; int b = c & 0xff;

float colorDifference = dist( r, g, b, trackR, trackG, trackB );

c.shifting , .int r = (c >> 16) & 0xff; , int r = red( c ), . , green() blue(). , , .Processing reference at http:// processing.org/reference/rightshift.html.

dist() . x, y z , . dist() -. , . colorDifference ,maxColorDifference, . , , , .

OpenCV

Processing - video. ,, OpenCV.

OpenCV Processing Java Mac OS X, Windows Linux. ,: http://ubaa.net/shared/processing/ opencv/.

178

8

-, OpenCV. Windows OpenCV 1.0 . Mac OS X OpenCV framework 1.1. DMG . LinuxOpenCV / .

- OpenCV Processing, -Processing. ,:

OpenCV (Open Source Computer Vision) Intel,

Willow Garage. Processing OpenCV 1.0 Windows 1.1

Mac OS X. 2006 2008 , . OpenCV 2.3.1, - . OpenCV 1.0 C. 2.0 C ++ C#, Ruby Java. , ProcessingOpenCV, .

OpenCV , 64- . , 32- . Processing | Preferences - , Launch programs 32-bit mode.

179

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]