megabulk3000
YaBB Newbies
Offline
Posts: 11
Re: Average color for movie playback
Reply #1 - Nov 30th , 2008, 5:36am
That's funny, I just did that. I'm brand-new to Processing (like less than 24 hours experience), but I was able to modify the "ColorSorting" example to do something like what you're looking for... Here's the code: import processing.video.*; Movie myMovie; boolean cheatScreen; int framerate = 8; color myColor; int w = 800; int h = 600; // How many pixels to skip in either direction int increment = 10; void setup() { size(w, h, P2D); background(0); frameRate(framerate); noCursor(); // Load and play the video in a loop myMovie = new Movie(this, "station.mov"); myMovie.loop(); } void draw() { if (myMovie.available()) { myMovie.read(); background(0); noStroke(); int index = 0; int rtot = 0; int gtot = 0; int btot = 0; for (int j = 0; j < myMovie.height; j += increment) { for (int i = 0; i < myMovie.width; i += increment) { int pixelColor = myMovie.pixels[j*myMovie.width + i]; int r = (pixelColor >> 16) & 0xff; int g = (pixelColor >> 8) & 0xff; int b = pixelColor & 0xff; rtot = rtot + r; gtot = gtot + g; btot = btot + b; index++; } } rtot = rtot/index; gtot = gtot/index; btot = btot/index; myColor = color(rtot,gtot,btot); fill(myColor); rect(0,0,w,h); if (cheatScreen) { // Faster method of displaying pixels array on screen set(0, height - myMovie.height, myMovie); } } } void keyPressed() { if (key == 'g') { saveFrame(); } else if (key == 'c') { cheatScreen = !cheatScreen; } } And that's it. A couple of comments/caveats: --because this averaging is pretty pokey, the program only takes every nth pixel (stored in the variable "increment") so it's unlikely that you'll be able to achieve realtime sampling of every pixel in a 640x480 move at 30fps. --I'm not sure, but the program may bomb if the increment's not evenly divisible into the width and height or your movie. --the program doesn't sample every frame of the movie, but rather only 8 times per second (the "framerate" variable) --you'll need to have your movie ("station.mov" in this example) in the same folder as your program --pressing 'g' will save a snapshot of the screen --pressing 'c' will show the original movie input Hope all that helps! jonathan