オブジェクトの位置を調べる

カメラが捉えた物体の座標を得るためのコードです。

//This code has been arranged by Yasushi Noguchi. Last updated on June 21, 2008.
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//BlobDetection by v3ga 
//May 2005
//Processing(Beta) v0.85
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

import processing.video.*;
import blobDetection.*;

Capture cam;    //Capture型変数
BlobDetection theBlobDetection;    //BlobDetection型変数
PImage img;
boolean newFrame=false;
PFont font;

// ==================================================
// setup()
// ==================================================
void setup(){
  //ウィンドウのサイズ
  size(640, 480);
  //キャプチャ型変数のcamを宣言
  cam = new Capture(this, 320, 240, 30);
  
  // BlobDetection
  //物体のトラッキングのために使われる画像(キャプチャ映像よりも小さい)
  img = new PImage(80,60); 
  //トラッキングを開始
  theBlobDetection = new BlobDetection(img.width, img.height);
  //暗い場所を探すにはfalse, 明るい場所はtrueにする
  theBlobDetection.setPosDiscrimination(false);
  theBlobDetection.setThreshold(0.5f); //しきい値の設定

  //オブジェクトの番号を表示するためのフォント
  hint(ENABLE_NATIVE_FONTS);
  font = createFont("Arial", 28);
  textFont(font, 28);
  textAlign(CENTER);
}

// ==================================================
// captureEvent()
// ==================================================
void captureEvent(Capture cam){
  cam.read();    //カメラから映像を常に取り込む
  newFrame = true;
}

// ==================================================
// draw()
// ==================================================
void draw(){

  if (newFrame){
      background(255);
    newFrame=false;
    image(cam,0,0,width,height);    //画像をウィンドウに表示
    //カメラ画像からトラッキング用の画像に変換しコピー
    img.copy(cam, 0, 0, cam.width, cam.height, 0, 0, img.width, img.height);
    theBlobDetection.computeBlobs(img.pixels);  //オブジェクトを探す
    drawBlobsAndEdges(true,true);    //オブジェクトの周辺と、オブジェクトを囲った矩形を描く
  }
}

// ==================================================
// drawBlobsAndEdges()
// ==================================================
void drawBlobsAndEdges(boolean drawBlobs, boolean drawEdges){
  noFill();
  Blob b;    //オブジェクト用の変数
  EdgeVertex eA,eB;    //エッジの点
  
  //nはオブジェクトの数
  for (int n=0; n