Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
97 lines (78 sloc) 2.59 KB
/*
Separable Filters - Horizontal
Author: Bhavna Mahadevan
Code Snippet - Fall 2010
*/
// Global variables for movie playback
import processing.video.*;
Movie myMovie;
float[] h_row = {1,2,1};
float[] h_col = {1,0,-1};
int T=30, f=0;
// Color for dividing line
color white = color(255,255,255);
// Image to store result from column pass
PImage columnPassResult = new PImage(1280,720);
// Global variables for image manipulation
int windowSize=3, squareWindowSize=windowSize*windowSize, halfWindowSize = windowSize/2;
// setup is called once
void setup() {
size(1280, 720, P2D); // Create applet of correct size, use fast 2D renderer
myMovie = new Movie(this, "movie_filename.MP4"); // call a new movie
myMovie.loop(); // makes movie loop
noLoop(); // makes it so draw only gets called on redraw()
}
// This function is called every time there is a new frame ready
// In it, we will create the output image from the movie frame, then call redraw.
void movieEvent(Movie m) {
// read the frame
m.read();
// apply column pass (in right-side of image only)
for (int x = 641; x < 1280-halfWindowSize; x++)
for (int y = halfWindowSize; y < 720-halfWindowSize; y++ )
columnPassResult.set(x, y, columnFilter(x,y,myMovie));
// copy the input image to the left-side of the final output image
for (int x = 0; x < 640; x++)
for (int y = 0; y < 720; y++ )
set(x, y, myMovie.get(x,y));
// draw a white line through the middle (x=640)
for (int y = 0; y < 720; y++ )
set(640, y, white);
// apply row pass (in right-side of image only) starting from columnPassResult
for (int x = 641; x < 1280-halfWindowSize; x++)
for (int y = halfWindowSize; y < 720-halfWindowSize; y++ )
set(x, y, rowFilter(x,y,columnPassResult));
// now redraw
redraw();
}
// This code does not change, it just draws the output image automatically
void draw() {
}
// Column pass
color columnFilter(int x, int y, PImage inputImage)
{
float r=0,g=0,b=0;
for (int j= 0; j < windowSize; j++) {
// Calculate location of window pixel (i,j) in original image
int s=x, t=y+j-halfWindowSize;
color c = inputImage.get(s,t);
r+=h_col[j]*red(c);
g+=h_col[j]*green(c);
b+=h_col[j]*blue(c);
}
return color(r,g,b);
}
// Row pass
color rowFilter(int x, int y, PImage inputImage)
{
float r=0,g=0,b=0;
for (int i = 0; i < windowSize; i++) {
// Calculate location of window pixel (i,j) in original image
int s = x+i-halfWindowSize, t=y;
color c = inputImage.get(s,t);
r+=h_row[i]*red(c);
g+=h_row[i]*green(c);
b+=h_row[i]*blue(c);
}
return color(r,g,b);
}