proce55ingで2値化をする。

以下1行でできる。

CaptureクラスはPImageをクラスを継承しているので、その中のfileterメソッドが利用できる。

cam.filter(THRESHOLD,threshold);

thresholdはしきい値で0.2~0.5くらいがちょうどよい

import processing.video.*;

Capture cam;

//フレームレート
int fps = 30;

boolean newFrame = false;

//しきい値
float threshold = 0.3;

void setup(){
  
  size(320, 240);//ウィンドサイズ
  
  //カメラ
  cam = new Capture(this, width, height, fps);
}

void draw(){
  if (newFrame)
  {
    newFrame=false;
    image(cam,0,0,width,height);
  }
}

void captureEvent(Capture cam)
{
  cam.read();
  
  //2値化
  cam.filter(THRESHOLD,threshold);
  newFrame = true;
}

//キー入力イベント
void keyPressed(){
  // 2値化閾値の調整
  if('A' <= key && key <= 'z'){
    switch(key){
      case 'a':
        if(threshold > 0.021) threshold -= 0.020;
        break;
      case 's':
        if(threshold < 0.5) threshold += 0.020;
        break;
    }
  }
  println("しきい値: "+threshold);
}

aとsキーでしきい値を変更。