[ Video ]
[ About ]
Day 21 Skate.
Y座標ごとに再生するフレーム番号をノイズで変化。OpenCVで画像処理。
Change the frame number to be play back at the Y coordinate. Image processing with OpenCV.
[ 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 |
#pragma once #include "ofMain.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) {}; cv::VideoCapture cap; cv::Size cap_size; int number_of_frames; vector<cv::Mat> frames; vector<cv::Mat> rect_frames; vector<unique_ptr<ofImage>> rect_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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(30); ofSetWindowTitle("openFrameworks"); this->cap.open("D:\\video\\image42.mp4"); this->cap_size = cv::Size(1280, 720); int height = 2; for (int y = 0; y < this->cap_size.height; y += height) { auto image = make_unique<ofImage>(); image->allocate(this->cap_size.width, height, 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(0, y, this->cap_size.width, height); this->rect_images.push_back(move(image)); this->rect_frames.push_back(frame); this->cv_rects.push_back(rect); } this->number_of_frames = 250; for (int i = 0; i < this->number_of_frames; i++) { 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); this->frames.push_back(frame); } } //-------------------------------------------------------------- void ofApp::update() { for (int i = 0; i < this->rect_images.size(); i++) { int frames_index = ofMap(ofNoise(this->cv_rects[i].y * 0.002, ofGetFrameNum() * 0.01), 0, 1, 0, this->number_of_frames); cv::Mat tmp_box_image(this->frames[frames_index], this->cv_rects[i]); tmp_box_image.copyTo(this->rect_frames[i]); this->rect_images[i]->update(); } } //-------------------------------------------------------------- void ofApp::draw() { for (int i = 0; i < this->rect_images.size(); i++) { this->rect_images[i]->draw(this->cv_rects[i].x, this->cv_rects[i].y); } } //======================================================================== int main() { ofSetupOpenGL(1280, 720, OF_WINDOW); ofRunApp(new ofApp()); } |