Skip to content

Instantly share code, notes, and snippets.

@prisonerjohn
Last active September 28, 2020 20:25
Show Gist options
  • Save prisonerjohn/249987b76ba66d33b5ecf1f2d9d904ad to your computer and use it in GitHub Desktop.
Save prisonerjohn/249987b76ba66d33b5ecf1f2d9d904ad to your computer and use it in GitHub Desktop.
Sensing Machines Computer Vision
#include "ofMain.h"
#include "ofApp.h"
int main()
{
ofSetupOpenGL(1280, 720, OF_WINDOW);
ofRunApp(new ofApp());
}
#include "ofApp.h"
void ofApp::setup()
{
grabber.setup(1280, 720);
resultImg.allocate(1280, 720, OF_IMAGE_COLOR);
captureBackground.set("Capture BG", true);
hueThreshold.set("Hue Thresh", 180, 0, 360);
satThreshold.set("Sat Thresh", 127, 0, 255);
briThreshold.set("Bri Thresh", 127, 0, 255);
guiPanel.setup("BG Subtraction");
guiPanel.add(captureBackground);
guiPanel.add(hueThreshold);
guiPanel.add(satThreshold);
guiPanel.add(briThreshold);
}
void ofApp::update()
{
grabber.update();
ofPixels& grabberPix = grabber.getPixels();
if (captureBackground)
{
backgroundImg.setFromPixels(grabber.getPixels());
captureBackground = false;
}
ofPixels& resultPix = resultImg .getPixels();
for (int y = 0; y < grabberPix.getHeight(); y++)
{
for (int x = 0; x < grabberPix.getWidth(); x++)
{
ofColor grabColor = grabberPix.getColor(x, y);
ofColor bgColor = backgroundImg.getColor(x, y);
if (abs(grabColor.getHueAngle() - bgColor.getHueAngle()) > hueThreshold ||
abs(grabColor.getSaturation() - bgColor.getSaturation()) > satThreshold ||
abs(grabColor.getBrightness() - bgColor.getBrightness()) > briThreshold)
{
resultPix.setColor(x, y, grabColor);
}
else
{
resultPix.setColor(x, y, ofColor(0));
}
}
}
resultImg.update();
}
void ofApp::draw()
{
resultImg.draw(0, 0, ofGetWidth(), ofGetHeight());
guiPanel.draw();
}
#pragma once
#include "ofMain.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> hueThreshold;
ofParameter<int> satThreshold;
ofParameter<int> briThreshold;
ofxPanel guiPanel;
};
#include "ofApp.h"
void ofApp::setup()
{
grabber.setup(1280, 720);
resultImg.allocate(1280, 720, OF_IMAGE_COLOR);
captureBackground.set("Capture BG", true);
colorThreshold.set("Color Thresh", 120, 0, 255);
guiPanel.setup("BG Subtraction");
guiPanel.add(captureBackground);
guiPanel.add(colorThreshold);
}
void ofApp::update()
{
grabber.update();
ofPixels& grabberPix = grabber.getPixels();
if (captureBackground)
{
backgroundImg.setFromPixels(grabber.getPixels());
captureBackground = false;
}
ofPixels& resultPix = resultImg .getPixels();
for (int y = 0; y < grabberPix.getHeight(); y++)
{
for (int x = 0; x < grabberPix.getWidth(); x++)
{
ofColor grabColor = grabberPix.getColor(x, y);
ofColor bgColor = backgroundImg.getColor(x, y);
if (abs(grabColor.r - bgColor.r) > colorThreshold ||
abs(grabColor.g - bgColor.g) > colorThreshold ||
abs(grabColor.b - bgColor.b) > colorThreshold)
{
resultPix.setColor(x, y, grabColor);
}
else
{
resultPix.setColor(x, y, ofColor(0));
}
}
}
resultImg.update();
}
void ofApp::draw()
{
resultImg.draw(0, 0, ofGetWidth(), ofGetHeight());
guiPanel.draw();
}
#pragma once
#include "ofMain.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> colorThreshold;
ofxPanel guiPanel;
};
#include "ofApp.h"
void ofApp::setup()
{
grabber.setup(1280, 720);
}
void ofApp::update()
{
grabber.update();
}
void ofApp::draw()
{
grabber.draw(0, 0, ofGetWidth(), ofGetHeight());
}
#pragma once
#include "ofMain.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofVideoGrabber grabber;
};
#include "ofApp.h"
void ofApp::setup()
{
grabber.setup(1280, 720);
resultImg.allocate(1280, 720, OF_IMAGE_COLOR);
brightnessThreshold.set("Bri Thresh", 120, 0, 255);
guiPanel.setup("Threshold");
guiPanel.add(brightnessThreshold);
}
void ofApp::update()
{
grabber.update();
ofPixels& grabberPix = grabber.getPixels();
ofPixels& resultPix = resultImg .getPixels();
for (int y = 0; y < grabberPix.getHeight(); y++)
{
for (int x = 0; x < grabberPix.getWidth(); x++)
{
ofColor pixColor = grabberPix.getColor(x, y);
if (pixColor.getBrightness() > brightnessThreshold)
{
resultPix.setColor(x, y, ofColor(255));
}
else
{
resultPix.setColor(x, y, ofColor(0));
}
}
}
resultImg.update();
}
void ofApp::draw()
{
resultImg.draw(0, 0, ofGetWidth(), ofGetHeight());
guiPanel.draw();
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofVideoGrabber grabber;
ofImage resultImg;
ofParameter<int> brightnessThreshold;
ofxPanel guiPanel;
};
#include "ofApp.h"
void ofApp::setup()
{
grabber.setup(1280, 720);
resultImg.allocate(1280, 720, OF_IMAGE_COLOR);
}
void ofApp::update()
{
grabber.update();
ofPixels grabberPix = grabber.getPixels();
ofPixels resultPix = resultImg .getPixels();
for (int y = 0; y < grabberPix.getHeight(); y++)
{
for (int x = 0; x < grabberPix.getWidth(); x++)
{
ofColor pixColor = grabberPix.getColor(x, y);
resultPix.setColor(x, y, pixColor);
}
}
}
void ofApp::draw()
{
resultImg.draw(0, 0, ofGetWidth(), ofGetHeight());
}
#pragma once
#include "ofMain.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofVideoGrabber grabber;
ofImage resultImg;
};
#include "ofApp.h"
void ofApp::setup()
{
grabber.setup(1280, 720);
resultImg.allocate(1280, 720, OF_IMAGE_COLOR);
}
void ofApp::update()
{
grabber.update();
ofPixels& grabberPix = grabber.getPixels();
ofPixels& resultPix = resultImg .getPixels();
for (int y = 0; y < grabberPix.getHeight(); y++)
{
for (int x = 0; x < grabberPix.getWidth(); x++)
{
ofColor pixColor = grabberPix.getColor(x, y);
resultPix.setColor(x, y, pixColor);
}
}
resultImg.update();
}
void ofApp::draw()
{
resultImg.draw(0, 0, ofGetWidth(), ofGetHeight());
}
#include "ofApp.h"
void ofApp::setup()
{
grabber.setup(1280, 720);
resultImg.allocate(1280, 720, OF_IMAGE_COLOR);
}
void ofApp::update()
{
grabber.update();
brightnessThreshold = ofMap(mouseX, 0, ofGetWidth(), 255, 0);
ofPixels& grabberPix = grabber.getPixels();
ofPixels& resultPix = resultImg.getPixels();
for (int y = 0; y < grabberPix.getHeight(); y++)
{
for (int x = 0; x < grabberPix.getWidth(); x++)
{
ofColor pixColor = grabberPix.getColor(x, y);
if (pixColor.getBrightness() > brightnessThreshold)
{
resultPix.setColor(x, y, ofColor(255));
}
else
{
resultPix.setColor(x, y, ofColor(0));
}
}
}
resultImg.update();
}
void ofApp::draw()
{
resultImg.draw(0, 0, ofGetWidth(), ofGetHeight());
}
#pragma once
#include "ofMain.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofVideoGrabber grabber;
ofImage resultImg;
int brightnessThreshold;
};
#include "ofApp.h"
void ofApp::setup()
{
grabber.setup(1280, 720);
resultImg.allocate(1280, 720, OF_IMAGE_COLOR);
}
void ofApp::update()
{
grabber.update();
int brightnessThreshold = 128;
ofPixels& grabberPix = grabber.getPixels();
ofPixels& resultPix = resultImg .getPixels();
for (int y = 0; y < grabberPix.getHeight(); y++)
{
for (int x = 0; x < grabberPix.getWidth(); x++)
{
ofColor pixColor = grabberPix.getColor(x, y);
if (pixColor.getBrightness() > brightnessThreshold)
{
resultPix.setColor(x, y, ofColor(255));
}
else
{
resultPix.setColor(x, y, ofColor(0));
}
}
}
resultImg.update();
}
void ofApp::draw()
{
resultImg.draw(0, 0, ofGetWidth(), ofGetHeight());
}
@billythemusical
Copy link

Hey Elie! On line 28 of ofApp-bgRgb.cpp, there's an extra space after resultImg

Best,
BB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment