メニュー 閉じる

110. Processingでウェブカメラの映像から差分でアニメーション

WEBカメラの映像をキャプチャしながら、現フレームと前フレームの
Brightness値の差分を閾値で判断し、細かな円が出るようにしています。
でもなんかホラーっぽい・・・。

http://www.velvet-number.com/p5_test/test078/20190313_mov.mov

import processing.video.*;

Capture ncam;
PImage pcam;

int num = 0;
int leng = 2000;

ArrayList<createBall> balls = new ArrayList<createBall> ();

void setup () {

	size (640, 480);
	frameRate (60);
	stroke (255);

	ncam = new Capture (this, 640, 480, 60);
	pcam = new PImage (640, 480);

	ncam.start ();

}

void draw () {

	background (255);

	pcam.loadPixels ();

	//image (ncam, 0, 0);

	for (int y = 0; y < 480; y += 5) {
		for (int x = 0; x < 640; x += 5) {
			int pos = (y * 640) + x;
			float diff = abs (brightness (pcam.pixels[pos]) - brightness (ncam.pixels[pos]));
			if (diff > 20) {
				balls.add (new createBall (float (x), float (y), diff, ncam.pixels[pos]));
			}
			if (balls.size () > leng) {
				balls.remove (0);
			}
		}
	}

	for (int i = 0; i < balls.size (); i++) {
		balls.get (i).move ();
		balls.get (i).draw ();
	}

	pcam.copy (ncam, 0, 0, 640, 480, 0, 0, 640, 480);

	if (ncam.available () == true) {
		ncam.read ();
	}

	saveFrame ();

}

public class createBall {

	int c;
	float gx = random (-2, 2);
	float gy = random (-4, 4);
	float x, y;
	float spx, spy;
	float diff;

	createBall (float _x, float _y, float _diff, int _c) {

		x = _x;
		y = _y;
		c = _c;
		diff = _diff;

		spx = random (-1, 1);
		spy = random (-1, 1);

	}

	public void move () {

		spx += gx;
		spy += gy;

		x += spx;
		y += spy;

	}

	public void draw () {

		stroke (40);
		fill (c);
		ellipse (x, y, diff / 10, diff / 10);

	}
}

Posted in processing