[ Video ]
[ About ]
動画の3フレームをシェーダーへ送信。各フレームからRGBの1要素のみを抽出して合成。
Send 3 frames to shader. Only one elements of RGB is extracted from each frame and synthesized.
[ 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 |
#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 mouseEntered(int x, int y) {}; void mouseExited(int x, int y) {}; void windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; cv::VideoCapture cap; cv::Size cap_size; vector<cv::Mat> frames; vector<ofImage> images; ofFbo src; ofFbo dst; ofShader shader; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(30); ofSetWindowTitle("openframeworks"); this->cap.open("image32.mp4"); this->cap_size = cv::Size(1280, 720); cv::Mat cap_frame; while(true){ this->cap >> cap_frame; if (cap_frame.empty()) { break; } ofImage image; image.allocate(this->cap_size.width, this->cap_size.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::resize(cap_frame, cap_frame, this->cap_size); cv::cvtColor(cap_frame, cap_frame, CV_BGR2RGB); cap_frame.copyTo(frame); this->frames.push_back(frame); this->images.push_back(image); } this->src.allocate(ofGetWidth(), ofGetHeight()); this->dst.allocate(ofGetWidth(), ofGetHeight()); this->shader.load("shader/shader.vert", "shader/shader.frag"); } //-------------------------------------------------------------- void ofApp::update() { } //-------------------------------------------------------------- void ofApp::draw() { int frame_number = ofGetFrameNum() % this->images.size(); this->dst.begin(); ofClear(0); this->shader.begin(); this->shader.setUniform1f("time", ofGetElapsedTimef()); this->shader.setUniform2f("resolution", ofGetWidth(), ofGetHeight()); this->shader.setUniformTexture("tex0", this->images[(frame_number + 0) % this->images.size()], 1); this->shader.setUniformTexture("tex1", this->images[(frame_number + 5) % this->images.size()], 2); this->shader.setUniformTexture("tex2", this->images[(frame_number + 10) % this->images.size()], 3); this->src.draw(0, 0); this->shader.end(); this->dst.end(); ofSetColor(255); this->dst.draw(0, 0); } //-------------------------------------------------------------- int main() { ofGLWindowSettings settings; settings.setGLVersion(3, 2); settings.setSize(1280, 720); ofCreateWindow(settings); ofRunApp(new ofApp()); } |
1 2 3 4 5 6 7 8 9 10 11 12 |
#version 150 uniform mat4 modelViewProjectionMatrix; in vec4 position; in vec2 texcoord; out vec2 texCoordVarying; void main(){ texCoordVarying = texcoord; gl_Position = modelViewProjectionMatrix * position; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#version 150 uniform float time; uniform vec2 resolution; uniform sampler2DRect tex0; uniform sampler2DRect tex1; uniform sampler2DRect tex2; in vec2 texCoordVarying; out vec4 outputColor; void main() { vec2 p = (gl_FragCoord.xy * 2.0 - resolution) / min(resolution.x, resolution.y); float r = texture(tex2, texCoordVarying).r; float g = texture(tex1, texCoordVarying).g; float b = texture(tex0, texCoordVarying).b; outputColor = vec4(r, g, b, 1.0); } |