[ Video ]
[ About ]
Day 7 Sea.
OpenCVで使って動画をグレースケール化(6階調) & エッジ検出させて両方の絵を重ねて表示。
I use OpenCV to grayscale and detect edge. And, I overlay tow images.
[ 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 gray; ofImage gray_image; cv::Mat edge; ofImage edge_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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(0); ofEnableBlendMode(ofBlendMode::OF_BLENDMODE_ADD); this->cap.open("D:\\video\\image15.mp4"); this->cap_size = cv::Size(1280, 720); ofSetFrameRate(this->cap.get(CV_CAP_PROP_FPS)); this->gray_image.allocate(this->cap_size.width, this->cap_size.height, OF_IMAGE_GRAYSCALE); this->gray = cv::Mat(this->gray_image.getHeight(), this->gray_image.getWidth(), CV_MAKETYPE(CV_8U, this->gray_image.getPixels().getNumChannels()), this->gray_image.getPixels().getData(), 0); this->edge_image.allocate(this->cap_size.width, this->cap_size.height,OF_IMAGE_GRAYSCALE); this->edge = cv::Mat(this->edge_image.getHeight(), this->edge_image.getWidth(), CV_MAKETYPE(CV_8U, this->edge_image.getPixels().getNumChannels()), this->edge_image.getPixels().getData(), 0); } //-------------------------------------------------------------- void ofApp::update() { cv::Mat cap_frame, resize_frame; this->cap >> cap_frame; if (cap_frame.empty()) { cap.set(CV_CAP_PROP_POS_FRAMES, 1); return; } cv::resize(cap_frame, resize_frame, this->cap_size); cv::cvtColor(resize_frame, this->gray, CV_RGB2GRAY); this->gray.forEach<uchar>([](uchar &value, const int* position) -> void { value = floor(value / 50) * 50; }); cv::Canny(this->gray, this->edge, 10, 200); this->gray_image.update(); this->edge_image.update(); } //-------------------------------------------------------------- void ofApp::draw() { this->gray_image.draw(0, 0); this->edge_image.draw(0, 0); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(1280, 720, OF_WINDOW); ofRunApp(new ofApp()); } |