[ Video ]
[ About ]
OpenCVでWebカメラから目を検出して、事前に保存した画像で上書きをしています。目の向こうの景色が見えるので、不思議な感じに。
[ 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 |
#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; cv::Mat frame; cv::Mat save_frame; ofImage image; cv::CascadeClassifier eye_cascade; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(30); ofBackground(0); ofSetWindowTitle("Insta"); this->cap.open(1); this->cap_size = cv::Size(640, 360); this->eye_cascade.load("opencv-3.3.1\\build\\install\\etc\\haarcascades\\haarcascade_eye.xml"); this->image.allocate(this->cap_size.width, this->cap_size.height, OF_IMAGE_COLOR); this->frame = cv::Mat(this->image.getHeight(), this->image.getWidth(), CV_MAKETYPE(CV_8UC3, this->image.getPixels().getNumChannels()), this->image.getPixels().getData(), 0); } //-------------------------------------------------------------- void ofApp::update() { cv::Mat cap_frame; this->cap >> cap_frame; if (cap_frame.empty()) { return; } cv::resize(cap_frame, this->frame, this->cap_size); cv::flip(this->frame, this->frame, 1); cv::cvtColor(this->frame, this->frame, CV_RGB2BGR); if (ofGetFrameNum() == 0) { this->frame.copyTo(this->save_frame); } vector<cv::Rect> eyes; this->eye_cascade.detectMultiScale(this->frame, eyes); for (int x = 0; x < this->frame.cols; x++) { for (int y = this->frame.rows - 1; y > -1; y--) { ofVec2f point(x, y); for (cv::Rect eye : eyes) { ofVec2f eye_point = ofVec2f(eye.x + eye.size().width / 2, eye.y + eye.size().height / 2); if (eye_point.distance(point) < 35) { this->frame.at<cv::Vec3b>(y, x) = this->save_frame.at<cv::Vec3b>(y, x); } } } } this->image.update(); } //-------------------------------------------------------------- void ofApp::draw() { this->image.draw(0, 0); } //-------------------------------------------------------------- void ofApp::keyPressed(int key) { if (key == 's') { this->frame.copyTo(this->save_frame); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(640, 360, OF_WINDOW); ofRunApp(new ofApp()); } |