[ Video ]
[ About ]
動画を分割してランダムウォーク。
Video split and random walk.
[ 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 |
#pragma once #include "ofMain.h" #include "Actor.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) {}; ofEasyCam cam; ofBoxPrimitive box; cv::VideoCapture cap; cv::Size cap_size; cv::Mat frame; float rect_size; vector<cv::Mat> rect_frames; vector<unique_ptr<ofImage>> rect_images; vector<cv::Rect> cv_rects; vector<glm::vec3> location_list; vector<vector<int>> next_index_list; vector<int> destination_list; vector<unique_ptr<Actor>> actor_list; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(30); ofSetWindowTitle("openFrameworks"); ofBackground(239); ofEnableDepthTest(); ofDisableArbTex(); this->cap.open("D:\\video\\image58.mp4"); this->cap_size = cv::Size(640, 360); this->rect_size = 40; 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_images.push_back(move(image)); this->rect_frames.push_back(frame); this->cv_rects.push_back(rect); } } this->box.set(this->rect_size - 1); for (int x = -140; x <= 140; x += this->rect_size) { for (int y = -140; y <= 140; y += this->rect_size) { for (int z = -140; z <= 140; z += this->rect_size) { this->location_list.push_back(glm::vec3(x, y, z)); } } } for (auto& location : this->location_list) { vector<int> next_index = vector<int>(); int index = -1; for (auto& other : this->location_list) { index++; if (location == other) { continue; } float distance = glm::distance(location, other); if (distance <= this->rect_size) { next_index.push_back(index); } } this->next_index_list.push_back(next_index); } for (int i = 0; i < this->rect_frames.size(); i++) { this->actor_list.push_back(make_unique<Actor>(this->location_list, this->next_index_list, this->destination_list)); } } //-------------------------------------------------------------- void ofApp::update() { cv::Mat src; this->cap >> src; if (src.empty()) { this->cap.set(cv::CAP_PROP_POS_FRAMES, 1); return; } cv::resize(src, this->frame, this->cap_size); cv::cvtColor(this->frame, this->frame, cv::COLOR_BGR2RGB); for (int i = 0; i < this->rect_images.size(); i++) { cv::Mat tmp_box_image(this->frame, this->cv_rects[i]); tmp_box_image.copyTo(this->rect_frames[i]); this->rect_images[i]->update(); } int frame_span = 15; int prev_index_size = 0; if (ofGetFrameNum() % frame_span == 0) { prev_index_size = this->destination_list.size(); } for (auto& actor : this->actor_list) { actor->update(frame_span, this->location_list, this->next_index_list, this->destination_list); } if (prev_index_size != 0) { this->destination_list.erase(this->destination_list.begin(), this->destination_list.begin() + prev_index_size); } } //-------------------------------------------------------------- void ofApp::draw() { this->cam.begin(); for (int i = 0; i < this->rect_images.size(); i++) { ofSetColor(255); ofFill(); this->rect_images[i]->getTexture().bind(); ofDrawBox(this->actor_list[i]->getLocation(), this->rect_size); this->rect_images[i]->getTexture().unbind(); ofSetColor(239); ofNoFill(); ofDrawBox(this->actor_list[i]->getLocation(), this->rect_size); } this->cam.end(); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#pragma once #include "ofMain.h" class Actor { public: Actor(vector<glm::vec3>& location_list, vector<vector<int>>& next_index_list, vector<int>& destination_list); void update(int& frame_span, vector<glm::vec3>& location_list, vector<vector<int>>& next_index_list, vector<int>& destination_list); glm::vec3 getLocation(); private: int select_index; int next_index; glm::vec3 location; }; |
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 |
#include "Actor.h" //-------------------------------------------------------------- Actor::Actor(vector<glm::vec3>& location_list, vector<vector<int>>& next_index_list, vector<int>& destination_list) { this->select_index = ofRandom(location_list.size()); while (true) { auto itr = find(destination_list.begin(), destination_list.end(), this->select_index); if (itr == destination_list.end()) { destination_list.push_back(this->select_index); break; } this->select_index = (this->select_index + 1) % location_list.size(); } this->next_index = this->select_index; } //-------------------------------------------------------------- void Actor::update(int& frame_span, vector<glm::vec3>& location_list, vector<vector<int>>& next_index_list, vector<int>& destination_list) { if (ofGetFrameNum() % frame_span == 0) { auto tmp_index = this->select_index; this->select_index = this->next_index; int retry = next_index_list[this->select_index].size(); this->next_index = next_index_list[this->select_index][(int)ofRandom(next_index_list[this->select_index].size())]; while (--retry > 0) { auto destination_itr = find(destination_list.begin(), destination_list.end(), this->next_index); if (destination_itr == destination_list.end()) { if (tmp_index != this->next_index) { destination_list.push_back(this->next_index); break; } } this->next_index = next_index_list[this->select_index][(this->next_index + 1) % next_index_list[this->select_index].size()]; } if (retry <= 0) { destination_list.push_back(this->select_index); this->next_index = this->select_index; } } auto param = ofGetFrameNum() % frame_span; auto distance = location_list[this->next_index] - location_list[this->select_index]; this->location = location_list[this->select_index] + distance / frame_span * param; } //-------------------------------------------------------------- glm::vec3 Actor::getLocation() { return this->location; } |
[ Link ]