Video with alpha channel in Processing

2019-03-07 05:17发布

问题:

I was wondering if anyone can be amazing and help me with something I'm working on in Processing. I need to play a video file with transparencies over a live feed so that the video isn't simply a rectangle. Here is the section of the code that I think I need to add something to or change. I'm extremely new to all of this and I'm extremely grateful to anyone that can help.

回答1:

If you're video has an alpha channel, that's great, otherwise, you should be able to blend() the other content.

Here's a basic proof of concept sketch. It overlays a grid of circles on top of a live feed. Use the space key to cycle though blend modes. Some will work better than others depending on your content and what you're trying to achieve:

import processing.video.*;

Capture cam;
int w = 320;
int h = 240;
int np = w*h;

PImage overlay;

int blendMode = 1;
int[] blendModes = {BLEND,ADD,SUBTRACT,DARKEST,LIGHTEST,DIFFERENCE,EXCLUSION,MULTIPLY,SCREEN,OVERLAY,HARD_LIGHT,SOFT_LIGHT,DODGE,BURN};
String[] blendModesNames = {"BLEND","ADD","SUBTRACT","DARKEST","LIGHTEST","DIFFERENCE","EXCLUSION","MULTIPLY","SCREEN","OVERLAY","HARD_LIGHT","SOFT_LIGHT","DODGE","BURN"};

void setup(){
  size(w,h);
  cam = new Capture(this,w,h);
  cam.start();

  //test content to overlay, a grid of circles
  background(0);fill(255);
  for(int y = 0 ; y < height; y += 30)
    for(int x = 0 ; x < width; x+= 30)
      ellipse(x,y,15,15);
  overlay = get();
}
void draw(){
  image(cam,0,0);
  blend(overlay,0,0,width,height,0,0,width,height,blendModes[blendMode]);
}
void keyReleased(){
  if(key == ' ') {
    blendMode = (blendMode+1)%blendModes.length;
    println("blendMode: " + blendModesNames[blendMode]);
  }
}
void captureEvent(Capture c){
  c.read();
}


回答2:

I solved (maybe can be improved) by using 2 videos: first footage is the color map with white color on the background; second footage is the matte mask: white for the "important" part, and black the others. Then apply mask() function, herunder is the important part of the code:

Movie mov1;
Movie mov2;
void setup() { 
....code...
 mov1 = new Movie(this, "matte.mov");
 mov2 = new Movie(this, "alpha.mov");
 mov1.play();   
 mov1.pause();   
 mov2.play();   
 mov2.pause();
 }
 void draw() {
 ...code...
 mov1.play();
 mov2.play();
 loadPixels();   
 mov2.mask(mov1);
 image(mov2, 0, 0);
 }

The video used for the test was 256x256, I always use power of two numbers for better performance (float maths). Hope this helps someone!