[ Video ]
[ About ]
WebCameraの映像をMaskしました。
背景の描写を止めてあるので、Mask部分以外には古い情報が残ります。
Mask to WebCamera input.
[ 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 |
#pragma once #include "ofMain.h" #include "ofFbo.h" // OpenCV 3.3.1 #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::Mat cap_frame; ofImage cap_image; ofFbo fbo; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofBackground(0); ofSetBackgroundAuto(false); ofSetWindowTitle("Insta"); ofSetLineWidth(5); ofSetRectMode(ofRectMode::OF_RECTMODE_CENTER); this->cap.open(1); this->cap_image.allocate(this->cap.get(CV_CAP_PROP_FRAME_WIDTH), this->cap.get(CV_CAP_PROP_FRAME_HEIGHT), OF_IMAGE_COLOR); this->cap_frame = cv::Mat(this->cap_image.getHeight(), this->cap_image.getWidth(), CV_MAKETYPE(CV_8UC3, this->cap_image.getPixels().getNumChannels()), this->cap_image.getPixels().getData(), 0); this->fbo.allocate(this->cap.get(CV_CAP_PROP_FRAME_WIDTH), this->cap.get(CV_CAP_PROP_FRAME_HEIGHT)); } //-------------------------------------------------------------- void ofApp::update() { this->cap >> this->cap_frame; cv::cvtColor(this->cap_frame, this->cap_frame, cv::COLOR_BGR2RGB); cv::flip(this->cap_frame, this->cap_frame, 1); this->fbo.begin(); ofClear(0, 0); ofDrawRectangle(ofGetMouseX() - (ofGetWidth() - this->cap_image.getWidth()) / 2, ofGetMouseY() - (ofGetHeight() - this->cap_image.getHeight()) / 2, 300, 300); this->fbo.end(); this->cap_image.getTextureReference().setAlphaMask(this->fbo.getTexture()); this->cap_image.update(); } //-------------------------------------------------------------- void ofApp::draw() { this->cap_image.draw(ofGetWidth() / 2, ofGetHeight() / 2 ); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |