Skip to content

Instantly share code, notes, and snippets.

@prisonerjohn
Last active October 9, 2019 04:34
Show Gist options
  • Save prisonerjohn/2b564edede9ff89de8bf495397a19a79 to your computer and use it in GitHub Desktop.
Save prisonerjohn/2b564edede9ff89de8bf495397a19a79 to your computer and use it in GitHub Desktop.
RealSense
#include "ofApp.h"
void ofApp::setup()
{
// Default RS resolution.
ofSetWindowShape(640, 360);
// true parameter starts the camera automatically.
rsContext.setup(true);
}
void ofApp::update()
{
rsContext.update();
}
void ofApp::draw()
{
// Try to get a pointer to a device.
std::shared_ptr<ofxRealSense2::Device> rsDevice = rsContext.getDevice(0);
if (rsDevice)
{
// Draw the depth texture.
rsDevice->getDepthTex().draw(0, 0);
}
}
#pragma once
#include "ofMain.h"
#include "ofxRealSense2.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofxRealSense2::Context rsContext;
};
#include "ofApp.h"
void ofApp::setup()
{
// Default RS resolution.
ofSetWindowShape(640, 720);
// Start the sensor.
rsContext.setup(true);
// Setup the parameters.
nearThreshold.set("Near Threshold", 0.01f, 0.0f, 0.1f);
farThreshold.set("Far Threshold", 0.02f, 0.0f, 0.1f);
// Setup the gui.
guiPanel.setup("Depth Threshold", "settings.json");
guiPanel.add(nearThreshold);
guiPanel.add(farThreshold);
}
void ofApp::update()
{
rsContext.update();
}
void ofApp::draw()
{
std::shared_ptr<ofxRealSense2::Device> rsDevice = rsContext.getDevice(0);
if (rsDevice)
{
rsDevice->getDepthTex().draw(0, 0);
// Get the point distance using the SDK function.
float distAtMouse = rsDevice->getDistance(ofGetMouseX(), ofGetMouseY());
ofDrawBitmapStringHighlight(ofToString(distAtMouse, 3), ofGetMouseX(), ofGetMouseY());
// Threshold the depth.
ofFloatPixels rawDepthPix = rsDevice->getRawDepthPix();
ofFloatPixels thresholdNear, thresholdFar, thresholdResult;
ofxCv::threshold(rawDepthPix, thresholdNear, nearThreshold);
ofxCv::threshold(rawDepthPix, thresholdFar, farThreshold, true);
ofxCv::bitwise_and(thresholdNear, thresholdFar, thresholdResult);
// Upload pixels to image.
thresholdImg.setFromPixels(thresholdResult);
// Draw the result image.
thresholdImg.draw(0, 360);
}
// Draw the gui.
guiPanel.draw();
}
#pragma once
#include "ofMain.h"
#include "ofxCv.h"
#include "ofxGui.h"
#include "ofxRealSense2.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofxRealSense2::Context rsContext;
ofImage thresholdImg;
ofParameter<float> nearThreshold;
ofParameter<float> farThreshold;
ofxPanel guiPanel;
};
include "ofApp.h"
void ofApp::setup()
{
// Default RS resolution.
ofSetWindowShape(640, 360);
rsContext.setup(true);
}
void ofApp::update()
{
rsContext.update();
}
void ofApp::draw()
{
std::shared_ptr<ofxRealSense2::Device> rsDevice = rsContext.getDevice(0);
if (rsDevice)
{
rsDevice->getDepthTex().draw(0, 0);
float distAtMouse = rsDevice->getDistance(ofGetMouseX(), ofGetMouseY());
ofDrawBitmapStringHighlight(ofToString(distAtMouse, 3), ofGetMouseX(), ofGetMouseY());
}
}
#include "ofApp.h"
void ofApp::setup()
{
// Default RS resolution.
ofSetWindowShape(640, 360);
guiPanel.setup("Depth", "settings.json");
rsContext.setup(true);
std::shared_ptr<ofxRealSense2::Device> rsDevice = rsContext.getDevice(0);
if (rsDevice)
{
guiPanel.add(rsDevice->params);
}
}
void ofApp::update()
{
rsContext.update();
}
void ofApp::draw()
{
std::shared_ptr<ofxRealSense2::Device> rsDevice = rsContext.getDevice(0);
if (rsDevice)
{
rsDevice->getDepthTex().draw(0, 0);
float distAtMouse = rsDevice->getDistance(ofGetMouseX(), ofGetMouseY());
ofDrawBitmapStringHighlight(ofToString(distAtMouse, 3), ofGetMouseX(), ofGetMouseY());
}
guiPanel.draw();
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
#include "ofxRealSense2.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofxRealSense2::Context rsContext;
ofxPanel guiPanel;
};
include "ofApp.h"
void ofApp::setup()
{
// Default RS resolution.
ofSetWindowShape(640, 360);
rsContext.setup(true);
}
void ofApp::update()
{
rsContext.update();
}
void ofApp::draw()
{
std::shared_ptr<ofxRealSense2::Device> rsDevice = rsContext.getDevice(0);
if (rsDevice)
{
rsDevice->getDepthTex().draw(0, 0);
// Get the point distance using the SDK function.
float distAtMouse = rsDevice->getDistance(ofGetMouseX(), ofGetMouseY());
ofDrawBitmapStringHighlight(ofToString(distAtMouse, 3), ofGetMouseX(), ofGetMouseY() - 10);
// Get the point depth using the texture directly.
ofShortPixels rawDepthPix = rsDevice->getRawDepthPix();
int depthAtMouse = rawDepthPix.getColor(ofGetMouseX(), ofGetMouseY()).r;
ofDrawBitmapStringHighlight(ofToString(depthAtMouse), ofGetMouseX() + 16, ofGetMouseY() + 10);
}
}
#include "ofApp.h"
void ofApp::setup()
{
// Default RS resolution.
ofSetWindowShape(640, 720);
// Start the sensor.
rsContext.setup(true);
// Allocate the image.
thresholdImg.allocate(640, 360, OF_IMAGE_GRAYSCALE);
// Setup the parameters.
nearThreshold.set("Near Threshold", 10, 0, 4000);
farThreshold.set("Far Threshold", 1000, 0, 4000);
// Setup the gui.
guiPanel.setup("Depth Threshold", "settings.json");
guiPanel.add(nearThreshold);
guiPanel.add(farThreshold);
}
void ofApp::update()
{
rsContext.update();
}
void ofApp::draw()
{
std::shared_ptr<ofxRealSense2::Device> rsDevice = rsContext.getDevice(0);
if (rsDevice)
{
rsDevice->getDepthTex().draw(0, 0);
// Get the point distance using the SDK function.
float distAtMouse = rsDevice->getDistance(ofGetMouseX(), ofGetMouseY());
ofDrawBitmapStringHighlight(ofToString(distAtMouse, 3), ofGetMouseX(), ofGetMouseY());
// Threshold the depth.
ofShortPixels rawDepthPix = rsDevice->getRawDepthPix();
ofPixels& thresholdPix = thresholdImg.getPixels();
for (int y = 0; y < rawDepthPix.getHeight(); y++)
{
for (int x = 0; x < rawDepthPix.getWidth(); x++)
{
int depth = rawDepthPix.getColor(x, y).r;
if (nearThreshold < depth && depth < farThreshold)
{
thresholdPix.setColor(x, y, ofColor(255));
}
else
{
thresholdPix.setColor(x, y, ofColor(0));
}
}
}
// Upload pixels to texture.
thresholdImg.update();
// Draw the result image.
thresholdImg.draw(0, 360);
}
// Draw the gui.
guiPanel.draw();
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
#include "ofxRealSense2.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofxRealSense2::Context rsContext;
ofImage thresholdImg;
ofParameter<int> nearThreshold;
ofParameter<int> farThreshold;
ofxPanel guiPanel;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment