Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apress
committed
Oct 16, 2016
0 parents
commit 7ea7ed9
Showing
153 changed files
with
7,721,473 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "testApp.h" | ||
#include "ofAppGlutWindow.h" | ||
|
||
int main() { | ||
ofAppGlutWindow window; | ||
ofSetupOpenGL(&window, 640 * 3/2, 240, OF_WINDOW); | ||
//ofSetupOpenGL(&window, 640, 480, OF_WINDOW); | ||
ofRunApp(new testApp()); | ||
} |
157 changes: 157 additions & 0 deletions
157
978-1-4302-3867-6/ch04/Background Subtraction/testApp.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
#include "testApp.h" | ||
|
||
int mod = 4; | ||
|
||
float threshold = 150; | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::setup() { | ||
kinect.init(); | ||
kinect.setVerbose(true); | ||
kinect.open(); | ||
|
||
prevImage.allocate(kinect.width/mod, kinect.height/mod, OF_IMAGE_GRAYSCALE); | ||
resultImage.allocate(kinect.width/mod, kinect.height/mod, OF_IMAGE_GRAYSCALE); | ||
|
||
// nearThreshold = 230; | ||
// farThreshold = 70; | ||
// bThreshWithOpenCV = true; | ||
// | ||
ofSetFrameRate(60); | ||
|
||
// zero the tilt on startup | ||
angle = 0; | ||
kinect.setCameraTiltAngle(angle); | ||
} | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::update() { | ||
ofBackground(100, 100, 100); | ||
|
||
threshold = ofMap(mouseX, 0, ofGetViewportWidth(), 0, 255, 255); | ||
|
||
// cout << mouseX << ":" << threshold << endl; | ||
|
||
kinect.update(); | ||
if(kinect.isFrameNew()) // there is a new frame and we are connected | ||
{ | ||
|
||
//currentImage.setFromPixels(kinect.getPixels(), kinect.getWidth(), kinect.getHeight(), OF_IMAGE_COLOR, true); | ||
currentImage.setFromPixels(kinect.getDepthPixels(), kinect.getWidth(), kinect.getHeight(), OF_IMAGE_GRAYSCALE, true); | ||
currentImage.mirror(false, true); | ||
currentImage.setImageType(OF_IMAGE_GRAYSCALE); | ||
currentImage.resize(currentImage.getWidth()/mod, currentImage.getHeight()/mod); | ||
|
||
unsigned char * pixels = resultImage.getPixels(); | ||
|
||
int brightest = 0; | ||
|
||
for(int x = 0; x < currentImage.width; x++){ | ||
for(int y = 0; y < currentImage.height; y++){ | ||
int i = x + y * currentImage.width; | ||
|
||
int color = blur(¤tImage, x, y, 5); | ||
int prevColor = prevImage.getPixels()[i]; | ||
|
||
if((color + threshold <= prevColor || color - threshold >= prevColor)){ | ||
pixels[i] = 255; | ||
} else { | ||
pixels[i] = 0; | ||
} | ||
} | ||
} | ||
|
||
resultImage.update(); | ||
} | ||
} | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::draw() { | ||
ofSetColor(255, 255, 255); | ||
resultImage.draw(640, 0, | ||
320, 240); | ||
currentImage.draw(320, 0, | ||
320, 240); | ||
prevImage.draw(0, 0, | ||
320, 240); | ||
} | ||
|
||
|
||
//-------------------------------------------------------------- | ||
void testApp::exit() { | ||
kinect.setCameraTiltAngle(0); // zero the tilt on exit | ||
kinect.close(); | ||
} | ||
|
||
|
||
//-------------------------------------------------------------- | ||
float testApp::blur(ofImage* img, int x, int y, int blurSize){ | ||
float greyLevel = 0; | ||
|
||
unsigned char* pixels = img->getPixels(); | ||
|
||
int numPixels = 0; | ||
|
||
for(int dx = -blurSize; dx <= blurSize; dx++){ | ||
for(int dy = -blurSize; dy <= blurSize; dy++){ | ||
|
||
|
||
int newX = ofClamp((dx + x), 0, img->getWidth() - 1); | ||
int newY = ofClamp((dy + y), 0, img->getHeight() - 1); | ||
|
||
numPixels++; | ||
|
||
int i = (newX + newY * img->getWidth()); | ||
|
||
greyLevel += pixels[i]; | ||
|
||
} | ||
} | ||
|
||
greyLevel = greyLevel/numPixels; | ||
|
||
return greyLevel; | ||
} | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::keyPressed (int key) { | ||
switch (key) { | ||
case OF_KEY_UP: | ||
angle++; | ||
if(angle>30) angle=30; | ||
kinect.setCameraTiltAngle(angle); | ||
break; | ||
|
||
case OF_KEY_DOWN: | ||
angle--; | ||
if(angle<-30) angle=-30; | ||
kinect.setCameraTiltAngle(angle); | ||
break; | ||
case OF_KEY_RETURN: | ||
prevImage.setFromPixels(currentImage.getPixels(), currentImage.getWidth(), currentImage.getHeight(), OF_IMAGE_GRAYSCALE, true); | ||
break; | ||
} | ||
} | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::mouseMoved(int x, int y) { | ||
} | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::mouseDragged(int x, int y, int button) | ||
{} | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::mousePressed(int x, int y, int button) | ||
{ | ||
cout << threshold << endl; | ||
} | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::mouseReleased(int x, int y, int button) | ||
{} | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::windowResized(int w, int h) | ||
{} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
#include "ofMain.h" | ||
#include "ofxOpenCv.h" | ||
#include "ofxKinect.h" | ||
|
||
class testApp : public ofBaseApp { | ||
public: | ||
|
||
void setup(); | ||
void update(); | ||
void draw(); | ||
void exit(); | ||
|
||
float blur(ofImage* img, int x, int y, int blurSize); | ||
|
||
void keyPressed (int key); | ||
void mouseMoved(int x, int y ); | ||
void mouseDragged(int x, int y, int button); | ||
void mousePressed(int x, int y, int button); | ||
void mouseReleased(int x, int y, int button); | ||
void windowResized(int w, int h); | ||
|
||
ofxKinect kinect; | ||
|
||
ofImage prevImage; | ||
ofImage currentImage; | ||
ofImage resultImage; | ||
|
||
int nearThreshold; | ||
int farThreshold; | ||
|
||
int angle; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "testApp.h" | ||
#include "ofAppGlutWindow.h" | ||
|
||
int main() { | ||
ofAppGlutWindow window; | ||
//ofSetupOpenGL(&window, 1024, 768, OF_WINDOW); | ||
ofSetupOpenGL(&window, 1280, 480, OF_WINDOW); | ||
ofRunApp(new testApp()); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
#include "testApp.h" | ||
|
||
int mod = 4; | ||
|
||
float threshold = 150; | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::setup() { | ||
kinect.init(); | ||
kinect.setVerbose(true); | ||
kinect.open(); | ||
|
||
resultImage.allocate(kinect.width/mod, kinect.height/mod, OF_IMAGE_GRAYSCALE); | ||
|
||
// nearThreshold = 230; | ||
// farThreshold = 70; | ||
// bThreshWithOpenCV = true; | ||
// | ||
ofSetFrameRate(60); | ||
|
||
// zero the tilt on startup | ||
angle = 0; | ||
kinect.setCameraTiltAngle(angle); | ||
} | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::update() { | ||
ofBackground(100, 100, 100); | ||
|
||
threshold = ofMap(mouseX, 0, ofGetViewportWidth(), 0, 255, 255); | ||
|
||
// cout << mouseX << ":" << threshold << endl; | ||
|
||
kinect.update(); | ||
if(kinect.isFrameNew()) // there is a new frame and we are connected | ||
{ | ||
|
||
greyImage.setFromPixels(kinect.getPixels(), kinect.getWidth(), kinect.getHeight(), OF_IMAGE_COLOR,true); | ||
greyImage.setFromPixels(kinect.getDepthPixels(), kinect.getWidth(), kinect.getHeight(), OF_IMAGE_GRAYSCALE,true); | ||
greyImage.setImageType(OF_IMAGE_GRAYSCALE); | ||
greyImage.resize(greyImage.getWidth()/mod, greyImage.getHeight()/mod); | ||
|
||
unsigned char * pixels = resultImage.getPixels(); | ||
|
||
for(int x = 0; x < greyImage.width; x++){ | ||
for(int y = 0; y < greyImage.height; y++){ | ||
int i = x + y * greyImage.width; | ||
|
||
int color = blur(&greyImage, x, y, 0); | ||
|
||
pixels[i] = color; | ||
|
||
if(color > threshold){ | ||
pixels[i] = 255; | ||
} else { | ||
pixels[i] = 0; | ||
} | ||
} | ||
} | ||
|
||
resultImage.update(); | ||
} | ||
} | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::draw() { | ||
ofSetColor(255, 255, 255); | ||
greyImage.draw(0, 0, 640, 480); | ||
resultImage.draw(640, 0, 640, 480); | ||
} | ||
|
||
|
||
//-------------------------------------------------------------- | ||
void testApp::exit() { | ||
kinect.setCameraTiltAngle(0); // zero the tilt on exit | ||
kinect.close(); | ||
} | ||
|
||
|
||
//-------------------------------------------------------------- | ||
float testApp::blur(ofImage* img, int x, int y, int blurSize){ | ||
float greyLevel = 0; | ||
|
||
unsigned char* pixels = img->getPixels(); | ||
|
||
int numPixels = 0; | ||
|
||
for(int dx = -blurSize; dx <= blurSize; dx++){ | ||
for(int dy = -blurSize; dy <= blurSize; dy++){ | ||
|
||
|
||
int newX = ofClamp((dx + x), 0, img->getWidth() - 1); | ||
int newY = ofClamp((dy + y), 0, img->getHeight() - 1); | ||
|
||
numPixels++; | ||
|
||
int i = (newX + newY * img->getWidth()); | ||
|
||
greyLevel += pixels[i]; | ||
|
||
} | ||
} | ||
|
||
greyLevel = greyLevel/numPixels; | ||
|
||
return greyLevel; | ||
} | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::keyPressed (int key) { | ||
switch (key) { | ||
case OF_KEY_UP: | ||
angle++; | ||
if(angle>30) angle=30; | ||
kinect.setCameraTiltAngle(angle); | ||
break; | ||
|
||
case OF_KEY_DOWN: | ||
angle--; | ||
if(angle<-30) angle=-30; | ||
kinect.setCameraTiltAngle(angle); | ||
break; | ||
} | ||
} | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::mouseMoved(int x, int y) { | ||
} | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::mouseDragged(int x, int y, int button) | ||
{} | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::mousePressed(int x, int y, int button) | ||
{} | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::mouseReleased(int x, int y, int button) | ||
{} | ||
|
||
//-------------------------------------------------------------- | ||
void testApp::windowResized(int w, int h) | ||
{} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include "ofMain.h" | ||
#include "ofxOpenCv.h" | ||
#include "ofxKinect.h" | ||
|
||
class testApp : public ofBaseApp { | ||
public: | ||
|
||
void setup(); | ||
void update(); | ||
void draw(); | ||
void exit(); | ||
|
||
float blur(ofImage* img, int x, int y, int blurSize); | ||
|
||
void keyPressed (int key); | ||
void mouseMoved(int x, int y ); | ||
void mouseDragged(int x, int y, int button); | ||
void mousePressed(int x, int y, int button); | ||
void mouseReleased(int x, int y, int button); | ||
void windowResized(int w, int h); | ||
|
||
ofxKinect kinect; | ||
|
||
ofImage greyImage; | ||
ofImage resultImage; | ||
|
||
int nearThreshold; | ||
int farThreshold; | ||
|
||
int angle; | ||
}; |
Oops, something went wrong.