[ Video ]
[ About ]
バラバラ動画
Divided video.
[ 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 |
#pragma once #include "ofMain.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 windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; ofFbo fbo; bool enable_gravity; ofxBox2d box2d; vector<shared_ptr<ofxBox2dRect>> rects; cv::VideoCapture cap; cv::Size cap_size; float rect_size; vector<cv::Mat> frames; vector<unique_ptr<ofImage>> images; vector<cv::Rect> cv_rects; }; |
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofBackground(39); ofSetWindowTitle("openframeworks"); this->cap.open("D:\\video\\image.mp4"); this->cap_size = cv::Size(1280, 720); ofSetFrameRate(this->cap.get(CV_CAP_PROP_FPS)); this->box2d.init(); this->box2d.setGravity(0, 0); this->box2d.createBounds(); this->box2d.setFPS(30); this->box2d.registerGrabbing(); this->rect_size = 30; for (int x = 100; x < this->cap_size.width - 100; x += this->rect_size) { for (int y = 100; y < this->cap_size.height - 100; y += this->rect_size) { ofPoint start_point(x + ofGetWidth() * 0.5 - this->cap_size.width * 0.5, y + ofGetHeight() * 0.5 - this->cap_size.height * 0.5); this->rects.push_back(shared_ptr<ofxBox2dRect>(new ofxBox2dRect)); this->rects.back().get()->setPhysics(1.0, 0.63, 0.1); this->rects.back().get()->setup(this->box2d.getWorld(), start_point.x, start_point.y, this->rect_size, this->rect_size); unique_ptr<ofImage> image(new ofImage()); image->allocate(this->rect_size, this->rect_size, OF_IMAGE_COLOR); cv::Mat frame = cv::Mat(cv::Size(image->getWidth(), image->getHeight()), CV_MAKETYPE(CV_8UC3, image->getPixels().getNumChannels()), image->getPixels().getData(), 0); this->images.push_back(std::move(image)); this->frames.push_back(frame); cv::Rect rect = cv::Rect(x, y, this->rect_size, this->rect_size); this->cv_rects.push_back(rect); } } this->fbo.allocate(ofGetWidth(), ofGetHeight()); } //-------------------------------------------------------------- void ofApp::update() { cv::Mat src, frame; this->cap >> src; if (src.empty()) { cap.set(CV_CAP_PROP_POS_FRAMES, 1); return; } cv::resize(src, frame, this->cap_size); cv::cvtColor(frame, frame, CV_BGR2RGB); for (int i = 0; i < this->rects.size(); i++) { cv::Mat tmp_box_image(frame, this->cv_rects[i]); tmp_box_image.copyTo(this->frames[i]); this->images[i]->update(); } if (this->enable_gravity == false) { for (int i = 0; i < this->rects.size(); i++) { for (int j = i + 1; j < this->rects.size(); j++) { float distance = this->rects[i]->getPosition().distance(this->rects[j]->getPosition()); if (distance < 33) { this->rects[i]->addForce(this->rects[i]->getPosition() - this->rects[j]->getPosition(), 0.5); this->rects[j]->addForce(this->rects[j]->getPosition() - this->rects[i]->getPosition(), 0.5); } } } } this->box2d.update(); this->fbo.begin(); ofClear(0); for (int i = 0; i < this->rects.size(); i++) { ofPushMatrix(); ofTranslate(this->rects[i].get()->getPosition().x, this->rects[i].get()->getPosition().y); ofRotate(this->rects[i].get()->getRotation()); this->images[i]->draw(-this->rect_size / 2, -this->rect_size / 2); ofPopMatrix(); } this->fbo.end(); } //-------------------------------------------------------------- void ofApp::draw() { this->fbo.draw(0, 0); } //-------------------------------------------------------------- void ofApp::keyPressed(int key) { if (key == 't') { this->box2d.setGravity(0, 10); this->enable_gravity = true; } else if (key == 'f') { this->box2d.setGravity(0, 0); this->enable_gravity = false; } } //======================================================================== int main() { ofSetupOpenGL(1280, 720, OF_WINDOW); ofRunApp(new ofApp()); } |