mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
Two new Capture examples from David Muth
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* Spatiotemporal
|
||||
* by David Muth
|
||||
*
|
||||
* Records a number of video frames into memory, then plays back the video
|
||||
* buffer by turning the time axis into the x-axis and vice versa
|
||||
*/
|
||||
|
||||
import processing.video.*;
|
||||
|
||||
Capture video;
|
||||
int signal = 0;
|
||||
|
||||
//the buffer for storing video frames
|
||||
ArrayList frames;
|
||||
|
||||
//different program modes for recording and playback
|
||||
int mode = 0;
|
||||
int MODE_NEWBUFFER = 0;
|
||||
int MODE_RECORDING = 1;
|
||||
int MODE_PLAYBACK = 2;
|
||||
|
||||
int currentX = 0;
|
||||
|
||||
void setup() {
|
||||
size(640, 480);
|
||||
video = new Capture(this, width, height, 30);
|
||||
video.start();
|
||||
}
|
||||
|
||||
void captureEvent(Capture c) {
|
||||
c.read();
|
||||
|
||||
//create a new buffer in case one is needed
|
||||
if (mode == MODE_NEWBUFFER) {
|
||||
frames = new ArrayList();
|
||||
mode = MODE_RECORDING;
|
||||
}
|
||||
|
||||
//record into the buffer until there are enough frames
|
||||
if (mode == MODE_RECORDING) {
|
||||
//copy the current video frame into an image, so it can be stored in the buffer
|
||||
PImage img = createImage(width, height, RGB);
|
||||
video.loadPixels();
|
||||
arrayCopy(video.pixels, img.pixels);
|
||||
|
||||
frames.add(img);
|
||||
|
||||
//in case enough frames have been recorded, switch to playback mode
|
||||
if (frames.size() >= width) {
|
||||
mode = MODE_PLAYBACK;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
loadPixels();
|
||||
|
||||
//code for the recording mode
|
||||
if (mode == MODE_RECORDING) {
|
||||
//set the image counter to 0
|
||||
int currentImage = 0;
|
||||
|
||||
//begin a loop for displaying pixel columns
|
||||
for (int x = 0; x < video.width; x++) {
|
||||
//go through the frame buffer and pick an image using the image counter
|
||||
if (currentImage < frames.size()) {
|
||||
PImage img = (PImage)frames.get(currentImage);
|
||||
|
||||
//display a pixel column of the current image
|
||||
if (img != null) {
|
||||
img.loadPixels();
|
||||
|
||||
for (int y = 0; y < video.height; y++) {
|
||||
pixels[x + y * width] = img.pixels[x + y * video.width];
|
||||
}
|
||||
}
|
||||
|
||||
//increase the image counter
|
||||
currentImage++;
|
||||
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//code for displaying the spatiotemporal transformation
|
||||
if (mode == MODE_PLAYBACK) {
|
||||
|
||||
//begin a loop for displaying pixel columns
|
||||
for (int x = 0; x < video.width; x++) {
|
||||
//get an image from the buffer using loopcounter x as the index
|
||||
PImage img = (PImage)frames.get(x);
|
||||
|
||||
if (img != null) {
|
||||
img.loadPixels();
|
||||
|
||||
//pick the same column from each image for display,
|
||||
//then distribute the columns over the x-axis on the screen
|
||||
for(int y = 0; y < video.height; y++) {
|
||||
pixels[x + y * width] = img.pixels[currentX + y * video.width];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//a different column shall be used next time draw() is being called
|
||||
currentX++;
|
||||
|
||||
//if the end of the buffer is reached
|
||||
if(currentX >= video.width) {
|
||||
//create a new buffer when the next video frame arrives
|
||||
mode = MODE_NEWBUFFER;
|
||||
//reset the column counter
|
||||
currentX = 0;
|
||||
}
|
||||
}
|
||||
|
||||
updatePixels();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Time Displacement
|
||||
* by David Muth
|
||||
*
|
||||
* Keeps a buffer of video frames in memory and displays pixel rows
|
||||
* taken from consecutive frames distributed over the y-axis
|
||||
*/
|
||||
|
||||
import processing.video.*;
|
||||
|
||||
Capture video;
|
||||
int signal = 0;
|
||||
|
||||
//the buffer for storing video frames
|
||||
ArrayList frames = new ArrayList();
|
||||
|
||||
void setup() {
|
||||
size(640, 480);
|
||||
video = new Capture(this, width, height, 30);
|
||||
video.start();
|
||||
}
|
||||
|
||||
void captureEvent(Capture camera) {
|
||||
camera.read();
|
||||
|
||||
//copy the current video frame into an image, so it can be stored in the buffer
|
||||
PImage img = createImage(width, height, RGB);
|
||||
video.loadPixels();
|
||||
arrayCopy(video.pixels, img.pixels);
|
||||
|
||||
frames.add(img);
|
||||
|
||||
//once there are enough frames, remove the oldest one when adding a new one
|
||||
if(frames.size() > height/4) {
|
||||
frames.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
//set the image counter to 0
|
||||
int currentImage = 0;
|
||||
|
||||
loadPixels();
|
||||
|
||||
//begin a loop for displaying pixel rows of 4 pixels height
|
||||
for(int y = 0; y < video.height; y+=4) {
|
||||
//go through the frame buffer and pick an image, starting with the oldest one
|
||||
if(currentImage < frames.size()) {
|
||||
PImage img = (PImage)frames.get(currentImage);
|
||||
|
||||
if(img != null) {
|
||||
img.loadPixels();
|
||||
|
||||
//put 4 rows of pixels on the screen
|
||||
for(int x = 0; x < video.width; x++) {
|
||||
pixels[x + y * width] = img.pixels[x + y * video.width];
|
||||
pixels[x + (y + 1) * width] = img.pixels[x + (y + 1) * video.width];
|
||||
pixels[x + (y + 2) * width] = img.pixels[x + (y + 2) * video.width];
|
||||
pixels[x + (y + 3) * width] = img.pixels[x + (y + 3) * video.width];
|
||||
}
|
||||
}
|
||||
|
||||
//increase the image counter
|
||||
currentImage++;
|
||||
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
updatePixels();
|
||||
|
||||
//for recording an image sequence
|
||||
//saveFrame("frame-####.jpg");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user