Professional Documents
Culture Documents
openCvPart01 PDF
openCvPart01 PDF
Seeing
Library
A Computer-Vision
experiment with manystandardcom-
,lPenCV - Intel'sfree,oPen-source
-
Overview
OpenCV puter vision algorithms withouthaving
t lcomputer-visionlibrarY can
simplifycomputer-vision OpenCVis a f ree, oPen-source to code them yourself. Theseinclude
Vgreatly
for C/C++ edge, line, and corner detection, ellipse
p r o g r a m m i n gl t. i n c l u d e sa d v a n c e d computervision library
- facedetection, facetrack- programmers. You can download it fitting, image pyramids for multiscale
capabilrties
Kalmanfiltering, from http://sourceforge'net/proj processing, template matching, various
ing,facerecognition,
(Al) ects/opencvlibrary. transforms (Fourier, discrete cosine,
anda varietyof artificial-intelligence
- in ready-to-use form.In addi- Intel releasedthe first version of and distance transforms), and more.
methods
tion, it provides manybasiccomputer- OpenCVin 1999.Initially, it required
visionalgorithms viaits lower-level APls. Intel'slmageProcessing Library. That High-teve I computer-visionmoduIes
of how dependency was eventually removed, OpenCV includes several high-level
A good understanding
get- and you can now use OPenCV as a capabilities. In addition to face-
thesemethodswork is the keyto
whenusingOpenCV standalone library. detection, recognition, and tracking, it
ting goodresults
l'll tntroduce you OpenCV is multi-platform. lt suP- includes optical flow (using camera
In thisfive-part series,
to OpenCV andshowYouhow to uselt oorts both Windowsand Linux,and motion to determine3D structure),
to implementface detection,face more recently, MacOSX. With one camera calibration, andstereo.
tracking,and face recognition. Then, exception (CVCAM, which l'll describe
later in this article),its interfaces are Al and machine-learning methods
l'll take you behindthe scenesto
these methods platform independent. Computer-vision applications often
explainhow each of
requiremachinelearning or other Al
worksand giveyou tips and tricksfor
Someof theseare available in
gettingthe mostout of them. Features methods.
OpenCV's Machine Learning package.
Thisfirstarticleintroduces OpenCV
l'll tellyou how to get it and give you a O p e n C Vh a s s o m a n Y c a P a b i l i t i e s ,
view
few pointersfor settingit up on your i t c a n s e e m o v e r w h e l m i n ga t f i r s t . lmagesamplingand
comouter. You'lllearnhow to readand Fortunately,you'll need only a few to transformations
It'softenusefulto Process a group
writeimagefiles,capturevideo,convert g e t s t a r t e d . l ' l l w a l k Y o u t h r o u g h a
pixel u s e f u ls u b s e ti n t h i s s e r i e s . of pixels as a unit. OpenCV includes
betweencolorformats,andaccess
data- allthroughOpenCV interfaces. Here'sa summaryof the major interfaces for extracting image
functionality categories in OpenCV, subregions, randomsampling, resizing,
version1.0,whichwas just released w a r p i n g , r o t a t i n g , a n d a PPlYing
Brnary
effects.
cvclonelmage (pVrdeoFrame ) ;
1. SimplePixelAccess
/ [nqart rzorrr im:ao-nrnna<<inn nnr]o horo The easiestway to read individualpixelsis with the
cvGet2D0 function:
// l.YoF rha enmr:ftsa .r usrng rL
cvReleaselmage ( &plmgTochange ) ;
CvScalar cr,Get2D(const CvArr*,
int row, int col);
Color Conversions
Figure5 showscode for convertinga color imageto Thisfunctiontakesthreeparameters:
a pointer
to a datacon-
grayscale.OpenCVhasbuilt-insupportfor converting to and tainer(CVArr*),andarrayindicesfor row andcolumnlocation.
from manyusefulcolormodels, includingRGB,HSVYCrCb, Thedatacontainer canbe an lpllmage structure.
Thetopmost
and CIELAB. (Fora discussion of color models,see "The rowof pixels
is row=0,andthe bottommost isrow=height-1.
Worldof Color,"SERVO Magazine,November 2005.)
Notethat the conversion function,cvCvtColorQ,
requires The cvGet2Dflfunction
returnsa C structure,CvScalar,
two imagesin its inputlist.Thefirstone,pRGBlmg, is the definedas
sourceimage.Thesecond, pGraylmg, isthedestination
image.
It willcontainthe conversion
resultwhencvCvtColoO returns. r\modof cf F,-ts CvScalar
Because thisparadigm of passingsourceanddestination {
double val l4l ;
imagesto a processing functioniscommonin OpenCV, you'll l
Grayscaleimagescontain
onlyonechannel - pixelbrightness.22 E; // Convetc rE Eo grrygcale
29 cvcvtcolor(pRGBIry, pcraylng, CI/_RCB2GRAy)t
OpenCVwithout directlyaccessing raw pixeldata. For 3 { !p!tnt!(sgde!r. "falIed !c r:lEe lnage flie\i")i
01.e007
SERVO 65
if your imagedata are
needto castthe data type. Forexamp|e,
input type),you,dcasteach
unsignedbytes(the most common using'rt'
Sourceforgesite or oth.erwise
opcncvllbrary ao unsignedchar* beforeassigning'
'" "lilou;;accessing
,.iri "a
trttp:/ / sourcctots c.nd I Wolccls I outt ttorn grlyscate (single-channel)
is eightbits (one byteper pixel)'
OfficialOPenCV usergrouP-^ imaoe,and the datadepth
grouqI OpcnCV with
http:/ /tcch. groupr'yatrooromI yorid u...tt PixelIrow][col]
OpenCVWiki
nct pixelIrow] Lcoll = ((uchar*)
lrttp://opcncvllbrary'courcctorgc' (PImg->r-mageData +
in thisarticleareavailable aela*prmq-twidthstep + col) );
Sourcecode for the Programlistings | *nlo
for download at wrrwr'cognotlcs'com/opcn€1 images'channelvalues, are interlaced'
In multi-channel values:
to "-tt" blue'green'and pixel
a codesnippet
Here's
red
row' rnt cort
void cvSet2D(CvArr*, int
Cvscalar) ;
qrpn - olmq->wjdthscep;
nchan = Plmg->nCharulels;
/l = 3 for a BGR image
2. FastPixel Access
-'' = Plmg->imageData;
ul9 easy to use' buf
afihough cvGet2DQand cvSet2D0
a few pixelvalues'ano
if you want to accessmore than directly
b l u e l r o w l I c o ] .I =
( (uchar*) (buf + row*widthsteP
+
oerformance matters,you'll want to read values nChan*col);
imageData
" ,f.r.raw databuffer,lpllmage
i.rn :- -^.., greenIrow] [col] =
asa 1Darray'In row- +
ftuq. datarnthe bufferarestored ( (uchar*) (buf + row*widchsceP
rowarelisted
values'llT.1':ti tt66u.'*69] + 1);
r.j;; ;'A; ilat rs,alfpixel row' and so on' redIrow] lcoll =
second
iitti, torfo*.d by pixelvaluesin the
+
( (uchar*) (buf + row*widthstep
pixel data are aligned' and
For performantt'*tont' four-byte
nchan*col + 2 ) ;
lfr
66 sgnvo01.eoo7
experimental ro(;"''""ognii6n -
i[aavision was buittwtthoPnCV'
tytt"' at lJc san Diego'