[ Video ]
[ About ]
WebカメラとMP4から取得した画像をテキスチャとしてシェーダーへ送信、指定した色を使ってクロマキー合成
Send the texture from web camera and mp4 video to shader. shader does chroma key.
[ 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 |
#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::Size cap_size; cv::VideoCapture cap1; cv::Mat frame1; ofImage image1; cv::VideoCapture cap2; cv::Mat frame2; ofImage image2; 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 78 79 80 81 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); this->cap1.open(1); this->cap2.open("beach.mp4"); this->cap_size = cv::Size(1280, 720); this->image1.allocate(this->cap_size.width, this->cap_size.height, OF_IMAGE_COLOR); this->frame1 = cv::Mat(cv::Size(this->image1.getWidth(), this->image1.getHeight()), CV_MAKETYPE(CV_8UC3, this->image1.getPixels().getNumChannels()), this->image1.getPixels().getData(), 0); this->image2.allocate(this->cap_size.width, this->cap_size.height, OF_IMAGE_COLOR); this->frame2 = cv::Mat(cv::Size(this->image2.getWidth(), this->image2.getHeight()), CV_MAKETYPE(CV_8UC3, this->image2.getPixels().getNumChannels()), this->image2.getPixels().getData(), 0); this->src.allocate(ofGetWidth(), ofGetHeight()); this->dst.allocate(ofGetWidth(), ofGetHeight()); this->shader.load("shader/shader.vert", "shader/shader.frag"); } //-------------------------------------------------------------- void ofApp::update() { cv::Mat src; this->cap1 >> src; if (src.empty()) { this->cap1.set(CV_CAP_PROP_POS_FRAMES, 1); return; } cv::resize(src, this->frame1, this->cap_size); cv::cvtColor(this->frame1, this->frame1, CV_BGR2RGB); cv::flip(this->frame1, this->frame1, 2); this->image1.update(); this->cap2 >> src; if (src.empty()) { this->cap2.set(CV_CAP_PROP_POS_FRAMES, 1); return; } cv::resize(src, this->frame2, this->cap_size); cv::cvtColor(this->frame2, this->frame2, CV_BGR2RGB); this->image2.update(); } //-------------------------------------------------------------- void ofApp::draw() { this->dst.begin(); ofClear(0); this->shader.begin(); this->shader.setUniform1f("time", ofGetElapsedTimef()); this->shader.setUniform2f("resolution", ofGetWidth(), ofGetHeight()); this->shader.setUniform2f("mouse", ofGetMouseX(), ofGetMouseY()); this->shader.setUniformTexture("tex1", this->image1, 1); this->shader.setUniformTexture("tex2", this->image2, 2); this->src.draw(0, 0); this->shader.end(); this->dst.end(); 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 22 23 |
#version 150 uniform float time; uniform vec2 resolution; uniform vec2 mouse; 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); vec4 c1 = texture(tex1, texCoordVarying); vec4 c2 = texture(tex2, texCoordVarying); vec4 m = texture(tex1, mouse); float d = length(c1 - m); outputColor = (d > 0.25) ? c1 : c2; } |
[ Link ]