背景差分のプログラム

Processing3の場合

bg_diff.pde
import gab.opencv.*;
import processing.video.*;
	
Capture cap;
OpenCV opencv;
	
int img_width = 640;
int img_height = 480;
	
PImage  bgImg, img, bgGray, imgGray, diff;
	
boolean flagS = false;
	
void setup() {
  size(100, 100);
  surface.setResizable(true);
  surface.setSize(img_width, img_height);
	
  String[] cameras = Capture.list();
	
  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println("[" + i + "] " + cameras[i]);
    }
	
    cap = new Capture(this, cameras[2]);
    cap.start();
    opencv = new OpenCV(this, img_width, img_height);
	
    bgImg = loadImage("BG.png");
    opencv.loadImage(bgImg);
    bgGray = opencv.getSnapshot();
	
    PFont font = createFont("MS-PMincho", 12);
    textFont(font);
  }
}
	
void keyPressed() {
  if (key == 's') {
    if (flagS == false) {
      flagS = true;
    } else {
      flagS = false;
    }
  }
}
	
void draw() {
  if (flagS == true) {
    return;
  }
	
  cap.read();    
  set(0, 0, cap);
  img = get(0, 0, img_width, img_height);
	
  opencv.loadImage(img);
  imgGray = opencv.getSnapshot();
	
  background(0);
	
  opencv.diff(bgGray);
  diff = opencv.getSnapshot();
	
  pushMatrix();
  scale(0.5);
  image(bgGray, 0, 0);  
  image(imgGray, img.width, 0);
  image(diff, 0, img.height);
  popMatrix();
	
  String s1 = "背景のグレースケール";
  String s2 = "現在のグレースケール";
  String s3 = "差分画像";
	
  fill(255, 0, 0);
  text(s1, img.width/2 - (textWidth(s1) + 10), 20 );
  text(s2, img.width - (textWidth(s2) + 10), 20 );
  text(s3, img.width/2 - (textWidth(s3) + 10), img.height/2 + 20 );
	
  delay(100);
}

Processing2の場合

bg_diff.pde
import gab.opencv.*;
import processing.video.*;
	
Capture cap;
OpenCV opencv;
	
int img_width = 640;
int img_height = 480;
	
PImage  bgImg, img, bgGray, imgGray, diff;
	
boolean flagS = false;
	
void setup() {
  size(img_width, img_height);
	
  String[] cameras = Capture.list();
	
  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println("[" + i + "] " + cameras[i]);
    }
	
    cap = new Capture(this, cameras[2]);
    cap.start();
    opencv = new OpenCV(this, img_width, img_height);
	
    bgImg = loadImage("BG.png");
    opencv.loadImage(bgImg);
    bgGray = opencv.getSnapshot();
	
    PFont font = createFont("MS-PMincho", 12);
    textFont(font);
  }
}
	
void keyPressed() {
  if (key == 's') {
    if (flagS == false) {
      flagS = true;
    } else {
      flagS = false;
    }
  }
}
	
void draw() {
  if (flagS == true) {
    return;
  }
	
  cap.read();    
  set(0, 0, cap);
  img = get(0, 0, img_width, img_height);
	
  opencv.loadImage(img);
  imgGray = opencv.getSnapshot();
	
  background(0);
	
  opencv.diff(bgGray);
  diff = opencv.getSnapshot();
	
  pushMatrix();
  scale(0.5);
  image(bgGray, 0, 0);  
  image(imgGray, img.width, 0);
  image(diff, 0, img.height);
  popMatrix();
	
  String s1 = "背景のグレースケール";
  String s2 = "現在のグレースケール";
  String s3 = "差分画像";
	
  fill(255, 0, 0);
  text(s1, img.width/2 - (textWidth(s1) + 10), 20 );
  text(s2, img.width - (textWidth(s2) + 10), 20 );
  text(s3, img.width/2 - (textWidth(s3) + 10), img.height/2 + 20 );
	
  delay(100);
}