Search code examples
processingpixelvideo-capturebackground-subtraction

How to export a video of foreground pixels in processing


I am doing a project about person extraction and background subtraction. I am going to export a video of the foreground people. Learnt from Daniel Shiffman, now I could get the pixels of foreground but I don't know how to export these pixels as a video. Or I don't need to export it immediately, but I need to do the further processing about these pixels as a video format. Is there anyone could help me? Thanks a lot in advance. Sorry for my English if there is any mistake.

Here is the code:

import processing.video.*;

Capture video;

PImage backgroundImage;
float threshold = 30;

void setup() {
  size(320, 240);
  video = new Capture(this, width, height);
  video.start();

  backgroundImage = createImage(video.width, video.height, RGB); 
}

void captureEvent(Capture video) {
  video.read();
}

void draw() {
  loadPixels();
  video.loadPixels();
  backgroundImage.loadPixels();

  image(video, 0, 0);
  for (int x = 0; x < video.width; x++) {
    for (int y = 0; y < video.height; y++) {
      int loc = x + y * video.width;



color fgColor = video.pixels[loc];
color bgColor = backgroundImage.pixels[loc];

float r1 = red(fgColor); float g1 = green(fgColor); float b1 = blue(fgColor);
float r2 = red(bgColor); float g2 = green(bgColor); float b2 = blue(bgColor);
float diff = dist(r1, g1, b1, r2, g2, b2);

if (diff > threshold) {
  pixels[loc] = fgColor;
} else {
  pixels[loc] = color(0, 0, 0);
}
    }}
    updatePixels();
}


void mousePressed() {
  backgroundImage.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
  backgroundImage.updatePixels();
}

Solution

  • Stack Overflow isn't really designed for general "how do I do this" type questions. It's for specific "I tried X, expected Y, but got Z instead" type questions. But I'll try to help in a general sense:

    You need to split your problem up into smaller pieces and then take on those pieces one at a time. Here's how I might approach the problem:

    Step 1: Isolate the pixels you want. You say you can get the foreground pixels. Now draw them to a buffer using the createGraphics() function.

    Step 2: Store each buffer as an image file on your hard drive. You can do this using the save() function of PImage or PGraphics.

    Step 3: Now that you have each frame, stitch them together into a video. You can do this using FFMPEG or maybe ImageMagick.

    Get those things working perfectly by themselves before you start thinking about combining them. Then if you get stuck on a specific step, you can post a MCVE of just that step. Good luck.