Sie sind auf Seite 1von 2

I prototype in Python using PIL/OpenCV and when I *really* need speed, I identify the bottleneck,

port it to C++ and epose it to the rest of the Python code using !oost Python" #he ad$antage to this
approach is that OpenCV is nati$ely C++ and you can use nu%py arrays&if you really want to' in
C++" (o you do not ha$e to learn yet another library when doing the porting, %ost of the ti%e"
I think that python can ofer the set of tools" python +%atplotlib and +%aya$i"
%atplotlib goes with scipy, it has a good i%agage processing set of options, and you can get also
%ore fro% other packages" )atplotlib ofers the best set of *+ tools for $isuali,ation and preparing
graphics of high -uality &together with late it offers the ability to $rite late code and rendering it
within the graph'" Last, %aya$i offers a co%plete set of open.L bindings for rendering /+ graphics
and i%ages" together this tools offers the basic working set for opti%al /+ $isuai,ation, i%age
processing and data %anipulation, free and well supported fro% the co%unity"
I# depends on your contet, if speed is an issue then i would reco%%end C++ with OpenCV or one
of the %any other i%age processing toolboes"
0owe$er if your concerned by perfor%ance or hardware integration then either Python or )atlab,
si%ply due to ease of use" Python has %any great libraries such as I%age1 and is superior to )atlab
for $isualisation" 0owe$er don2t feel li%ited to 3ust one, you can perfor% your analysis in )atlab
and use %at*py to integrate python calls $isualisation"
If your intending to analyse neuroi%aging or %edical data I would definitely reco%%end )atlab
due to the $ast nu%ber of superior toolboes a$ailable for )atlab in this field"
0aar features are inherently slow 4 they %ake etensi$e use of floating point operations, which are a
bit slow on %obile de$ices"
5 -uick solution would be to turn to L!P cascades 4 all you need is a few lines changed in your
code" #he perfor%ance gain is significant, and the loss in accuracy is %ini%al" Look for
lbpcascades/lbpcascade_frontalface.xml"
If you want to dig deeper into opti%,ations, here is a generic opti%i,ation tip list &cross4posted
fro% (O' Please note that face detection, being one of the %ost re-uested features of OpenCV, is
already -uite opti%i,ed, so ad$ancing it further %ay %ean deep knowledge"
Advice for optimization
A. Profile your app" +o it first on your co%puter, since it is %uch easier" 6se $isual studio profiler,
and see what functions take the %ost" Opti%i,e the%" 7e$er e$er opti%i,e because you think is
slow, but because you %easure it" (tart with the slowest function, opti%i,e it as %uch as possible,
then take the second slower"
B. 8irst, focus on algorith%s" 5 faster algorith% can i%pro$e perfor%ance with orders of %agnitude
&9::'" 5 C++ trick will gi$e you %aybe * perfor%ance boost"
Classical techni-ues;
<esi,e you $ideo fra%es to be s%aller" %any ti%es, you can etract the infor%ation fro% a
*::/::p i%age, instead of a 9:*=>?@" #he area of the first one is 9: ti%es s%aller"
6se si%pler operations instead of co%plicated ones" 6se integers instead of floats" 5nd
ne$er use double in a %atri or a for loop that eecutes thousands of ti%es"
+o as little calculation as possible" Can you track an ob3ect only in a specific area of the
i%age, instead of processing it all for all the fra%esA Can you %ake a rough/approi%ate
detection on a $ery s%all i%age and then refine it on a <OI in the full fra%eA
C. In for loops, it %ay %ake sense to use C style instead of C++" 5 pointer to data %atri or a float
array is %uch faster than %at"atBi, 3CDDE or std;;$ectorBE" !ut change only if it2s needed" 6sually, a
lot of processing &F:G' is done in so%e double for loop" 8ocus on it" It doesn2t %ake sense to
replace $ectorBE all o$er the place, ad %ake your code look like spaghetti"
D. (o%e OpenCV functions con$ert data to double, process it, then con$ert back to the input
for%at" !eware of the%, they kill perfor%ance on %obile de$ices" Ha%ples; warping, scaling, type
con$ersions" 5lso, color space con$ersions are known to be la,y" Prefer grayscale obtained directly
fro% nati$e I6V"
E. 5<) processors ha$e 7HO7" Learn and use it" It is powerfullJ
5 s%all ea%ple;
float* a, *b, *c;
// init a and b to 1000001 elements
for(int i=0;i<1000001;i++)
ci! = ai!*bi!;
can be rewritten as follows" It2s %ore $erbose, but trust %e it2s faster"
float* a, *b, *c;
// init a and b to 1000001 elements
float"#x$_t _a, _b, _c;
int i;
for(i=0;i<1000001;i+=$)
%
a_ = &ld1'_f"#( (ai! ); // load $ floats from a in a )*+) re,ister
b_ = &ld1'_f"#( (bi! );
c_ = &mul'_f"#(a_, b_); // perform $ float multiplies in parrallel
&st1'_f"#( (ci!, c_); // store t-e four results in c
.
// t-e &ector si/e is not al0a1s multiple of $ or 2 or 13.
// 4rocess t-e remainin, elements
for(;i<1000001;i++)
ci! = ai!*bi!;
Purists say you %ust write in asse%bler, but for the regular progra%%er guy that2s a bit daunting" I
found good results writing with intrinsics, like in the abo$e ea%ple"
5lso check this blog post and the following posts about 7HO7"
5nd, last but not least, I should %ention that I had $ery good success con$erting the ((H
opti%i,ations &this is the 7HO7 counterpart in @?4?= processors' in OpenCV to 7HO7, like here"
#his is the i%age filtering code for uchar %atrices &the regular i%age for%at'" Iou should2t blindly
con$ert instructions one by one, because there are better ways to do it, but take it as an ea%ple to
start with"

Das könnte Ihnen auch gefallen