[ Video ]
[ About ]
OpenCVのエッジ処理で輪郭を抽出して、Particleと線を結ぶようにしました。Mac Book Proの友情出演もありです。
そしてエンコ泣かせ。YouTubeだとつぶれていますが、思ったよりパーリーピーポー感でて良い感じです。
[ 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 |
#pragma once #include "ofMain.h" #include "ofFbo.h" #include "opencv2/opencv.hpp" #include "Particle.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) {}; ofFbo fbo; cv::VideoCapture cap; cv::Size cap_size; cv::Mat frame; ofImage image; vector<unique_ptr<Particle>> particles; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofBackground(0); ofSetWindowTitle("Insta"); ofEnableBlendMode(ofBlendMode::OF_BLENDMODE_ADD); this->cap.open(1); this->cap_size = cv::Size(this->cap.get(CV_CAP_PROP_FRAME_WIDTH), this->cap.get(CV_CAP_PROP_FRAME_HEIGHT)); ofSetFrameRate(this->cap.get(CV_CAP_PROP_FPS)); this->image.allocate(this->cap_size.width, this->cap_size.height, OF_IMAGE_GRAYSCALE); this->frame = cv::Mat(this->image.getHeight(), this->image.getWidth(), CV_MAKETYPE(CV_8UC1, this->image.getPixels().getNumChannels()), this->image.getPixels().getData(), 0); for (int i = 0; i < 2048; i++) { unique_ptr<Particle> particle(new Particle()); this->particles.push_back(std::move(particle)); } this->fbo.allocate(ofGetWidth(), ofGetHeight()); } //-------------------------------------------------------------- void ofApp::update() { cv::Mat src; this->cap >> src; if (src.empty()) { return; } cv::Canny(src, this->frame, 50, 200); cv::flip(this->frame, this->frame, 1); vector<ofVec2f> edges; for (int y = 0; y < this->frame.rows; y += 3) { for (int x = 0; x < this->frame.cols; x += 3) { if (this->frame.at<unsigned char>(y, x) > 0) { edges.push_back(ofVec2f(x, y)); } } } this->fbo.begin(); ofClear(0); for (int i = 0; i < this->particles.size(); i++) { this->particles[i]->update(); for (ofVec2f edge : edges) { float distance = this->particles[i]->get_location().distance(edge); if (distance < 30) { ofSetColor(this->particles[i]->get_body_color(), ofMap(distance, 0, 30, 255, 0)); ofDrawLine(this->particles[i]->get_location(), edge); } } } //this->image.update(); //this->image.draw(0, 0); this->fbo.end(); } //-------------------------------------------------------------- void ofApp::draw() { this->fbo.draw(0, 0); } //======================================================================== int main() { ofSetupOpenGL(640, 480, OF_WINDOW); ofRunApp(new ofApp()); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#pragma once #include "ofMain.h" class Particle { public: Particle(); ~Particle() {}; void apply_force(ofVec2f force); void update(); void draw(); void set_color(ofColor body_color); ofVec2f get_location(); ofColor get_body_color(); private: ofVec2f location; ofVec2f velocity; ofVec2f acceleration; ofColor body_color; }; |
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 |
#include "Particle.h" Particle::Particle() { this->location = ofVec2f(ofRandom(ofGetWidth()), ofRandom(ofGetHeight())); this->velocity = ofVec2f(ofRandom(-3, 3), ofRandom(-3, 3)); this->velocity = this->velocity.normalized() * ofRandom(3, 5); this->acceleration = ofVec2f(); this->body_color.setHsb(ofRandom(255), 255, 255); } void Particle::apply_force(ofVec2f force) { this->acceleration += force; } void Particle::update() { this->acceleration += this->velocity; this->location += this->acceleration; this->acceleration *= 0; if (this->location.x < 0) { this->location.x = 0; this->velocity.x *= -1; } else if (this->location.x > ofGetWidth()) { this->location.x = ofGetWidth(); this->velocity.x *= -1; } if (this->location.y < 0) { this->location.y = 0; this->velocity.y *= -1; } else if (this->location.y > ofGetHeight()) { this->location.y = ofGetHeight(); this->velocity.y *= -1; } } void Particle::draw() { ofSetColor(this->body_color); ofDrawCircle(this->location, 3); } void Particle::set_color(ofColor body_color) { this->body_color = body_color; } ofVec2f Particle::get_location() { return this->location; } ofColor Particle::get_body_color() { return this->body_color; } |