ofxKinect サンプルコードをいじる setup()編
ofxKinectについて、サンプルコードをいじりつつ、使い方を学んでいく。
ofxKinect内の/exampleファイルにサンプルコードがある。
サンプルをビルドすると以下のようになる。
左上、左下が白黒映像だが、描画の具合が違う。
左下は距離センサーの決められた範囲で、単純に白黒で表示するのに対して、
上は色の濃度にあわせて白黒のグラデーション表示している。
右上はkinectからとった普通のカラー映像。
ofxKinectをアドオンに追加する際、ofxOpenCvも同時にアドオンに追加する。
testapp.hに
ofxKinect kinect(オブジェクト名 ) を追加
ofxCvColorImage colorImg;
ofxCvGrayscaleImage grayImage; // grayscale depth image
ofxCvGrayscaleImage grayThreshNear; // the near thresholded image
ofxCvGrayscaleImage grayThreshFar; // the far thresholded image
も同時に追加。
ofxCVColorImageと、ofxCvGrayscaleImageクラスについては、ofxCvImageクラスを継承している。 ofxCVアドオンの中にコードがあるので、詳しくはそこをみる。
testapp.cppの testapp::setup()内に以下を追加
ofSetLogLevel(OF_LOG_VERBOSE);
// enable depth->video image calibration
kinect.setRegistration(true);
kinect.init();
/ /kinect.init(true); // shows infrared instead of RGB video image
/ /kinect.init(false, false); // disable video image (faster fps)
kinect.open();// opens first available kinect
//kinect.open(1);// open a kinect by id, starting with 0 (sorted by serial #
lexicographically))
//kinect.open("A00362A08602047A");// open a kinect using it's unique serial #
ここでは、要はofxKinectクラスのオブジェクトであるkinectオブジェクトを、キャリブレーションし、kinectからとった映像を使えるようにしている。
ここで、kinect.setRegistration(true)をとってみても、起動できたので、ここではおそらくsetRegistrationの値を明示的にtrueにしていると思われる。
そしてkinect.init()で起動して、kinect.open()で使えるようにする。
続いて
colorImg.allocate(kinect.width, kinect.height); //colorImg = ofxCvColorImage class
grayImage.allocate(kinect.width, kinect.height); //ofxCvgrayimagescale class
grayThreshNear.allocate(kinect.width, kinect.height);
grayThreshFar.allocate(kinect.width, kinect.height);
はkinectで取得した情報を、colorImg,grayImage・・に投げている。詳しい挙動は現在のところ理解していない。kinect.width.kinect.heightは、kinectで取得した映像の縦横のピクセルだと思う。
allocateメソッドは、ofxCvImageクラス内に処理が記述してある。
引数に(int w,int h)を用いる。
続いてtestapp::setup()内
nearThreshold = 230;//?
farThreshold = 90;//?
bThreshWithOpenCV = true;
ofSetFrameRate(60);
// zero the tilt on startup
angle = 0;
kinect.setCameraTiltAngle(angle);// angle change
// start from the front
bDrawPointCloud = false;
ここで、
near Threshold = 230
farThreshold = 90
は、左下の、黒しろ2値の映像に対応する。
nearの方は、値が高いほど近くの物が認識される。
farは、低いほど、遠くの物が認識される。
kinect.setCameraTiltAngle(angle)は、kinect自体の縦のアングルを指定された角度に変更する。ここでは-30<=angle<=30 の間をいじれる。
bDrawPointCloud は、起動した後にpをおすと、カラー映像で、ピクセルを距離に応じて丸で表現するモードをオンにする。初期状態はオフ
。
setup()内に書かれているコードはこれまで。
ここまで
以上が、setup()内の内容である。
update()
draw()
内の記述については、また次のエントリで書こうと思う。
このサンプルのコードは、kinectを使うにあたって、基本的なものとなると思うので、しっかり理解していきたい。
ちなみに、このofxKinectのサンプルで、pを押すとdrawpointcloudモードになるが、これがめちゃくちゃいじってて楽しい。
映像のところでマウスドラッグすると、仮想カメラの位置が動いて、kinectでとった映像と、距離の情報にあわせて疑似3D映像になる。