Last active
September 16, 2019 02:38
-
-
Save prisonerjohn/95ad94acc37baf167c4f2f15129f737b to your computer and use it in GitHub Desktop.
Sensing Machines OpenCV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofMain.h" | |
#include "ofApp.h" | |
int main() | |
{ | |
ofSetupOpenGL(1280, 720, OF_WINDOW); | |
ofRunApp(new ofApp()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
void ofApp::setup() | |
{ | |
grabber.setup(1280, 720); | |
captureBackground.set("Capture BG", true); | |
briThreshold.set("Bri Thresh", 120, 0, 255); | |
guiPanel.setup("BG Subtraction"); | |
guiPanel.add(captureBackground); | |
guiPanel.add(briThreshold); | |
} | |
void ofApp::update() | |
{ | |
grabber.update(); | |
if (grabber.isFrameNew()) | |
{ | |
// Convert the grabber image to CV space. | |
grabberColorMat = ofxCv::toCv(grabber.getPixels()); | |
// Convert input image to grayscale. | |
ofxCv::copyGray(grabberColorMat, grabberGrayMat); | |
if (captureBackground) | |
{ | |
// Copy input image to background. | |
backgroundMat = grabberGrayMat; | |
captureBackground = false; | |
} | |
// Compute the difference image between the background and grabber. | |
cv::absdiff(backgroundMat, grabberGrayMat, resultMat); | |
// Threshold the difference image. | |
ofxCv::threshold(resultMat, briThreshold); | |
// Convert the result CV image back to OF space. | |
ofxCv::toOf(resultMat, resultImg); | |
// Update the image to draw it. | |
resultImg.update(); | |
} | |
} | |
void ofApp::draw() | |
{ | |
resultImg.draw(0, 0, ofGetWidth(), ofGetHeight()); | |
guiPanel.draw(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
#include "ofxCv.h" | |
#include "ofxGui.h" | |
class ofApp : public ofBaseApp | |
{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
ofVideoGrabber grabber; | |
cv::Mat grabberColorMat; | |
cv::Mat grabberGrayMat; | |
cv::Mat backgroundMat; | |
cv::Mat resultMat; | |
ofImage resultImg; | |
ofParameter<bool> captureBackground; | |
ofParameter<int> briThreshold; | |
ofxPanel guiPanel; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
void ofApp::setup() | |
{ | |
grabber.setup(1280, 720); | |
captureBackground.set("Capture BG", true); | |
briThreshold.set("Bri Thresh", 120, 0, 255); | |
guiPanel.setup("BG Subtraction"); | |
guiPanel.add(captureBackground); | |
guiPanel.add(briThreshold); | |
} | |
void ofApp::update() | |
{ | |
grabber.update(); | |
ofImage grabberColorImg; | |
grabberColorImg.setFromPixels(grabber.getPixels()); | |
// Convert input image to grayscale. | |
ofImage grabberGrayImg; | |
ofxCv::copyGray(grabberColorImg, grabberGrayImg); | |
if (captureBackground) | |
{ | |
// Copy input image to background. | |
backgroundImg = grabberGrayImg; | |
captureBackground = false; | |
} | |
// Compute the difference image between the background and grabber. | |
ofxCv::absdiff(backgroundImg, grabberGrayImg, resultImg); | |
// Threshold the difference image. | |
ofxCv::threshold(resultImg, briThreshold); | |
// Update the image to draw it. | |
resultImg.update(); | |
} | |
void ofApp::draw() | |
{ | |
resultImg.draw(0, 0, ofGetWidth(), ofGetHeight()); | |
guiPanel.draw(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
#include "ofxCv.h" | |
#include "ofxGui.h" | |
class ofApp : public ofBaseApp | |
{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
ofVideoGrabber grabber; | |
ofImage backgroundImg; | |
ofImage resultImg; | |
ofParameter<bool> captureBackground; | |
ofParameter<int> briThreshold; | |
ofxPanel guiPanel; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
void ofApp::setup() | |
{ | |
grabber.setup(1280, 720); | |
contourFinder.setUseTargetColor(true); | |
colorTarget.set("Color Target", ofColor::red); | |
colorOffset.set("Color Offset", 10, 0, 255); | |
minArea.set("Min Area", 60, 0, 10000); | |
maxArea.set("Max Area", 120, 0, 10000); | |
guiPanel.setup("Contour Finding"); | |
guiPanel.add(colorTarget); | |
guiPanel.add(colorOffset); | |
guiPanel.add(minArea); | |
guiPanel.add(maxArea); | |
} | |
void ofApp::update() | |
{ | |
grabber.update(); | |
if (grabber.isFrameNew()) | |
{ | |
// Get the pixel color under the mouse. | |
ofPixels& grabberPixels = grabber.getPixels(); | |
colorUnderMouse = grabberPixels.getColor(mouseX, mouseY); | |
// Convert the grabber pixels to CV space. | |
grabberColorMat = ofxCv::toCv(grabberPixels); | |
// Update parameters. | |
contourFinder.setTargetColor(colorTarget, ofxCv::TRACK_COLOR_HSV); | |
contourFinder.setThreshold(colorOffset); | |
contourFinder.setMinArea(minArea); | |
contourFinder.setMaxArea(maxArea); | |
// Find contours. | |
contourFinder.findContours(grabberColorMat); | |
} | |
} | |
void ofApp::draw() | |
{ | |
ofSetColor(255); | |
// Draw the grabber image. | |
grabber.draw(0, 0, ofGetWidth(), ofGetHeight()); | |
// Draw the found contours. | |
contourFinder.draw(); | |
// Draw the color under the mouse. | |
ofSetColor(colorUnderMouse); | |
ofFill(); | |
ofDrawRectangle(mouseX - 25, mouseY - 25, 50, 50); | |
ofNoFill(); | |
ofSetColor(colorUnderMouse.getInverted()); | |
ofDrawRectangle(mouseX - 25, mouseY - 25, 50, 50); | |
// Draw the gui. | |
guiPanel.draw(); | |
} | |
void ofApp::mousePressed(int x, int y, int button) | |
{ | |
// Track the color under the mouse. | |
colorTarget = colorUnderMouse; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
#include "ofxCv.h" | |
#include "ofxGui.h" | |
class ofApp : public ofBaseApp | |
{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
void mousePressed(int x, int y, int button); | |
ofVideoGrabber grabber; | |
cv::Mat grabberColorMat; | |
ofxCv::ContourFinder contourFinder; | |
ofColor colorUnderMouse; | |
ofParameter<ofColor> colorTarget; | |
ofParameter<int> colorOffset; | |
ofParameter<int> minArea; | |
ofParameter<int> maxArea; | |
ofxPanel guiPanel; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
void ofApp::setup() | |
{ | |
grabber.setup(1280, 720); | |
contourFinder.setUseTargetColor(true); | |
colorTarget.set("Color Target", ofColor::red); | |
colorOffset.set("Color Offset", 10, 0, 255); | |
minArea.set("Min Area", 0.1f, 0, 1.0f); | |
maxArea.set("Max Area", 0.5f, 0, 1.0f); | |
guiPanel.setup("Contour Finding"); | |
guiPanel.add(colorTarget); | |
guiPanel.add(colorOffset); | |
guiPanel.add(minArea); | |
guiPanel.add(maxArea); | |
} | |
void ofApp::update() | |
{ | |
grabber.update(); | |
if (grabber.isFrameNew()) | |
{ | |
// Get the pixel color under the mouse. | |
ofPixels& grabberPixels = grabber.getPixels(); | |
colorUnderMouse = grabberPixels.getColor(mouseX, mouseY); | |
// Convert the grabber pixels to CV space. | |
grabberColorMat = ofxCv::toCv(grabberPixels); | |
// Update parameters. | |
contourFinder.setTargetColor(colorTarget, ofxCv::TRACK_COLOR_HSV); | |
contourFinder.setThreshold(colorOffset); | |
contourFinder.setMinAreaNorm(minArea); | |
contourFinder.setMaxAreaNorm(maxArea); | |
// Find contours. | |
contourFinder.findContours(grabberColorMat); | |
} | |
} | |
void ofApp::draw() | |
{ | |
ofSetColor(255); | |
// Draw the grabber image. | |
grabber.draw(0, 0, ofGetWidth(), ofGetHeight()); | |
// Draw the found contours. | |
contourFinder.draw(); | |
// Draw the color under the mouse. | |
ofSetColor(colorUnderMouse); | |
ofFill(); | |
ofDrawRectangle(mouseX - 25, mouseY - 25, 50, 50); | |
ofNoFill(); | |
ofSetColor(colorUnderMouse.getInverted()); | |
ofDrawRectangle(mouseX - 25, mouseY - 25, 50, 50); | |
// Draw the gui. | |
guiPanel.draw(); | |
} | |
void ofApp::mousePressed(int x, int y, int button) | |
{ | |
// Track the color under the mouse. | |
colorTarget = colorUnderMouse; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
#include "ofxCv.h" | |
#include "ofxGui.h" | |
class ofApp : public ofBaseApp | |
{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
void mousePressed(int x, int y, int button); | |
ofVideoGrabber grabber; | |
cv::Mat grabberColorMat; | |
ofxCv::ContourFinder contourFinder; | |
ofColor colorUnderMouse; | |
ofParameter<ofColor> colorTarget; | |
ofParameter<int> colorOffset; | |
ofParameter<float> minArea; | |
ofParameter<float> maxArea; | |
ofxPanel guiPanel; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
using namespace ofxCv; | |
using namespace cv; | |
void ofApp::setup() { | |
ofSetVerticalSync(true); | |
ofSetFrameRate(120); | |
finder.setup("haarcascade_frontalface_default.xml"); | |
finder.setPreset(ObjectFinder::Fast); | |
cam.setup(640, 480); | |
} | |
void ofApp::update() { | |
cam.update(); | |
if (cam.isFrameNew()) { | |
finder.update(cam); | |
} | |
} | |
void ofApp::draw() { | |
cam.draw(0, 0); | |
finder.draw(); | |
ofDrawBitmapStringHighlight(ofToString(finder.size()), 10, 20); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
#include "ofxCv.h" | |
class ofApp : public ofBaseApp { | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
ofVideoGrabber cam; | |
ofxCv::ObjectFinder finder; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
void ofApp::setup() | |
{ | |
grabber.setup(1280, 720); | |
contourFinder.setUseTargetColor(true); | |
colorTarget.set("Color Target", ofColor::red); | |
colorOffset.set("Color Offset", 10, 0, 255); | |
minArea.set("Min Area", 60, 0, 10000); | |
maxArea.set("Max Area", 120, 0, 10000); | |
guiPanel.setup("Contour Finding"); | |
guiPanel.add(colorTarget); | |
guiPanel.add(colorOffset); | |
guiPanel.add(minArea); | |
guiPanel.add(maxArea); | |
} | |
void ofApp::update() | |
{ | |
grabber.update(); | |
if (grabber.isFrameNew()) | |
{ | |
// Convert the grabber image to CV space. | |
grabberColorMat = ofxCv::toCv(grabber.getPixels()); | |
// Update parameters. | |
contourFinder.setTargetColor(colorTarget); | |
contourFinder.setThreshold(colorOffset); | |
contourFinder.setMinArea(minArea); | |
contourFinder.setMaxArea(maxArea); | |
// Find contours. | |
contourFinder.findContours(grabberColorMat); | |
} | |
} | |
void ofApp::draw() | |
{ | |
grabber.draw(0, 0, ofGetWidth(), ofGetHeight()); | |
contourFinder.draw(); | |
guiPanel.draw(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
#include "ofxCv.h" | |
#include "ofxGui.h" | |
class ofApp : public ofBaseApp | |
{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
ofVideoGrabber grabber; | |
cv::Mat grabberColorMat; | |
ofxCv::ContourFinder contourFinder; | |
ofParameter<ofColor> colorTarget; | |
ofParameter<int> colorOffset; | |
ofParameter<int> minArea; | |
ofParameter<int> maxArea; | |
ofxPanel guiPanel; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment