[ Video ]
[ About ]
OpenCVでWebカメラの映像を取得、毎フレームの差分を取得して、その情報をもとにParticleを生成しています。画像に動きのある部分からカラフルな玉が発生します。
本当はもうちょっと細かな粒子のようなものや、映像の色を反映した破片のようなオブジェクトを作りたかったのですが、リアルタイムだと処理性能的に厳しかったです。また今度、MPEGファイルから生成するような形で試してみようと思います。
[ Source ]
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 |
#pragma once #include "ofMain.h" #include "ofFbo.h" #include "ofxBox2d.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) {}; ofFbo fbo; ofxBox2d box2d; vector<shared_ptr<ofxBox2dCircle>> circles; vector<ofColor> circles_color; cv::VideoCapture cap; cv::Mat pre_frame; cv::Mat frame; ofImage image; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(30); ofBackground(0); ofSetWindowTitle("Insta"); this->box2d.init(); this->box2d.setGravity(0, 50); //this->box2d.createBounds(); this->box2d.setFPS(30); //this->box2d.registerGrabbing(); this->cap.open(1); this->image.allocate(this->cap.get(CV_CAP_PROP_FRAME_WIDTH), this->cap.get(CV_CAP_PROP_FRAME_HEIGHT), OF_IMAGE_COLOR); this->frame = cv::Mat(this->image.getHeight(), this->image.getWidth(), CV_MAKETYPE(CV_8UC3, this->image.getPixels().getNumChannels()), this->image.getPixels().getData(), 0); this->fbo.allocate(ofGetWidth(), ofGetHeight()); } //-------------------------------------------------------------- void ofApp::update() { cv::Mat src; this->cap >> src; cv::flip(src, src, 1); cv::cvtColor(src, this->frame, cv::COLOR_BGR2RGB); float span = 15; if (ofGetFrameNum() > 10) { cv::Mat gap = this->pre_frame - src; vector<ofVec2f> gap_points; for (int y = 0; y < gap.rows; y += span) { cv::Vec3b* value = gap.ptr<cv::Vec3b>(y, 0); for (int x = 0; x < gap.cols; x += span) { cv::Vec3b v = value[x]; if ((v[0] + v[1] + v[2]) > 32) { shared_ptr<ofxBox2dCircle> circle = shared_ptr<ofxBox2dCircle>(new ofxBox2dCircle); circle.get()->setPhysics(3.0, 0.5, 0.1); circle.get()->setup(this->box2d.getWorld(), x + ofGetWidth() / 2 - this->image.getWidth() / 2 + ofRandom(-span / 2, span / 2), y + 30 + ofRandom(-span / 2, span / 2), span / 2); this->circles.push_back(circle); ofColor c; c.setHsb(ofRandom(255), 220, 220); this->circles_color.push_back(c); } } } } this->pre_frame = src; this->image.update(); this->fbo.begin(); ofClear(0); ofSetColor(255); this->image.draw(ofGetWidth() / 2 - this->image.getWidth() / 2, 30); for (int i = this->circles.size() - 1; i >= 0; i--) { if (this->circles[i].get()->getPosition().y > ofGetHeight()) { this->circles[i].get()->destroy(); this->circles.erase(this->circles.begin() + i); this->circles_color.erase(this->circles_color.begin() + i); } else { ofSetColor(this->circles_color[i]); ofDrawCircle(this->circles[i].get()->getPosition(), this->circles[i].get()->getRadius()); } } this->fbo.end(); this->box2d.update(); } //-------------------------------------------------------------- void ofApp::draw() { this->fbo.draw(0, 0); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |