在Processing中从相机获取两个连续帧

时间:2018-01-05 18:35:01

标签: image video camera processing frames

我一直在尝试使用Processing来实现这一目标,但每次都没有得到一些完全连续的方法。谁会知道正确的方式"这样做?

提前致谢!

2 个答案:

答案 0 :(得分:0)

我查看了KetaiCamera sourcesissue reports,尤其是this one。不幸的是,这段代码不是为了提供实时相机帧而构建的。

您可以尝试将AndroidCapture项目作为入门者,并修改原生Android class以实现目标。

答案 1 :(得分:0)

理论上应该是听captureEvent()得到一个新的帧并跟踪先前是否记录了第一帧,如果是后记录了第二帧的话。

这是一个基本的注释草图来说明这一点(按任意键来抓住另一对帧):

import processing.video.*;

Capture camera;

PImage firstFrame;
PImage secondFrame;

void setup(){
  size(1920,480);

  camera = new Capture(this,640,480);
  camera.start();
}
void draw(){
  image(camera,0,0);
  if(firstFrame != null){
    image(firstFrame,640,0);
  }
  if(secondFrame != null){
    image(secondFrame,1280,0);
  }
}
//this is the callback from the video library when a new camera frame is available
void captureEvent(Capture c){
  //read a new frame
  c.read();
  //if the first frame wasn't recorded yet, record(copy) it's pixels
  if(firstFrame == null){
    firstFrame = c.get();
  }
  //same for the second frame, but check if the first frame has been recorded first
  if(firstFrame != null && secondFrame == null){
    secondFrame = c.get();
  }
}

void keyPressed(){
  //reset consecutive frames on keypress
  firstFrame = secondFrame = null;
}

理论上(正如您在Processing Video Library's source code中所见),只有在准备好新的相机样本时才会触发captureEvent。 在实践中,您会发现两个连续的帧看起来可能相同(即使它们可能在时间上相隔一秒),甚至是您在评论中指出的噪声。

感觉就像你所追求的是一个连续的帧,但与之前的帧不同。如果是这种情况,您可以使用FrameDifferencing example进行游戏(处理>示例>库>视频>捕获> FrameDifferencing

以上是上述草图的修改版本,使用Golan Levin的FrameDifferencing代码只能获取第二帧,如果它稍有不同:

import processing.video.*;

Capture camera;

PImage firstFrame;
PImage secondFrame;
PImage diff;

void setup(){
  size(1920,960);

  camera = new Capture(this,640,480);
  camera.start();

  diff = createImage(640,480,RGB);
}
void draw(){
  image(camera,0,0);
  if(firstFrame != null){
    image(firstFrame,640,0);
  }
  if(secondFrame != null){
    image(secondFrame,1280,0);
  }
  image(diff,0,480);
}
//this is the callback from the video library when a new camera frame is available
void captureEvent(Capture c){
  //read a new frame
  c.read();
  //if the first frame wasn't recorded yet, record(copy) it's pixels
  if(firstFrame == null){
    firstFrame = c.get();
    println("recorded first frame at",new java.util.Date());
  }
  //same for the second frame, but check if the first frame has been recorded first
  if(firstFrame != null && secondFrame == null){
    //if the difference between the first frame cand the current frame is even ever so slightly off, record the second frame
    if(difference(firstFrame,camera) > 100){
      secondFrame = c.get();
    }
  }

}

int difference(PImage first,PImage second){
  final int numPixels = 640*480;
  camera.loadPixels();
  int movementSum = 0; // Amount of movement in the frame
  for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
    color currColor = first.pixels[i];
    color prevColor = second.pixels[i];
    // Extract the red, green, and blue components from current pixel
    int currR = (currColor >> 16) & 0xFF; // Like red(), but faster
    int currG = (currColor >> 8) & 0xFF;
    int currB = currColor & 0xFF;
    // Extract red, green, and blue components from previous pixel
    int prevR = (prevColor >> 16) & 0xFF;
    int prevG = (prevColor >> 8) & 0xFF;
    int prevB = prevColor & 0xFF;
    // Compute the difference of the red, green, and blue values
    int diffR = abs(currR - prevR);
    int diffG = abs(currG - prevG);
    int diffB = abs(currB - prevB);
    // Render the difference image to the screen
    diff.pixels[i] = color(diffR, diffG, diffB);
    // Add these differences to the running tally
    movementSum += diffR + diffG + diffB;
  }
  diff.updatePixels();
  return movementSum;
}

void keyPressed(){
  //reset consecutive frames on keypress
  firstFrame = secondFrame = null;
}

在上面的示例中,100是任意值。 最大值为255*3*640*480(每通道0-255 *通道数*宽*高)