(メモ)OpenCV3.1で背景差分MOG2を使う時のサンプル
createBackgroundSubtractorMOG2なる関数を使うようです。
ofApp.h
#pragma once #include "ofMain.h" #include "opencv2/opencv.hpp" 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 mouseEntered(int x, int y); void mouseExited(int x, int y); void windowResized(int w, int h); void dragEvent(ofDragInfo dragInfo); void gotMessage(ofMessage msg); cv::Mat frame; cv::Mat fgMask; cv::Ptr<cv::BackgroundSubtractorMOG2> pMOG2; cv::VideoCapture cap; };
ofApp.cpp
#include "ofApp.h" #include "opencv2/opencv.hpp" #pragma comment(lib, "opencv_calib3d310.lib") #pragma comment(lib, "opencv_core310.lib") #pragma comment(lib, "opencv_features2d310.lib") #pragma comment(lib, "opencv_flann310.lib") #pragma comment(lib, "opencv_highgui310.lib") #pragma comment(lib, "opencv_imgcodecs310.lib") #pragma comment(lib, "opencv_imgproc310.lib") #pragma comment(lib, "opencv_ml310.lib") #pragma comment(lib, "opencv_objdetect310.lib") #pragma comment(lib, "opencv_photo310.lib") #pragma comment(lib, "opencv_shape310.lib") #pragma comment(lib, "opencv_stitching310.lib") #pragma comment(lib, "opencv_superres310.lib") #pragma comment(lib, "opencv_ts310.lib") #pragma comment(lib, "opencv_video310.lib") #pragma comment(lib, "opencv_videoio310.lib") #pragma comment(lib, "opencv_videostab310.lib") //-------------------------------------------------------------- void ofApp::setup(){ cap.open(1); if (!cap.isOpened()) { cout << "Can't open the camera" << endl; ofExit(); } int history = 500; float varThreshold = 16; bool bShadowDetection = true; pMOG2 = cv::createBackgroundSubtractorMOG2(history, varThreshold, bShadowDetection); pMOG2->setVarThreshold(10); } //-------------------------------------------------------------- void ofApp::update(){ if (cap.grab()) { cap >> frame; pMOG2->apply(frame, fgMask); } } //-------------------------------------------------------------- void ofApp::draw(){ if (!fgMask.empty()) { cv::imshow("MOG2", fgMask); } } //-------------------------------------------------------------- void ofApp::keyPressed(int key){ } //-------------------------------------------------------------- void ofApp::keyReleased(int key){ } //-------------------------------------------------------------- void ofApp::mouseMoved(int x, int y ){ } //-------------------------------------------------------------- void ofApp::mouseDragged(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mousePressed(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mouseReleased(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mouseEntered(int x, int y){ } //-------------------------------------------------------------- void ofApp::mouseExited(int x, int y){ } //-------------------------------------------------------------- void ofApp::windowResized(int w, int h){ } //-------------------------------------------------------------- void ofApp::gotMessage(ofMessage msg){ } //-------------------------------------------------------------- void ofApp::dragEvent(ofDragInfo dragInfo){ }
パラメータ調整GUI付きサンプル
#pragma once #include "ofMain.h" #include "ofxGui.h" #include "opencv2/opencv.hpp" 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 mouseEntered(int x, int y); void mouseExited(int x, int y); void windowResized(int w, int h); void dragEvent(ofDragInfo dragInfo); void gotMessage(ofMessage msg); cv::Mat frame; cv::Mat fgMask; cv::Mat bgMask; cv::Ptr<cv::BackgroundSubtractorMOG2> pMOG2; cv::VideoCapture cap; ofxPanel gui; ofxFloatSlider background_ratio; ofxFloatSlider complexity_reduction_threshold; ofxToggle shadow_detection_flag; ofxIntSlider history; // the number of last frames that affect the background model ofxIntSlider nMixtures; // the number of gaussian components in the background model ofxFloatSlider shadow_threshold; ofxIntSlider shadow_value; ofxFloatSlider varInit; // the initial variance of each gaussian component ofxFloatSlider varMax; ofxFloatSlider varMin; ofxFloatSlider varThreshold; // the variance threshold for the pixel - model match ofxFloatSlider varThresholdGen; // the variance threshold for the pixel - model match used for new mixture component generation. void BackgroundRatioChanged(float& value) { pMOG2->setBackgroundRatio(value); } void ComplexityReductionThresholdChanged(float& value) { pMOG2->setComplexityReductionThreshold(value); } void ShadowDetectionFlagChanged(bool& value) { pMOG2->setDetectShadows(value); } void HistoryChanged(int & value) { pMOG2->setHistory(value); } void nMixturesChanged(int & value) { pMOG2->setNMixtures(value); } void ShadowThresholdChanged(float & value) { pMOG2->setShadowThreshold(value); } void ShadowValueChanged(int & value) { pMOG2->setShadowValue(value); } void VarInitChanged(float & value) { pMOG2->setVarInit(value); } void VarMaxChanged(float & value) { pMOG2->setVarMax(value); varMin.setMax(value); } void VarMinChanged(float & value) { pMOG2->setVarMin(value); varMax.setMin(value); } void VarThresholdChanged(float & value) { pMOG2->setVarThreshold(value); } void VarThresholdGenChanged(float & value) { pMOG2->setVarThresholdGen(value); } };
#include "ofApp.h" #include "opencv2/opencv.hpp" #pragma comment(lib, "opencv_calib3d310.lib") #pragma comment(lib, "opencv_core310.lib") #pragma comment(lib, "opencv_features2d310.lib") #pragma comment(lib, "opencv_flann310.lib") #pragma comment(lib, "opencv_highgui310.lib") #pragma comment(lib, "opencv_imgcodecs310.lib") #pragma comment(lib, "opencv_imgproc310.lib") #pragma comment(lib, "opencv_ml310.lib") #pragma comment(lib, "opencv_objdetect310.lib") #pragma comment(lib, "opencv_photo310.lib") #pragma comment(lib, "opencv_shape310.lib") #pragma comment(lib, "opencv_stitching310.lib") #pragma comment(lib, "opencv_superres310.lib") #pragma comment(lib, "opencv_ts310.lib") #pragma comment(lib, "opencv_video310.lib") #pragma comment(lib, "opencv_videoio310.lib") #pragma comment(lib, "opencv_videostab310.lib") //-------------------------------------------------------------- void ofApp::setup(){ cap.open(1); if (!cap.isOpened()) { cout << "Can't open the camera" << endl; ofExit(); } int initHistory = 500; float initVarThreshold = 16; bool bShadowDetection = true; pMOG2 = cv::createBackgroundSubtractorMOG2(initHistory, initVarThreshold, bShadowDetection); pMOG2->setVarThreshold(10); gui.setup("settings", "settings.xml", 10, 10); gui.add(background_ratio.setup("background ratio", pMOG2->getBackgroundRatio(), 0.0, 1.0)); gui.add(complexity_reduction_threshold.setup("complexity reduction", pMOG2->getComplexityReductionThreshold(), 0.0, 1.0)); gui.add(shadow_detection_flag.setup("shadow detection flag", pMOG2->getDetectShadows())); gui.add(history.setup("hisotry", pMOG2->getHistory(), 1, 1000)); gui.add(nMixtures.setup("nMixtures", pMOG2->getNMixtures(), 1, 100)); gui.add(shadow_threshold.setup("shadow threshold", pMOG2->getShadowThreshold(), -1.0, 1.0)); gui.add(varInit.setup("varInit", pMOG2->getVarInit(), 0.0, 100.0)); gui.add(varMax.setup("varMax", pMOG2->getVarMax(), pMOG2->getVarMin(), 10000.0)); gui.add(varMin.setup("varMin", pMOG2->getVarMin(), 0.0, pMOG2->getVarMax())); gui.add(varThreshold.setup("varThreshold", pMOG2->getVarThreshold(), 0.0, 10000.0)); gui.add(varThresholdGen.setup("varThresholdGen", pMOG2->getVarThresholdGen(), 0.0, 10000.0)); background_ratio.addListener(this, &ofApp::BackgroundRatioChanged); complexity_reduction_threshold.addListener(this, &ofApp::ComplexityReductionThresholdChanged); shadow_detection_flag.addListener(this, &ofApp::ShadowDetectionFlagChanged); history.addListener(this, &ofApp::HistoryChanged); nMixtures.addListener(this, &ofApp::nMixturesChanged); shadow_threshold.addListener(this, &ofApp::ShadowThresholdChanged); varInit.addListener(this, &ofApp::VarInitChanged); varMax.addListener(this, &ofApp::VarMaxChanged); varMin.addListener(this, &ofApp::VarMinChanged); varThreshold.addListener(this, &ofApp::VarThresholdChanged); varThresholdGen.addListener(this, &ofApp::VarThresholdGenChanged); } //-------------------------------------------------------------- void ofApp::update(){ if (cap.grab()) { cap >> frame; pMOG2->apply(frame, fgMask); pMOG2->getBackgroundImage(bgMask); } } //-------------------------------------------------------------- void ofApp::draw(){ if (!fgMask.empty()) { cv::imshow("FG", fgMask); } if(!bgMask.empty()) { cv::imshow("BG", bgMask); } gui.draw(); } //-------------------------------------------------------------- void ofApp::keyPressed(int key){ } //-------------------------------------------------------------- void ofApp::keyReleased(int key){ } //-------------------------------------------------------------- void ofApp::mouseMoved(int x, int y ){ } //-------------------------------------------------------------- void ofApp::mouseDragged(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mousePressed(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mouseReleased(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mouseEntered(int x, int y){ } //-------------------------------------------------------------- void ofApp::mouseExited(int x, int y){ } //-------------------------------------------------------------- void ofApp::windowResized(int w, int h){ } //-------------------------------------------------------------- void ofApp::gotMessage(ofMessage msg){ } //-------------------------------------------------------------- void ofApp::dragEvent(ofDragInfo dragInfo){ }
OpenFrameworksのofxGuiとofParameterの仕様が変わってしまい、とても使いにくくなっていますね。
Qtでもあった現象ですが、綺麗にプログラムが書ける人がライブラリを書き直すと、プログラム的には美しくしくても使いづらい関数が続発するという悲しい事態に陥ります。方言を許容したまま開発を進めてほしいものです。
[1] OpenCV: segment_objects.cpp
今日の防災
Kindle版無料だそうですから、ダウンロードして一読しておくのが良いですね。
- 出版社/メーカー: 東京都
- 発売日: 2016/03/30
- メディア: Kindle版
- この商品を含むブログ (3件) を見る
簡易トイレも買っておいた方が良いでしょうか。
サッと固まる非常用トイレ袋(30回分) 災害での断水時でもトイレが使える!
- 出版社/メーカー: ブレイン(BRAIN)
- メディア: ホーム&キッチン
- 購入: 7人 クリック: 27回
- この商品を含むブログ (4件) を見る