Made a little mosaic webcam-filter:
http://wliia.org/projects/mosaic/
Code:
import processing.video.*;
Capture myCap;
PImage arrMosaic;
int steps = 0, vwidth = 320, vheight = 240;
void setup()
{
background(255);
myCap = new Capture(this, myCap.list()[1], vwidth, vheight, 30);
size(vwidth, vheight);
arrMosaic = new PImage(vwidth/steps,vheight/steps);
steps = vheight;
}
void captureEvent(Capture myCap) {
myCap.read();
}
void draw()
{
drawMosaic(myCap,steps);
}
void keyPressed()
{
if(key == '+')
{
steps *= 2;
}
if(key == '-')
{
steps /= 2;
}
}
void drawMosaic(PImage img, int steps)
{
int m = 0;
copy(img,0,0,img.width,img.height,0,0,img.width/steps,img.height/steps);
arrMosaic = get(0,0,img.width/steps,img.height/steps);
background(255);
for(int i = 0; i < width; i+=img.width/steps)
{
for(int j = 0; j < height; j+=img.height/steps)
{
tint(getAreaColor(i,j,img.width/steps,img.height/steps));
image(arrMosaic,i,j);
}
}
}
color getAreaColor(int x, int y, int w, int h)
{
int c = 0;
int cR = 0, cG = 0, cB = 0;
for(int i = x; i < min(x+w,width); i++)
{
for(int j = y; j < min(y+h,height); j++)
{
c++;
cR += red(myCap.pixels[j*width+i]);
cG += green(myCap.pixels[j*width+i]);
cB += blue(myCap.pixels[j*width+i]);
}
}
if(c==0) c = 1;
return color(cR/c,cG/c,cB/c,255);
}
-seltar