[ Video ]
[ About ]
フレーム差分に対して着色
Color to frame difference.
[ 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 <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> cap_frames; vector<unique_ptr<ofImage>> images; 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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(30); ofSetWindowTitle("openframeworks"); ofBackground(0); this->cap.open("D:\\video\\image26.mp4"); this->cap_size = cv::Size(1280, 720); cv::Mat cap_frame; while (true) { this->cap >> cap_frame; if (cap_frame.empty()) { break; } cv::resize(cap_frame, cap_frame, this->cap_size); cv::cvtColor(cap_frame, cap_frame, CV_BGR2RGB); this->cap_frames.push_back(cap_frame); unique_ptr<ofImage> image(new ofImage()); image->allocate(this->cap_size.width, this->cap_size.height, OF_IMAGE_COLOR); this->images.push_back(move(image)); } this->blend_image.allocate(this->cap_size.width, this->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() { int blend_count = 5; int frame_span = 1; cv::Mat diff, gray_diff, color_diff; this->blend_frame = this->cap_frames[(ofGetFrameNum() + blend_count * frame_span) % this->cap_frames.size()] * 1.0; for (int i = 0; i < blend_count; i++) { cv::absdiff(this->cap_frames[(ofGetFrameNum() + i * frame_span) % this->cap_frames.size()], this->cap_frames[(ofGetFrameNum() + (i + 1) * frame_span) % this->cap_frames.size()], diff); cv::cvtColor(diff, gray_diff, CV_RGB2GRAY); cv::threshold(gray_diff, gray_diff, 64, 255, CV_THRESH_BINARY); cv::applyColorMap(diff, color_diff, cv::COLORMAP_HSV); /* cv::imshow("diff", diff); cv::imshow("gray_diff", gray_diff); cv::imshow("color_diff", color_diff); cv::waitKey(1); */ cv::add(this->blend_frame, diff, this->blend_frame, gray_diff); } } //-------------------------------------------------------------- void ofApp::draw() { ofSetColor(255); this->blend_image.update(); this->blend_image.draw(0, 0); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(1280, 720, OF_WINDOW); ofRunApp(new ofApp()); } |