We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Mosaic (Read 1158 times)
Mosaic
Sep 8th, 2005, 3:53pm
 
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
Page Index Toggle Pages: 1