カメラを扱う基礎的な方法をみてゆきます。
プロジェクト名は camera とします。
以下、ofApp.hとofApp.cpp の違いに注意してください。
ofVideoGrabber オブジェクトを使うと、カメラから映像を取得することができます。
はじめに、ofApp.h ヘッダファイルに ofVideoGrabber オブジェクトを追加します。
class ofApp : public ofBaseApp {
public:
...略...
ofVideoGrabber camera;
int camWidth;
int camHeight;
};
そして、カメラの映像を描画します。
void ofApp::setup() {
camWidth = 320;
camHeight = 240;
camera.setVerbose(true);
camera.initGrabber(camWidth, camHeight);
}
void ofApp::update() {
camera.update();
}
void ofApp::draw() {
ofSetColor(255, 255, 255);
camera.draw(20, 20);
// camera.draw(20, 20, camWidth, camHeight);
}
動画のときと同様に、getPixels() で、ピクセルを取得することができます。
void ofApp::draw() {
ofSetColor(255, 255, 255);
camera.draw(20, 20);
// camera.draw(20, 20, camWidth, camHeight);
// ピクセルの取得
ofPixels pixels = camera.getPixels();
pixels.resize(camWidth, camHeight);
int w = camWidth;
int h = camHeight;
for(int i=0; i<w; i+=1) {
for(int j=0; j<h; j+=1) {
int valueR = pixels[j * 3 * w + i * 3];
int valueG = pixels[j * 3 * w + i * 3 + 1];
int valueB = pixels[j * 3 * w + i * 3 + 2];
ofSetColor(255-valueR, 255-valueG, 255-valueB);
ofDrawRectangle(20 + w + i, 20 + j, 1, 1);
}
}
}