for HW2 Lightfields by Sheng Kai Tang (Tony)

0. Shift and Add in Processing

The idea of lightfields taught in the class is to create a 1D or 2D array of pinhole cameras to capture images . With slightly shifting of images based on camera positions, adding up values of pixels, and averaging these pixels values, we can create a general sense of lightfields. The code below is written in Processing according to the concept of 1D array:


int imageWidth;// image width
int imageHeight;// image height
PImage show;//final image
PImage[] images;//source images
String[] fileNames;//source filenames
int imageAmount;//number of source images
color c;//pixel color
float r, g, b;//r,g,b of a pixel color
int l = 0;//keycode counter


void setup(){
imageWidth = 420;//set image width
imageHeight = 240;//set image height
imageAmount = 15;//set image amount
size(imageWidth, imageHeight);//set canvas size
images = new PImage[imageAmount];//instantiate source images
fileNames = new String[imageAmount];//instantiate image filenames


//load image filenames
for(int i = 0; i < fileNames.length; i++){
fileNames[i] = "IMG_24" + (i + 10) + ".JPG";
}
//load source images
for(int i = 0; i < images.length; i++ ){
images[i] = loadImage(fileNames[i]);
}
//instantiate the final image
show = createImage(width, height, RGB);
}


void draw(){
//shift and add
show.loadPixels();
for(int i = 0; i < width; i++){
for(int j = 0; j < height; j++){
for(int k = 0; k < imageAmount; k++){
r = r + red(images[k].get(i + l*k, j));
g = g + green(images[k].get(i + l*k, j));
b = b + blue(images[k].get(i + l*k, j));
}
c = color(r/imageAmount, g/imageAmount, b/imageAmount);
show.set(i, j, c);
r = 0;
g = 0;
b = 0;
}
}
show.updatePixels();
image(show, 0, 0);
}


void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
l++;
}
else if (keyCode == DOWN) {
l--;
}
}
}

1. Code Test 

With the code developed above, we conduct test by importing 16 photos provided.

1. Focus at the window

2. Focus at the dots board

3. Focus at the Almo

4. Focus at the Pumpkin

 

2. Refocus and See through

 

3. Interactive Application

 Shift and Add (Mac version, Windows 32, Windows 64), original photos and source codes are included.