LoginSignup
5
8

More than 5 years have passed since last update.

ofxOpenCv背景差分処理ソースメモ

Last updated at Posted at 2015-05-22

一行、getPixels().getData()に変更しました
このコピペでも動きますが、

基礎からちゃんとやるのをオススメします。
僕も最近になってブログ書きながら勉強始めました。 ホンキートンクスーダラブルース
http://www.sudara-bluse.tokyo/entry/openframeworks_9

あとはyoppa大先生のとかアルゴリズム乗ってます
http://yoppa.org/ma2_10/2214.html

ofApp.h
#pragma once

#include "ofMain.h"
#include "ofxOpenCv.h"

class ofApp : public ofBaseApp{

    public:
        void setup();
        void update();
        void draw();

        void keyPressed(int key);
        void keyReleased(int key);
        void mouseMoved(int x, int y );
        void mouseDragged(int x, int y, int button);
        void mousePressed(int x, int y, int button);
        void mouseReleased(int x, int y, int button);
        void windowResized(int w, int h);
        void dragEvent(ofDragInfo dragInfo);
        void gotMessage(ofMessage msg);

    ofVideoGrabber videoGrabber; // ビデオ入力
    ofxCvColorImage colorImg; // openCvで扱うイメージ
    ofxCvGrayscaleImage grayImage; // openCvで扱うグレースケールイメージ
    ofxCvGrayscaleImage grayBg; // 背景のグレースケール
    ofxCvGrayscaleImage grayDiff; // 背景と差分
    ofxCvContourFinder conntourFinder; // 輪郭抽出のためのインスタンス

    bool bLearnBackground; // 背景記録モードかどうか
    int treshold; // 悶値     
};
ofApp.cpp
#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){

    ofBackground(0, 0, 0);

    // 360 * 240 でビデオ取り込み初期化
    videoGrabber.initGrabber(320, 240);

    // openCvで解析する領域確保
    colorImg.allocate(320, 240);

    // グレースケール
    grayImage.allocate(320, 240);

    //背景画像
    grayBg.allocate(320, 240);

    // 背景との差分
    grayDiff.allocate(320, 240);

    bLearnBackground = true;

    treshold = 100;

}

//--------------------------------------------------------------
void ofApp::update(){

    videoGrabber.update();


    if(videoGrabber.isFrameNew()){

        // OpenCVで解析するカラー画像領域に取得した映像を格納
        colorImg.setFromPixels(videoGrabber.getPixels().getData(), 320, 240);

        // 画像をグレースケールに変換
        grayImage = colorImg;

        if(bLearnBackground){

            // グレースケールイメージ記録
            grayBg = grayImage;
            // 記録しないモードに
            bLearnBackground = false;
        }

        // 背景画像と現在の画像の差分の絶対値を取得
        grayDiff.absDiff(grayBg, grayImage);

        // 差分画像を設定した悶値を境に二値化
        grayDiff.threshold(treshold);

        // 二値化した差分画像から、輪郭抽出
        // findContours(分析する画像:ofxCvGrayscaleImage, 最小エリア, 最大エリア, 認識する物体の数, 穴を検出するか);
        conntourFinder.findContours(grayDiff, 20, (340*240)/3, 10, true);
    }



}

//--------------------------------------------------------------
void ofApp::draw(){

    // 取り込んだ画像を表示
    colorImg.draw(0, 0);

    // グレースケール画像
    grayImage.draw(340, 10);

    // 背景画像を表示
    grayBg.draw(10, 260);

    // 差分表示
    grayDiff.draw(340, 260);

    // 輪郭線表示
    conntourFinder.draw(340, 500);

}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){

    switch (key) {
        case ' ':
            bLearnBackground = true;
            break;
        case '+':
            treshold ++;
            if(treshold > 255){
                treshold = 255;
            }
            break;
        case '-':
            treshold --;
            if(treshold < 0){
                treshold = 0;
            }
            break;
        default:
            break;
    }

}
5
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
8