Skip to content

Instantly share code, notes, and snippets.

@sansumbrella
Created May 25, 2011 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sansumbrella/991708 to your computer and use it in GitHub Desktop.
Save sansumbrella/991708 to your computer and use it in GitHub Desktop.
libCinder demo showing openCV contour detection
#include "cinder/app/AppBasic.h"
#include "cinder/gl/gl.h"
#include "cinder/params/Params.h"
#include "cinder/ImageIo.h"
#include "cinder/Utilities.h"
#include "cinder/Capture.h"
#include "cinder/gl/Texture.h"
#include "CinderOpenCV.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class CvBlobsApp : public AppBasic {
public:
void prepareSettings(Settings *settings);
void setup();
void keyDown( KeyEvent event );
void update();
void draw();
private:
params::InterfaceGl mParams;
Capture mCap;
gl::Texture mTexture;
typedef vector< vector<cv::Point> > ContourVector;
ContourVector mContours;
int mStepSize;
int mBlurAmount;
};
void CvBlobsApp::prepareSettings(Settings *settings)
{
settings->setWindowSize( 1024, 768 );
}
void CvBlobsApp::setup()
{
mParams = params::InterfaceGl( "Settings", Vec2i( 200, 100 ) );
mParams.addParam( "Threshold Step Size", &mStepSize, "min=1 max=255" );
mParams.addParam( "CV Blur amount", &mBlurAmount, "min=3 max=55" );
mStepSize = 10;
mBlurAmount = 5;
try {
mCap = Capture( 640, 480 );
mCap.start();
}
catch( ... ) {
console() << "Failed to initialize capture" << std::endl;
}
}
void CvBlobsApp::keyDown( KeyEvent event )
{
switch( event.getChar() ){
case 'f':
setFullScreen( !isFullScreen() );
break;
default:
break;
}
}
void CvBlobsApp::update()
{
if( mCap && mCap.checkNewFrame() ) {
cv::Mat input( toOcv( mCap.getSurface() ) );
cv::Mat gray;
cv::Mat thresh;
cv::cvtColor( input, gray, CV_RGB2GRAY );
cv::blur( gray, gray, cv::Size( mBlurAmount, mBlurAmount ) );
mContours.clear();
for( int t = 0; t <= 255; t += mStepSize )
{
ContourVector vec;
cv::threshold( gray, thresh, t, 255, CV_8U );
cv::findContours( thresh, vec, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );
// put into mContours
mContours.insert( mContours.end(), vec.begin(), vec.end() );
}
mTexture = gl::Texture( fromOcv( gray ) );
}
}
void CvBlobsApp::draw()
{
gl::clear( Color::black() );
gl::pushMatrices();
gl::translate( Vec2f( getWindowWidth() - 640, getWindowHeight() - 480 ) * 0.5f );
if( mTexture )
{
gl::color( Color::white() );
gl::draw( mTexture );
}
// draw the contours
for( ContourVector::iterator iter = mContours.begin(); iter != mContours.end(); ++iter )
{
glBegin( GL_LINE_LOOP );
for( vector<cv::Point>::iterator pt = iter->begin(); pt != iter->end(); ++pt )
{
gl::color( Color( 1.0f, 0.0f, 0.0f ) );
gl::vertex( fromOcv( *pt ) );
}
glEnd();
}
gl::popMatrices();
// draw interface
params::InterfaceGl::draw();
}
CINDER_APP_BASIC( CvBlobsApp, RendererGl )
@sansumbrella
Copy link
Author

Note that getUniquePath() is a function in my fork of Cinder, and is not (yet?) standard. Removed from the sample.

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