[ Video ]
[ About ]
画像として大きく動きがあった場所にオブジェクトを生成させる。
Let the object be generated in the place where there was a large movement as an image.
[ 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 39 40 |
#pragma once #include "ofMain.h" #include "opencv2/opencv.hpp" #include "ofxBox2d.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) {}; cv::VideoCapture cap; cv::Size cap_size; int number_of_frames; vector<cv::Mat> frame_list; float rect_size; vector<cv::Mat> rect_frame_list; vector<unique_ptr<ofImage>> rect_image_list; vector<cv::Rect> cv_rect_list; ofxBox2d box2d; vector<shared_ptr<ofxBox2dRect>> box2d_rect_list; vector<int> box2d_frame_index_list, box2d_rect_index_list; vector<float> box2d_life_list; vector<ofColor> box2d_color_list; ofEasyCam cam; }; |
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(239); ofEnableDepthTest(); ofDisableArbTex(); this->cap_size = cv::Size(640, 360); this->rect_size = 20; for (int x = 0; x < this->cap_size.width; x += this->rect_size) { for (int y = 0; y < this->cap_size.height; y += this->rect_size) { auto image = make_unique<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); cv::Rect rect = cv::Rect(x, y, this->rect_size, this->rect_size); this->rect_image_list.push_back(move(image)); this->cv_rect_list.push_back(rect); this->rect_frame_list.push_back(frame); } } auto file_path = "D:\\MP4\\VN20210917_145135.mp4"; this->cap.open(file_path); this->number_of_frames = 0; while(true) { cv::Mat src, tmp; this->cap >> src; if (src.empty()) { break; } cv::resize(src, tmp, this->cap_size); cv::cvtColor(tmp, tmp, cv::COLOR_BGR2RGB); this->frame_list.push_back(tmp); this->number_of_frames++; } this->box2d.init(); this->box2d.setGravity(0, 50); this->box2d.createBounds(); this->box2d.setFPS(60); this->box2d.registerGrabbing(); } //-------------------------------------------------------------- void ofApp::update() { for (int i = this->box2d_rect_list.size() - 1; i > -1; i--) { this->box2d_life_list[i] -= 3; if (this->box2d_life_list[i] < 0) { this->box2d_rect_list.erase(this->box2d_rect_list.begin() + i); this->box2d_frame_index_list.erase(this->box2d_frame_index_list.begin() + i); this->box2d_rect_index_list.erase(this->box2d_rect_index_list.begin() + i); this->box2d_life_list.erase(this->box2d_life_list.begin() + i); this->box2d_color_list.erase(this->box2d_color_list.begin() + i); } } if (ofGetFrameNum() % 5 == 0) { int frame_index = (int)(ofGetFrameNum() * 0.5) % this->number_of_frames; int next_frame_index = (frame_index + 1) % this->number_of_frames; if (frame_index < next_frame_index) { cv::Mat gap = this->frame_list[frame_index] - this->frame_list[next_frame_index]; vector<ofColor> base_color_list; ofColor color; vector<int> hex_list = { 0x247BA0, 0x70C1B3, 0xB2DBBF, 0xF3FFBD, 0xFF1654 }; for (auto hex : hex_list) { color.setHex(hex); base_color_list.push_back(color); } for (int i = 0; i < this->cv_rect_list.size(); i++) { int x = this->cv_rect_list[i].x + this->rect_size * 0.5; int y = this->cv_rect_list[i].y + this->rect_size * 0.5; cv::Vec3b* gap_value = gap.ptr<cv::Vec3b>(y, 0); cv::Vec3b* color_value = this->frame_list[frame_index].ptr<cv::Vec3b>(y, 0); cv::Vec3b gv = gap_value[x]; cv::Vec3b cv = color_value[x]; if ((gv[0] + gv[1] + gv[2]) > 80) { int rect_index = i; auto box2d_rect = make_shared<ofxBox2dRect>(); box2d_rect->setPhysics(0.5, 0.83, 0.1); box2d_rect->setup(this->box2d.getWorld(), this->cv_rect_list[rect_index].x + 50, this->cv_rect_list[rect_index].y + 30, this->rect_size, this->rect_size); this->box2d_rect_list.push_back(box2d_rect); this->box2d_frame_index_list.push_back(next_frame_index); this->box2d_rect_index_list.push_back(rect_index); this->box2d_life_list.push_back(255); this->box2d_color_list.push_back(base_color_list[ofRandom(base_color_list.size())]); } } } } this->box2d.update(); } //-------------------------------------------------------------- void ofApp::draw() { this->cam.begin(); ofRotateX(180); ofTranslate(ofGetWidth() * -0.5, ofGetHeight() * -0.5); ofSetLineWidth(0.8); for (int i = 0; i < this->rect_frame_list.size(); i++) { ofPushMatrix(); ofTranslate(this->cv_rect_list[i].x + 50, this->cv_rect_list[i].y + 30, 1); ofRotateX(180); int frame_index = (int)(ofGetFrameNum() * 0.5) % this->number_of_frames; cv::Mat tmp_box_image(this->frame_list[frame_index], this->cv_rect_list[i]); tmp_box_image.copyTo(this->rect_frame_list[i]); this->rect_image_list[i]->update(); this->rect_image_list[i]->getTexture().bind(); ofSetColor(255); ofFill(); ofDrawPlane(glm::vec3(), this->rect_size, this->rect_size); this->rect_image_list[i]->unbind(); ofSetColor(39); ofNoFill(); ofDrawPlane(glm::vec3(), this->rect_size, this->rect_size); ofPopMatrix(); } for (int i = 0; i < this->box2d_rect_list.size(); i++) { ofPushMatrix(); ofTranslate(this->box2d_rect_list[i]->getPosition().x, this->box2d_rect_list[i]->getPosition().y, -1); ofRotateX(180); ofRotate(this->box2d_rect_list[i]->getRotation()); ofSetColor(this->box2d_color_list[i], this->box2d_life_list[i] > 128 ? 192 : ofMap(this->box2d_life_list[i], 128, 0, 192, 0)); ofFill(); ofDrawPlane(glm::vec3(), this->rect_size, this->rect_size); /* int frame_index = this->box2d_frame_index_list[i]; int rect_index = this->box2d_rect_index_list[i]; cv::Mat tmp_box_image(this->frame_list[frame_index], this->cv_rect_list[rect_index]); tmp_box_image.copyTo(this->rect_frame_list[rect_index]); this->rect_image_list[rect_index]->update(); this->rect_image_list[rect_index]->getTexture().bind(); ofSetColor(255, this->box2d_life_list[i] > 128 ? 255 : ofMap(this->box2d_life_list[i], 128, 0, 255, 0)); ofFill(); ofDrawPlane(glm::vec3(), this->rect_size, this->rect_size); this->rect_image_list[rect_index]->unbind(); */ ofSetColor(39, this->box2d_life_list[i] > 128 ? 255 : ofMap(this->box2d_life_list[i], 128, 0, 255, 0)); ofNoFill(); ofDrawPlane(glm::vec3(), this->rect_size, this->rect_size); ofPopMatrix(); } ofSetLineWidth(3); ofSetColor(39); ofDrawRectangle(glm::vec3(), 720, 720); this->cam.end(); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |