Cinder++::Cinder Creative Coding Cookbook 3 and 4

Bookmark and Share

On chapter 3 the writers focus the recipes on image manipulation, while explains the basic concepts of OpenCV C++ library. The recipes are very useful if you plan to start working with OpenCV. The Recipes include detecting edges, faces, features in image and convert images to vector graphics. In chapter 4 the writers focus in multimedia and the recipes push you to deal with video and sound applications. From load videos to Share graphics between applications.

In Chapter 3 they explain how to load the OpenCV block for cinder to Xcode. I will do the same for windows and for Cinder 0.8.5. Then based on their recipes I will explain the basic of face detection.

OPENCV
In the book the Rui and Dawid define openCV as a powerful open-source library for computer vision. The library is written in C++ so it can be easily integrated in any cinder application (you will see soon).

opencv1
To Create a basic application with OpenCV click on the tinderBox and select OpenCV: Basic, set a name and your IDE. Click Next.

opencv2
In the second window you have the option to copy the files to the folders application. and other options. click on finish.

After that. TinderBox creates a simple code snippet to start playing with OpenCV.

CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "cinder/app/AppNative.h"
#include "cinder/gl/gl.h"
#include "cinder/ImageIo.h"
#include "cinder/gl/Texture.h"
#include "cinder/Surface.h" //add this one
#include "CinderOpenCv.h"
 
using namespace ci;
using namespace ci::app;
 
class MyFirstOpenCVApp : public AppNative {
public:
	void setup();
	void draw();
	void MakeFlower();
 
	Surface8u mImage;
	cv::CascadeClassifier mFaceCC;
	std::vector<Rectf> mFaces;
	Vec2f ctrRec;
	Vec2f nCenter;
 
	gl::Texture	mTexture;
};
 
void MyFirstOpenCVApp::setup()
{
	nCenter = Vec2f(0,0);
 
	mImage = ( loadImage( loadAsset( "people.jpg" ) ) );
	mFaceCC.load(getAssetPath("haarcascade_frontalface_alt.xml").string());
 
	cv::Mat input( toOcv( mImage, CV_8UC1 ) );
	std::vector<cv::Rect> faces;
	mFaceCC.detectMultiScale(input, faces);
	std::vector<cv::Rect>::const_iterator faceIter;
 
	//loop per each face...
	for(faceIter = faces.begin(); faceIter != faces.end(); ++faceIter)
	{
		Rectf faceRect(fromOcv(*faceIter));
		mFaces.push_back(faceRect);
	}
	cv::Mat output;
 
	mTexture = gl::Texture( fromOcv( output ) );
}   
 
void MyFirstOpenCVApp::draw()
{
	gl::color(Color::white());
	gl::draw( mImage );
 
	gl::color(1, 0, 0 );
	std::vector<Rectf>::const_iterator faceIter;
	for(faceIter = mFaces.begin(); faceIter != mFaces.end(); ++faceIter)
	{
		ctrRec = faceIter->getCenter(); 
 
		for(int i = 0; i < 20; ++i)
		{
			gl::pushModelView();
			gl::translate(ctrRec);
			gl::rotate(10*i);
			gl::drawStrokedEllipse(nCenter, 20, 60, 20);
			gl::popModelView();
		}
	}
}
 
CINDER_APP_NATIVE( MyFirstOpenCVApp, RendererGl )

faceflower

Leave a Reply