-
Notifications
You must be signed in to change notification settings - Fork 0
/
CartoonEffect.cxx
69 lines (55 loc) · 1.52 KB
/
CartoonEffect.cxx
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
#include "CartoonEffect.h"
CartoonEffect::CartoonEffect()
{
// constructor.
this->m_numDown = 2;
this->m_numBilateral = 7;
this->m_blurKernelSize = 7;
}
void CartoonEffect::init()
{
// nothing to initialize just yet.
return;
}
void CartoonEffect::drawEffect()
{
if( this->m_lastFrame.empty() )
{
cout << "CartoonEffect Warning: Last frame is empty." << endl;
return;
}
// // Cartoon Effect: http://www.askaswiss.com/2016/01/how-to-create-cartoon-effect-opencv-python.html
// vars for the effect.
Mat colorFrame, bwFrame, blurFrame, edgeFrame, tempImg;
// down sample using Gaussian pyramid.
this->m_lastFrame.copyTo(colorFrame);
for( int i=0; i<this->m_numDown; i++)
{
pyrDown(colorFrame, colorFrame);
}
// repeatedly apply small bilateral filer instead of applying one large filter.
for( int i=0; i<this->m_numDown; i++)
{
bilateralFilter(colorFrame, tempImg, 9, 9, 7);
tempImg.copyTo(colorFrame);
}
// upsample back to original size
for( int i=0; i<this->m_numDown; i++)
{
pyrUp(colorFrame, colorFrame);
}
// covert to B&W and blur
cvtColor(this->m_lastFrame, bwFrame, CV_BGR2GRAY);
medianBlur(bwFrame, blurFrame, this->m_blurKernelSize);
// detect edges.
adaptiveThreshold(blurFrame, edgeFrame, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 9, 2);
// convert back to color.
cvtColor(edgeFrame, edgeFrame, COLOR_GRAY2RGB);
// combine two images.
bitwise_and(colorFrame, edgeFrame, this->m_outFrame);
return;
}
void CartoonEffect::togglePresets()
{
//TODO: set-up code to toggle presets.
}