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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//This code has been arranged by Yasushi Noguchi. Last updated on June 21, 2008.
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//BlobDetection by v3ga <http ://processing.v3ga.net>
//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<theblobdetection .getBlobNb(); n++){
    b=theBlobDetection.getBlob(n);
    if (b!=null){  //もしオブジェクトがあったら、
      // Edges
      if (drawEdges){
        strokeWeight(3);
        stroke(0,0,255);
        for (int m=0;m<b.getEdgeNb();m++){
          eA = b.getEdgeVertexA(m);  //エッジの点をそれぞれeA,eBに代入
          eB = b.getEdgeVertexB(m);
          if (eA !=null && eB !=null)
            //線を描く
            line(eA.x*width, eA.y*height, eB.x*width, eB.y*height);
        }
      }
 
      // Blobs
      if (drawBlobs){
        noFill();
        strokeWeight(1);
        stroke(255,0,0);
        //オブジェクトを囲む矩形を描く
        rect(b.xMin*width, b.yMin*height, b.w*width, b.h*height);
        fill(0, 255, 0);
        //オブジェクト番号を表示
        text(n, b.xMin*width + b.w*width/2, b.yMin*height+ b.h*height/2);
      }
    }
  }
}