#include "ofApp.h" #define VIDEO_WIDTH 360 #define VIDEO_HEIGHT 640 //-------------------------------------------------------------- void ofApp::setup(){ vidPlayer.load("video.mkv"); // in /bin/data/ vidPlayer.play(); vidPlayer.setLoopState(OF_LOOP_NORMAL); colorVideo.allocate(VIDEO_WIDTH, VIDEO_HEIGHT); grayVideo.allocate(VIDEO_WIDTH, VIDEO_HEIGHT); learnBackground = true; gui.setup("Parameter"); gui.add(threshhold.set("Threshhold", 80, 0, 255)); } //-------------------------------------------------------------- void ofApp::update(){ vidPlayer.update(); colorVideo.setFromPixels(vidPlayer.getPixels()); grayVideo = colorVideo; // Convert Color to Grayscale //cvInRange(); //colorVideo.convertToGrayscalePlanarImage(red, 0); if (learnBackground){ recognizer.setBackground(grayVideo); learnBackground = false; } recognizer.setThreshhold(threshhold); recognizer.update(grayVideo, VIDEO_WIDTH, VIDEO_HEIGHT); background = recognizer.background; subtracted = recognizer.subtracted; binarized = recognizer.binarized; contourFinder = recognizer.contourFinder; } //-------------------------------------------------------------- void ofApp::draw(){ ofSetHexColor(0xffffff); colorVideo.draw(20, 20); grayVideo.draw(40 + VIDEO_WIDTH, 20); subtracted.draw(60 + 2 * VIDEO_WIDTH, 20); //binarized.draw(80 + 3 * VIDEO_WIDTH, 20); //colorVideo.draw(20, 20); //grayVideo.draw(40 + VIDEO_WIDTH, 20); //background.draw(20, 40 + VIDEO_HEIGHT); //binarized.draw(40 + VIDEO_WIDTH, 40 + VIDEO_HEIGHT); //red.draw(40 + VIDEO_WIDTH, 20); //green.draw(20, 40 + VIDEO_HEIGHT); //blue.draw(40 + VIDEO_WIDTH, 40 + VIDEO_HEIGHT); ofFill(); ofSetHexColor(0x333333); ofDrawRectangle(80 + 3 * VIDEO_WIDTH, 20, VIDEO_WIDTH, VIDEO_HEIGHT); ofSetHexColor(0xffffff); for (int i = 0; i < contourFinder.blobs.size(); i++) { ofxCvBlob blob = contourFinder.blobs[i]; // draw over the centroid if the blob is a hole ofSetColor(255); // https://docs.opencv.org/master/d1/d32/tutorial_py_contour_properties.html float aspect_ratio = blob.boundingRect.getWidth() / blob.boundingRect.getHeight(); stringstream sizeStr; sizeStr << std::to_string(i) << ": " << std::to_string(contourFinder.blobs[i].area); sizeStr << " | F=" << aspect_ratio; if (aspect_ratio > 0.5 && aspect_ratio < 1.5) { blob.draw(80 + 3 * VIDEO_WIDTH, 20); ofDrawBitmapString(sizeStr.str(), contourFinder.blobs[i].boundingRect.getCenter().x + 80 + 3 * VIDEO_WIDTH, contourFinder.blobs[i].boundingRect.getCenter().y + 20); } } gui.draw(); } //-------------------------------------------------------------- void ofApp::keyPressed(int key){ if (key == '-') threshhold--; else if (key == '+') threshhold++; else if (key == ' ') learnBackground = true; else if (key == '0') vidPlayer.setPaused(!vidPlayer.isPaused()); else if (key == OF_KEY_RIGHT) vidPlayer.nextFrame(); else if (key == OF_KEY_LEFT) vidPlayer.previousFrame(); else; } //-------------------------------------------------------------- void ofApp::keyReleased(int key){ } //-------------------------------------------------------------- void ofApp::mouseMoved(int x, int y ){ } //-------------------------------------------------------------- void ofApp::mouseDragged(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mousePressed(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mouseReleased(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mouseEntered(int x, int y){ } //-------------------------------------------------------------- void ofApp::mouseExited(int x, int y){ } //-------------------------------------------------------------- void ofApp::windowResized(int w, int h){ } //-------------------------------------------------------------- void ofApp::gotMessage(ofMessage msg){ } //-------------------------------------------------------------- void ofApp::dragEvent(ofDragInfo dragInfo){ }