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
Average color for movie playback (Read 1112 times)
Average color for movie playback
Nov 17th, 2008, 12:36am
 
I want to average all the colors(brightness too) of a movie frame and draw the result to the player frame by frame. Ive looked at sketches that have done this with live feeds (Michael Shaub used JMyron for a color analyzer), Im just looking to go off of a movie file. Im pretty new to processing. Anyone point me in the right direction?
do I need to get into arrays? the pixels[] function? thanks a ton ton ton
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
Page Index Toggle Pages: 1