motion tracking/blob detection move pixels of background image?
in
Contributed Library Questions
•
2 years ago
Hi,
I'm working on my first Processing project and would like to ask for some advice.
Thanks very much for all the great and helpful information posted in this forum.
I am using the flob library and an example from the library to detect blobs and movement. So far so good but what I would ideally like to do is instead of having the detected movement create the white squares that move I would like to have a background image and have the detected movement move the pixels of the image around. Then I would want the changed pixels to stay where they were moved to until moved again. Sort of like finger painting I guess.
Here's what I am using right now, some stuff that was in the example file I started with I didn't need but I just commented it out for now so sorry for the messiness. Like I said, this is my first try so any advice you can give me as to what might be the best way to approach my idea would be greatly appreciated.
Thanks in advance,
Rebecca
*/
import processing.opengl.*;
import processing.video.*;
import s373.flob.*;
Capture video;
Flob flob;
ArrayList blobs;
PSys psys;
int tresh = 20; // adjust treshold value here or keys t/T
int fade = 25;
int om = 1;
int videores=128;
String info="";
PFont font;
float fps = 60;
int videotex = 2; //case 0: videotex = videoimg;//case 1: videotex = videotexbin;
//case 2: videotex = videotexmotion//case 3: videotex = videoteximgmotion;TTTtf
void setup() {
// osx quicktime bug 882 processing 1.0.1
try {
quicktime.QTSession.open();
}
catch (quicktime.QTException qte) {
qte.printStackTrace();
}
size(1024,512,OPENGL);
frameRate(fps);
rectMode(CENTER);
// init video data and stream
video = new Capture(this, videores, videores, (int)fps);
flob = new Flob(videores, videores, width, height);
flob.setThresh(tresh).setSrcImage(videotex)
.setBackground(video).setBlur(0).setOm(1).setFade(fade)
.setMirror(true,false);
font = createFont("monaco",10);
textFont(font);
psys = new PSys(1000);//number of squares
stroke(200,200);//shape of squares
strokeWeight(25);//size of squares
}
void draw() {
if(video.available()) {
video.read();
blobs = flob.calc(flob.binarize(video));
}
image(flob.getSrcImage(), 0, 0, width, height);
rectMode(CENTER);
int numblobs = blobs.size();
for(int i = 0; i < numblobs; i++) {
ABlob ab = (ABlob)flob.getABlob(i);
psys.touch(ab);
//box
// fill(0,0,255,100);
// rect(ab.cx,ab.cy,ab.dimx,ab.dimy);
//centroid
// fill(0,255,0,200);
// rect(ab.cx,ab.cy, 5, 5);
// info = ""+ab.id+" "+ab.cx+" "+ab.cy;
// text(info,ab.cx,ab.cy+20);
}
psys.go();
psys.draw();
//report presence graphically
// fill(255,152,255);
// rectMode(CORNER);
// rect(5,5,flob.getPresencef()*width,10);
//String stats = ""+frameRate+" flob.numblobs: "+numblobs+" flob.thresh:"+tresh+
// " <t/T>"+" flob.fade:"+fade+" <f/F>"+" flob.om:"+flob.getOm()+
// " flob.image:"+videotex+" flob.presence:"+flob.getPresencef();
//fill(0,255,0);
//text(stats,5,25);
}
void keyPressed() {
if (key=='S')
video.settings();
if (key=='i') {
videotex = (videotex+1)%4;
flob.setImage(videotex);
}
if(key=='t') {
flob.setTresh(tresh--);
}
if(key=='T') {
flob.setTresh(tresh++);
}
if(key=='f') {
flob.setFade(fade--);
}
if(key=='F') {
flob.setFade(fade++);
}
if(key=='o') {
om=(om +1) % 3;
flob.setOm(om);
}
if(key==' ') //space clear flob.background
flob.setBackground(video);
}
and the Psys:
float drag = 0.957;
class Part {
float x, y, vx, vy, ax, ay;
float px,py,force;
Part() {
x = random(width);
y = random(height);
vx = random(-2,2);
vy = random(-2,2);
force = random(-2,2);
px = x;
py = y;
}
void go() {
vx += ax;
vy += ay;
vx *= drag;
vy *= drag;
px = x;
py = y;
x+=vx;
y+=vy;
ax = 0;
ay = 0;
bounds();
}
void bounds(){
boolean c = false;
if(x>width){
x-=width;
c = true;
}
if(x<0){
c = true;
x+=width;
}
if(y>height){
c = true;
y-=height;
}
if(y<0){
y+=height;
c = true;
}
if(c){
px = x;
py = y;
}
}
void draw() {
line(px,py,x,y);
}
void touch(ABlob ab) {
float dx = ab.cx - x;
float dy = ab.cy - y;
float d = sqrt(dx*dx+dy*dy);
if(d > 0 && d < 200) {
d = 1.0f/d * force;
dx *= d;
dy *= d;
ax += dx;
ay += dy;
}
}
}
class PSys {
Part p[];
PSys (int num) {
p = new Part[num];
for(int i=0;i<p.length;i++)
p[i] = new Part();
}
void go() {
for(int i=0; i<p.length;i++)
p[i].go();
}
void draw() {
for(int i=0; i<p.length;i++)
p[i].draw();
}
void touch(ABlob ab) {
for(int i=0; i<p.length;i++)
p[i].touch(ab);
}
}
I'm working on my first Processing project and would like to ask for some advice.
Thanks very much for all the great and helpful information posted in this forum.
I am using the flob library and an example from the library to detect blobs and movement. So far so good but what I would ideally like to do is instead of having the detected movement create the white squares that move I would like to have a background image and have the detected movement move the pixels of the image around. Then I would want the changed pixels to stay where they were moved to until moved again. Sort of like finger painting I guess.
Here's what I am using right now, some stuff that was in the example file I started with I didn't need but I just commented it out for now so sorry for the messiness. Like I said, this is my first try so any advice you can give me as to what might be the best way to approach my idea would be greatly appreciated.
Thanks in advance,
Rebecca
*/
import processing.opengl.*;
import processing.video.*;
import s373.flob.*;
Capture video;
Flob flob;
ArrayList blobs;
PSys psys;
int tresh = 20; // adjust treshold value here or keys t/T
int fade = 25;
int om = 1;
int videores=128;
String info="";
PFont font;
float fps = 60;
int videotex = 2; //case 0: videotex = videoimg;//case 1: videotex = videotexbin;
//case 2: videotex = videotexmotion//case 3: videotex = videoteximgmotion;TTTtf
void setup() {
// osx quicktime bug 882 processing 1.0.1
try {
quicktime.QTSession.open();
}
catch (quicktime.QTException qte) {
qte.printStackTrace();
}
size(1024,512,OPENGL);
frameRate(fps);
rectMode(CENTER);
// init video data and stream
video = new Capture(this, videores, videores, (int)fps);
flob = new Flob(videores, videores, width, height);
flob.setThresh(tresh).setSrcImage(videotex)
.setBackground(video).setBlur(0).setOm(1).setFade(fade)
.setMirror(true,false);
font = createFont("monaco",10);
textFont(font);
psys = new PSys(1000);//number of squares
stroke(200,200);//shape of squares
strokeWeight(25);//size of squares
}
void draw() {
if(video.available()) {
video.read();
blobs = flob.calc(flob.binarize(video));
}
image(flob.getSrcImage(), 0, 0, width, height);
rectMode(CENTER);
int numblobs = blobs.size();
for(int i = 0; i < numblobs; i++) {
ABlob ab = (ABlob)flob.getABlob(i);
psys.touch(ab);
//box
// fill(0,0,255,100);
// rect(ab.cx,ab.cy,ab.dimx,ab.dimy);
//centroid
// fill(0,255,0,200);
// rect(ab.cx,ab.cy, 5, 5);
// info = ""+ab.id+" "+ab.cx+" "+ab.cy;
// text(info,ab.cx,ab.cy+20);
}
psys.go();
psys.draw();
//report presence graphically
// fill(255,152,255);
// rectMode(CORNER);
// rect(5,5,flob.getPresencef()*width,10);
//String stats = ""+frameRate+" flob.numblobs: "+numblobs+" flob.thresh:"+tresh+
// " <t/T>"+" flob.fade:"+fade+" <f/F>"+" flob.om:"+flob.getOm()+
// " flob.image:"+videotex+" flob.presence:"+flob.getPresencef();
//fill(0,255,0);
//text(stats,5,25);
}
void keyPressed() {
if (key=='S')
video.settings();
if (key=='i') {
videotex = (videotex+1)%4;
flob.setImage(videotex);
}
if(key=='t') {
flob.setTresh(tresh--);
}
if(key=='T') {
flob.setTresh(tresh++);
}
if(key=='f') {
flob.setFade(fade--);
}
if(key=='F') {
flob.setFade(fade++);
}
if(key=='o') {
om=(om +1) % 3;
flob.setOm(om);
}
if(key==' ') //space clear flob.background
flob.setBackground(video);
}
and the Psys:
float drag = 0.957;
class Part {
float x, y, vx, vy, ax, ay;
float px,py,force;
Part() {
x = random(width);
y = random(height);
vx = random(-2,2);
vy = random(-2,2);
force = random(-2,2);
px = x;
py = y;
}
void go() {
vx += ax;
vy += ay;
vx *= drag;
vy *= drag;
px = x;
py = y;
x+=vx;
y+=vy;
ax = 0;
ay = 0;
bounds();
}
void bounds(){
boolean c = false;
if(x>width){
x-=width;
c = true;
}
if(x<0){
c = true;
x+=width;
}
if(y>height){
c = true;
y-=height;
}
if(y<0){
y+=height;
c = true;
}
if(c){
px = x;
py = y;
}
}
void draw() {
line(px,py,x,y);
}
void touch(ABlob ab) {
float dx = ab.cx - x;
float dy = ab.cy - y;
float d = sqrt(dx*dx+dy*dy);
if(d > 0 && d < 200) {
d = 1.0f/d * force;
dx *= d;
dy *= d;
ax += dx;
ay += dy;
}
}
}
class PSys {
Part p[];
PSys (int num) {
p = new Part[num];
for(int i=0;i<p.length;i++)
p[i] = new Part();
}
void go() {
for(int i=0; i<p.length;i++)
p[i].go();
}
void draw() {
for(int i=0; i<p.length;i++)
p[i].draw();
}
void touch(ABlob ab) {
for(int i=0; i<p.length;i++)
p[i].touch(ab);
}
}
1