Last active
September 22, 2019 23:41
-
-
Save prisonerjohn/ef90fb5ad3487fab711470119cab5949 to your computer and use it in GitHub Desktop.
Object Tracking
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(640, 480, 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() | |
{ | |
ofSetWindowShape(640, 480); | |
// Setup the grabber. | |
grabber.setup(640, 480); | |
// Setup the contour finder and parameters. | |
contourFinder.setUseTargetColor(true); | |
colorTarget.set("Color Target", ofColor(255, 0, 0)); | |
colorOffset.set("Color Offset", 10, 0, 255); | |
// Setup the gui. | |
guiPanel.setup("Color Tracker", "settings.json"); | |
guiPanel.add(colorTarget); | |
guiPanel.add(colorOffset); | |
} | |
void ofApp::update() | |
{ | |
grabber.update(); | |
if (grabber.isFrameNew()) | |
{ | |
// Update parameters. | |
contourFinder.setTargetColor(colorTarget); | |
contourFinder.setThreshold(colorOffset); | |
// Find contours. | |
contourFinder.findContours(grabber); | |
} | |
} | |
void ofApp::draw() | |
{ | |
ofSetColor(255); | |
// Draw the grabber image. | |
grabber.draw(0, 0, ofGetWidth(), ofGetHeight()); | |
// Draw the found contours. | |
contourFinder.draw(); | |
// Draw the gui. | |
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; | |
ofxCv::ContourFinder contourFinder; | |
ofParameter<ofColor> colorTarget; | |
ofParameter<int> colorOffset; | |
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() | |
{ | |
ofSetWindowShape(640, 480); | |
// Setup the grabber. | |
grabber.setup(640, 480); | |
// Setup the contour finder and parameters. | |
contourFinder.setUseTargetColor(true); | |
colorTarget.set("Color Target", ofColor(255, 0, 0)); | |
colorOffset.set("Color Offset", 10, 0, 255); | |
// Setup the gui. | |
guiPanel.setup("Color Tracker", "settings.json"); | |
guiPanel.add(colorTarget); | |
guiPanel.add(colorOffset); | |
} | |
void ofApp::update() | |
{ | |
grabber.update(); | |
if (grabber.isFrameNew()) | |
{ | |
processImg.setFromPixels(grabber.getPixels()); | |
// Save the color of the pixel under the mouse. | |
colorUnderMouse = processImg.getColor(ofGetMouseX(), ofGetMouseY()); | |
// Update parameters. | |
contourFinder.setTargetColor(colorTarget, ofxCv::TRACK_COLOR_HSV); | |
contourFinder.setThreshold(colorOffset); | |
// Find contours. | |
contourFinder.findContours(processImg); | |
} | |
} | |
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. | |
ofPushStyle(); | |
ofSetColor(colorUnderMouse); | |
ofDrawRectangle(ofGetMouseX() - 25, ofGetMouseY() - 25, 50, 50); | |
ofNoFill(); | |
ofSetColor(colorUnderMouse.getInverted()); | |
ofDrawRectangle(ofGetMouseX() - 25, ofGetMouseY() - 25, 50, 50); | |
ofPopStyle(); | |
// Draw the gui. | |
guiPanel.draw(); | |
} | |
void ofApp::mousePressed(int x, int y, int button) | |
{ | |
if (!guiPanel.getShape().inside(x, y)) | |
{ | |
// 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; | |
ofImage processImg; | |
ofxCv::ContourFinder contourFinder; | |
ofParameter<ofColor> colorTarget; | |
ofParameter<int> colorOffset; | |
ofColor colorUnderMouse; | |
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() | |
{ | |
ofSetWindowShape(640, 480); | |
// Setup the grabber. | |
grabber.setup(640, 480); | |
// Setup the contour finder and parameters. | |
contourFinder.setUseTargetColor(true); | |
colorTarget.set("Color Target", ofColor(255, 0, 0)); | |
colorOffset.set("Color Offset", 10, 0, 255); | |
minArea.set("Min Area", 0.01f, 0, 0.5f); | |
maxArea.set("Max Area", 0.05f, 0, 0.5f); | |
// Setup the gui. | |
guiPanel.setup("Color Tracker", "settings.json"); | |
guiPanel.add(colorTarget); | |
guiPanel.add(colorOffset); | |
guiPanel.add(minArea); | |
guiPanel.add(maxArea); | |
} | |
void ofApp::update() | |
{ | |
grabber.update(); | |
if (grabber.isFrameNew()) | |
{ | |
processImg.setFromPixels(grabber.getPixels()); | |
// Save the color of the pixel under the mouse. | |
colorUnderMouse = processImg.getColor(ofGetMouseX(), ofGetMouseY()); | |
// Update parameters. | |
contourFinder.setTargetColor(colorTarget, ofxCv::TRACK_COLOR_HSV); | |
contourFinder.setThreshold(colorOffset); | |
contourFinder.setMinAreaNorm(minArea); | |
contourFinder.setMaxAreaNorm(maxArea); | |
// Find contours. | |
contourFinder.findContours(processImg); | |
} | |
} | |
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. | |
ofPushStyle(); | |
ofSetColor(colorUnderMouse); | |
ofDrawRectangle(ofGetMouseX() - 25, ofGetMouseY() - 25, 50, 50); | |
ofNoFill(); | |
ofSetColor(colorUnderMouse.getInverted()); | |
ofDrawRectangle(ofGetMouseX() - 25, ofGetMouseY() - 25, 50, 50); | |
ofPopStyle(); | |
// Draw the gui. | |
guiPanel.draw(); | |
} | |
void ofApp::mousePressed(int x, int y, int button) | |
{ | |
if (!guiPanel.getShape().inside(x, y)) | |
{ | |
// 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; | |
ofImage processImg; | |
ofxCv::ContourFinder contourFinder; | |
ofParameter<ofColor> colorTarget; | |
ofParameter<int> colorOffset; | |
ofColor colorUnderMouse; | |
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" | |
void ofApp::setup() | |
{ | |
ofSetWindowShape(640, 480); | |
// Setup the grabber. | |
grabber.setup(640, 480); | |
// Setup the contour finder and parameters. | |
contourFinder.setUseTargetColor(true); | |
colorTarget.set("Color Target", ofColor(255, 0, 0)); | |
colorOffset.set("Color Offset", 10, 0, 255); | |
minArea.set("Min Area", 0.01f, 0, 0.5f); | |
maxArea.set("Max Area", 0.05f, 0, 0.5f); | |
blurAmount.set("Blur Amount", 0, 0, 100); | |
debugProcess.set("Debug Process", false); | |
// Setup the gui. | |
guiPanel.setup("Color Tracker", "settings.json"); | |
guiPanel.add(colorTarget); | |
guiPanel.add(colorOffset); | |
guiPanel.add(minArea); | |
guiPanel.add(maxArea); | |
guiPanel.add(blurAmount); | |
guiPanel.add(debugProcess); | |
} | |
void ofApp::update() | |
{ | |
grabber.update(); | |
if (grabber.isFrameNew()) | |
{ | |
processImg.setFromPixels(grabber.getPixels()); | |
// Filter the image. | |
if (blurAmount > 0) | |
{ | |
//ofxCv::blur(processImg, blurAmount); | |
//ofxCv::GaussianBlur(processImg, blurAmount); | |
ofxCv::medianBlur(processImg, blurAmount); | |
processImg.update(); | |
} | |
// Save the color of the pixel under the mouse. | |
colorUnderMouse = processImg.getColor(ofGetMouseX(), ofGetMouseY()); | |
// Update parameters. | |
contourFinder.setTargetColor(colorTarget, ofxCv::TRACK_COLOR_HSV); | |
contourFinder.setThreshold(colorOffset); | |
contourFinder.setMinAreaNorm(minArea); | |
contourFinder.setMaxAreaNorm(maxArea); | |
// Find contours. | |
contourFinder.findContours(processImg); | |
} | |
} | |
void ofApp::draw() | |
{ | |
ofSetColor(255); | |
if (debugProcess) | |
{ | |
// Draw the process image. | |
processImg.draw(0, 0, ofGetWidth(), ofGetHeight()); | |
} | |
else | |
{ | |
// Draw the grabber image. | |
grabber.draw(0, 0, ofGetWidth(), ofGetHeight()); | |
} | |
// Draw the found contours. | |
contourFinder.draw(); | |
// Draw the color under the mouse. | |
ofPushStyle(); | |
ofSetColor(colorUnderMouse); | |
ofDrawRectangle(ofGetMouseX() - 25, ofGetMouseY() - 25, 50, 50); | |
ofNoFill(); | |
ofSetColor(colorUnderMouse.getInverted()); | |
ofDrawRectangle(ofGetMouseX() - 25, ofGetMouseY() - 25, 50, 50); | |
ofPopStyle(); | |
// Draw the gui. | |
guiPanel.draw(); | |
} | |
void ofApp::mousePressed(int x, int y, int button) | |
{ | |
if (!guiPanel.getShape().inside(x, y)) | |
{ | |
// 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; | |
ofImage processImg; | |
ofxCv::ContourFinder contourFinder; | |
ofParameter<ofColor> colorTarget; | |
ofParameter<int> colorOffset; | |
ofColor colorUnderMouse; | |
ofParameter<float> minArea; | |
ofParameter<float> maxArea; | |
ofParameter<int> blurAmount; | |
ofParameter<bool> debugProcess; | |
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() | |
{ | |
ofSetWindowShape(640, 480); | |
// Setup the grabber. | |
grabber.setup(640, 480); | |
// Setup the contour finder and parameters. | |
contourFinder.setUseTargetColor(true); | |
colorTarget.set("Color Target", ofColor(255, 0, 0)); | |
colorOffset.set("Color Offset", 10, 0, 255); | |
minArea.set("Min Area", 0.01f, 0, 0.5f); | |
maxArea.set("Max Area", 0.05f, 0, 0.5f); | |
blurAmount.set("Blur Amount", 0, 0, 100); | |
erodeIterations.set("Erode Iterations", 0, 0, 10); | |
debugProcess.set("Debug Process", false); | |
// Setup the gui. | |
guiPanel.setup("Color Tracker", "settings.json"); | |
guiPanel.add(colorTarget); | |
guiPanel.add(colorOffset); | |
guiPanel.add(minArea); | |
guiPanel.add(maxArea); | |
guiPanel.add(blurAmount); | |
guiPanel.add(erodeIterations); | |
guiPanel.add(debugProcess); | |
} | |
void ofApp::update() | |
{ | |
grabber.update(); | |
if (grabber.isFrameNew()) | |
{ | |
processImg.setFromPixels(grabber.getPixels()); | |
// Filter the image. | |
if (blurAmount > 0) | |
{ | |
//ofxCv::blur(processImg, blurAmount); | |
//ofxCv::GaussianBlur(processImg, blurAmount); | |
ofxCv::medianBlur(processImg, blurAmount); | |
} | |
if (erodeIterations > 0) | |
{ | |
ofxCv::erode(processImg, erodeIterations.get()); | |
} | |
processImg.update(); | |
// Save the color of the pixel under the mouse. | |
colorUnderMouse = processImg.getColor(ofGetMouseX(), ofGetMouseY()); | |
// Update parameters. | |
contourFinder.setTargetColor(colorTarget, ofxCv::TRACK_COLOR_HSV); | |
contourFinder.setThreshold(colorOffset); | |
contourFinder.setMinAreaNorm(minArea); | |
contourFinder.setMaxAreaNorm(maxArea); | |
// Find contours. | |
contourFinder.findContours(processImg); | |
} | |
} | |
void ofApp::draw() | |
{ | |
ofSetColor(255); | |
if (debugProcess) | |
{ | |
// Draw the process image. | |
processImg.draw(0, 0, ofGetWidth(), ofGetHeight()); | |
} | |
else | |
{ | |
// Draw the grabber image. | |
grabber.draw(0, 0, ofGetWidth(), ofGetHeight()); | |
} | |
// Draw the found contours. | |
contourFinder.draw(); | |
// Draw the color under the mouse. | |
ofPushStyle(); | |
ofSetColor(colorUnderMouse); | |
ofDrawRectangle(ofGetMouseX() - 25, ofGetMouseY() - 25, 50, 50); | |
ofNoFill(); | |
ofSetColor(colorUnderMouse.getInverted()); | |
ofDrawRectangle(ofGetMouseX() - 25, ofGetMouseY() - 25, 50, 50); | |
ofPopStyle(); | |
// Draw the gui. | |
guiPanel.draw(); | |
} | |
void ofApp::mousePressed(int x, int y, int button) | |
{ | |
if (!guiPanel.getShape().inside(x, y)) | |
{ | |
// 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; | |
ofImage processImg; | |
ofxCv::ContourFinder contourFinder; | |
ofParameter<ofColor> colorTarget; | |
ofParameter<int> colorOffset; | |
ofColor colorUnderMouse; | |
ofParameter<float> minArea; | |
ofParameter<float> maxArea; | |
ofParameter<int> blurAmount; | |
ofParameter<int> erodeIterations; | |
ofParameter<bool> debugProcess; | |
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() | |
{ | |
ofSetWindowShape(640, 480); | |
// Setup the grabber. | |
grabber.setup(640, 480); | |
// Setup the contour finder and parameters. | |
contourFinder.setUseTargetColor(true); | |
colorTarget.set("Color Target", ofColor(255, 0, 0)); | |
colorOffset.set("Color Offset", 10, 0, 255); | |
minArea.set("Min Area", 0.01f, 0, 0.5f); | |
maxArea.set("Max Area", 0.05f, 0, 0.5f); | |
blurAmount.set("Blur Amount", 0, 0, 100); | |
erodeIterations.set("Erode Iterations", 0, 0, 10); | |
persistence.set("Persistence", 15, 0, 60); | |
maxDistance.set("Max Distance", 64, 0, 640); | |
showLabels.set("Show Labels", false); | |
debugProcess.set("Debug Process", false); | |
// Setup the gui. | |
guiPanel.setup("Color Tracker", "settings.json"); | |
guiPanel.add(colorTarget); | |
guiPanel.add(colorOffset); | |
guiPanel.add(minArea); | |
guiPanel.add(maxArea); | |
guiPanel.add(blurAmount); | |
guiPanel.add(erodeIterations); | |
guiPanel.add(persistence); | |
guiPanel.add(maxDistance); | |
guiPanel.add(showLabels); | |
guiPanel.add(debugProcess); | |
} | |
void ofApp::update() | |
{ | |
grabber.update(); | |
if (grabber.isFrameNew()) | |
{ | |
processImg.setFromPixels(grabber.getPixels()); | |
// Filter the image. | |
if (blurAmount > 0) | |
{ | |
//ofxCv::blur(processImg, blurAmount); | |
//ofxCv::GaussianBlur(processImg, blurAmount); | |
ofxCv::medianBlur(processImg, blurAmount); | |
} | |
if (erodeIterations > 0) | |
{ | |
ofxCv::erode(processImg, erodeIterations.get()); | |
} | |
processImg.update(); | |
// Save the color of the pixel under the mouse. | |
colorUnderMouse = processImg.getColor(ofGetMouseX(), ofGetMouseY()); | |
// Update parameters. | |
contourFinder.setTargetColor(colorTarget, ofxCv::TRACK_COLOR_HSV); | |
contourFinder.setThreshold(colorOffset); | |
contourFinder.setMinAreaNorm(minArea); | |
contourFinder.setMaxAreaNorm(maxArea); | |
contourFinder.getTracker().setPersistence(persistence); | |
contourFinder.getTracker().setMaximumDistance(maxDistance); | |
// Find contours. | |
contourFinder.findContours(processImg); | |
} | |
} | |
void ofApp::draw() | |
{ | |
ofSetColor(255); | |
if (debugProcess) | |
{ | |
// Draw the process image. | |
processImg.draw(0, 0, ofGetWidth(), ofGetHeight()); | |
} | |
else | |
{ | |
// Draw the grabber image. | |
grabber.draw(0, 0, ofGetWidth(), ofGetHeight()); | |
} | |
// Draw the found contours. | |
contourFinder.draw(); | |
if (showLabels) | |
{ | |
ofxCv::RectTracker& tracker = contourFinder.getTracker(); | |
ofSetColor(255); | |
for (int i = 0; i < contourFinder.size(); i++) | |
{ | |
ofPoint center = ofxCv::toOf(contourFinder.getCenter(i)); | |
int label = contourFinder.getLabel(i); | |
string msg = ofToString(label) + ":" + ofToString(tracker.getAge(label)); | |
ofDrawBitmapString(msg, center.x, center.y); | |
ofVec2f velocity = ofxCv::toOf(contourFinder.getVelocity(i)); | |
ofDrawLine(center.x, center.y, center.x + velocity.x, center.y + velocity.y); | |
} | |
} | |
// Draw the color under the mouse. | |
ofPushStyle(); | |
ofSetColor(colorUnderMouse); | |
ofDrawRectangle(ofGetMouseX() - 25, ofGetMouseY() - 25, 50, 50); | |
ofNoFill(); | |
ofSetColor(colorUnderMouse.getInverted()); | |
ofDrawRectangle(ofGetMouseX() - 25, ofGetMouseY() - 25, 50, 50); | |
ofPopStyle(); | |
// Draw the gui. | |
guiPanel.draw(); | |
} | |
void ofApp::mousePressed(int x, int y, int button) | |
{ | |
if (!guiPanel.getShape().inside(x, y)) | |
{ | |
// 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; | |
ofImage processImg; | |
ofxCv::ContourFinder contourFinder; | |
ofParameter<ofColor> colorTarget; | |
ofParameter<int> colorOffset; | |
ofColor colorUnderMouse; | |
ofParameter<float> minArea; | |
ofParameter<float> maxArea; | |
ofParameter<int> blurAmount; | |
ofParameter<int> erodeIterations; | |
ofParameter<int> persistence; | |
ofParameter<float> maxDistance; | |
ofParameter<bool> showLabels; | |
ofParameter<bool> debugProcess; | |
ofxPanel guiPanel; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment