带处理的像素化视频

时间:2016-04-19 14:32:35

标签: java video processing video-processing

我正在尝试加载视频,然后以像素化的方式显示它。它在加载很长一段时间后工作了一次,但后来它停止工作 - 只是一个黑屏,什么也没有出现,没有错误信息我想知道出了什么问题。感谢。

import processing.video.*;
Movie movie;

int videoScale = 8;
int cols, rows;

void setup() {
  size(640, 360);
  background(0);
  movie = new Movie(this, "movie.mp4");
  movie.loop();

  cols = width / videoScale;
  rows = height / videoScale;
}

void draw() {
  movie.loadPixels();

  for (int i = 0; i < cols; i++) {
   for (int j = 0; j < rows; j++) {
     int x = i * videoScale;
     int y = j * videoScale;
     color c = movie.pixels[i + j * movie.width];
     fill(c);
     noStroke();
     rect(x, y, videoScale, videoScale);
   }
  } 
}

// Called every time a new frame is available to read
void movieEvent(Movie movie) {
  movie.read();
}

1 个答案:

答案 0 :(得分:1)

你可能在这里从错误的地方采样:

color c = movie.pixels[i + j * movie.width];

首先,i是你的cols计数器,它是x维度,j是行计数器,y维度。 其次,您可能希望以相同的比例进行采样,因此需要乘以videoScale。您已经拥有x,y变量,因此请尝试这样的采样:

color c = movie.pixels[y * movie.width + x];

或者,您可以使用PGraphics实例作为帧缓冲区以较小的比例绘制(重新采样),然后以较大比例绘制小缓冲区:

import processing.video.*;
Movie movie;

int videoScale = 8;
int cols, rows;
PGraphics resized;

void setup() {
  size(640, 360);
  background(0);
  noSmooth();//remove aliasing

  movie = new Movie(this, "transit.mov");
  movie.loop();

  cols = width / videoScale;
  rows = height / videoScale;

  //setup a smaller sized buffer to draw into
  resized = createGraphics(cols, rows);
  resized.beginDraw();
  resized.noSmooth();//remove aliasing
  resized.endDraw();
}

void draw() {
  //draw video resized smaller into a buffer
  resized.beginDraw();
  resized.image(movie,0,0,cols,rows);
  resized.endDraw();
  //draw the small buffer resized bigger
  image(resized,0,0,movie.width,movie.height);
}

// Called every time a new frame is available to read
void movieEvent(Movie movie) {
  movie.read();
}