[ Video ]
[ About ]
お花見日和だったので、散歩に出かけたら4時間ほど経っていました(笑)
近所の公園でとった動画をOpenCVで加工・動画出力、openFrameworksで描写しています。
[ 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 |
#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::VideoWriter writer; cv::Size cap_size; static const int frame_number = 60; cv::Mat save_frame[frame_number]; cv::Mat blend_frame; ofImage blend_image; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofBackground(255); ofSetWindowTitle("Insta"); this->cap.open("D:\\video\\MOV_0506.mp4"); int fourcc = (int)this->cap.get(CV_CAP_PROP_FOURCC); double fps = this->cap.get(CV_CAP_PROP_FPS); cv::Size cap_size = cv::Size(this->cap.get(CV_CAP_PROP_FRAME_WIDTH), this->cap.get(CV_CAP_PROP_FRAME_HEIGHT)); this->writer = cv::VideoWriter("D:\\video\\out.mp4", fourcc, fps, cap_size); this->blend_image.allocate(cap_size.width, cap_size.height, OF_IMAGE_COLOR); this->blend_frame = cv::Mat(this->blend_image.getHeight(), this->blend_image.getWidth(), CV_MAKETYPE(CV_8UC3, this->blend_image.getPixels().getNumChannels()), this->blend_image.getPixels().getData(), 0); } //-------------------------------------------------------------- void ofApp::update() { cv::Mat src; this->cap >> src; if (src.empty()) { return; } cv::cvtColor(src, src, CV_RGB2BGR); if (ofGetFrameNum() < this->frame_number) { for (int i = ofGetFrameNum() - 1; i > 0; i--) { this->save_frame[i - 1].copyTo(this->save_frame[i]); } src.copyTo(this->save_frame[0]); } else { for (int i = this->frame_number - 1; i > 0; i--) { this->save_frame[i - 1].copyTo(this->save_frame[i]); } src.copyTo(this->save_frame[0]); } if (ofGetFrameNum() > this->frame_number) { for (int y = 0; y < this->blend_frame.rows; y += 1) { for (int x = 0; x < this->blend_frame.cols; x += 1) { int index = ofNoise(x * 0.003, y * 0.003, ofGetFrameNum() * 0.03) * this->frame_number; this->blend_frame.at<cv::Vec3b>(y, x) = this->save_frame[index].at<cv::Vec3b>(y, x); } } this->blend_image.update(); cv::cvtColor(blend_frame, blend_frame, CV_RGB2BGR); writer << blend_frame; } } //-------------------------------------------------------------- void ofApp::draw() { if (ofGetFrameNum() > this->frame_number) { this->blend_image.draw(0, 0); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(1920, 1080, OF_WINDOW); ofRunApp(new ofApp()); } |