You are on page 1of 163

TRNG CAO NG K THUT L T TRNG TP.

H CH MINH KHOA CNG NGH THNG TIN NGH

ti nghin cu khoa hc sinh vin: c

BIN DCH TI H CH HNG DN S DNG B TH VI VIN NGUN M OPENCV


Sinh vin thc hin: BI T n: TN AN Lp: 10CDTP1 N Ngi hng dn: NGUY NGC TRANG

Mc lc: Phn I. M u.................. 3 1. L do chn ti................. 3


2. Xc nh mc tiu nghin cu: ............. 3. i tng nghin cu: ............. 4. Phm vi nghin cu: ............. 5. t gi thuyt: .............. 6. Thc trng v kho st v b th vin ngun m OpenCV: ... 6.1. Thc trng: ................ 6.2. Kho st................... 3 3 4 4 5 5 5 15 16 17 17 19

Phn II. Quy trnh thc hin............

Phn III. Tng kt................


1. Hng pht trin ca ti: .......... 2. Kinh nghim: ...............

Ti liu hng dn s sng OpenCV v2.1..........

ti: BIN DCH TI H CH HNG DN S DNG B TH VI VIN NGUN M OPENCV

Phn I. M u:
1. L do chn ti: Ti rt quan tm ti cng ngh nhn dng my tnh, cng ngh ny rt mi, ng dng rng ri t trn nhiu ngnh khc nhau, c bit l trong lnh vc ch to robot. Nh c Nhng vic tm kim c ngun ti liu Ting Vit v OpenCV hin nay rt him, phn ln ti liu l bng ting Anh, n gy nhiu kh khn trong cho sinh vin mun hc hoc ng dng. V l do trn ti chn ti n mun ny, mong mun cung cp cho cc bn ngun ti liu Ting Vit, rt mong s gip ch cho cc n bn. 2. Xc nh mc tiu nghin cu: - Mc tiu: ti ny,ti s bin dch ti liu h ng dn t ting Anh sang ting Vit, cng hng vi code mu gip cho ngi hc d dng trong cch s dng b th vin lp trnh. i th - Mc ch: Cung cp ngun ti liu ting Vit cho sinh vin Vit Nam tham kho, hc tp, p nghin cu lp trnh d dng trn b th vin OpenCV. u th 3. i tng nghin cu: - i tng nghin cu: B th vin OpenCV 2.1 C API n - Khch th nghin cu: Sinh vin ngnh Cng ngh thng tin, c bit l cc lp trnh vin c mun hc tp, nghin cu b th vin ny. n 3

4. Phm vi nghin cu: OpenCV 2.1 C API 5. t gi thuyt: Vic ti bin dch ti liu OpenCV ny, n tht s gip ch cho bn trong vic hc, s dng b th vin ny ? Tht s cn tht khi bn quan tm ti n ? C n s gip bn t hc b th vin ny ? Vi ngun ti liu c bin dch sang ting Vit, vic bn hc b th vin ny s tht s d dng. N cn thit khi bn quan tm ti. Vi ngun ti liu Ting Vit s rt cn thit vic hc ca bn tr nn d dng hn rt nhiu. Bin dch ti liu ny vi code mu s gip bn tht s d hiu v c th t hc mt cch hiu qu, t kt qu nh mong mun. Khi hon thnh ny, cc sinh vin vin, cc lp trnh vin Vit Nam c th d dng s dng b th vin OpenCV vit ra cc phn mm ng dng nh nu trn, nhanh chng n bt cng ngh th gii.

6. Thc trng v kho st v b th vin ngun m OpenCV: 6.1. Thc trng: - Ngun ti liu Ting Vit v OpenCV hin nay rt him, phn ln ti liu l bng ting Anh, gy nhiu kh khn trong cho sinh vin mun hc hoc ng dng. Trn cc din n hay website cng c rt nhiu ti ni v OpenCV. V d nh http://khoinguonit.com/f93/, http://www.ieev.org/... Nhng hu ht cc trang ny u khng c ti liu thng nht v OpenCV ting Vit m ch c mt s ti liu n l, hoc ti liu bng ting Anh. cc trng i hc cng tng t l ti liu hc tp cho sinh vin u l ting Anh. Sinh vin Vit Nam mun khi u vic nghin cu u phi gp rt nhiu kh khn do ro cn ngn ng (Ting Anh), vic hiu mt thut ng mang tnh k thut vi mt ngi mi rt mt thi gian v khng bao gi l d dng. 6.2. Kho st: Phiu kho st pht ra l 60 phiu v thu v c 56 phiu. Di y l biu chi tit cc cu hi kho st c.

1. Bn c quan tm v Robot khng?

3 11%

1 30%

2 59%

1. Khng 2. C, nhng khng nhiu 3. Rt quan tm

2. Bn c quan tm v cng ngh nhn dng khun mt ngi bng my tnh?

3 7%

1 21%

2 72%

1. Khng 2. C, nhng khng nhiu 3. Rt quan tm

3. Bn c bit Th gic my tnh?

(3) 0% 1 41% 2 59%

1. Cha tng nghe qua 2. C nghe qua 6

3. ang tm hiu

4. y l cng ngh nhn dng my tnh, bn quan tm ti n ch?

3 18%

1 20%

2 62%

1. Khng 2. Cng ch bit qua 3. Rt quan tm

5. Nhng ng dng ca b th vin ny m bn bit?

4 41%

1 31%

3 5%

2 23%

1. Khng 7

2. X l mu sc v phn gii 3. An ninh (camera thng minh, nhn dng) 4. C 2 v nhiu ng dng khc na

6. bao gi bn tm kim ti liu t hc v mt b ngun th vin no?

3 29%

1 34%

2 37%

1. Cha bao gi 2. Ch c qua cho bit 3. Chc chn ri

6b. Bn mun t hc b th vin ny ch?

3 16%

1 14%

2 70%

4. Khng 5. Cng mun th qua 6. Rt mun

7. Nu hc, bn mun ngun ti hng dn bng Ting Vit ch?

2 7%

1 93%

1. Tt nhin 2. Khng quan tm

8. Ti liu v th vin ny bng Ting Vit c nhiu khng?

3 9%

1 29%

2 62%

1. Khng bit 2. t 3. Nhiu

9. Bn c tm kim ti liu lin quan n b th vin khng? 4 3% 1 27% 3 43%

2 27%

1. 2. 3. 4.

Khng Cng thnh thong Khi no cn mi tm Thng xuyn

10

10. Ngun ti liu Ting Vit v n rt him, bn c nhiu khng? 3 5% 1 32%

2 63%

1. Khng 2. C, nhng t 3. Nhiu

11. Bn mun c ti liu v OpenCV bng ngoi ng hay bng ting Vit?

3 1 5% 11%

2 84%

1. Khng quan tm 2. Tc nhin l Ting Vit 3. Sao cng c 11

12. Bn tng lp trnh vi th vin OpenCV ny cha?

4 12% 3 16% 2 14% 1 58%

1. 2. 3. 4.

Cha bao gi Ch mi th qua Cng thnh thong Thng xuyn

13. Nu bn tng lp trnh vi th vin OpenCV, bn c gp kh vi cch dng cc hm trong OpenCV khi ti liu bng ting Anh? 4 3 5% 11% 2 11%

1 73%

1. Cha tng lp trnh vi n 12

2. Khng 3. Cng khng kh lm 4. Qu kh khn

14. Vic chng ti bin dch ngun ti liu ny sang Ting Vit bn thy th no?

3 14%

1 33%

2 53%

1. Mnh khng quan tm 2. Cng c y 3. Rt c ch

13

14

Phn II. Quy trnh thc hin:


- T 5/9/2011 n 1/10/2011: Xc nh ti nghin cu v ng k ti - T 3/10/2011 n 22/10/2011: Bin son cng nghin cu, pht bng kho st v thng k kt qu kho st. - T 24/10/2011 n 25/12/2011: Thc hin sn phm: 1. 2. 3. 4. Lc ngun ti liu cn dch t ngun ti liu nc ngoi Chia ni dung cn dch ra cc chng, phn, hm c th v sp xp Duyt s lt cc ton b cc phn chn v sp xp li ln na Bt u dch ton b theo th t xp. Cng c h tr: translate.google.com.vn, tratu.soha.vn, vdict.com 5. Sau khi dch xong, c li sa li. 6. ng gi sn phm, em i in.

- T 25/12/2011: Np bi v cho khoa.

15

Phn III. Tng kt

Ti liu tham kho: - OpenCV Reference Manual v2.1 http://www.box.com/s/l8pq7x8q1ktfmecbatfv - Learning OpenCV: computer vision with the OpenCV library http://books.google.com/books?id=seAgiOfu seAgiOfu2EIC&lpg=PP1&hl=vi&pg=PA364#v v=onepage& q&f=false Tc gi: Gary Bradski, Adrian Kaehler - Algorithms for Image Processing and Computer Vision http://books.google.com/books?id=BK BK3oXzpxC44C&lpg=PP1&dq=related%3AISBN AISBN059651 6134&hl=vi&pg=PA15#v=onepage& &q&f=false Tc gi: J. R. Parker Trang ch: http://opencv.willowgarage willowgarage.com

Gii thiu v OpenCV: - OpenCV l vit tt ca Open Source Computer Vision. l mt th vin ngun m lp t n trnh gm nhiu hm C v mt s lp C++ da trn thut ton ph bin trong x l nh v th m gic my tnh. -B th vin ny cn c vit trn ngn ng Python v Java. Lp trnh c trn nhiu nn c c tn: Windows, Linux, Android v Mac - OpenCV cung cp rt nhiu kiu d liu c cu trc nhm h tr ti vic x l. p ng dng cng ngh nhn dng : - Hiu chnh v phc ch nh 16

- X l mu sc v phn gii nh - X l hnh trng v phn tch chi tit - Ti dng nh ba chiu v din gii nh - Nhn dng v gim st thc th - Y khoa (chn on, gii phu) - An ninh (camera thng minh, nhn dng) - Giao thng (theo di lu thng, iu khin xe) - T ng ha sn xut (o lng, kim nh cht lng) v v.v

Kinh nghim: Trong qu trnh thc hin ti, s t vng ting Anh ca ti tng ln, thm c nhiu t vng mi, c bit l cc t chuyn ngnh cng ngh thng tin. Ti c bit v quy trnh thc hin mt ti nghin cu khoa hc, vit bo co tng kt nghin cu khoa hc. Hng pht trin ca ti: B th vin OpenCV ng dng c trong rt nhiu ngnh ngh khc nhau, nh an ninh trong vic nhn dng vn tay, i tng, tit kim rt nhiu thi gian v cng sc thay cho con ngi, nng cao hiu qu cng vic. Ch to robot t ng ha. X l, phc ch hnh nh trong siu m Rt nhiu ng dng phc v con ngi, y l s n u cng ngh th gii cho Vit Nam. Ti liu hng dn s dng OpenCV ny s to n by a cng ngh tng lai v vi Vit Nam. Nh ni, cc lp trnh vin Vit Nam s s dng ti liu hng dn s dng b th vin OpenCV vit cc phn mm ng dng cao cp trn, phc v cho cuc sng, pht trin t nc. Trong thi gian ti, ti s tip tc hon thin b ti hng dn ny hn na, nng cp b ti liu ny theo cc phin bn OpenCV mi hn, tip tc lm b ti liu hng dn cho OpenCV C++ API, java. B ti liu ny s c chia s min ph hon trn mng, mi ngi u c s dng d dng ng gp, hon thin n hn na. Mi ngi cng c th chia s kin thc, kinh nghim lp trnh vi OpenCV, cng ng cng nhau pht trin v pht trin t nc.

17

18

Ti liu hng dn s sng OpenCV v2.1

Mc lc: Chng 1: cxcore. Hm ct li.. 1.1 Cc Struct c bn.. 1.2 Hot ng trn mng.. 1.3 Cu trc ng... 1.4 Cc hm v. 1.5 XML/YAML Persistence 1.6 Phn nhm v Tm kim trong khng gian a chiu. Chng 2: cv. X l hnh nh v th gic my tnh. 2.1 Lc hnh nh. 2.2 nh hnh hc bin i 2.3 Hnh nh hn hp bin i 2.4 Biu ... Chng 3: cvaux. Cc hm m rng ca Th Gic My Tnh... Chng 4: highgui. Cao cp giao din v I/O....... 4.1 Giao din ngi dng........ 4.2 c / ghi hnh nh v video.......... 20 20 27 59 86 94 111 113 113 121 128 142 152 153 153 157

19

Chng 1: cxcore. Hm ct li
1.1 Cc Struct c bn CvPoint 2D im vi s nguyn ta (thng l zero-based). typedef struct CvPoint { int x; int y; } CvPoint; x x-phi hp y y-phi hp / * Constructor * / inline CvPoint cvPoint( int x, int y ); / * Chuyn i t CvPoint2D32f * / inline CvPoint cvPointFrom32f( CvPoint2D32f point ); CvPoint2D32f 2D im vi ta im ni typedef struct CvPoint2D32f { float x; float y; } CvPoint2D32f; x x-phi hp y y-phi hp / * Constructor * / inline CvPoint2D32f cvPoint2D32f (double x, double y); / * Chuyn i t CvPoint * / inline CvPoint2D32f cvPointTo32f( CvPoint point ); CvPoint3D32f 3D im vi ta im ni typedef struct CvPoint3D32f { float x; float y; float z; } CvPoint3D32f; x x-phi hp y y-phi hp z z-phi hp / * Constructor * / 20

inline CvPoint3D32f cvPoint3D32f( double x, double y, double z ); CvPoint2D64f 2D im vi ta im chnh xc kp ni typedef struct CvPoint2D64f { double x; double y; } CvPoint2D64f; x x-phi hp y y-phi hp / * Constructor * / inline CvPoint2D64f cvPoint2D64f( double x, double y ); / * Chuyn i t CvPoint * / inline CvPoint2D64f cvPointTo64f( CvPoint point ); CvPoint3D64f 3D im vi ta im chnh xc kp ni typedef struct CvPoint3D64f { double x; double y; double z; } CvPoint3D64f; x x-phi hp y y-phi hp z z-phi hp / * Constructor * / inline CvPoint3D64f cvPoint3D64f( double x, double y, double z ); CvSize Pixel chnh xc kch thc ca hnh ch nht. typedef struct CvSize { int width; int height; } CvSize; Chiu rng chiu rng ca hnh ch nht Chiu cao chiu cao ca hnh ch nht / * Constructor * / inline CvSize cvSize( int width, int height ); CvSize2D32f Sub-pixel chnh xc kch thc ca hnh ch nht. 21

typedef struct CvSize2D32f { float width; float height; } CvSize2D32f; Chiu rng chiu rng ca hnh ch nht Chiu cao chiu cao ca hnh ch nht / * Constructor * / inline CvSize2D32f cvSize2D32f( double width, double height ); CvRect B p (thng l gc trn bn tri) v kch thc ca hnh ch nht. typedef struct CvRect { int x; int y; int width; int height; } CvRect; x x-phi hp ca cc gc trn cng, bn tri y y-phi hp ca gc trn bn tri (pha di bn tri cho Windows bitmap ) Chiu rng chiu rng ca hnh ch nht Chiu cao chiu cao ca hnh ch nht / * Constructor * / inline CvRect cvRect( int x, int y, int width, int height ); CvScalar Mt container cho 1, 2, 3 - 4-tuples ca i. typedef struct CvScalar { double val[4]; } CvScalar; / * Constructor: khi to val [0] vi val0, val [1] vi val1. */ inline CvScalar cvScalar( double val0, double val1=0, double val2=0, double val3=0 ); / * Constructor: khi to tt c ca val [0] ... val [3] vi val0123 */ inline CvScalar cvScalarAll( double val0123 ); / * Constructor: khi to val [0 vi val0, v tt c cc val [1] ... val [3] vi s khng */ inline CvScalar cvRealScalar( double val0 ); 22

CvTermCriteria Tiu ch chm dt cho lp i lp li cc thut ton. #define CV_TERMCRIT_ITER 1 #define CV_TERMCRIT_NUMBER CV_TERMCRIT_ITER #define CV_TERMCRIT_EPS 2 typedef struct CvTermCriteria { int type; int max_iter; double epsilon; } CvTermCriteria; type Mt s kt hp ca CV_TERMCRIT ITER v CV_TERMCRIT EPS max_iter s ln lp ti a epsilon yu cu chnh xc / * Constructor * / inline CvTermCriteria cvTermCriteria( int type, int max_iter, double epsilon ); / * Kim tra v chuyn i mt CvTermCriteria type = CV_TERMCRIT_ITER + CV_TERMCRIT_EPS v c hai max_iter v epsilon l hp l * / CvTermCriteria cvCheckTermCriteria( CvTermCriteria criteria, double default_eps, int default_max_iters ); CvMat Mt ma trn a knh. typedef struct CvMat { int type; int step; int* refcount; union { uchar* ptr; short* s; int* i; float* fl; double* db; } data; #ifdef __cplusplus union { int rows; int height; }; union { int cols; 23

int width; }; #else int rows; int cols; #endif } CvMat; type g ch k CvMat (CV MAT MAGIC VAL) c cha cc loi ca cc yu t v c step hng chiu di theo byte refcount Bn di tham chiu truy cp d liu data Con tr d liu cc d liu ma trn thc t rows S hng ca hng cols S ct ca ct Ma trn c lu tr hng bi hng. Tt c cc hng c lin kt bi 4 byte. CvMatND a chiu a knh mng dy c. typedef struct CvMatND { int type; int dims; int* refcount; union { uchar* ptr; short* s; int* i; float* fl; double* db; } data; struct { int size; int step; } dim[CV_MAX_DIM]; } CvMatND; type loi ch k CvMatND (CV MATND MAGIC VAL), kt hp cc loi ca cc yu t v c dims lm m s ca kch thc mng refcount Bn di tham chiu truy cp d liu data Con tr d liu cc d liu ma trn thc t dim m i vi mi chiu, cp i ny (s yu t, khong cch gia cc thnh phn trong byte ) CvSparseMat Multi-chiu tha tht mng a knh. 24

typedef struct CvSparseMat { int type; int dims; int* refcount; struct CvSet* heap; void** hashtable; int hashsize; int total; int valoffset; int idxoffset; int size[CV_MAX_DIM]; } CvSparseMat; type loi ch k CvSparseMat (CV_SPARSE_MAT_MAGIC_VAL), kt hp cc loi ca cc yu t v c. dims S m ca kch thc refcount Bn di tham chiu truy cp. Khng c s dng. heap Mt ng h bi ca cc nt bng bm hashtable Cc bng bm. Mi mc l mt danh sch cc nt. hashsize Kch thc ca bng bm total tng ca cc nt mng tha tht valoffset gi tr b p ca cc nt mng, theo byte idxoffset Ch s b p ca cc nt mng, theo byte size mng c kch thc kch thc IplImage IPL hnh nh tiu typedef struct _IplImage { int nSize; int ID; int nChannels; int alphaChannel; int depth; char colorModel[4]; char channelSeq[4]; int dataOrder; int origin; int align; int width; int height; struct _IplROI *roi; struct _IplImage *maskROI; void *imageId; struct _IplTileInfo *tileInfo; int imageSize; char *imageData; 25

int widthStep; int BorderMode[4]; int BorderConst[4]; char *imageDataOrigin; } IplImage; nSize sizeof(IplImage) ID Phin bn, lun lun bng 0 nChannels S ca cc knh. Hu ht cc Hm OpenCV h tr 1-4 knh. alphaChannel b b qua bi OpenCV depth knh su trong bit + bit du ty chn (IPL SIGN chiu su) . H tr su l: IPL_DEPTH_8U Unsigned s nguyn 8-bit IPL_DEPTH_8S ng k s nguyn 8-bit IPL_DEPTH_16U Unsigned s nguyn 16-bit IPL_DEPTH_16S ng k s nguyn 16-bit IPL_DEPTH_32S ng k s nguyn 32-bit IPL_DEPTH_32F n chnh xc ni IPL_DEPTH_64F su chnh xc im ni Double-64F colorModel B qua OpenCV. Hm OpenCV CvtColor yu cu ngun v destination mu khng gian nh cc thng s. channelSeq b b qua bi OpenCV dataOrder 0 = IPL_DATA_ORDER_PIXEL - knh mu xen k, 1 mu sc ring bit knh. CreateImage ch to ra hnh nh vi cc knh xen k. V d, thng thng b tr ca mt hnh nh mu sc l: b00g00r00b10g10r10 ... origin ngun gc 0 - gc trn bn tri, 1 - di bn tri c ngun gc (Windows bitmap phong cch ) align sp xp sp cc dng hnh nh (4 hoc 8) . OpenCV b qua iu ny v s dng widthStep thay v. width nh rng chiu rng tnh bng pixel height nh cao chiu cao tnh bng pixel roi khu vc quan tm (ROI). Nu khng phi l NULL, ch c khu vc ny hnh nh s c x l. maskROI Phi c NULL trong OpenCV imageId Phi c NULL trong OpenCV tileInfo Phi c NULL trong OpenCV imageSize nh kch thc d liu theo byte. i vi d liu xen k, bng hnh nh-> chiu cao mage-> widthStep imageData Mt con tr vo cc d liu hnh nh lin kt widthStep Kch thc ca mt dng hnh nh lin kt, trong byte BorderMode bin gii hon thnh ch , b b qua bi OpenCV BorderConst bin gii hon thnh ch , b b qua bi OpenCV imageDataOrigin Mt con tr n ngun gc ca d liu hnh nh (khng nht thit phi ph hp). y l c s dng cho deallocation hnh nh. Ca IplImage cu trc c tha k t Th vin x l hnh nh ca Intel, trong 26

nh dng c ngun gc. OpenCV ch h tr mt tp hp con ca cc nh dng IplImage c th , nh c nu trong tham s danh sch trn. Ngoi ra cc hn ch trn, OpenCV x l ROIs khc nhau. OpenCV Hm yu cu rng kch thc hnh nh hoc kch c ROI ca tt c cc ngun v hnh nh im n ph hp chnh xc. Trn Mt khc, nh x l Intel Th vin x l khu vc giao nhau gia cc hnh nh ngun v ch n (hoc ROIs), cho php h thay i mt cch c lp. CvArr Ty tin mng typedef void CvArr; CvArr metatype ch c s dng nh mt tham s Hm xc nh rng Hm chp nhn mng ca nhiu loi, chng hn nh IplImage, CvMat * hoc thm ch CvSeq * i khi. Cc c th loi mng c xc nh ti thi gian chy bng cch phn tch 4 byte u tin ca tiu . 1.2 Hot ng trn mng cvAbsDiff Tnh khc bit tuyt i gia hai mng. void cvAbsDiff(const CvArr* src1, const CvArr* src2, CvArr* dst); src1 Cc mng ngun u tin src2 Cc mng ngun th hai dst Cc mng ch Hm tnh ton s khc bit tuyt i gia hai mng. Tt c cc mng phi c cng mt kiu d liu v kch thc tng t (hoc thu nhp t u kch thc). cvAbsDiffS Tnh khc bit tuyt i gia cc mng v mt v hng. void cvAbsDiffS(const CvArr* src, CvArr* dst, CvScalar value); #define cvAbs(src, dst) cvAbsDiffS(src, dst, cvScalarAll(0)) src Cc mng ngun dst Cc mng ch value Cc v hng Hm tnh ton s khc bit tuyt i gia cc mng v mt v hng. Tt c cc mng phi c cng mt kiu d liu v kch thc tng t (hoc thu nhp t u kch thc). cvAdd Tnh ton s tin cho mi phn t ca hai mng. void cvAdd(const CvArr* src1, const CvArr* src2, 27

CvArr* dst, const CvArr* mask=NULL); src1 Cc mng ngun u tin src2 Cc mng ngun th hai dst Cc mng ch mask mt n hot ng, 8-bit knh mng; quy nh c th cc yu t ca mng ch c thay i Hm b sung thm mt mng khc: dst(I)=src1(I)+src2(I) if mask(I)!=0 Tt c cc mng phi c cng loi, tr mt n, v kch thc tng t (hoc kch thc ROI). i vi loi c phm vi gii hn hot ng ny l bo ha. cvAddS Tnh tng ca mt mng v mt v hng. void cvAddS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL); src Cc mng ngun value v hng dst Cc mng ch mask mt n hot ng, 8-bit knh mng; quy nh c th cc yu t ca mng ch c thay i Hm b sung thm mt gi tr v hng mi phn t trong mng ngun src1 v lu tr kt qu trong dst. i vi cc loi c phm vi gii hn hot ng ny l bo ha. dst(I)=src(I)+value if mask(I)!=0 Tt c cc mng phi c cng loi, tr mt n, v kch thc tng t (hoc kch thc ROI). cvAddWeighted Tnh ton tng trng s ca hai mng. void cvAddWeighted(const CvArr* src1, double alpha, const CvArr* src2, double beta, double gamma, CvArr* dst); src1 Cc mng ngun u tin alpha Trng lng cho cc phn t mng u tin src2 Cc mng ngun th hai beta Trng lng cho cc phn t mng th hai dst Cc mng ch gamma v hng, thm vo s tin mi Hm tnh ton tng hp c trng s ca hai mng nh sau: dst(I)=src1(I)*alpha+src2(I)*beta+gamma Tt c cc mng phi c cng loi v kch thc tng t (hoc kch thc ROI). i vi cc loi c hn ch phm vi hot ng ny c bo ha. 28

cvAnd Tnh ton cho mi phn t bit-khn ngoan kt hp ca hai mng. void cvAnd(const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask=NULL); src1 Cc mng ngun u tin src2 Cc mng ngun th hai dst Cc mng ch mt n mt n hot ng, 8-bit knh mng; quy nh c th cc yu t ca mng ch c thay i Hm tnh ton cho mi phn t bit-khn ngoan kt hp hp l ca hai mng: dst(I)=src1(I)&src2(I) if mask(I)!=0 Trong trng hp mng im ni i din bit c s dng cho hot ng. Tt c cc mng phi c cng loi, tr mt n, v kch thc ging nhau. cvAndS Tnh ton kt hp khn ngoan-bit cho mi phn t ca mt mng v mt v hng. void cvAndS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL); src Cc mng ngun value v hng s dng trong hot ng dst Cc mng ch mask mt n hot ng, 8-bit knh mng; quy nh c th cc yu t ca mng ch c thay i Hm tnh ton kt hp khn ngoan-bit cho mi phn t ca mt mng v mt v hng: dst(I)=src(I)&value if mask(I)!=0 Trc khi hot ng thc t, v hng c chuyn i cng loi nh ca mng (s). Trong trng hp ca cc mng i din ca h bit floating-point c s dng cho hot ng. Tt c cc mng phi c cng loi, tr mt n, v kch thc ging nhau. Cc mu sau y s chng minh lm th no tnh gi tr tuyt i ca mng floating-point cc yu t bng cch thanh ton b tr cc bit c ngha nht: float a[] = { -1, 2, -3, 4, -5, 6, -7, 8, -9 }; CvMat A = cvMat(3, 3, CV\_32F, &a); int i, absMask = 0x7fffffff; cvAndS(&A, cvRealScalar(*(float*)&absMask), &A, 0); for(i = 0; i < 9; i++ ) printf("%.1f ", a[i]); M nn in: 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 cvAvg Tnh trung bnh (trung bnh) ca cc phn t mng. CvScalar cvAvg(const CvArr* arr, const CvArr* mask=NULL); arr Mng 29

mask Mt n mt n hot ng ty chn Hm tnh ton M gi tr trung bnh ca cc phn t mng, c lp cho mi knh:

Nu mng l IplImage v COI c thit lp, Hm x l cc knh c la chn ch v cc ca hng trung bnh S0 thnh phn u tin v hng. cvAvgSdv Tnh trung bnh (trung bnh) ca cc phn t mng. void cvAvgSdv(const CvArr* arr, CvScalar* mean, CvScalar* stdDev, const CvArr* mask=NULL); arr Mng mean l Con tr tr ti cc gi tr trung bnh u ra, c th c NULL nu n khng phi l cn thit stdDev Pointer n lch chun u ra Mt n mt n hot ng ty chn Hm tnh ton lch gi tr v tiu chun trung bnh ca cc phn t mng, independently cho mi knh:

Nu mng l IplImage v COI c thit lp, Hm x l cc knh c la chn ch v cc ca hng trung bnh v lch tiu chun cc thnh phn u tin ca v hng u ra (mean0 v stdDev0). cvCalcCovarMatrix Tnh ma trn hip phng sai ca mt tp cc vect. void cvCalcCovarMatrix(const CvArr** vects, int count, CvArr* covMat, CvArr* avg, int flags); vects vector u vo, tt c u phi c cng loi v kch thc ging nhau . Cc vect khng c c 1D, h c th c 2D (v d, hnh nh) ... count s ca vect u vo covMat ma trn hip phng sai u ra nn c th ni im v vung avg u vo hay u ra (ty thuc vo nhng l c) mng - trung bnh (trung bnh) vector ca u vo vector flags c hot ng, mt s kt hp ca cc gi tr sau CV_COVAR_SCRAMBLED tranh ginh ma trn hip phng sai u ra c tnh nh sau : 30

scale * [vects[0] - avg, vects[1] avg,]T .[vects[0] avg, vects[1] avg,], l, ma trn hip phng sai s count x count. Nh mt ma trn hip phng sai bt thng c s dng nhanh chng PCA ca mt tp hp cc vector rt ln (xem, v d, EigenFaces k thut nhn din khun mt). Gi tr ring ca ma trn ny "tranh ginh" s ph hp vi gi tr ring ca ma trn hip phng sai s tht v "true" eigenvectors c th d dng tnh t cc eigenvectors ca ma trn "tranh ginh" hip phng sai. CV_COVAR_NORMAL ma trn hip phng sai u ra c tnh nh sau: scale * [vects[0] - avg, vects[1] avg,] .[vects[0] avg, vects[1] avg,] T , l, covMat s l mt ma trn hip phng sai vi kch thc tuyn tnh tng t nh tng s phn t trong mi vector u vo. Mt v ch c mt CV_COVAR_SCRAMBLED v CV_COVAR_NORMAL phi c xc nh CV_COVAR_USE_AVG Nu l c c quy nh c th Hm khng tnh ton avg vector u vo, nhng thay vo , s dng vector avg thng qua. iu ny l hu ch nu avg c c tnh ton bng cch no , hoc nu ma trn hip phng sai c tnh bi cc b phn trong trng hp, avg l khng phi l mt vector c ngha l u vo thit lp ca vect, m l gi tr trung bnh vector ca ton b thit lp. CV_COVAR_SCALE Nu l c c quy nh c th, ma trn hip phng sai c thu nh. Trong "bnh thng" ch quy m '1 / s, quy m "tranh ginh" ch i ng trong tng s s phn t trong mi vector u vo. Theo mc nh (nu c khng c ch nh) ma trn hip phng sai khng phi l quy m ('quy m = 1'). CV_COVAR_ROWS C ngha l tt c cc vect u vo c lu tr nh cc hng ca mt ma trn duy nht, vects [0].count c b qua trong trng hp ny, v avg phi l mt vector n hng ca mt thch hp kch thc. CV_COVAR_COLS C ngha l tt c cc vect u vo c lu tr nh cc ct ca mt ma trn duy nht, vects [0]. s c b qua trong trng hp ny, v avg phi l mt vector ct duy nht ca mt kch thc thch hp. Hm tnh ton ma trn hip phng sai, v ty chn, vector trung bnh ca tp hp cc u vo vector. Hm ny c th c s dng cho PCA, so snh vector s dng khong cch Mahalanobis v vv.

cvCartToPolar Tnh cng v/hoc gc ca vector 2D. void cvCartToPolar( const CvArr* x, const CvArr* y, CvArr* magnitude, CvArr* angle=NULL, int angleInDegrees=0); x Cc mng ca x-ta y Cc mng ca y-ta magnitude ln mng ch ca ln, c th c thit lp NULL nu n khng phi l cn thit angle gc mng ch ca gc , c th c thit lp NULL nu n khng phi l cn thit. Cc gc l o bng radian (0-2) hoc cc mc (t 0 n 360 ). angleInDegrees L c cho bit cc gc c o bng radian, l ch li, hoc cc mc Hm tnh ton ln, gc, hoc c hai ca tt c cc vector 2d (x (I), y (I)): 31

magnitude(I)=sqrt(x(I)2+y(I)2 ), angle(I)=atan(y(I)/x(I) ) Cc gc c tnh ton vi 0.1 chnh xc. i vi im (0,0), gc c thit lp l 0. cvCbrt Tnh gc khi float cvCbrt(float value); value Gi tr u vo gi tr du chm ng Hm tnh ton cn khi ca cc i s, v thng thng n l nhanh hn so vi pow (gi tr, 1 / 3.). Ngoi ra, lp lun tiu cc c x l ng cch. Gi tr c bit (NaN) khng c x l. cvClearND Xa mt phn t mng c th. void cvClearND(CvArr* arr, int* idx); arr u vo mng idx mng ca cc ch s thnh phn CvClearND Hm xa b khng mt yu t c th ca mt mng dy c hoc xa cc phn t ca mt mng tha tht. Nu phn t mng tha tht khng tn ti, Hm khng c g. cvCloneImage Lm cho mt bn sao y ca mt hnh nh, bao gm tiu , d liu, v ROI. IplImage* cvCloneImage(const IplImage* image); image hnh nh ban u Tr li IplImage * im cho cc bn sao hnh nh.

cvCloneMat To mt bn sao ma trn y . CvMat* cvCloneMat(const CvMat* mat); mat ma trn c sao chp To mt bn sao y ca mt ma trn v tr v mt con tr sao chp cc. cvCloneMatND To bn sao y ca mt mng a chiu v tr v mt con tr sao chp cc. CvMatND* cvCloneMatND(const CvMatND* mat); mat u vo mng cvCloneSparseMat To bn sao y ca mng tha tht. CvSparseMat* cvCloneSparseMat(const CvSparseMat* mat); mat u vo mng Hm ny to ra mt bn sao ca mng u vo v tr v con tr sao chp. cvCmp Thc hin mi phn t so snh ca hai mng. void cvCmp(const CvArr* src1, 32

const CvArr* src2, CvArr* dst, int cmpOp); src1 Cc mng ngun u tin src2 Cc mng ngun th hai. C hai mng ngun phi c mt knh duy nht. dst Mng ch, phi c loi 8u hoc 8s cmpOp Cc c xc nh mi quan h gia cc yu t c kim tra CV_CMP_EQ src1 (I) "bng" gi tr CV_CMP_GT src1 (I) "qu mc" gi tr CV_CMP_GE src1 (I) ln hn hoc bng gi tr CV_CMP_LT src1 (I) "thp hn" gi tr CV_CMP_LE src1 (I) "t hn hoc bng gi tr CV_CMP_NE src1 (I) "khng bng" gi tr Hm so snh cc yu t tng ng ca hai mng v in vo cc mt n ch mng: dst(I)=src1(I) op src2(I), dst (I) c thit lp 0xff (tt c 1-bit) nu mi quan h c th gia cc yu t l ng v 0 khc. Tt c cc mng phi c cng loi, tr im n, v kch thc tng t (hoc ROI kch thc) cvCmpS Thc hin so snh mi phn t ca mt mng v mt v hng. void cvCmpS(const CvArr* src, double value, CvArr* dst, int cmpOp); src mng ngun, phi c mt knh duy nht value gi tr v hng so snh tng phn t mng dst Mng ch, phi c loi 8u hoc 8s cmpOp Cc c xc nh mi quan h gia cc yu t c kim tra CV_CMP_EQ src1 (I) "bng" gi tr CV_CMP_GT src1 (I) "qu mc" gi tr CV_CMP_GE src1 (I) ln hn hoc bng gi tr CV_CMP_LT src1 (I) "thp hn" gi tr CV_CMP_LE src1 (I) "t hn hoc bng gi tr CV_CMP_NE src1 (I) "khng bng" gi tr Hm so snh cc yu t tng ng ca mt mng v mt v hng v lp y destination mt n mng: dst (I) = src(I) op v hng ni op l =,>, >=, <=, hoc !=. dst (I) c thit lp 0xff (tt c 1-bit) nu mi quan h c th gia cc yu t l ng v 0 khc. Tt c cc mng phi c cng kch thc (hoc kch thc ROI). cvConvertScale Chuyn i mt mng khc vi ty chn chuyn i tuyn tnh. void cvConvertScale(const CvArr* src, CvArr* dst, double scale=1, double shift=0); #define cvCvtScale cvConvertScale #define cvScale cvConvertScale #define cvConvert(src, dst ) cvConvertScale((src), (dst), 1, 0 ) 33

src Ngun mng dst im n mng scale quy m Quy m shift thay i gi tr gia tng cho cc yu t ngun mng quy m Hm c mt s mc ch khc nhau, v do c nhiu tn khc nhau. N sao chp mt mng khc vi ty chn m rng quy m, l thc hin u tin v / hoc chuyn i loi hnh ty chn, thc hin sau: dst(I) = scalesrc(I) + (shift0, shift1,) Tt c cc knh ca cc mng a knh c x l c lp. Loi chuyn i c thc hin lm trn s v bo ha, l nu kt qu ca m rng quy m + chuyn i c th khng c i din chnh xc bi mt gi tr ca loi phn t mng ch, l thit lp cc gi tr biu din gn nht trn trc thc. Trong trng hp c scale=1, shift=0 prescaling khng c thc hin. y l mt trng hp c bit ti u v n cvConvert tn thch hp . Nu ngun v ch cc loi mng c bng nhau loi, iu ny cng l mt trng hp c bit c th c s dng quy m v thay i mt ma trn hay mt hnh nh v c caled cvScale. cvConvertScaleAbs Chuyn i cc phn t mng u vo mt s nguyn unsigned 8-bit vi ty chn chuyn i tuyn tnh. void cvConvertScaleAbs(const CvArr* src, CvArr* dst, double scale=1, double shift=0); src Ngun mng dst im n mng dst (nn c chiu su 8u) quy m ScaleAbs yu t thay i gi tr gia tng cho cc yu t ngun mng quy m Hm ny tng t nh cvConvertScale , nhng n lu tr gi tr tuyt i ca chuyn i kt qu: dst (I) = | scalesrc (I) + (shift0, shift1, ...)| Hm h tr cc im n mng ch ca 8u (8-bit s nguyn khng du) loi, cho cc loi Hm c th c m phng bi mt s kt hp ca cc Hm cvConvertScale v cvAbs . cvCvtScaleAbs Chuyn i cc phn t mng u vo mt s nguyn unsigned 8-bit vi ty chn chuyn i tuyn tnh. void cvCvtScaleAbs(const CvArr* src, CvArr* dst, double scale=1, double shift=0); src Ngun mng dst im n mng (nn c chiu su 8u) scale quy m ScaleAbs yu t shift thay i gi tr gia tng cho cc yu t ngun mng quy m Hm ny tng t nh cvConvertScale , nhng n lu tr gi tr tuyt i ca chuyn i kt qu: dst (I) = | scalesrc (I) + (shift0, shift1, ...)| Hm h tr cc im n mng ch ca 8u (8-bit s nguyn khng du) loi, cho cc loi Hm c th c m phng bi mt s kt hp ca cc Hm cvConvertScale v cvAbs . 34

cvCopy Bn sao mt mng khc. void cvCopy(const CvArr* src, CvArr* dst, const CvArr* mask=NULL); src Cc mng ngun dst Cc mng ch mask mt n hot ng, 8-bit knh mng; quy nh c th cc yu t ca mng ch c thay i Hm sao chp cc yu t c la chn t mt mng u vo mt mng u ra: dst(I) = src(I) if mask(I) = 0: Nu bt k ca cc mng thng qua IplImage loi, sau thu nhp t u ca n v cc lnh vc COI c s dng. C hai mng phi c cng loi, cng kch thc, v kch thc ging nhau. Cc Hm cng c th sao chp cc mng tha tht (mt n khng c h tr trong trng hp ny). cvCountNonZero S lng cc yu t mng khc khng. int cvCountNonZero(const CvArr* arr); arr mng phi l mt knh duy nht mng hoc mt hnh nh a knh vi COI thit lp Hm tr v s lng cc yu t khc khng trong arr: (arr( 0)

Trong trng hp ca IplImage c hai ROI v COI c h tr. cvCreateData Phn b mng d liu void cvCreateData(CvArr* arr); arr mng tiu Hm phn b hnh nh, ma trn hoc mng d liu a chiu. Lu rng trong trng hp cc loi ma trn Hm OpenCV phn b c s dng v trong trng hp ca IplImage h c s dng tr khi CV_TURN_ON_IPL_COMPATIBILITY c gi l. Trong trng hp th hai, Hm IPL c s dng phn b d liu. cvCreateImage To mt tiu hnh nh v phn b cc d liu hnh nh. IplImage* cvCreateImage(CvSize size, int depth, int channels); size Hnh nh kch thc chiu rng v chiu cao depth Bit su ca cc yu t hnh nh. Xem IplImage cho su hp l. channels s knh ca cc knh cho mi im nh. Xem IplImage bit thm chi tit. Hm ny ch to ra hnh nh vi cc knh xen k. Cuc gi ny l mt hnh thc rt gn ca header = cvCreateImageHeader(size, depth, channels); cvCreateData(header); 35

cvCreateImageHeader To mt tiu hnh nh nhng khng phn b d liu hnh nh. IplImage* cvCreateImageHeader(CvSize size, int depth, int channels); size kch thc chiu rng v chiu cao depth Hnh nh chiu su chiu su (xem cvCreateImage) channels S lng cc knh ca cc knh (xem cvCreateImage) Cuc gi ny l mt cht tng t hdr=iplCreateImageHeader(channels, 0, depth, channels == 1 ? "GRAY" : "RGB", channels == 1 ? "GRAY" : channels == 3 ? "BGR" : channels == 4 ? "BGRA" : "", IPL_DATA_ORDER_PIXEL, IPL_ORIGIN_TL, 4, size.width, size.height, 0,0,0,0); nhng n khng s dng cc Hm IPL theo mc nh (xem CV_TURN_ON_IPL_COMPATIBILITY).

cvCreateMat To ra mt tiu ma trn v phn b d liu ma trn. CvMat* cvCreateMat(int rows,int cols,int type); rows S lng hng trong ma trn cols S lng ct trong ma trn type Cc loi ma trn cc yu t trong hnh thc CV_<bit depth><S|U|F> C <s knh>, trong S = k kt, U = unsigned, F = float. V d, CV 8UC1 c ngha l cc yu t c 8bit unsigned v c 1 knh, v CV 32SC2 c ngha l cc yu t 32-bit k v c 2 knh. y l hnh thc ngn gn: CvMat* mat = cvCreateMatHeader(rows, cols, type); cvCreateData(mat); cvCreateMatHeader To mt tiu ma trn, nhng khng phn b d liu ma trn. CvMat* cvCreateMatHeader(int rows,int cols,int type); rows S lng hng trong ma trn cols S lng ct trong ma trn type Loi cc yu t ma trn, xem cvCreateMat Hm phn b mt tiu ma trn mi v tr v mt con tr n n. Cc d liu ma trn c th sau c phn b s dng cvCreateData hoc thit lp mt cch r rng ngi s dng d liu thng qua giao cvSetData. cvCreateMatND To tiu v phn b d liu cho mt mng a chiu dy c. CvMatND* cvCreateMatND(int dims,const int* sizes,int type); dims s kch thc mng. iu ny khng c vt qu CV_MAX_DIM (32 theo mc nh, nhng c th c thay i thi gian xy dng). sizes mng c kch thc kch thc. type Loi phn t mng, xem cvCreateMat. 36

y l mt hnh thc vit tt ca: CvMatND* mat = cvCreateMatNDHeader(dims, sizes, type); cvCreateData(mat); cvCreateMatNDHeader To mt tiu ma trn mi nhng khng phn b d liu ma trn. CvMatND* cvCreateMatNDHeader(int dims, const int* sizes, int type); dims Number of array dimensions kch thc mng c kch thc kch thc Loi kiu ca cc phn t mng, xem cvCreateMat Hm phn b mt tiu cho mt mng a chiu dy c. Cc mng d liu c th tip tc c phn b bng cch s dng cvCreateData hoc thit lp mt cch r rng ngi s dng d liu thng qua giao cvSetData. cvCreateSparseMat To mng tha tht. CvSparseMat* cvCreateSparseMat(int dims, const int* sizes, int type); dims s kch thc mng. Ngc li vi ma trn dy c, s lng kch thc thc t khng gii hn (ln n 216). sizes kch thc mng c kch thc kch thc type Loi phn t mng. Ging nh cho CvMat Hm phn b mt mng a chiu tha tht. Ban u, cc mng khng c tnh, cvGet hoc cvGetReal tr v zero cho mi ch s. cvCrossProduct Tnh sn phm cho ca hai vect 3D. void cvCrossProduct(const CvArr* src1, const CvArr* src2, CvArr* dst); src1 Cc vector ngun u tin src2 Cc vector ngun th hai dst Cc vector ch Hm tnh ton sn phm cho ca hai vect 3D: dst = src1 x src2 hoc: dst1 = src12src23 - src13src22 dst2 = src13src21 - src11src23 dst3 = src11src22 - src12src21 CvtPixToPlane ng ngha cho chia . cvDCT Thc hin mt v pha trc hoc ngc ri rc c sin chuyn i ca mt mng oating-point 1D hoc 2D. void cvDCT(const CvArr* src, CvArr* dst, int flags); src Ngun mng, bt mng 1D hoc 2D dst im n mng c cng kch thc v cng mt loi nh l ngun gc c chuyn i c, mt s kt hp ca cc gi tr sau 37

CV_DXT_FORWARD mt bin i tin 1D hoc 2D. CV_DXT_ngc mt 1D ngc hoc chuyn i 2D. CV_DXT_ROWS lm mt bin i v pha trc hoc ngc ca tt c cc hng c nhn ca ma trn u vo. L c ny cho php ngi dng chuyn i cc vector nhiu cng mt lc v c th c s dng gim cc chi ph (m i khi ln hn gp nhiu ln so vi ch bin chnh n), lm bin i 3D v chiu cao hn v vv. Hm thc hin mt bin i v pha trc hoc nghch o ca mt 1D hoc mng 2D ni im: Chuyn tip c sin bin i ca vector 1D ca cc yu t N: Y = C(N) . X ni V = 1, = 2 v > 0 Inverse cosin bin i ca vector 1D ca cc yu t N: (C(N) l ma trn trc giao, C(N) . C(N)T = I) Chuyn tip c sin bin i 2D M x N ma trn:

Inverse c sin bin i ca vector 2D ca cc yu t M x N:

cvDFT Thc hin mt Fourier ri rc v pha trc hoc ngc bin i ca mt mng ni im 1D hoc 2D. void cvDFT(const CvArr* src, CvArr* dst, int flags, int nonzeroRows=0); src Source mng, thc hay phc tp dst im n mng c cng kch thc v cng mt loi nh l ngun gc flags chuyn i c, mt s kt hp ca cc gi tr sau : CV_DXT_FORWARD mt bin i tin 1D hoc 2D. Kt qu l khng thu nh li. CV_DXT_INVERSE mt 1D ngc hoc chuyn i 2D. Kt qu l khng thu nh li. CV_DXT_FORWARD v CV_DXT_INVERSE nghch o l loi tr ln nhau, tt nhin. CV_DXT_SCALE quy m kt qu: chia n cho s lng cc phn t mng. Thng thng, l kt hp vi CV_DXT_INVERSE, v ngi ta c th s dng mt phm tt CV_DXT_INV_SCALE. CV_DXT_ROWS lm mt bin i v pha trc hoc ngc ca tt c cc hng c nhn ca ma trn u vo. C ny cho php ngi s dng chuyn i vector nhiu cng mt lc v c th c s dng gim chi ph (m i khi ln hn gp nhiu ln so vi ch bin chnh n), lm bin i 3D v chiu cao hn v vv. CV_DXT_INVERSE_SCALE ging nh CV_DXT_INVERSE + CV_DXT_SCALE 38

nonzeroRows S hng khc khng trong mng ngun (trong trng hp ca mt tin 2d xuyn hnh thc), hoc mt s hng quan tm trong mng im n (trong trng hp ca mt 2d ngc bin i). Nu gi tr l tiu cc, bng khng, hoc ln hn tng s ca hng. Tham s c th c s dng tng tc chp 2d / tng quan khi tnh ton thng qua DFT. Xem v d di y. Hm thc hin mt bin i v pha trc hoc nghch o ca mt 1D hoc mng 2D ni im: Chuyn tip bin i Fourier ca vector 1D ca cc yu t N:

Inverse bin i Fourier ca vector 1D ca cc yu t N: Chuyn tip bin i Fourier ca vector 2D ca cc yu t M x N: Y = F(M) . X . F(N) Inverse Fourier bin i ca vector 2D ca cc yu t M x N: Trong trng hp cc d liu thc s (mt knh), nh dng ng gi, c vay mn t IPL, c s dng i din cc kt qu ca mt bin i Fourier v pha trc hoc u vo cho mt bin i Fourier ngc:

Lu : ct cui cng l nu N l thm ch, hng cui cng l nu M thm ch cn. Trong trng hp ca 1D thc s chuyn i kt qu trng ging nh dng u tin ca ma trn trn. Di y l v d v lm th no tnh ton 2D chp s dng DFT. CvMat* A = cvCreateMat(M1, N1, CVg32F); CvMat* B = cvCreateMat(M2, N2, A->type); // it is also possible to have only abs(M2-M1)+1 times abs(N2-N1)+1 // part of the full convolution result CvMat* conv = cvCreateMat(A->rows + B->rows - 1, A->cols + B->cols - 1, A->type); // initialize A and B ... int dftgM = cvGetOptimalDFTSize(A->rows + B->rows - 1); int dftgN = cvGetOptimalDFTSize(A->cols + B->cols - 1); CvMat* dftgA = cvCreateMat(dft\_M, dft\_N, A->type); CvMat* dftgB = cvCreateMat(dft\_M, dft\_N, B->type); CvMat tmp; // copy A to dftgA and pad dft\_A with zeros 39

cvGetSubRect(dftgA, &tmp, cvRect(0,0,A->cols,A->rows)); cvCopy(A, &tmp); cvGetSubRect(dftgA, &tmp, cvRect(A->cols,0,dft\_A->cols - A->cols,A->rows)); cvZero(&tmp); // no need to pad bottom part of dftgA with zeros because of // use nonzerogrows parameter in cvDFT() call below cvDFT(dftgA, dft\_A, CV\_DXT\_FORWARD, A->rows); // repeat the same with the second array cvGetSubRect(dftgB, &tmp, cvRect(0,0,B->cols,B->rows)); cvCopy(B, &tmp); cvGetSubRect(dftgB, &tmp, cvRect(B->cols,0,dft\_B->cols - B->cols,B->rows)); cvZero(&tmp); // no need to pad bottom part of dftgB with zeros because of // use nonzerogrows parameter in cvDFT() call below cvDFT(dftgB, dft\_B, CV\_DXT\_FORWARD, B->rows); cvMulSpectrums(dftgA, dft\_B, dft\_A, 0 /* or CV\_DXT\_MUL\_CONJ to get correlation rather than convolution */); cvDFT(dftgA, dft\_A, CV\_DXT\_INV\_SCALE, conv->rows); // calculate only // the top part cvGetSubRect(dftgA, &tmp, cvRect(0,0,conv->cols,conv->rows)); cvCopy(&tmp, conv);

cvDecRefData Decrements mt mng tham chiu d liu truy cp. void cvDecRefData(CvArr* arr); arr con tr n mt mng tiu Decrements Hm truy cp d liu tham chiu trong mt CvMat hoc CvMatND nu cc ti liu tham kho con tr truy cp khng phi l NULL. Nu truy cp t n s khng, d liu c deallocated. Trong hin ti truy cp thc hin cc ti liu tham kho khng phi l NULL ch khi d liu c phn b bng cch s dng cc cvCreateData Hm. Truy cp s c NULL trong cc trng hp khc nh: d liu bn ngoi c giao cho tiu bng cch s dng cvSetData, cc tiu ma trn l mt phn ca mt ma trn ln hn hoc hnh nh , hoc tiu c chuyn i t mt hnh nh hoc tiu ma trn n-chiu. cvDet Tr v yu t quyt nh ca mt ma trn. double cvDet(const CvArr* mat); mat Cc ma trn ngun Hm tr v yu t quyt nh ca mat ma trn vung. Cc phng php trc tip c s dng cho ma trn nh v loi b Gaussian c s dng cho ma trn ln hn. i vi i xng tch cc xc nh cc ma trn, n cng c th chy cvSVD vi U = V = 0 v sau tnh ton yu t quyt nh l mt sn phm ca cc yu t ng cho ca W. cvDiv Performs per-element division of two arrays. void cvDiv(const CvArr* src1, 40

const CvArr* src2, CvArr* dst, double scale=1); src1 Cc mng ngun u tin. Nu con tr NULL, mng l gi nh l tt c 1. src2 Cc mng ngun th hai dst Cc mng ch scale ty chn quy m yu t Hm phn chia mt mng khc:

Tt c cc mng phi c cng loi v kch thc tng t (hoc kch thc ROI). cvDotProduct Tnh ton du chm sn phm ca hai mng s liu clt. double cvDotProduct(const CvArr* src1, const CvArr* src2); src1 Cc mng ngun u tin src2 Cc mng ngun th hai Hm tnh ton v tr v sn phm dot Euclide ca hai mng. src1 . src2 =

Trong trng hp ca nhiu mng knh, cc kt qu cho tt c cc knh c tch ly. C th, cvDotProduct(a,a) l mt vector phc tp, s tr v ||a||2. Hm c th qu trnh a chiu mng, hng ca hng, lp, tng lp, v nh vy. cvEigenVV Tnh gi tr ring v eigenvectors ca mt ma trn i xng. void cvEigenVV(CvArr* mat, CvArr* evects, CvArr* evals, double eps=0, int lowindex = -1, int highindex = -1); mat u vo i xng vung ma trn, sa i trong qu trnh ch bin evects ma trn u ra ca eigenvectors, c lu tr nh cc dng tip theo evals vector u ra ca gi tr ring, c lu tr theo th t gim dn (th t ca gi tr ring v eigenvectors syncronized, tt nhin) eps chnh xc diagonalization. Thng thng, DBL_EPSILON (khong 10^-15) hot ng tt. THNG S NY HIN L B QUA. lowindex ty chn ch s ln nht eigenvalue /-vector tnh ton . (Xem di y.) highindex Ty chn ch s nh nht-gi tr ring /-vector tnh ton . (Xem di y.) Hm tnh ton cc gi tr ring v eigenvectors ca ma trn A: mat*evects(i,:) = evals(i)*evects(i,:) (trong MATLAB k hiu) Nu hoc thp hoc highindex c cung cp khc l cn thit, qu. Lp ch mc l 0-da. V d: tnh ton ln nht eigenvector / gi tr thit lp lowindex = highindex = 0. tnh ton tt c cc gi tr ring, li lowindex = highindex =- 1. i vi cc l do di sn Hm ny lun lun tr v mt ma trn vung kch thc tng t nh ma trn ngun eigenvectors v 41

src1(I) . src2(I)

vector mt chiu di ca ma trn ngun vi gi tr ring. Cc eigenvectors chn / gi tr lun l ngi u tin highindex - lowindex + 1 hng. Ni dung ca ma trn A l b ph hy bi Hm. Hin nay Hm l chm hn so vi cvSVD chnh xc nhng t hn, v vy nu A c bit n l tch cc nh ngha (v d, l mt ma trn hip phng sai), nn s dng cvSVD tm gi tr ring v eigenvectors A, c bit l nu eigenvectors l khng cn thit.

cvExp Tnh s m ca mi phn t mng. void cvExp(const CvArr* src, CvArr* dst); src Cc mng ngun dst Mng ch, n cn phi c loi tng gp i hoc loi tng t nh ngun Hm tnh ton s m ca tt c cc yu t ca mng u vo: dst[I] = esrc(I) Cc li tng i ti a l khong 7.10-6. Hin nay, Hm chuyn i denormalized gi tr cho s khng v sn lng. cvFastArctan Tnh cc gc ca mt vector 2D. float cvFastArctan (float y, float x); x x-phi hp ca 2D vector y y-phi hp ca 2D vector Hm tnh ton gc y tm ca mt vector u vo 2D. Gc c o bng v thay i t 0 n 360 . Chnh xc l khong 0,1 . cvFlip Lt mt mng 2D xung quanh trc thng ng, ngang hoc c hai. void cvFlip(const CvArr* src, CvArr* dst=NULL, int flipMode=0); src Ngun mng dst im n mng. Nu dst = NULL lt c thc hin ti ch. flipMode Ch nh lm th no lt cc mng: 0 c ngha l flipping xung quanh trc x, tch cc (v d, 1 ) c ngha l flipping xung quanh trc y, tiu cc (v d, -1) c ngha l lt xung quanh c hai trc. Xem cng tho lun di y cc cng thc: Hm bt cc mng trong mt trong ba cch khc nhau (ch s hng v ct l 0-):

Cc kch bn v d v s dng Hm l: Lt theo chiu dc ca hnh nh (flipMode = 0) chuyn i gia cc hnh nh trn cng bn tri v pha di bn tri ngun gc, m l mt hot ng in hnh trong x l video theo cc h thng Win32. Lt ngang ca hnh nh vi s thay i ngang tip theo v s khc bit tuyt i? Calculation kim tra mt i xng trc thng ng (flipMode > 0) 42

ng thi ngang, lt dc ca hnh nh vi s thay i tip theo v tuyt i tnh ton kim tra s khc bit cho mt i xng trung tm (flipMode <0) o ngc th t ca cc mng im 1d (flipMode? 0)? cvGEMM Thc hin php nhn ma trn tng qut. void cvGEMM(const CvArr* src1, const CvArr* src2, double alpha, const CvArr* src3, double beta, CvArr* dst, int tABC=0); #define cvMatMulAdd(src1, src2, src3, dst ) cvGEMM(src1, src2, 1, src3, 1, dst, 0 ) #define cvMatMul(src1, src2, dst ) cvMatMulAdd(src1, src2, 0, dst ) src1 Cc mng ngun u tin src2 Cc mng ngun th hai src3 mng ngun th ba (thay i). C th c NULL, nu c s thay i. dst Cc mng ch tABC c hot ng c th l 0 hoc s kt hp ca cc gi tr sau CV_GEMM_AT chuyn src1 CV_GEMM_BT chuyn src2 CV_GEMM_CT chuyn src3 V d, CV_GEMM_AT + CV_GEMM_CT tng ng alpha src1T src2 + beta src3T Hm thc hin php nhn ma trn tng qut: dst = alpha op(src1) op(src2) + beta op(src3) where op(X) is X or XT Tt c cc ma trn cn phi c cng mt kiu d liu v kch c phi hp. Real hoc phc tp ma trn ni-im c h tr.

cvGet1D Tr li mt phn t mng c th. CvScalar cvGet1D(const CvArr* arr, int idx0); CvScalar cvGet2D(const CvArr* arr, int idx0, int idx1); CvScalar cvGet3D(const CvArr* arr, int idx0, int idx1, int idx2); CvScalar cvGetND(const CvArr* arr, int* idx); arr u vo mng idx0 s khng da trn thnh phn ca ch s yu t u tin idx1 s khng da trn thnh phn ca ch s yu t idx2 s khng da trn th ba thnh phn ca ch s yu t idx mng ca cc ch s thnh phn Cc Hm tr li mt phn t mng c th. Trong trng hp ca mt mng tha tht Hm quay tr li 0 nu nt c yu cu khng tn ti (khng c nt mi c to ra bi cc Hm). cvGetCol (s) Tr v mng ct hoc khong ct. 43

CvMat* cvGetCol(const CvArr* arr, CvMat* submat, int col); CvMat* cvGetCols(const CvArr* arr, CvMat* submat, int startCol, int endCol); arr u vo mng submat Con tr tr ti cc tiu ph mng kt qu col Zero-da trn ch s ca ct c la chn startCol Zero-da trn ch s ca ct bt u (bao gm) ca nhp endCol Zero-da trn ch s ca ct kt thc (c quyn) ca nhp GetCol Hm v GetCols tr li tiu , tng ng vi mt ct c th khong mng u vo. GetCol l mt phm tt cho cvGetCols: cvGetCol(arr, submat, col); // cvGetCols(arr, submat, col, col + 1); cvGetDiag Tr li mt ng cho mng. CvMat* cvGetDiag(const CvArr* arr, CvMat* submat, int diag=0); arr u vo mng submat Con tr tr ti cc tiu ph mng kt qu chn mng ng cho. Khng tng ng vi ng cho chnh, tng ng vi -1 ng cho trn, tng ng vi chnh 1 ng cho di chnh, v vv. Hm tr v tiu , tng ng vi mt ng cho quy nh ca mng u vo. cvGetDims, cvGetDimSize Quay tr li s kch thc mng v kch thc ca chng, hoc kch thc ca mt kch thc c th. int cvGetDims(const CvArr* arr, int* sizes=NULL); int cvGetDimSize(const CvArr* arr, int index); arr mng u vo sizes kch thc ty chn u ra ca cc kch thc kch thc mng. i vi mng 2d s hng (Chiu cao) i s, u tin ca ct (chiu rng). index ch s da trn kch thc Zero-(cho ma trn 0 c ngha l s lng hng, 1 c ngha l s lng ct, cho hnh nh 0 c ngha l chiu cao, 1 c ngha l chiu rng) Hm CvGetDims tr v mng v mt mng c kch thc kch thc chiu. Trong trng hp ca IplImage hoc CvMat n lun lun tr v 2 bt k s lng hnh nh / ma trn hng. CvGetDimSize Hm tr v kch thc kch thc c th (s lng ca cc yu t mi rng kch thc). V d, cc m sau y tnh ton tng s ca cc phn t mng trong hai cch: // Thng qua cvGetDims () int sizes[CV_MAX_DIM]; int i, total = 1; int dims = cvGetDims(arr, size); for(i = 0; i < dims; i++ ) total *= sizes[i]; // Thng qua cvGetDims () v cvGetDimSize () int i, total = 1; int dims = cvGetDims(arr); for(i = 0; i < dims; i++ ) 44

total *= cvGetDimsSize(arr, i); cvGetElemType Tr v loi phn t mng. int cvGetElemType(const CvArr* arr); arr u vo mng Hm ny tr v kiu ca cc phn t mng nh m t trong cuc tho lun cvCreateMat : CV_8UC1 ... CV_64FC4. cvGetImage Tr v tiu hnh nh cho mng ty . IplImage* cvGetImage(const CvArr* arr, IplImage* imageHeader); arr u vo mng imageHeader Pointer IplImage cu trc c s dng nh mt b m tm thi Hm tr v tiu hnh nh cho cc mng u vo c th l mt ma trn CvMat , hoc mt hnh nh - IplImage *. Trong trng hp ca mt hnh nh Hm ch n gin l tr v con tr u vo. Trong trng hp ca CvMat n khi to mt cu trc imageHeader vi cc thng s ca ma trn u v. Lu rng nu chng ta bin i IplImage CvMat v sau bin i CvMat tr li IplImage, chng ti c th nhn c tiu khc nhau, nu ROI c thit lp, v do mt s Hm IPL tnh stride hnh nh t chiu rng ca n v sp xp c th tht bi trn cc hnh nh kt qu. cvGetImageCOI Tr v ch s ca knh quan tm. int cvGetImageCOI(const IplImage* image); image con tr n cc tiu hnh nh Tr v knh quan tm ca mt trong IplImage. Gi tr tr v tng ng vi coi trong cvSetImageCOI. cvGetImageROI Tr v ROI hnh nh. CvRect cvGetImageROI(const IplImage* image); image con tr n cc tiu hnh nh Nu c khng c thit lp ROI, cvRect(0, 0, image->width, image->height) c tr v. cvGetMat Tr v tiu ma trn cho mng ty . CvMat* cvGetMat(const CvArr* arr, CvMat* header, int* coi=NULL, int allowND=0); arr u vo mng tiu Pointer CvMat cu trc c s dng nh mt b m tm thi coi ty chn u ra tham s cho lu tr COI allowND Nu khng phi s khng, Hm chp nhn a chiu mng dy c (CvMatND *) v ti quay 2D (nu CvMatND c hai chiu) hoc ma trn 1D (khi CvMatND c 1 kch thc hoc nhiu hn 2 chiu). Cc mng phi c lin tc. Hm tr v mt ma trn tiu cho cc mng u vo c th l mt ma trn - CvMat , mt hnh nh - IplImage hoc mt mng a chiu dy c - CvMatND (sau trng hp cho php 45

ch khi allowND = 0). Trong trng hp ca ma trn Hm ch n gin l tr v con tr u vo. Trong trng hp ca IplImage * hoc CvMatND n khi to cc cu trc tiu vi cc thng s ca ROI hnh nh hin ti v tr v con tr n cu trc tm thi. Bi v COI l khng c h tr bi CvMat , n c tr li mt cch ring bit. Hm ny cung cp mt cch d dng x l c hai loi ca cc mng - IplImage v CvMat Bng cch s dng cng mt m. Ngc li bin i t CvMat IplImage c th c thc hin bng cch s dng cvGetImage. u vo mng phi c d liu c bn c phn b hoc nh km, nu khng Hm khng thnh cng. Nu cc mng u vo l IplImage vi cch b tr d liu phng v thit lp COI, Hm tr v con tr chn my bay v COI = 0. N cho php mi my bay x l hnh nh a knh vi cch b tr d liu phng bng cch s dng Hm OpenCV. cvGetNextSparseNode Tr v cc yu t ma trn tha tht tip theo CvSparseNode* cvGetNextSparseNode(CvSparseMatIterator* matIterator); matIterator tha tht mng iterator Hm di chuyn iterator ti phn t ma trn tha tht tip theo v tr v con tr n n. Trong phin bn hin hnh khng c bt k th t c th ca cc yu t, bi v chng c lu tr trong bng bm. Cc mu di y gii thch lm th no lp qua ma trn tha tht: S dng cvInitSparseMatIterator v cvGetNextSparseNode tnh ton tng ca floating-point tha tht mng. double sum; int i, dims = cvGetDims(array); CvSparseMatIterator mat_iterator; CvSparseNode* node = cvInitSparseMatIterator(array, &mat_iterator); for(; node != 0; node = cvGetNextSparseNode(&mat_iterator )) { /* get pointer to the element indices */ int* idx = CV_NODE_IDX(array, node); /* get value of the element (assume that the type is CV_32FC1) */ float val = *(float*)CV_NODE_VAL(array, node); printf("("); for(i = 0; i < dims; i++ ) printf("%4d%s", idx[i], i < dims - 1 "," : "): "); printf("%g\n", val); sum += val; } printf("\nTotal sum = %g\n", sum);

cvGetOptimalDFTSize Tr v kch thc DFT ti u cho mt kch thc vector nht nh. int cvGetOptimalDFTSize(int size0); size0 Vector kch thc 46

Hm tr v N s lng ti thiu ln hn hoc bng size0, nh vy m DFT ca mt vector c kch thc N c th c tnh ton nhanh. Trong vic thc hin hin nay ca N = 2p x 3q x 5r, i vi mt s p, q, r. Hm tr v mt s m nu size0 l qu ln (rt gn vi INT_MAX) cvGetRawData Ly thng tin mc thp v mng. void cvGetRawData(const CvArr* arr, uchar** data, int* step=NULL, CvSize* roiSize=NULL); arr mng tiu data u ra con tr n ngun gc ton b hnh nh hoc ngun gc thu nhp t u nu ROI c thit lp step u ra y hng chiu di theo byte roiSize u ra ROI kch thc Hm y bin u ra vi mc thp thng tin v cc d liu mng. Tt c cc u ra cc thng s l ty chn, v vy mt s cc con tr c th c thit lp NULL. Nu mng l IplImage vi thu nhp t u thit lp, cc thng s ca ROI c tr v. V d sau y cho thy lm th no c c quyn truy cp vo cc phn t mng. GetRawData tnh ton gi tr tuyt i ca cc yu t trong mt knh duy nht, im ni mng. float* data; int step; CvSize size; int x, y; cvGetRawData(array, (uchar**)&data, &step, &size); step /= sizeof(data[0]); for(y = 0; y < size.height; y++, data += step ) for(x = 0; x < size.width; x++ ) data[x] = (float)fabs(data[x]);

cvGetReal*D Quay tr li mt yu t c th ca mng knh duy nht. double cvGetReal1D(const CvArr* arr, int idx0); double cvGetReal2D(const CvArr* arr, int idx0, int idx1); double cvGetReal3D(const CvArr* arr, int idx0, int idx1, int idx2); double cvGetRealND(const CvArr* arr, int* idx); arr u vo mng. Phi c mt knh duy nht. idx0 s khng da trn thnh phn ca ch s yu t u tin idx1 s khng da trn thnh phn ca ch s yu t idx2 s khng da trn th ba thnh phn ca ch s yu t idx mng ca cc ch s thnh phn Cc Hm cvGetReal*D tr v mt yu t c th ca mt mng mt knh. Nu cc mng c nhiu knh khc nhau, mt li thi gian chy c nu ra. Lu rng cvGet Hm c th c 47

s dng mt cch an ton cho c hai knh duy nht v nhiu knh mng mc d chng chm hn mt cht. Trong trng hp ca mt mng tha tht Hm tr v 0 nu nt c yu cu khng tn ti (khng c nt mi c to ra bi cc Hm). cvGetRow(s) Tr v mng hng hoc khong hng. CvMat* cvGetRow(const CvArr* arr, CvMat* submat, int row); CvMat* cvGetRows(const CvArr* arr, CvMat* submat, int startRow, int endRow, int deltaRow=1); arr u vo mng submat Con tr tr ti cc tiu ph mng kt qu row da trn ch s ca Zero-hng chn startRow Zero-da trn ch s ca hng bt u (bao gm) ca nhp endRow Zero-da trn ch s ca hng kt thc (c quyn) ca nhp deltaRow Index bc trong khong thi gian hng. l, Hm chit xut mi hng th deltaRow startRow v n (nhng khng bao gm) endRow. Cc hm quay tr li tiu , tng ng vi mt khong hng / hng quy nh ca mng u vo. Lu rng GetRow l mt phm tt cho cvGetRows: cvGetRow(arr, submat, row ) cvGetRows(arr, submat, row, row + 1, 1); cvGetSize Tr v kch thc ca ma trn hoc hnh nh ROI. CvSize cvGetSize(const CvArr* arr); arr mng tiu Hm tr v s hng (CvSize::height) v s ct CvSize::width) ca ma trn u vo hoc hnh nh. Trong trng hp ca hnh nh, kch thc ca ROI c tr v. cvGetSubRect Tr v tiu ma trn tng ng vi cc tiu mng ca hnh nh u vo hoc ma trn hnh ch nht. CvMat* cvGetSubRect(const CvArr* arr, CvMat* submat, CvRect rect); arr u vo mng submat Con tr tr ti cc tiu ph mng kt qu rect Zero-ta ca hnh ch nht quan tm Hm tr v tiu , tng ng vi mt hnh ch nht quy nh c th ca mng u vo. Ni cch khc t, n cho php ngi s dng iu tr mt phn hnh ch nht ca mng u vo nh l mt mng c lp. ROI l a vo ti khon theo Hm, do cc mng ca ROI tht s rt ra. cvInRange Kim tra rng cc phn t mng nm gia cc yu t ca hai mng khc. void cvInRange(const CvArr* src, const CvArr* lower, const CvArr* upper, CvArr* dst); 48

src Cc mng ngun u tin lower mng ranh gii thp hn, bao gm upper ranh gii c quyn trn mng dst Mng ch, phi c loi 8u hoc 8s Hm kim tra phm vi cho mi phn t ca mng u vo: dst(I) = lower(I)0 <= src(I)0 < upper(I)0 i vi mng knh duy nht, dst(I) = lower(I)0 <= src(I)0 < upper(I)0 ^ lower(I)1 <= src(I)1 < upper(I)1 i vi hai knh mng v v.v, dst (I) c thit lp 0xff (tt c 1-bit) nu src (I) trong phm vi v 0 nu ngc li. Tt c cc mng c cng loi, tr im n, v kch thc tng t (hoc thu nhp t u kch thc). cvInRangeS Kim tra cc phn t mng nm gia hai v hng. void cvInRangeS(const CvArr* src, CvScalar lower, CvScalar upper, CvArr* dst); src Cc mng ngun u tin lower Cng thp bao gm ranh gii upper Trn ranh gii vng c quyn dst Cc mng ch, phi c loi 8u hoc 8s Hm kim tra phm vi cho mi phn t ca mng u vo: dst(I) = lower0 <= src(I)0 < upper0 i vi mng knh duy nht, dst(I) = lower0 <= src(I)0 < upper0 ^ lower1 <= src(I)1 < upper1 i vi hai knh mng th hai ..., dst (I) c thit lp 0xff (tt c 1-bit) nu 'src (I), trong phm vi v 0 nu ngc li.Tt c cc mng phi c cng kch thc (hoc kch thc ROI). cvIncRefData Increments mng d liu tham kho truy cp. int cvIncRefData(CvArr* arr); arr mng tiu Hm gia tng CvMat hoc CvMatND truy cp ti liu tham kho d liu v tr v mi truy cp gi tr nu con tr truy cp ti liu tham kho khng phi l NULL, nu khng n s tr v s khng. cvInitImageHeader Khi to mt tiu hnh nh c giao trc y. IplImage* cvInitImageHeader( IplImage* image, CvSize size, int depth, int channels, int origin=0, int align=4); image Hnh nh tiu khi to size Hnh nh kch thc chiu rng v chiu cao 49

depth Hnh nh chiu su chiu su (xem cvCreateImage) channels S lng cc knh ca cc knh (xem cvCreateImage) origin trn tri IPL_ORIGIN_TL hoc pha di bn tri IPL_ORIGIN_TL align sp xp sp cho cc hng hnh nh, thng l 4 hoc 8 byte Tr li IplImage* im tiu khi to. cvInitMatHeader Khi to mt tiu ma trn c cp pht trc. CvMat* cvInitMatHeader( CvMat* mat, int rows, int cols, int type, void* data=NULL, int step=CV_AUTOSTEP); mat con tr n cc tiu ma trn c khi to rows S lng hng trong ma trn cols S lng ct trong ma trn type Loi cc yu t ma trn, xem cvCreateMat. data ty chn: d liu con tr c gn cho tiu ma trn step Ty chn: chiu rng hng y trong byte d liu c giao. Theo mc nh, ti thiu c th bc c s dng gi nh khng c khong trng gia cc hng tip theo ca ma trn. Hm ny thng c s dng x l d liu th vi cc Hm ma trn OpenCV. V d, cc m sau y tnh sn phm ma trn ca hai ma trn, c lu tr nh mng bnh thng: double a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; double b[] = { 1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12 }; double c[9]; CvMat Ma, Mb, Mc ; cvInitMatHeader(&Ma, 3, 4, CV_64FC1, a); cvInitMatHeader(&Mb, 4, 3, CV_64FC1, b); cvInitMatHeader(&Mc, 3, 3, CV_64FC1, c); cvMatMulAdd(&Ma, &Mb, 0, &Mc); // Mng c gi cha cc sn phm ca mt (3x4) v b (4x3)

cvInitMatNDHeader Khi to mt tiu a chiu mng c cp pht trc. CvMatND* cvInitMatNDHeader( CvMatND* mat, int dims, const int* sizes, 50

int type, void* data=NULL); mat con tr n mng tiu c khi to dims s ca kch thc mng sizes mt mng c kch thc kch thc type kiu ca cc phn t mng, xem cvCreateMat data ty chn d liu con tr c gn cho tiu ma trn cvInitSparseMatIterator Khi to mng tha tht yu t lp. CvSparseNode* cvInitSparseMatIterator(const CvSparseMat* mat, CvSparseMatIterator* matIterator); mat u vo mng matIterator khi to Iterator Hm khi to bin lp ca cc phn t mng tha tht v tr v con tr n cc yu t u tin, hoc NULL nu mng c sn phm no. cvInvSqrt Tnh nghch o vung gc. float cvInvSqrt(float value); value Gi tr u vo du chm ng Hm tnh ton gc nghch o ca cc i s vung, v thng thng n l nhanh hn 1./sqrt (gi tr). Nu i s l s khng hoc tiu cc, kt qu l khng xc nh. c bit gi tr ( ) khng c x l. cvInv ng ngha cho Invert

cvInvert Tm nghch o hoc gi nghch o ca mt ma trn. double cvInvert(const CvArr* src, CvArr* dst, int method=CV LU); src Cc ma trn ngun dst Cc ma trn im n method Phng php phng php Inversion CV_LU Gaussian loi tr c yu t trc ti u c la chn CV_SVD phng php phn hy gi tr t (SVD) CV_SVD_SYM SVD phng php cho mt ma trn tch cc xc nh i xng Hm o ngc ca ma trn src1 v lu tr kt qu trong src2. Trong cc trng hp phng php LU, Hm tr v cc yu t quyt nh src1 (src1 phi l hnh vung). Nu n l 0, ma trn l khng o ngc v src2 c lm y vi s khng. Trong trng hp cc phng php SVD, Hm tr v tnh trng nghch o ca src1 (t l ca gi tr nh nht t cc gi tr ln nht t) v 0 nu src1 l tt c cc s khng. SVD phng php tnh ton ma trn gi nghch o nu src1 l s t. cvIsInf Xc nh nu i s l v cc. int cvIsInf(double value); 51

value Gi tr u vo du chm ng Hm tr v 1 nu i s l (theo quy nh ca IEEE754 tiu chun), 0 nu ngc li. cvIsNaN Xc nh nu i s l khng phi l mt s. int cvIsNaN(double value); Gi tr u vo gi tr du chm ng Hm tr v 1 nu i s l A Number (theo quy nh ca IEEE754 tiu chun), 0 khc. cvLUT Thc hin mt ci nhn ln bng bin i ca mt mng. void cvLUT(const CvArr* src, CvArr* dst, const CvArr* lut); src Ngun lot cc yu t 8-bit dst im n mng dst ca mt su nht nh v cng mt s knh nh cc mng ngun lut Nhn ln bng l 256 yu t, nn c su tng t nh cc mng ch. Trong trng hp ca a knh ngun v mng ch, cc bng c cn phi c mt n knh (trong trng hp ny cng mt bng c s dng cho tt c cc knh) hoc s lng ca cc knh nh l ngun gc / mng ch. Hm lp y cc mng im n c gi tr t bng nhn ln. Ch s ca cc mc c ly t mng ngun. l, Hm x l mi phn t ca src nh sau: dsti lutsrci+d ni: d= | 0 nu src c chiu su CV_8U | 128 nu src c chiu su CV_8S cvLog Tnh logarit t nhin ca gi tr tuyt i tt c cc phn t mng. void cvLog(const CvArr* src, CvArr* dst); src Cc mng ngun dst Mng ch, n cn phi c loi tng gp i hoc loi tng t nh ngun Hm tnh ton logarit t nhin ca gi tr tuyt i ca tt c cc yu t u vo mng:

Trng hp C l mt s tiu cc ln (khong -700 trong vic thc hin hin ti). cvMahalonobis Tnh khong cch Mahalonobis gia hai vect. double cvMahalanobis( const CvArr* vec1, const CvArr* vec2, CvArr* mat); vec1 1D ngun u tin vector vec2 Cc ngun 1D th hai vector mat ma trn hip phng sai nghch o Hm tnh ton v tr v khong cch trng gia hai vect:

52

Ma trn hip phng sai c th c tnh bng cch s dng Hm cvCalcCovarMatrix v hn na o ngc bng cch s dng cc Hm cvInvert (CV_SVD phng php l mt trong nhng u tin bi v cc ma trn c th l s t). cvMat Khi to ma trn tiu (bin th nh). CvMat cvMat( int rows, int cols, int type, void* data=NULL); rows S lng hng trong ma trn cols S lng ct trong ma trn type loi ca cc yu t ma trn - xem cvCreateMat data ty chn d liu con tr c gn cho tiu ma trn Khi to mt tiu ma trn v gn d liu cho n. Ma trn l y hng khn ngoan (ct u tin cc yu t ca d liu hng u tin ca ma trn, v.v) Hm ny l mt s thay th ni tuyn nhanh chng cho cvInitMatHeader. C th, n tng ng vi: CvMat mat; cvInitMatHeader(&mat, rows, cols, type, data, CV\_AUTOSTEP); cvMax Tm ti a mi phn t ca hai mng. void cvMax(const CvArr* src1, const CvArr* src2, CvArr* dst); src1 Cc mng ngun u tin src2 Cc mng ngun th hai dst Cc mng ch Hm tnh ton ti a cho mi phn t ca hai mng: dst(I) = max(src1(I); src2(I)) Tt c cc mng phi c mt knh duy nht, kiu d liu ging nhau v kch thc tng t (hoc thu nhp t u kch thc). cvMaxS Tm thy ti a cho mi phn t ca mng v v hng. void cvMaxS(const CvArr* src, double value, CvArr* dst); src Cc mng ngun u tin value gi tr v hng dst Cc mng ch Hm tnh ton ti a cho mi phn t ca mng v v hng: dst (I) = max (src1(I), src2(I)) Tt c cc mng phi c mt knh duy nht, kiu d liu ging nhau v kch thc tng t (hoc kch thc ROI). 53

cvMerge Sng tc mt mng a knh t knh duy nht mt s mng hoc chn mt knh duy nht vo mng. void cvMerge(const CvArr* src0, const CvArr* src1, const CvArr* src2, const CvArr* src3, CvArr* dst); #define cvCvtPlaneToPix cvMerge src0 u vo knh 0 src1 u vo knh 1 src2 u vo knh 2 src3 u vo knh 3 dst im n mng Hm l ngc li vi cvSplit. Nu cc mng ch N knh sau nu ngi u tin N knh u vo l khng NULL, tt c chng u c sao chp vo mng ch; nu ch c mt ngun duy nht knh N u tin khng phi l NULL, knh ny c sao chp vo mng ch; nu khng mt li c nng ln. Phn cn li ca cc knh ngun (ngoi N u tin) phi lun lun l NULL. i vi IplImage cvCopy vi COI thit lp cng c th c s dng chn mt knh duy nht vo hnh nh. cvMin Tm thy ti thiu mi phn t ca hai mng. void cvMin(const CvArr* src1, const CvArr* src2, CvArr* dst); src1 Cc mng ngun u tin src2 Cc mng ngun th hai dst Cc mng ch Hm tnh ton ti thiu cho mi phn t ca hai mng: dst (I) = min (src1 (I), src2 (I)) Tt c cc mng phi c mt knh duy nht, kiu d liu ging nhau v kch thc tng t (hoc thu nhp t u kch thc). cvMinMaxLoc Tm thy ton cu ti thiu v ti a trong mng hoc subarray. void cvMinMaxLoc(const CvArr* arr, double* minVal, double* maxVal, CvPoint* minLoc=NULL, CvPoint* maxLoc=NULL, const CvArr* mask=NULL); arr mng ngun, n knh hoc a knh vi COI thit lp minVal Pointer gi tr ti thiu tr li maxVal Pointer gi tr ti a tr li minLoc con tr n v tr ti thiu tr li maxLoc con tr n v tr ti a tr li mask mt n ty chn c s dng la chn mt subarray Hm tm thy gi tr ca cc phn t ti thiu v ti a v v tr ca h. Cc extremums c tm kim qua ton b mng, chn ROI (trong trng hp ca IplImage) hoc, nu mask khng phi l 54

NULL, trong khu vc mng quy nh. Nu mng c nhiu hn mt knh, n phi c IplImage Sau khi COI thit lp. Trong trng hp mng a chiu, minLoc-> x v maxLoc-> x s cha nguyn (tuyn tnh) v tr ca extremums. cvMinS Tm thy ti thiu cho mi phn t ca mt mng v mt v hng. void cvMinS(const CvArr* src, double value, CvArr* dst); src Cc mng ngun u tin value gi tr v hng dst Cc mng ch Hm tnh ton ti thiu ca mt mng v mt v hng: dst(I) = min(src(I), value) Tt c cc mng phi c mt knh duy nht, kiu d liu ging nhau v kch thc tng t (hoc thu nhp t u kch thc). Mirror ng ngha vi hm Flip. cvMixChannels Bn sao mt s knh t mng u vo cc knh ca mng u ra void cvMixChannels(const CvArr** src, int srcCount, CvArr** dst, int dstCount, const int* fromTo, int pairCount); src u vo mng srcCount s mng u vo. dst im n mng dstCount S lng cc mng u ra. fromTo mng ca cc cp ca cc ch s ca cc my bay sao chp. fromTo Cc mng ca cc cp ca cc ch s ca cc my bay sao chp.fromTo [k * 2] l ch s 0 ca knh u vo trong src v fromTo [k * 2 +1] l ch s ca cc knh u ra trong dst.S knh lin tc c s dng, c ngha l, cc knh u vo hnh nh u tin c lp ch mc t 0 n channels (src [0]) -1, cc knh u vo hnh nh th hai c lp ch mc t cc channels (src [0]) channels (src[0]) + channels (src [1]) -1 v.v, v cc chng trnh tng t c s dng cho cc knh hnh nh u ra.Nh mt trng hp c bit, khi fromTo [k * 2] l tiu cc, cc knh u ra tng ng l y vi s khng. Hm l mt dng tng qut ca cvSplit v cvMerge v mt s hnh thc ca CvtColor. N c th c s dng thay i th t ca nhng chic my bay, thm / g b cc knh alpha, gii nn hoc chn mt mt my bay hoc my bay nhiu. V d, m ny chia tch 4-knh RGBA hnh nh vo mt 3-knh BGR (tc l vi R v B trao i) v knh alpha hnh nh ring bit: CvMat* rgba = cvCreateMat(100, 100, CV_8UC4); CvMat* bgr = cvCreateMat(rgba->rows, rgba->cols, CV_8UC3); CvMat* alpha = cvCreateMat(rgba->rows, rgba->cols, CV_8UC1); cvSet(rgba, cvScalar(1,2,3,4)); CvArr* out[] = { bgr, alpha }; int from_to[] = { 0,2, 1,1, 2,0, 3,3 }; 55

cvMixChannels(&bgra, 1, out, 2, from_to, 4);

MulAddS ng ngha cho ScaleAdd. cvMul Tnh sn phm cho mi phn t ca hai mng. void cvMul(const CvArr* src1, const CvArr* src2, CvArr* dst, double scale=1); src1 Cc mng ngun u tin src2 Cc mng ngun th hai dst Cc mng ch scale ty chn quy m yu t Hm tnh ton cc sn phm cho mi phn t ca hai mng: dst(I) = scale . src1(I) . src2(I) Tt c cc mng phi c cng loi v kch thc tng t (hoc kch thc ROI). i vi cc loi c hn ch phm vi hot ng ny c bo ha. cvMulSpectrums Thc hin mi phn t nhn ca hai quang ph Fourier. void cvMulSpectrums( const CvArr* src1, const CvArr* src2, CvArr* dst, int flags); src1 Cc mng ngun u tin src2 Cc mng ngun th hai dst mng im n ca cng loi v kch thc ging nh cc mng ngun flags Mt s kt hp ca cc gi tr sau; CV_DXT_ROWS x l mi hng ca cc mng nh l mt quang ph ring bit (xem cc thng s m t cvDFT). CV_DXT_MUL_CONJ lin hp cc mng ngun th hai trc khi nhn rng. Hm thc hin php nhn cho mi phn t ca hai ma trn CCS-ng gi hoc phc tp l kt qu ca mt thc hay phc tp bin i Fourier. Hm, cng vi cvDFT, c th c s dng tnh tch chp ca hai mng nhanh chng. cvMulTransposed Tnh sn phm ca mt mng v mt mng hon. void cvMulTransposed(const CvArr* src, CvArr* dst, int order, const CvArr* delta=NULL, double scale=1.0); src Cc ma trn ngun dst ma trn im n. Phi CV_32F hoc CV_64F. order Sp xp th t ca s nhn 56

delta Mt mng ty chn, tr t src trc khi nhn scale Mt ty chn m rng quy m Hm tnh ton l sn phm ca src v chuyn v ca n: dst = scale(src - delta)(src - delta)T nu order = 0, v dst = scale(src - delta)T (src - delta) khc. cvNorm Tnh ton mng tuyt i tiu chun, nh mc khc bit tuyt i, hoc nh mc chnh lch tng i. double cvNorm(const CvArr* arr1, const CvArr* arr2=NULL, int normType=CV L2, const CvArr* mask=NULL); arr1 Cc ngun hnh nh u tin arr2 Cc hnh nh ngun th hai. Nu n l NULL, ch tiu tuyt i ca arr1 c tnh ton, nu khng cc ch tiu tuyt i hay tng i ca arr1, arr2 c tnh ton. normType Loi nh mc, xem cc cuc tho lun mask mt n hot ng ty chn Hm tnh ton cc ch tiu tuyt i arr1 nu arr2 l NULL:

hoc nh mc chnh lch tng i nu arr2 khng phi l NULL

hoc nh mc chnh lch tng i nu arr2 khng phi l NULL v (normType & CV_RELATIVE) !=0:

Hm tr v cc ch tiu tnh ton. Mt mng nhiu knh c x l nh mt n knh, c ngha l, cc kt qu cho tt c cc knh c kt hp. cvNot Thc hin cho mi phn t o bit-khn ngoan ca cc yu t mng. void cvNot(const CvArr* src, CvArr* dst); src Cc mng ngun dst Cc mng ch Hm ngc mi bit ca mi phn t mng: dst(I)=src(I) 57

cvOr Tnh mi yu t bit khn ngoan phn ly ca hai mng. void cvOr(const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask=NULL); src1 Cc mng ngun u tin src2 Cc mng ngun th hai dst Cc mng ch mask mt n hot ng, 8-bit knh mng; quy nh c th cc yu t ca mng ch c thay i Hm tnh ton cho mi phn t bit-khn ngoan phn ly ca hai mng: dst (I) = src1 (I) | src2 (I) Trong trng hp mng im ni i din bit c s dng cho hot ng. Tt c cc mng phi c cng loi, tr mt n, v kch thc ging nhau. cvOrS Tnh ton mt yu t cho mi bit-khn ngoan phn ly ca mt mng v mt v hng. void cvOrS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL); src Cc mng ngun value v hng s dng trong hot ng dst Cc mng ch mask mt n hot ng, 8-bit knh mng; quy nh c th cc yu t ca mng ch c thay i ORS Hm tnh ton phn ly mi phn t bit-khn ngoan ca mt mng v mt v hng: dst(I)=src(I)|value if mask(I)!=0 Trc khi hot ng thc t, v hng c chuyn i cng loi nh ca mng (s). Trong trng hp ca cc mng i din ca h bit floating-point c s dng cho hot ng. Tt c cc mng phi c cng loi, tr mt n, v kch thc ging nhau. cvPerspectiveTransform Thc hin chuyn i ma trn quan im ca mt mng vector. void cvPerspectiveTransform(const CvArr* src, CvArr* dst, const CvMat* mat); src Ngun ba knh mng ni im dst im n ba knh mng ni im mat 3 x 3 hoc 4 4 chuyn i ma trn Hm bin i mi phn t ca src (bng cch x l n nh l vector 2D hoc 3D) nh sau nh sau: (x, y, z) (x / w, y / w, z/w) ni (x, y, z, w) = mat [x y z 1] V

cvPolarToCart Tnh ta -cc vect 2d i din trong hnh thc cc. void cvPolarToCart( const CvArr* magnitude, const CvArr* angle, 58

CvArr* x, CvArr* y, int angleInDegrees=0); magnitude Cc mng ln ca bin . Nu n l NULL, bin c gi nh l tt c 1. angle xoay nhiu gc , cho d trong radian hoc x Cc mng ch ca x-ta , c th c thit lp NULL nu n khng phi l cn thit y Cc mng ch ca y-ta , mau c thit lp NULL nu n khng phi l cn thit angleInDegrees L c cho bit cc gc c o bng radian, l ch mc nh, hoc cc mc Hm tnh ton hoc x-coodinate, y- coodinate hoc c hai ca tt c cc cng vector magnitude (I) * exp (angle(I) * j), j = sqrt (-1): x(I)=magnitude(I)*cos(angle(I)), y(I)=magnitude(I)*sin(angle(I)) cvPow Tng tt c cc phn t mng quyn lc. void cvPow( const CvArr* src, CvArr* dst, double power); src Cc mng ngun dst Mng ch, nn c cng loi nh l ngun gc power s m ca quyn lc Hm lm tng mi phn t ca mng u vo p:

l, mt s m in khng nguyn cc gi tr tuyt i ca cc phn t mng u vo c s dng.Tuy nhin, n c th c c gi tr ch thc cho cc gi tr tiu cc bng cch s dng mt s hot ng thm, nh v d sau y, tnh cn bc ba ca cc phn t mng, cho thy: CvSize size = cvGetSize(src); CvMat* mask = cvCreateMat(size.height, size.width, CV_8UC1); cvCmpS(src, 0, mask, CV_CMP_LT); /* find negative elements */ cvPow(src, dst, 1./3); cvSubRS(dst, cvScalarAll(0), dst, mask); /* negate the results of negative inputs */ cvReleaseMat(&mask); i vi mt s gi tr ca quyn lc, chng hn nh cc gi tr s nguyn, 0,5, v -0,5, chuyn cc thut ton nhanh hn c s dng. 1.3 Cu trc ng CvMemStorage Gia tng b nh lu tr. typedef struct CvMemStorage { struct CvMemBlock* bottom; / * u tin c phn b khi * / 59

struct CvMemBlock* top; * / * khi b nh hin ti trn cng ca ngn xp * / struct CvMemStorage* parent; / * vay mi t khi * / int block\_size; / * kch thc khi * / int free\_space; / * min ph khng gian trong khi \ {u} texttt (theo byte) * / } CvMemStorage; B nh lu tr l mt cu trc mc thp c s dng lu tr cc cu trc d liu dynamicly nh trnh t, ng nt, th, phn khu, vv, c t chc nh mt danh sch ca cc khi b nh ca bng kch thc di cng lnh vc l s khi u ca danh sch cc khi v cc u khi hin ang c s dng, nhng khng nht thit phi l khi cui cng ca danh sch. Tt c cc khi t pha di v pha trn, khng bao gm th hai, c coi l hon ton b chim ng, tt c cc khi t u v ngn chn cui cng, khng bao gm hng u, c coi l min ph v u t n l mt phn ocupied - min ph khng gian cha s lng byte min ph cn li vo cui hng u. Mt b nh m mi c th c phn b mt cch r rng theo Hm cvMemStorageAlloc hoc implicitly Hm cp cao hn, chng hn nh cvSeqPush, cvGraphAddEdge, vv, lun lun bt u kt thc ca khi hin ti nu n ph hp vi c. Sau khi phn b, khng gian min ph c gim i kch thc ca b m c phn b cng vi mt s padding gi s lin kt thch hp. Khi c phn b b m khng ph hp vi thnh phn c sn ca u, khi lu tr tip theo t danh sch c thc hin nh khng gian hng u v min ph c thit lp li kch thc khi ton b trc khi vic phn b. Nu khng c cc khi t do hn, mt khi mi c giao (hoc vay t cha m, xem ) cvCreateChildMemStorage v thm vo cui danh sch. V vy, lu tr cc hot ng nh mt chng pha di ch ra di cng ca ngn xp v mt i (hng u, khng gian min ph) cho thy u ca stack. u ngn xp c th c lu thng qua cvSaveMemStoragePos, phc hi thng qua cvRestoreMemStoragePos, hoc thit lp li thng qua cvClearStorage. CvMemBlock B nh lu tr khi. typedef struct CvMemBlock { struct CvMemBlock* prev; struct CvMemBlock* next; } CvMemBlock; CvMemBlock cu trc i din cho mt khi duy nht ca b nh lu tr. Cc d liu thc t trong cc khi b nh theo tiu , l, byte th i ca khi b nh c th c ly ra vi biu thc ((char*)(mem_block_ptr+1))[i]. Tuy nhin, c bnh thng khng cn phi truy cp vo cc lnh vc cu trc lu tr trc tip. CvMemStoragePos B nh lu tr v tr. typedef struct CvMemStoragePos { CvMemBlock* top; int free\_space; } CvMemStoragePos; Cu trc m t trn cc ca hng v tr ca nh ngn xp c th c lu thng qua cvSaveMemStoragePos v phc hi thng qua cvRestoreMemStoragePos. 60

CvSeq Growable trnh t ca cc yu t. #define CV_SEQUENCE\_FIELDS() \ int flags; /* micsellaneous flags */ \ int header_size; /* size of sequence header */ \ struct CvSeq* h_prev; /* previous sequence */ \ struct CvSeq* h_next; /* next sequence */ \ struct CvSeq* v_prev; /* 2nd previous sequence */ \ struct CvSeq* v_next; /* 2nd next sequence */ \ int total; /* total number of elements */ \ int elem_size;/* size of sequence element in bytes */ \ char* block_max;/* maximal bound of the last block */ \ char* ptr; /* current write pointer */ \ int delta_elems; /* how many elements allocated when the sequence grows (sequence granularity) */ \ CvMemStorage* storage; /* where the seq is stored */ \ CvSeqBlock* free_blocks; /* free blocks list */ \ CvSeqBlock* first; /* pointer to the first sequence block */ typedef struct CvSeq { CV_SEQUENCE_FIELDS() } CvSeq; CvSeq cu trc l c s cho tt c cc ca OpenCV cu trc d liu ng. Mt nh ngha khc thng thng qua mt macro tr gip n gin ha phn m rng ca CvSeq cu trc vi cc thng s b sung. m rng CvSeq ngi s dng c th nh ngha mt cu trc mi, a ngi s dng xc nh cc lnh vc sau khi tt c cc lnh vc CvSeq c bao gm thng qua cc trnh t LNH VC v m CV (). C hai loi chui - dy c v tha tht. Cc loi c s cho chui dy c, CvSeq v trnh t nh vy c s dng i din cho mng growable 1d - vect, ngn xp, hng i, v deques. H khng c khong trng gia - nu mt phn t c ly ra t gia hay a vo gia trnh t, cc yu t t khi kt thc gn hn c thay i. Tha tht trnh t c CvSet nh l mt lp c s v h s c tho lun chi tit hn. H l trnh t ca cc nt, mi c th c hoc chim hoc min ph nh c ch ra bi l c nt. Nh vy trnh t c s dng cho d liu khng c th t cc cu trc nh b ca cc yu t, th, bng bm v vv. header size lnh vc c cha cc kch thc thc t ca tiu trnh t v cn phi c ln hn hn hoc bng sizeof(CvSeq). Cc h_prev, h_next, v_prev, v_next c th c s dng to ra cc cu trc phn cp t ring bit trnh t. Cc trng h trc v h im tip theo n trc v trnh t tip theo trn cng mt cp th bc, trong khi cc lnh vc v trc v v tip theo im trc v trnh t tip theo trong hng thng ng, c ngha l, cha m v a con u tin ca mnh. Nhng y l nhng ch tn v cc con tr c th c s dng theo mt cch khc. Trng u tin first khi th t u tin, c cu trc c m t di y. Trng total c s lng thc t ca cc yu t trnh t dy c v s lng ca phn b cc nt trong mt chui tha tht. Trng flags c cha cc loi ch k c bit nng ng (CV_SEQ_MAGIC_VAL dy c trnh t v CV_SET_MAGIC_VAL cho chui tha tht) trong 16 bit cao nht v miscella 61

neous thng tin v trnh t. Thp nht ca CV_SEQ_ELTYPE_BITS bit cha cc ID ca loi yu t. Hu ht cc Hm x l chui khng s dng loi yu t m l elem_size c lu gi trong kch thc Elem. Nu chui c cha cc d liu s mt ca CvMat loi sau cc loi yu t ph hp loi yu t CvMat tng ng, v d nh, CV_32SC2 c th c s dng cho mt chui cc im 2D, CV_32FC1 cho cc chui gi tr du chm ng, v.v. CV_SEQ_ELTYPE (seq_header_ptr) v m ly cc loi ca cc yu t trnh t. Ch bin Hm lm vic vi chui s kim tra xem kch thc Elem bng m tnh t kch thc phn t loi. Bn cnh cc loi CvMat tng thch, c rt t loi yu t thm quy nh ti tiu cvtypes.h: Cc loi tiu chun ca cc yu t trnh t #define CV_SEQ_ELTYPE_POINT CV_32SC2 /* (x,y) */ #define CV_SEQ_ELTYPE_CODE CV_8UC1 /* freeman code: 0..7 */ #define CV_SEQ_ELTYPE_GENERIC 0 /* unspecified type of sequence elements */ #define CV_SEQ_ELTYPE_PTR CV_USRTYPE1 /* =6 */ #define CV_SEQ_ELTYPE_PPOINT CV_SEQ_ELTYPE_PTR /* &elem: pointer to element of other sequence */ #define CV_SEQ_ELTYPE_INDEX CV_32SC1 /* #elem: index of element of some other sequence */ #define CV_SEQ_ELTYPE_GRAPH_EDGE CV_SEQ_ELTYPE_GENERIC /* &next_o, &next_d, &vtx_o, &vtx_d */ #define CV_SEQ_ELTYPE_GRAPH_VERTEX CV_SEQ_ELTYPE_GENERIC /* first_edge, &(x,y) */ #define CV_SEQ_ELTYPE_TRIAN_ATR CV_SEQ_ELTYPE_GENERIC /* vertex of the binary tree */ #define CV_SEQ_ELTYPE_CONNECTED_COMP CV_SEQ_ELTYPE_GENERIC /* connected component */ #define CV_SEQ_ELTYPE_POINT3D CV_32FC3 /* (x,y,z) */ CV_SEQ_KIND_BITS bit ch nh cc loi trnh t: Cc loi tiu chun ca Sequences /* Chung (khng xc nh) ca chui */#define CV_SEQ_KIND_GENERIC (0 << CV_SEQ_ELTYPE_BITS) /* dense sequence suntypes */ #define CV_SEQ_KIND_CURVE (1 << CV_SEQ_ELTYPE_BITS) #define CV_SEQ_KIND_BIN_TREE (2 << CV_SEQ_ELTYPE_BITS) /* sparse sequence (or set) subtypes */ #define CV_SEQ_KIND_GRAPH (3 << CV_SEQ_ELTYPE_BITS) #define CV_SEQ_KIND_SUBDIV2D (4 << CV_SEQ_ELTYPE_BITS) Cc bit cn li c s dng xc nh cc tnh nng khc nhau c th vi cc loi trnh t nht nh v yu t loi. V d, cc ng cong lm bng im (CV SEQ HNH THC NG CONG | CV SEQ ELTYPE POINT), cng vi l c CV SEQ FLAG CLOSED, thuc v loi CV SEQ a gic, hoc nu cc l c khc 62

c s dng, kiu ph ca n. Nhiu Hm x l ng vin kim tra cc loi ca chui u vo v bo co mt li nu h khng h tr kiu ny. Cvtypes.h tp tin lu tr danh sch y ca tt c cc loi h tr c xc nh trc chui v macro tr gip c thit k c c nhng loi chui cc ti sn khc. nh ngha ca cc khi xy dng ca cc chui c th c tm thy di y. CvSeqBlock Khi chui lin tc. typedef struct CvSeqBlock { struct CvSeqBlock* prev; /* previous sequence block */ struct CvSeqBlock* next; /* next sequence block */ int start_index; /* index of the first element in the block + sequence->first->start_index */ int count; /* number of elements in the block */ char* data; /* pointer to the first element of the block */ } CvSeqBlock; Khi Trnh t lp mt danh sch lin kt i trn, v vy cc con tr trc v tip theo l khng bao gi NULL v im trc v ngn chn cc trnh t tip theo trong chui. N c ngha l tip theo ca khi cui cng l khi u tin v trc ca khi u tin l khi cui cng. Cc trng startIndex v m theo di v tr khi trong trnh t. V d, nu trnh t bao gm 10 yu t v phn chia thnh ba khi 3, 5, v 2 yu t, v khi u tin c startIndex tham s = 2, sau cp (startIndex, count) trnh t cc khi (2,3), (5, 5), v (10, 2) tng ng. StartIndex tham s ca khi u tin thng l 0, tr khi mt s yu t c a vo u ca chui.

CvSlice Mt lt trnh t. typedef struct CvSlice { int start_index; int end_index; } CvSlice; inline CvSlice cvSlice( int start, int end ); #define CV_WHOLE_SEQ_END_INDEX 0x3fffffff #define CV_WHOLE_SEQ cvSlice(0, CV_WHOLE_SEQ_END_INDEX) /* calculates the sequence slice length */ int cvSliceLength( CvSlice slice, const CvSeq* seq ); Mt s Hm hot ng trn cc trnh t phi mt mt tham s lt CvSlice l mi thit lp trnh t ton b (CV TON SEQ) theo mc nh. Mt trong cc startIndex endIndex c th c tiu cc hoc vt qu di chui, startIndex bao gm, v endIndex 63

l mt ranh gii c quyn. Nu chng bng nhau, ct ming c coi l rng (tc l, khng ele ments). Bi v trnh t ang c coi l cu trc hnh trn, lt c th la chn mt vi yu t vo cui ca mt chui tip theo l mt vi yu t u ca chui. i vi c phong ph, cvSlice (-2, 3) trong trng hp ca mt chui 10-yu t s chn mt slice 5 phn t, c tin cui cng (8), (9), u tin (0), th hai (1Th) v th ba (2) yu t. Cc Hm bnh thng ha cc i s slice trong cc cch sau: u tin cvSliceLength, c gi l xc nh di ca slice, sau , startIndex ca slice c chun ha tng t nh cc ar gument cvGetSeqElem (tc l, ch s tiu cc c php). Lt thc t x l bt u t startIndex bnh thng v ko di cc yu t cvSliceLength (mt ln na, gi s chui l mt cu trc trn). Nu mt Hm khng chp nhn mt i s slice, nhng bn mun qu trnh ch l mt phn ca trnh t, trnh t ph c th c chit xut bng cch s dng Hm cvSeqSlice, hoc c lu tr vo mt lin tc m vi CvtSeqToArray (ty chn, theo sau bi cvMakeSeqHeaderForArray).

CvSet B su tp ca cc nt. typedef struct CvSetElem { int flags; /* it is negative if the node is free and zero or positive otherwise */ struct CvSetElem* next_free; /* if the node is free, the field is a pointer to next free node */ } CvSetElem; #define CV_SET_FIELDS() \ CV_SEQUENCE_FIELDS() /* inherits from [#CvSeq CvSeq] */ \ struct CvSetElem* free_elems; /* list of free nodes */ typedef struct CvSet { CV_SET_FIELDS() } CvSet; CvSet cu trc l c s cho cu trc d liu OpenCV tha tht. Nh sau tuyn b trn, CvSet tha hng t CvSeq v n cho bit thm elems min ph lnh vc, m l mt danh sch cc nt min ph, vi n. Mi nt thit lp, cho d min ph hay khng, l mt phn t ca c bn trnh t. Trong khi c khng hn ch v cc yu t ca chui dy c, cc thit lp (V c ngun gc cu trc) cc yu t phi bt u vi mt trng s nguyn v c th ph hp vi CvSetElem cu trc, bi v hai lnh vc (mt s nguyn theo sau bi mt con tr) c yu cu cho Orga nization ca mt nt thit lp vi danh sch ca cc nt t do. Nu mt nt no l min ph, lnh vc c l tiu cc 64

(Bit quan trng nht, MSB, ca lnh vc ny c thit lp), v min ph ti cc im n min ph tip theo nt (nt u tin c tham chiu bi trng elems min CvSet). V nu mt nt l chim, lnh vc c l tch cc v c cha cc ch s nt c th c ly ra bng cch s dng (thit lp Elem-> c & CV SET Elem IDX MASK) cc biu thc, phn cn li ca ni dung nt c xc nh bi ngi s dng. c bit, cc nt b chim ng khng lin kt vi cc nt min ph c, v vy trng th hai c th c s dng cho lin kt nh vy cng nh i vi mt s mc ch khc nhau. Cc v m CV IS SET Elem (thit lp Elem ptr) c th c s dng xc nh liu cc nt c ch nh l chim ng hay khng. Ban u thit lp v danh sch c sn phm no. Khi mt nt mi c yu cu t thit lp, n c thc hin t danh sch ca cc nt, sau cp nht. Nu danh sch trng rng, mt chui mi khi c cp pht v tt c cc nt trong khi c tham gia vo danh sch cc nt t do. Nh vy, tng s lnh vc ca b ny l tng s ca cc nt b chim ng v min ph. Khi mt chim ng nt c pht hnh, n c thm vo danh sch cc nt t do. Cc nt pht hnh cui cng s c chim ng u tin. Trong CvSet OpenCV c s dng i din cho th (CvGraph), tha tht a chiu mng (CvSparseMat), v cc phn khu phng CvSubdiv2D. CvGraph nh hng th trng unoriented. #define CV_GRAPH_VERTEX_FIELDS() \ int flags; /* vertex flags */ \ struct CvGraphEdge* first; /* the first incident edge */ typedef struct CvGraphVtx { CV_GRAPH_VERTEX_FIELDS() } CvGraphVtx; #define CV_GRAPH_EDGE_FIELDS() \ int flags; /* edge flags */ \ float weight; /* edge weight */ \ struct CvGraphEdge* next[2]; /* the next edges in the incidence lists for staring (0) * /* and ending (1) vertices */ \ struct CvGraphVtx* vtx[2]; /* the starting (0) and ending (1) vertices */ typedef struct CvGraphEdge { CV_GRAPH_EDGE_FIELDS() } CvGraphEdge; #define CV_GRAPH_FIELDS() \ 65

CV_SET_FIELDS() /* set of vertices */ \ CvSet* edges; /* set of edges */ typedef struct CvGraph { CV_GRAPH_FIELDS() } CvGraph; CvGraph cu trc l c s cho cc th c s dng trong OpenCV. Cu trc th c tha hng t CvSet - trong m t cc thuc tnh th thng thng v cc nh th, v c mt thit lp nh l mt thnh vin - trong m t cc cnh th. Cc nh, cnh, v cc cu trc tiu th c khai bo bng cch s dng k thut tng t nh OpenCV khc th m rng cu trc - thng qua cc macro, n gin ha vic m rng v tu bin ca cc cu trc. Trong khi cc cu trc nh v cnh khng k tha t CvSetElem mt cch r rng, p ng c hai iu kin cc yu t theo quy nh: c mt trng s nguyn trong u v ph hp trong cu trc CvSetElem. Cc lnh vc c c s dng ch ra nh chim ng v cnh cng nh cho cc mc ch khc, v d, th traversal (xem cvCreateGraphScanner et al.), do , n l tt hn khng s dng chng trc tip. th c biu din nh l mt tp hp ca cc cnh trong c mt danh sch ca cc cnh s c. Cc danh sch ti i vi cc nh khc nhau xen k trnh trng lp cng nhiu thng tin posssible. th c th c nh hng hoc unoriented. Trong trng hp th hai khng c distiction gia cnh kt ni nh A vi B nh v cnh cc kt ni nh B vi nh A - ch mt trong s h c th tn ti trong th ti cng mt thi im v n i din cho c hai AB v BA cnh. CvGraphScanner Biu traversal nh nc. typedef struct CvGraphScanner { CvGraphVtx* vtx; /* current graph vertex (or current edge origin) */ CvGraphVtx* dst; /* current graph edge destination vertex */ CvGraphEdge* edge; /* current edge */ CvGraph* graph; /* the graph */ CvSeq* stack; /* the graph vertex stack */ int index; /* the lower bound of certainly visited vertices */ int mask; /* event mask */ } CvGraphScanner; CvGraphScanner cu trc c s dng cho su u tin ca th traversal. Xem tho lun ca Hm di y. CV_TREE_NODE_FIELDS Helper v m k khai mt loi nt cy. 66

CV_TREE_NODE_FIELDS c s dng khai bo cc cu trc c th c t chc vo th bc strucutures (cy), chng hn nh CvSeq - loi c bn cho tt c cc cu trc nng ng. Cc cy c to ra vi cc nt tuyn b bng cch s dng macro ny c th c x l bng cch s dng cc Hm m t di y trong phn ny. CvTreeNodeIterator M hin ti hoc to ra tp tin lu tr mi. typedef struct CvTreeNodeIterator { const void* node; int level; int max_level; } CvTreeNodeIterator; #define CV_TREE_NODE_FIELDS(node_type) \ int flags; /* micsellaneous flags */ \ int header_size; /* size of sequence header */ \ struct node_type* h_prev; /* previous sequence */ \ struct node_type* h_next; /* next sequence */ \ struct node_type* v_prev; /* 2nd previous sequence */ \ struct node_type* v_next; /* 2nd next sequence */ CvTreeNodeIterator cu trc c s dng i qua cy. Mi nt cy nn bt u vi mt s lnh vc c xc nh bi CV TREE NODE (...) LNH VC v m. Trong C + + v, mi cy nt phi l mt cu trc "bt ngun" t struct _BaseTreeNode { CV_TREE_NODE_FIELDS(_BaseTreeNode); } CvSeq, CvSet, CvGraph v cc cu trc nng ng khc c ngun gc t CvSeq thc hin theo yu cu. cvClearGraph Xa mt th. void cvClearGraph( CvGraph* graph ); graph th Hm loi b tt c cc nh v cc cnh ca mt th. Hm c O (1) thi gian complexity.

cvClearMemStorage Xa b nh lu tr. void cvClearMemStorage( CvMemStorage* storage ); storage B nh lu tr Thit lp li Hm hng u (min ph khng gian ranh gii) ca lu tr bt u. iu ny Hm ny khng deallocate bt k b nh. Nu lu tr c mt ph huynh, Hm tr v tt c khi ph huynh. 67

cvClearSeq Xa mt chui. void cvClearSeq( CvSeq* seq ); seq trnh t Hm loi b tt c cc yu t t mt chui. Cc Hm khng tr li phn mm khi lu tr ORY, nhng b nh ny c ti s dng sau khi cc yu t mi c thm vo trnh t. Hm c 'O (1)' thi gian phc tp. cvClearSet Xa mt Set. void cvClearSet( CvSet* setHeader ); setHeader r ph thit lp Hm loi b tt c cc yu t t thit lp. N c phc tp thi gian O (1). cvCloneGraph Bn sao mt th. CvGraph* cvCloneGraph(const CvGraph* graph,CvMemStorage* storage ); graph th sao chp storage container sao chp Hm to ra mt bn sao y ca th quy nh. Nu cc nh th hoc cnh con tr n mt s d liu bn ngoi, n vn c th c chia s gia cc bn sao. Cc nh v cnh ch s trong th mi c th khc nhau t bn gc bi v Hm chng phn mnh The b nh v cnh. cvCloneSeq To mt bn sao ca mt chui. CvSeq* cvCloneSeq(const CvSeq* seq, CvMemStorage* storage=NULL ); seq tip theo trnh t storage Cc lu tr ch khi gi tiu trnh t v cc d liu c sao chp, nu bt k. Nu n l NULL, Hm s dng khi lu tr c cha cc chui u vo. Hm lm cho mt bn sao hon chnh ca chui u vo v tr v n. Cc cuc gi cvCloneSeq( seq, storage ) tng ng vi cvSeqSlice( seq, CV_WHOLE_SEQ, storage, 1 ) cvCreateChildMemStorage To b nh lu tr tr em. CvMemStorage* cvCreateChildMemStorage(CvMemStorage* parent); parent lu tr b nh cha Hm to ra mt b nh lu tr ca con l tng t nh b nh lu tr n gin, ngoi tr cho s khc bit trong c ch phn b / deallocation b nh. Khi mt a tr cn lu tr mt khi mi thm vo danh sch chn, n s c gng c c khi ny t cha m. Trng u tin cha m c sn khi c thc hin v b loi khi danh sch chn m. Nu khng c cc khi c sn, cha m hoc phn b mt khi hoc vay mn mt t cha m ca ring mnh, nu c. Ni cch khc, dy chuyn, hay mt cu trc phc tp hn, kho b nh ni lu tr tt c l 68

mt a tr / ph huynh ca khc l c th. Khi lu tr tr em c pht hnh hoc thm ch xa, n s tr v tt c cc khi. cha m. Trong cc kha cnh khc, con lu tr l tng t nh lu tr n gin. Lu tr con l hu ch trong tnh hung sau y. Hy tng tng rng ngi s dng cn qu trnh d liu ng c tr trong mt khu vc lu tr v t kt qu tr li vi cng mt khu vc lu tr. Vi cch tip cn n gin, khi d liu tm thi c tr ti cng mt khu vc lu tr nh l u vo v cc d liu u ra, khu vc lu tr s xem xt nh sau sau khi ch bin: Nng ng, x l d liu m khng cn s dng lu tr con.

l, rc xut hin gia dung lng lu tr. Tuy nhin, nu mt trong to ra mt b nh con lu tr vo lc bt u ch bin, ghi d liu tm thi , v pht hnh lu tr con Cui cng, khng c rc thi s xut hin trong ngun / ch lu tr: Nng ng, x l d liu bng cch s dng mt lu tr con

cvCreateGraph To ra mt th rng. CvGraph* cvCreateGraph( int graph flags, int header size, int vtx size, int edge size, CvMemStorage* storage ); graph_flags Loi th ca th c to ra. Thng thng, n l mt trong hai CV_SEQ_KIND_GRAPH cho chung unoriented th v CV_SEQ_KIND_GRAPH | CV_GRAPH_FLAG_ORIENTED theo nh hng chung chung oriented th. header_size kch thc th tiu , khng c th t hn so vi sizeof (CvGraph) vtx_size kch thc th nh, nh ty chnh cu trc phi bt u vi CvGraphVtx( s dng CV GRAPH VERTEX FIELDS() ) 69

storage container lu tr Hm to ra mt th rng v tr v mt con tr n n. cvCreateGraphScanner To cu trc su u tin ca th traversal. CvGraphScanner* cvCreateGraphScanner( CvGraph* graph, CvGraphVtx* vtx=NULL, int mask=CV GRAPH ALL ITEMS ); graph th vtx nh ban u bt u t. Nu NULL, cy bt u t nh u tin (mt nh vi ti thiu ch mc trong trnh t ca cc nh). mask S kin mt n cho thy nhng s kin c ngi dng quan tm (ni cvNextGraphItem Hm tr v kim sot cho ngi s dng) c th c CV_GRAPH_ALL_ITEMS tt c cc mc (tt c cc s kin c lin quan) hoc s kt hp ca nhng l c sau y: CV GRAPH VERTEX dng li cc nh th truy cp cho ln u tin CV GRAPH TREE EDGE dng li cnh cy (tree edge l cc cnh kt ni cui cng visited nh v cc nh c truy cp tip theo) CV GRAPH BACK EDGE dng li cnh tr li (back edge l mt cnh kt ni cui cng i vi nh vi mt s t tin ca n trong cy tm kim) CV GRAPH FORWARD EDGE dng li cnh pha trc (forward edge l mt cnh conecting cui cng n thm nh vi mt s hu du ca n trong cy tm kim. Pha trc cnh ch c th trong nh hng th traversal) CV GRAPH CROSS EDGE dng li cnh cho (cross edge l mt cnh khc nhau kt ni tm kim cy hoc chi nhnh ca cng mt cy. Cc cross edge ch c th c trong nh hng th traversal) CV GRAPH ANY EDGE dng cnh bt k ((tree, back, forward, v cross edges) CV GRAPH NEW TREE dng li s bt u ca tt c cc cy tm kim mi. Khi cc traversal th tc truy cp vo tt c cc nh v cc cnh c th truy cp t nh ban u (cc truy cp nh cng vi cc cnh cy lm cho cy), tm kim i vi mt s nh gh thm thng xuyn trong th v h s qu trnh traversal t nh . Trc khi bt u mi cy (bao gm c cy u tin, khi cvNextGraphItem c gi l ln u tin) n to ra mt th s kin CV_GRAPH_NEW_TREE. i vi cc th unoriented, mi cy tm kim tng ng vi mt thnh phn kt ni ca th. CV GRAPH BACKTRACKING tht li ti tt c n thm nh trong thi gian tht li - tr li ving thm cc nh ca cy traversal. Hm ny to ra mt cu trc su u tin th traversal / search. Cu trc khi to c s dng trong cvNextGraphItem Hm - traversal th tc gia tng. cvCreateMemStorage To b nh lu tr. CvMemStorage* cvCreateMemStorage( int blockSize=0 ); blockSize Kch thc ca cc khi lu tr trong byte. Nu n l 0, kch thc khi c thit lp gi tr mc nh hin nay n l khong 64K. Hm to ra mt b nh lu tr trng. Xem m t CvMemStorage. 70

cvCreateSeq To mt chui. CvSeq* cvCreateSeq( int seqFlags, int headerSize, int elemSize, CvMemStorage* storage); seqFlags Flags ca chui c to ra. Nu chui khng c thng qua bt k Hm lm vic vi mt loi hnh c th trnh t, chui gi tr c th c thit lp l 0, nu khngloi thch hp phi c la chn t danh sch cc loi trnh t c xc nh trc. headerSize Kch thc ca tiu trnh t, phi ln hn hoc bng sizeof (CvSeq). Nu mt loi hnh c th hoc gia hn ca n c ch nh, loi hnh ny phi ph hp vi nhng tiu kiu c s. elemSize Kch thc ca cc yu t trnh t trong byte. Kch thc phi ph hp vi loi. V d, i vi mt chui cc im c to ra, nguyn t cc loi CV SEQ ELTYPE POINT nn c quy nh c th v cc elemSize tham s phi c bng sizeof (CvPoint). storage Trnh t v tr Hm to ra mt chui v tr v con tr n n. Hm giao tiu trong cc khi lu tr nh l on lin tc v thit lp c cu cc lnh vc flags, elemSize, headerSize, v storage cc gi tr thng qua, b delta_elems vi gi tr mc nh (C th c b tr bng cch s dng Hm cvSetSeqBlockSize), v xa cc lnh vc tiu khc, bao gm c khng gian u tin sizeof(CvSeq) byte. cvCreateSet To mt tp rng. CvSet* cvCreateSet( int set flags, int header size, int elem size, CvMemStorage* storage ); set_flags Loi thit lp c to ra header_size kch thc kch thc tiu ; khng c th t hn so vi sizeof (CvSet) elem_size Thit lp kch thc kch thc ; c th khng c t hn CvSetElem storage container lu tr cho thit lp Hm ny to ra mt tp rng vi kch thc tiu quy nh v yu t kch thc, v tr v con tr thit lp. Hm ny ch l mt lp mng trn u trang ca cvCreateSeq. cvCvtSeqToArray Bn sao mt chui lin tc ngn chn mt b nh. void* cvCvtSeqToArray( const CvSeq* seq, void* elements, CvSlice slice=CV WHOLE SEQ ); seq trnh t elements con tr mng ch phi ln. N phi l mt con tr n d liu, khng phi l mt tiu ma trn. slice Phn trnh t sao chp vo mng 71

Hm sao chp ton b chui hoc dy cc b m v tr v con tr n b m. cvEndWriteSeq Kt thc qu trnh vit mt chui. CvSeq* cvEndWriteSeq( CvSeqWriter* writer ); writer Writer state Hm kt thc qu trnh bng vn bn v tr v con tr n chui bng vn bn. Cc Hm cng ct cc khi khng y trnh t cui cng tr li phn cn li ca khi lu tr b nh. Sau , trnh t c th c c v sa i mt cch an ton. Xem cvcvStartWriteSeq v cvcvStartAppendToSeq cvFindGraphEdge Tm thy mt li th trong mt th. CvGraphEdge* cvFindGraphEdge( const CvGraph* graph, int start_idx, int end_idx ); #define cvGraphFindEdge cvFindGraphEdge graph th start_idx ca nh bt u ca cnh end_idx kt thc ca nh kt thc ca cnh. i vi mt th unoriented, th t ca nh cc thng s khng quan trng. Hm tm thy cnh biu kt ni hai nh quy nh v tr v mt con tr n n hoc NULL nu cnh khng tn ti. cvFindGraphEdgeByPtr Tm mt cnh trong mt th bng cch s dng con tr ca n. CvGraphEdge* cvFindGraphEdgeByPtr( const CvGraph* graph, const CvGraphVtx* startVtx, const CvGraphVtx* endVtx ); #define cvGraphFindEdgeByPtr cvFindGraphEdgeByPtr graph th startVtx Pointer bt u t nh ca cnh endVtx Pointer nh kt thc ca cnh. i vi mt th unoriented, th t ca nh cc thng s khng quan trng. Hm tm cnh biu kt ni hai nh quy nh v tr v con tr n n. NULL nu cnh khng tn ti. cvFlushSeqWriter Tiu trnh t cp nht t nh vn. void cvFlushSeqWriter( CvSeqWriter* writer ); writer Writer state Hm c thit k cho php ngi dng c cc yu t theo th t, bt c khi no cn thit, trong qu trnh vit, v d nh, kim tra iu kin c th. Hm cp nht tiu trnh t lm cho vic c trnh t c th. Tc gi khng phi l ng ca, tuy nhin, qu trnh vit c th c tip tc bt c lc no. Nu mt thut ton i hi phi xa thng xuyn, xem xt vic s dng cvSeqPush thay v. cvGetGraphVtx Tm thy mt th nh bng cch s dng ch s ca n. 72

CvGraphVtx* cvGetGraphVtx( CvGraph* graph, int vtx idx ); graph Graph vtx_idx Index ca nh Hm tm thy th nh bng cch s dng ch s ca n v tr v con tr n n hoc NULL nu nh khng thuc v th. cvGetSeqElem Tr v mt con tr n mt yu t trnh t theo ch s ca n. char* cvGetSeqElem( const CvSeq* seq, int index ); #define CV_GET_SEQ_ELEM(TYPE, seq,index)(TYPE*)cvGetSeqElem((CvSeq*)(seq),(index) ) seq trnh t index ch s ca nguyn t Hm tm thy cc phn t vi cc ch s c a ra trong trnh t v tr v con tr ti n. Nu phn t khng tm thy, Hm tr v 0. Hm h tr ch s tiu cc, -1 L vit tt ca cc yu t trnh t, -2 l vit tt ca mt trong nhng trc khi cui cng, vv Nu cc trnh t rt c th bao gm mt khi th t hoc cc yu t mong mun c th t trong khi u tin, sau v m CV_GET_SEQ_ELEM (elemType, seq, index) nn c c s dng, ni elemType tham s l loi yu t th t (CvPoint v d), seq tham s l mt chui, v ch s tham s l ch s ca phn t mong mun. V m s kim tra ln u tin cho d cc yu t mong mun thuc v khi u tin ca chui v tr li n nu n khng, nu khng v m cc cuc gi GetSeqElem Hm chnh. Tiu cc ch s lun lun gy ra cc cuc gi cvGetSeqElem. Hm O (1) thi gian gi nh phc tp s khi nh hn nhiu hn s lng cc yu t. cvGetSeqReaderPos Tr v v tr u c hin ti. int cvGetSeqReaderPos( CvSeqReader* reader ); reader Reader state Hm tr v v tr ngi c hin ti (trong 0 ... c-> seq-> tng - 1). cvGetSetElem Tm thy mt yu t c thit lp bi ch s ca n. CvSetElem* cvGetSetElem( const CvSet* setHeader, int index ); setHeader Set Index ch s ca phn t thit lp trong mt chui Hm tm thy mt yu t c thit lp bi ch s ca n. Hm tr v con tr n n hoc 0 nu ch s l khng hp l hoc cc nt tng ng l min ph. Hm ny h tr ch s tiu cc nh n s dng cvGetSeqElem xc nh v tr cc nt. cvGraphAddEdge Thm mt cnh mt th. 73

int cvGraphAddEdge( CvGraph* graph, int start idx, int end idx, const CvGraphEdge* edge=NULL, CvGraphEdge** inserted edge=NULL ); graph th start_idx ch s ca nh bt u ca cnh end_idx Index kt thc ca nh kt thc ca cnh. i vi mt th unoriented, th t ca nh cc thng s khng quan trng. edge tham s u vo cnh ty chn, khi to d liu cho cnh inserted_edge ty chn u ra tham s cha cc a ch ca cnh c chn vo Hm kt ni hai nh quy nh. Hm tr v 1 nu cnh c thm thnh cng, 0 nu cc cnh ni hai nh tn ti v -1 nu mt trong cc nh khng c tm thy, bt u v nh kt thc l nh nhau, hoc c khc mt s quan trng tnh hnh. Trong trng hp th hai (tc l, khi kt qu l m tnh), Hm cng bo co mt li theo mc nh. cvGraphAddEdgeByPtr Thm mt cnh mt th bng cch s dng con tr ca n. int cvGraphAddEdgeByPtr( CvGraph* graph, CvGraphVtx* start vtx, CvGraphVtx* end vtx, const CvGraphEdge* edge=NULL, CvGraphEdge** inserted edge=NULL ); graph th start_vtx Con tr tr ti nh bt u ca cnh end_vtx Con tr tr ti nh kt thc ca cnh. i vi mt th unoriented, th t ca nh cc thng s khng quan trng. edge tham s u vo cnh ty chn, khi to d liu cho cnh inserted_edge ty chn u ra tham s cha cc a ch ca cnh c chn vo bn trong tp cnh. Hm kt ni hai nh quy nh. Hm tr v 1 nu cnh c thm thnh cng, 0 nu cnh ni hai nh tn ti, v -1 nu mt trong cc nh khng c tm thy, bt u v nh kt thc l nh nhau hoc c khc mt s quan trng tnh hnh. Trong trng hp th hai (tc l, khi kt qu l m tnh), Hm cng bo co mt li theo mc nh. cvGraphAddVtx Thm mt nh mt th. int cvGraphAddVtx( CvGraph* graph, const CvGraphVtx* vtx=NULL, CvGraphVtx** inserted vtx=NULL ); graph th vtx Ty chn u vo i s c s dng khi to nh gia tng (ch ngi s dng xc nh cc lnh vc vt ra ngoi sizeof (CvGraphVtx) c sao chp) 74

inserted_vertex u ra ty chn nh. Nu khng phi l NULL, a ch ca nh mi vit y. Hm b sung thm mt nh v th v tr v ch s nh. cvGraphEdgeIdx Tr v ch s cnh th. int cvGraphEdgeIdx( CvGraph* graph, CvGraphEdge* edge ); graph th edge con tr cnh th Hm tr v ch s cnh th. cvGraphRemoveEdge Loi b mt cnh t mt th. void cvGraphRemoveEdge( CvGraph* graph, int start idx, int end idx ); graph th start_idx ch s ca nh bt u ca cnh end_idx kt thc ca nh kt thc ca cnh. i vi mt th unoriented, th t ca nh cc thng s khng quan trng. Hm loi b cc cnh ni hai nh quy nh. Nu cc nh khng cn [theo th t], Hm khng c g. cvGraphRemoveEdgeByPtr Loi b mt cnh t mt th bng cch s dng con tr ca n. void cvGraphRemoveEdgeByPtr( CvGraph* graph, CvGraphVtx* start vtx, CvGraphVtx* end vtx ); graph th start_vtx Con tr tr ti nh bt u ca cnh end_vtx Con tr tr ti nh kt thc ca cnh. i vi mt th unoriented, th t ca nh cc thng s khng quan trng. Hm loi b cc cnh ni hai nh quy nh. Nu cc nh khng cn nected [theo th t], Hm khng c g. cvGraphRemoveVtx Loi b mt nh t mt th. int cvGraphRemoveVtx( CvGraph* graph, int index ); graph th vtx_idx Index nh loi b

75

Hm loi b mt nh t mt th cng vi tt c cc v vic cnh n. Cc Hm bo co li nu nh u vo khng thuc v th. Gi tr tr v l s ca cc cnh b xa, hoc -1 nu nh khng thuc v th. cvGraphRemoveVtxByPtr Loi b mt nh t mt th bng cch s dng con tr ca n. int cvGraphRemoveVtxByPtr( CvGraph* graph, CvGraphVtx* vtx ); graph th vtx con tr n nh loi b Hm loi b mt nh ca th bng cch s dng con tr ca n cng vi tt c cc cnh s vic cho n. Hm bo co mt li nu nh khng thuc th. S tr li gi tr l s ca cc cnh b xa, hoc -1 nu cc nh khng thuc v th. cvGraphVtxDegree m s ca cc cnh indicent nh. int cvGraphVtxDegree( const CvGraph* graph, int vtxIdx ); graph th vtxIdx Index ca nh th Hm tr v s ca cc cnh s c nh quy nh, c hai vo v gi i. m cc cnh, cc m sau y c s dng: CvGraphEdge* edge = vertex->first; int count = 0; while( edge ) { edge = CV_NEXT_GRAPH_EDGE( edge, vertex ); count++; } Cc v m CV_NEXT_GRAPH_EDGE ( edge, vertex) tr v v vic vertex theo sau khi edge. cvGraphVtxDegreeByPtr Tm thy mt li th trong mt th. int cvGraphVtxDegreeByPtr(const CvGraph* graph,const CvGraphVtx* vtx ); graph th vtx con tr n nh th Hm tr v s ca cc cnh s c nh quy nh, c hai vo v outcoming. cvGraphVtxIdx Tr v ch s ca mt th nh. int cvGraphVtxIdx(CvGraph* graph,CvGraphVtx* vtx ); graph th vtx con tr n nh th Hm tr v ch s ca mt nh ca th. cvInitTreeNodeIterator Khi to bin lp nt cy. void cvInitTreeNodeIterator(CvTreeNodeIterator* tree iterator, const void* first,int max level ); tree_iterator cy khi to bi Hm first nt ban u bt u i qua t 76

max_level mc ti a ca cy (first nt gi nh l cp u tin) i qua n. V d, 1 c ngha l cc nt ch mc tng t nh first phi c truy cp, 2 c ngha l cc nt trn cng cp nh l first v tr em trc tip ca h nn c truy cp, v vv. Hm khi to bin lp cy. Cy l i qua su sc lnh u tin. cvInsertNodeIntoTree Thm mt nt mi vo cy. void cvInsertNodeIntoTree(void* node,void* parent,void* frame ); node Cc nt chn parent Ph huynh nt cha c trong cy frame Khung nt cp cao nht. Nu parent v frame u ging nhau, v_prev trng ca node c thit lp NULL thay v phi l parent. Hm b sung thm mt nt vo cy. Hm khng phn b b nh, n c th ch sa i cc lin kt ca cc nt cy. cvMakeSeqHeaderForArray Xy dng mt tiu trnh t cho mt mng. CvSeq* cvMakeSeqHeaderForArray(int seq type,int header size,int elem size,void* elements,int total,CvSeq* seq,CvSeqBlock* block ); seq_type loi hnh ca chui c to ra header_size Kch thc ca phn u ca chui. Thng s th t phi im n cu trng thy kch thc hoc cao hn elem_size Kch thc ca cc yu t chui elements Cc yu t s to thnh mt chui total Tng s phn t trong chui. S lng cc phn t mng phi bng vi gi tr ca tham s ny. seq con tr vo bin a phng c s dng nh tiu trnh t block Con tr tr ti bin a phng l tiu ca khi chui Hm khi to mt tiu trnh t cho mt mng. Cc tiu trnh t cng nh cc khi trnh t c phn b bi ngi s dng (v d, trn stack). Khng c d liu c sao chp Hm. Chui kt qu s bao gm mt khi duy nht v c con tr lu tr NULL, do , n l c th c cc phn t ca n, nhng nhng n lc thm cc yu t trnh t s nng cao mt li trong nhiu trng hp. cvMemStorageAlloc Cp pht mt b nh m trong mt khi lu tr. void* cvMemStorageAlloc(CvMemStorage* storage,size t size ); storage lu tr lu tr size b m kch thc Hm cp pht mt b nh m trong mt khi lu tr. Kch thc b m khng c vt qu kch thc lu tr khi, nu khng mt li thi gian chy c nu ra. a ch m ph hp CV_STRUCT_ALIGN = sizeof(double) (i vi thi im ny) byte. cvMemStorageAllocString Cp pht mt chui vn bn trong mt khi lu tr. CvString cvMemStorageAllocString(CvMemStorage* storage, const char* ptr, int len=-1); typedef struct CvString 77

{ int len; char* ptr; } CvString; storage b nh lu tr ptr Chui len Chiu di ca chui (khng k NULL kt thc). Nu tham s ny l tiu cc, Hm tnh chiu di. Hm to bn sao ca chui trong b nh lu tr. N tr v cc cu trc c cha ngi s dng thng qua hoc tnh ton chiu di ca chui v con tr n chui sao chp. cvNextGraphItem Thc hin mt hoc nhiu bc ca th tc th traversal. int cvNextGraphItem( CvGraphScanner* scanner ); scanner th traversal nh nc. N c cp nht Hm ny. Cc Hm i qua thng qua cc th cho n khi mt s kin ca ngi dng quan tm (c ngha l, mt s kin, quy nh trong mask trong cc cuc gi cvCreateGraphScanner) c p ng hoc traversal c hon thnh. Trong trng hp u tin, n s tr v mt trong cc s kin c lit k trong m t ca cc tham s mask trn v vi cc cuc gi tip theo s yu l lch cc traversal. Trong trng hp th hai, n s tr CV_GRAPH_OVER(-1). Khi s kin ny l CV_GRAPH_VERTEX, CV_GRAPH_BACKTRACKING, hoc CV_GRAPH_NEW_TREE, hin ang quan st nh c lu tr trong scanner->vtx. V nu s kin lin quan n cnh, cnh chnh n c lu tr cnh scanner->edge, truy cp trc nh scanner->vtx v khc kt thc nh ca cnh - ti scanner->dst. cvNextTreeNode Tr v nt hin ang quan st v di chuyn iterator v pha nt tip theo. void* cvNextTreeNode( CvTreeNodeIterator* tree iterator ); tree_iterator cy iterator khi to bi Hm Cc Hm tr v nt hin ang quan st v sau cp nht cc bin lp - di chuyn n i vi cc nt tip theo. Ni cch khc, hnh vi Hm tng t nh biu hin *p++ trn in hnh con tr C hoc C++ b su tp iterator. Hm tr v NULL nu khng c cc nt hn. cvPrevTreeNode Tr v nt hin ang quan st v di chuyn iterator v pha nt trc . void* cvPrevTreeNode( CvTreeNodeIterator* tree iterator ); tree_iterator cy Iterator khi to bi Hm Cc Hm tr v nt hin ang quan st v sau cp nht cc bin lp - di chuyn n i vi cc nt trc . Ni cch khc, hnh vi Hm tng t nh *p-- biu hin trn mt con tr C hoc C++ b su tp iterator. Hm tr v NULL nu c khng cc nt. cvReleaseGraphScanner Hon thnh th tc traversal th. void cvReleaseGraphScanner( CvGraphScanner** scanner ); scanner p con tr n biu traverser Hm hon thnh th tc traversal th v pht hnh nh nc traverser. 78

cvReleaseMemStorage Phin bn b nh lu tr. void cvReleaseMemStorage( CvMemStorage** storage ); storage Pointer lu tr pht hnh Hm deallocates tt c cc khi b nh lu tr hoc tr v cho ph huynh, nu c. Sau , deallocates tiu lu tr v xa cc con tr lu tr. Tt c cc lu tr lin quan n tr em vi cha m khi lu tr phi c pht hnh trc khi lu tr khi m c pht hnh. cvRestoreMemStoragePos Phc hi v tr b nh lu tr. void cvRestoreMemStoragePos(CvMemStorage* storage,CvMemStoragePos* pos); storage b nh lu tr pos New v tr lu tr hng u Hm khi phc li v tr u lu tr t pos tham s. Hm ny v cvClearMemStorage Hm l phng php duy nht gii phng b nh b chim ng trong b nh khi. Lu mt ln na rng khng c cch no b nh min ph gia ca mt phn chim ng ca mt lu tr khi. cvSaveMemStoragePos Tit kim b nh lu tr v tr. void cvSaveMemStoragePos(const CvMemStorage* storage,CvMemStoragePos* pos); storage b nh lu tr pos V tr u ra ca u lu tr Hm s lu v tr hin ti ca u lu tr pos tham s. Cc Hm cvRestoreMemStoragePos hn na c th ly v tr ny. cvSeqElemIdx Tr v ch s ca mt yu t th t c th. int cvSeqElemIdx(const CvSeq* seq,const void* element,CvSeqBlock** block=NULL ); seq trnh t element con tr vo phn t trong dy block i s ty chn. Nu con tr khng phi l NULL, a ch ca khi trnh t c cha yu t c lu tr trong v tr ny. Hm tr v ch s ca mt yu t trnh t hoc mt s tiu cc nu phn t khng tm thy. cvSeqInsert Chn mt phn t gia ca mt dy. char* cvSeqInsert( CvSeq* seq, int beforeIndex, void* element=NULL ); seq trnh t beforeIndex Index trc khi m yu t c a vo. Chn trc khi 0 (ti thiu cho php gi tr ca tham s ny) bng cvSeqPushFront v chn trc khi seq->total (gi tr ti a cho php ca tham s ny) l bng cvSeqPush. element Chn yu t Hm thay i cc yu t trnh t t v tr c chn vo cui gn nht ca trnh t v cc bn sao ni dung phn t c nu con tr khng phi l NULL. Cc Hm tr v mt con tr n cc yu t c chn vo. 79

cvSeqInsertSlice Chn mt mng gia ca mt dy. void cvSeqInsertSlice( CvSeq* seq, int beforeIndex, const CvArr* fromArr ); seq trnh t slice mt phn ca trnh t loi b fromArr mng cc yu t t Hm chn tt c cc yu t mng fromArr ti v tr quy nh trnh t. Cc fromArr mng c th l mt ma trn hay trnh t khc. cvSeqInvert o ngc th t ca cc yu t trnh t. void cvSeqInvert( CvSeq* seq ); seq trnh t Hm o ngc trnh t ti ch - lm cho cc yu t u tin i cui cng, yu t cui cng i u tin v vv. cvSeqPop Loi b mt phn t t ngy kt thc ca mt chui. void cvSeqPop( CvSeq* seq, void* element=NULL ); seq trnh t element ty chn tham s. Nu con tr khng phi l bng khng, cc Hm sao chp cc yu t loi b v tr ny. Hm loi b mt phn t t mt chui. Hm bo co mt li nu c sn phm no. Hm c O (1) phc tp. cvSeqPopFront Loi b mt phn t t u ca mt chui. void cvSeqPopFront( CvSeq* seq, void* element=NULL ); seq trnh t element ty chn tham s. Nu con tr khng phi l bng khng, cc Hm sao chp cc yu t loi b v tr ny. Hm loi b mt phn t t u ca mt chui. Hm bo co mt li nu trnh t c sn phm no. Hm c O (1) phc tp. cvSeqPopMulti Loi b mt s yu t t hai u ca mt chui. void cvSeqPopMulti( CvSeq* seq, void* elements, 80

int count, int in front=0 ); seq trnh t elements Loi b cc yu t count S m ca cc yu t bt in_front l c xc nh kt thc ca chui sa i. CV_BACK cc yu t c thm vo cui ca chui CV_FRONT cc yu t c thm vo u ca chui Hm loi b mt s yu t t hai u ca chui. Nu s lng cc yu t c loi b vt qu s lng tng s cc yu t trong trnh t, Hm loi b cc yu t nhiu cng tt. cvSeqPush Thm mt yu t kt thc ca mt chui. char* cvSeqPush( CvSeq* seq, void* element=NULL ); seq trnh t element yu t gia tng Hm b sung thm mt yu t kt thc ca mt chui v tr v mt con tr n cc tp yu t. Nu cc element u vo l NULL, Hm ch n gin l phn b khng gian cho thm mt phn t. Cc m sau y chng minh lm th no to ra mt chui mi bng cch s dng Hm ny: CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* seq = cvCreateSeq( CV_32SC1, /* chui cc yu t s nguyn */ sizeof(CvSeq),/* kch thc tiu khng c thm cc lnh vc */ sizeof(int), /* yu t kch thc */ storage /* lu tr container */); int i; for( i = 0; i < 100; i++ ) { int* added = (int*)cvSeqPush( seq, &i ); printf( "%d is added\n", *added ); } ... /* Gii phng b nh lu tr cui cng */ cvReleaseMemStorage( &storage ); Hm c O (1) phc tp, nhng c mt phng php nhanh hn vit cc chui ln (xem cvStartWriteSeq v cc Hm lin quan). cvSeqPushFront Thm mt yu t bt u ca mt chui. char* cvSeqPushFront( CvSeq* seq, void* element=NULL ); seq trnh t element yu t gia tng Hm ny tng t nh cvSeqPush nhng n cho bit thm cc yu t mi bt u ca trnh t. Hm c O (1) phc tp. 81

cvSeqPushMulti y mt s yu t n hai u ca mt chui. void cvSeqPushMulti( CvSeq* seq, void* elements, int count, int in front=0 ); seq trnh t elements tng yu t gia count s cc yu t thc y in_front trc l c xc nh kt thc ca chui sa i. CV_BACK cc yu t c thm vo cui ca chui CV_FRONT cc yu t c thm vo u ca chui Hm b sung thm mt s yu t hai u ca mt chui. Cc yu t c thm vo trnh t theo th t ging nh h c b tr trong mng u vo, nhng h c th ri vo khi th t khc nhau. cvSeqRemove Loi b mt phn t t gia mt chui. void cvSeqRemove( CvSeq* seq, int index ); seq trnh t index Index ca yu t loi b Hm loi b cc phn t c ch s nht nh. Nu ch s c ra khi phm vi Hm bo co mt li. Mt c gng loi b mt phn t t mt chui sn phm no l mt trng hp c bit tnh trng ny. Hm loi b mt phn t bng cch thay i cc yu t trnh t gia gn nht kt thc ca trnh t v index-th v tr , cha k sau ny. cvSeqRemoveSlice Loi b mt phn trnh t. void cvSeqRemoveSlice( CvSeq* seq, CvSlice slice ); seq trnh t slice mt phn ca trnh t loi b Hm loi b mt phn t trnh t. cvSeqSearch Tm kim mt yu t trong mt chui. char* cvSeqSearch( CvSeq* seq, const void* elem, CvCmpFunc func, int is sorted, int* elem idx, void* userdata=NULL ); seq Trnh t Elem Cc yu t tm kim func Hm so snh tr v gi tr, tch cc hoc tiu cc khng ty thuc vo mi quan h gia cc yu t (xem thm cvSeqSort) is_sorted Cho d c sp xp theo trnh t hay khng elem_idx tham s u ra, ch s ca cc yu t c tm thy 82

userdata tham s ngi dng thng qua Hm compasion; gip trnh cc bin ton cu trong mt s trng hp /* a < b ? -1 : a > b ? 1 : 0 */ typedef int (CV_CDECL* CvCmpFunc)(const void* a, const void* b, void* userdata); Hm tm kim cho cc phn t theo th t. Nu trnh t c sp xp, mt nh phn O(log(N)) l c s dng, nu khng, mt tm kim n gin tuyn tnh c s dng. Nu phn t khng tm thy, hm tr v mt con tr NULL v ch s c thit lp vi s lng ca cc yu t chui tm kim tuyn tnh c s dng, hoc ch s nh nht, seq(i)>elem. cvSeqSlice Lm cho mt tiu ring bit cho mt phn trnh t. CvSeq* cvSeqSlice( const CvSeq* seq, CvSlice slice, CvMemStorage* storage=NULL, int copy data=0 ); seq trnh t slice Cc phn ca chui c trch xut storage Cc lu tr ch khi gi tiu trnh t v cc d liu c sao chp, nu bt k. Nu n l NULL, Hm s dng khi lu tr c cha cc chui u vo. copy_data l c cho bit c sao chp cc yu t ca slice c chit xut (copy_data != 0 ) hoc khng (copy_data = 0) Hm ny to ra mt chui i din cho slice quy nh ca chui u vo. Cc trnh t mi hoc chia s cc yu t vi cc trnh t ban u hoc c bn sao ring ca mnh. cc yu t. V vy, nu mt trong nhng nhu cu x l mt phn ca chui nhng cc Hm x l khng c mt tham s slice, yu cu trnh t ph c th c chit xut bng cch s dng Hm ny. cvSeqSort Sp xp th t cc yu t bng cch s dng Hm so snh quy nh. void cvSeqSort( CvSeq* seq, CvCmpFunc func, void* userdata=NULL ); /* a < b ? -1 : a > b ? 1 : 0 */ typedef int (CV_CDECL* CvCmpFunc)(const void* a, const void* b, void* userdata); seq Cc trnh t sp xp func Hm so snh rng tr v mt gi tr, tiu cc khng, hay tch cc ty thuc vo mi quan h gia cc yu t (xem tuyn b trn v v d di y) - mt Hm tng t c s dng bi qsort t C runline tr th hai, userdata khng s dng userdata tham s ngi dng thng qua Hm compasion; gip trnh cc bin ton cu trong mt s trng hp Hm sp xp cc chui ti ch bng cch s dng cc tiu chun quy nh. Di y l mt v d v bng cch s dng Hm ny: /* Sp xp 2d im theo th t t tri sang bn phi hng u-to-bottom */ static int cmp_func( const void* _a, const void* _b, void* userdata ) { CvPoint* a = (CvPoint*)_a; CvPoint* b = (CvPoint*)_b; int y_diff = a->y - b->y; int x_diff = a->x - b->x; 83

return y_diff ? y_diff : x_diff; } ... CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* seq = cvCreateSeq( CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), storage ); int i; for( i = 0; i < 10; i++ ) { CvPoint pt; pt.x = rand() % 1000; pt.y = rand() % 1000; cvSeqPush( seq, &pt ); } cvSeqSort( seq, cmp_func, 0 /* userdata is not used here */ ); /* print out the sorted sequence */ for( i = 0; i < seq->total; i++ ) { CvPoint* pt = (CvPoint*)cvSeqElem( seq, i ); printf( "(%d,%d)\n", pt->x, pt->y ); } cvReleaseMemStorage( &storage );

cvSetAdd Chim mt nt trong thit lp. int cvSetAdd( CvSet* setHeader, CvSetElem* elem=NULL, CvSetElem** inserted elem=NULL ); setHeader Set elem tham s u vo ty chn, mt yu t c chn vo. Nu khng phi l NULL, Hm sao chp d liu cc nt c phn b (MSB ca trng s nguyn u tin s b xa sau khi sao chp). inserted_elem u ra ty chn, con tr n cc t bo c phn b Hm phn b mt nt mi, ty chn cc bn sao d liu yu t u vo, v tr v con tr v ch s nt. Cc gi tr ch s c ly t cc bit thp hn ca lnh vc c ca nt. Hm c O (1) phc tp, tuy nhin, c tn ti mt Hm nhanh hn cho phn b thit lp cc nt (xem cvSetNew). cvSetNew Thm mt yu t thit lp (bin th nhanh chng). CvSetElem* cvSetNew( CvSet* setHeader ); setHeader Set Hm l mt bin th nh ni tuyn ca cvSetAdd. N chim mt nt mi v tr v mt con tr n n hn l mt ch s. 84

cvSetRemove Loi b mt phn t t tp hp. void cvSetRemove( CvSet* setHeader, int index ); setHeader Set index ch s ca phn t loi b Hm loi b mt phn t vi mt ch s xc nh t thit lp. Nu nt v tr ified khng phi l chim ng, Hm khng c g. Hm c O (1) phc tp; nh th no bao gi ht, cvSetRemoveByPtr cung cp mt cch nhanh hn loi b mt yu t thit lp nu n nm . cvSetRemoveByPtr Loi b mt phn t thit lp da trn con tr ca n. void cvSetRemoveByPtr( CvSet* setHeader, void* elem ); setHeader Set elem Loi b yu t Hm l mt bin th nh ni tuyn ca cvSetRemove yu cu mt con tr yu t. Cc Hm khng kim tra xem nt l chim ng hay khng, ngi dng nn chm sc . cvSetSeqBlockSize Thit lp kch thc khi chui. void cvSetSeqBlockSize( CvSeq* seq, int deltaElems ); seq trnh t deltaElems mong mun trnh t kch thc khi cho cc yu t Hm ny nh hng n cp pht b nh granularity. Khi khng gian trng trong chui b m ht, Hm phn b khng gian cho cc yu t trnh t deltaElems. Nu y ngn chn ngay lp tc sau mt giao trc y, hai khi c ni; khn ngoan, mt khi chui mi c to ra. V vy, tham s ln hn, thp hn chui phn mnh c th c, nhng khng gian trong khi lu tr l lng ph. Khi trnh t c to ra, deltaElems tham s c thit lp gi tr mc nh ca khong 1K. Cc Hm c th c gi bt c lc no sau khi trnh t c to ra v nh hng n phn b tng lai. Cc Hm c th thay i gi tr ca tham s p ng hn ch b nh lu tr thng qua. cvSetSeqReaderPos Di chuyn u c n v tr quy nh. void cvSetSeqReaderPos( CvSeqReader* reader, int index, int is relative=0 ); reader Reader state index v tr im n. Nu ch nh v c s dng (xem cc thng s tip theo), thc t v tr s c index mod reader->seq->total. 85

is_relative Nu n khng phi l s khng, sau ch s l tng i so vi v tr hin ti Hm di chuyn v tr c cho mt v tr tuyt i hay tng i so vi v tr hin ti. 1.4 Cc hm v Hm v lm vic vi cc ma trn / hnh nh c chiu su ty . Ranh gii ca cc hnh dng c th c tr li vi kh rng ca (thc hin ch dnh cho hnh nh 8-bit cho by gi). Tt c cc Hm bao gm mu sc tham s c s dng mt gi tr rgb (c th c xy dng vi CV_RGB v m hoc cc Hm cv) Cho hnh nh mu sc v sng mu xm. i vi hnh nh mu sc knh thng thng l mu xanh, xanh, , y l nhng g cv, cv v cv mong i, v vy nu bn to thnh mt mu sc s dng cv, n s ging nh: cvScalar (thnh phn mu xanh da tri, xanh l cy thnh phn, thnh phn, thnh phn alpha]) Nu bn ang s dng dng hnh nh ca ring bn v cc Hm I / O, bn c th s dng bt c knh no t hng, cc Hm v qu trnh mi knh c lp v khng ph thuc vo knh trt t hoc thm ch trn khng gian mu sc c s dng. Ton b hnh nh c th c chuyn i t BGR RGB hoc mt khng gian mu khc nhau bng cch s dng cv. Nu mt con s rt ra mt phn hoc hon ton bn ngoi hnh nh, Hm v clip n. Ngoi ra, Hm v c th x l quy nh ta im nh vi chnh xc sub-pixel, ta c th c thng qua nh l s im c nh, m ha l s nguyn. S lng bit phn on c quy nh bi tham s thay i v cc ta im thc t c tnh nh im (x, y) Point2f (x2-ca, y 2-ca). Tnh nng ny c bit hiu qu khi rendering antialiased hnh dng. Ngoi ra, lu rng cc Hm khng h tr alpha-minh bch - khi cc hnh nh mc tiu l 4channnel, sau mu sc [3] ch n gin l sao chp cc im nh sn li. V vy, nu bn mun sn bn trong sut hnh dng, bn c th v trong mt b m ring bit v sau pha trn n vi cc chnh hnh nh. cvCircle V mt vng trn. void cvCircle( CvArr* img, CvPoint center, int radius, CvScalar color, int thickness=1, int lineType=8, int shift=0 ); img Hnh nh vng trn c v center trung tm ca vng trn radius bn knh ca vng trn color mu sc vng trn thickness dy ca cng vng trn nu tch cc, nu khng iu ny ch ra rng mt vng trn y c rt ra linetype Loi ranh gii vng trn, xem m t dng shift s bit phn on trong cc ta trung tm v gi tr bn knh Hm v mt vng trn n gin hoc y vi mt trung tm v bn knh. cvClipLine Clip dng chng li cc hnh ch nht hnh nh. 86

int cvClipLine( CvSize imgSize, CvPoint* pt1, CvPoint* pt2 ); imgSize Kch thc ca hnh nh pt1 u tin kt thc im ca on thng. N c sa i theo Hm. pt2 th hai kt thc im ca on thng. N c sa i theo Hm. Hm tnh ton l mt phn ca on ng l hon ton bn trong hnh nh. N tr v 0 nu on ng l hon ton nm ngoi hnh nh v 1 khc cvDrawContours V phc tho ng vin bn trong mt hnh nh. void cvDrawContours( CvArr *img, CvSeq* contour, CvScalar external color, CvScalar hole color, int max level, int thickness=1, int lineType=8 ); img Hnh nh cc ng vin c rt ra. Nh vi bt k Hm v khc, cc ng vin c ct bt vi thu nhp t u. contour con tr n cc ng vin u tin external_color mu sc ca cc ng vin bn ngoi hole_color Mu sc ca ng nt ni b (l) max_level mc ti a cho ng nt v. Nu 0, ch contour c rt ra. Nu 1, ng vin v tt c cc ng nt sau n trn cng mt mc c rt ra. Nu 2, tt c sau nhng ng nt v tt c cc ng nt mt mc thp hn cc ng vin c rt ra, v vv. Nu gi tr l tiu cc, Hm ny khng v cc ng vin sau y sau khi contour rt nhng nhng ng nt con ca contour ti cc |max_level| - 1 level. thickness dy ca ng dy cc ng vin c rt ra. Nu n l tiu cc (V d, = CV_FILLED), ni tht ng vin c rt ra. lineType Loi ca cc on ng vin, xem m t Line Hm v phc tho ng vin trong hnh nh nu thickness>= 0 hoc lp y cc khu vc gip bi nhng ng nt nu thickness<0. V d: Kt ni thnh phn pht hin thng qua cc Hm ng vin #include "cv.h" #include "highgui.h" int main( int argc, char** argv ) { IplImage* src; // the first command line parameter must be file name of binary // (black-n-white) image if( argc == 2 && (src=cvLoadImage(argv[1], 0))!= 0) { IplImage* dst = cvCreateImage( cvGetSize(src), 8, 3 ); CvMemStorage* storage = cvCreateMemStorage(0); 87

CvSeq* contour = 0; cvThreshold( src, src, 1, 255, CV_THRESH_BINARY ); cvNamedWindow( "Source", 1 ); cvShowImage( "Source", src ); cvFindContours( src, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); cvZero( dst ); for( ; contour != 0; contour = contour->h_next ) { CvScalar color = CV_RGB( rand()&255, rand()&255, rand()&255 ); /* replace CV_FILLED with 1 to see the outlines */ cvDrawContours( dst, contour, color, color, -1, CV_FILLED, 8 ); } cvNamedWindow( "Components", 1 ); cvShowImage( "Components", dst ); cvWaitKey(0); } } cvEllipse V mt vng cung elip n gin hoc dy hoc mt lp y khu vc hnh elip. void cvEllipse( CvArr* img, CvPoint center, CvSize axes, double angle, double start angle, double end angle, CvScalar color, int thickness=1, int lineType=8, int shift=0 ); img Hnh nh center trung tm ca hnh elip axes Chiu di ca trc ellipse angle gc xoay start_angle gc bt u ca vng cung elip end_angle gc kt thc ca vng cung elip. color mu sc Ellipse thickness dy ca cng h quang hnh elip nu tch cc, nu khng iu ny ch ra rng mt y khu vc hnh elip c rt ra linetype Loi ranh gii hnh elip, xem m t dng shift s bit phn on trong cc ta trung tm v trc gi tr Hm v mt vng cung elip n gin hoc dy hoc in vo mt khu vc hnh elip. H quang c gi bi hnh ch nht ROI. Mt xp x tuyn tnh piecewise c s dng cho cc vng cung antialiased v vng cung dy. Tt c cc gc c a ra cc mc . Nhng hnh nh di y gii thch ngha ca cc thng s. 88

Cc thng s ca Arc Elliptic

cvEllipseBox V mt vng cung elip n gin hoc dy hoc in vo mt khu vc hnh elip. void cvEllipseBox( CvArr* img, CvBox2D box, CvScalar color, int thickness=1, int lineType=8, int shift=0 ); img Hnh nh box hp km theo cc hnh elip v thickness dy ca ranh gii hnh elip linetype Loi ranh gii hnh elip, xem m t dng shift s bit phn on trong cc ta nh hp Hm rt ra mt phc tho hnh elip n gin hoc dy, hoc in vo mt hnh elip. Hm cung cp mt cch thun tin v mt hnh elip xp x mt s hnh dng, l nhng g CamShift v FitEllipse lm. Cc hnh elip rt ra l ct bt thu nhp t u hnh ch nht. Mt xp x tuyn tnh piecewise c s dng cho vng cung antialiased v vng cung dy. cvFillConvexPoly in vo cc mt a gic li. void cvFillConvexPoly( CvArr* img, CvPoint* pts, int npts, CvScalar color, 89

int lineType=8, int shift=0 ); img Hnh nh pts mng ca con tr n mt a gic npts nh Polygon truy cp color a gic mu sc linetype Loi ranh gii a gic, xem m t dng shift s bit phn on trong cc ta nh Hm y bn trong ca mt a gic li. Hm ny l nhanh hn nhiu so vi Hm, nhim v cvFillPoly v c th lm khng ch a gic li nhng bt k hnh a gic n iu, tc l, mt a gic c ng vin ct tt c cc ng ngang (qut dng) hai ln l nhiu nht. cvFillPoly in vo cc bn trong ca mt a gic. void cvFillPoly( CvArr* img, CvPoint** pts, int* npts, int contours, CvScalar color, int lineType=8, int shift=0 ); img Hnh nh pts mng ca con tr cho cc a gic npts mng ca cc quy nh a gic contours S ng nt lin kt khu vc y color a gic mu sc linetype Loi ranh gii a gic, xem m t dng shift s bit phn on trong cc ta nh Hm lp y din tch b gii hn bi cc ng nt mt s a gic. Hm y phc tp khu vc, v d, khu vc c l, ng vin t giao l, v vv. cvGetTextSize Ly chiu rng v chiu cao ca mt chui vn bn. void cvGetTextSize( const char* textString, const CvFont* font, CvSize* textSize, int* baseline ); font con tr n cu trc phng ch textString Chui u vo textSize kch thc Resultant ca chui vn bn. Chiu cao ca vn bn khng bao gm chiu cao ca phn di ng c s. baseline y-phi hp ca ng c s lin quan n pha di hu ht cc im vn bn Hm tnh ton kch thc ca hnh ch nht km theo mt chui vn bn khi mt quy nh phng ch c s dng. 90

cvInitFont Khi to cu trc phng ch. void cvInitFont( CvFont* font, int fontFace, double hscale, double vscale, double shear=0, int thickness=1, int lineType=8 ); font con tr cu trc ch khi to bi Hm fontFace nhn dng tn ch. Ch c mt tp hp con ca phng ch Hershey http://sources.isc.org/utils/misc/hershey-font.txt c h tr: CV_FONT_HERSHEY_SIMPLEX kch thc bnh thng phng ch sans-serif CV_FONT_HERSHEY_PLAIN nh kch thc phng ch sans-serif CV_FONT_HERSHEY_DUPLEX bnh thng kch thc phng ch sans-serif (phc tp hn CV_FONT_HERSHEY_SIMPLEX) CV_FONT_HERSHEY_COMPLEX bnh thng kch thc phng ch serif CV_FONT_HERSHEY_TRIPLEX bnh thng kch thc serif font (phc tp hn CV_FONT_HERSHEY_COMPLEX) CV_FONT_HERSHEY_COMPLEX_SMALL phin bn nh hn ca CV_FONT_HERSHEY_COMPLEX CV_FONT_HERSHEY_SCRIPT_SIMPLEX vit tay kiu ch CV_FONT_HERSHEY_SCRIPT_COMPLEX phc tp hn bin th ca CV_FONT_HERSHEY_SCRIPT_SIMPLEX Tham s c th c ghp li t mt trong nhng gi tr trn v mt CV_FONT_ITALIC c, m ch ra ch in nghing hoc xin. hscale quy m ngang. Nu bng 1.0f, cc nhn vt c chiu rng ban u ty thuc vo loi phng ch. Nu bng 0.5f, cc nhn vt na chiu rng ban u. vscale dc quy m. Nu bng 1.0f, cc nhn vt c chiu cao ban u ty thuc vo phng ch. Nu bng 0.5f, cc nhn vt ca mt na chiu cao ban u. shear ct tip tuyn ca cc sn ni nhn vt lin quan n ng thng ng. Mt gi tr khng c ngha l mt phng ch khng nghing, 1.0f c ngha l khong mt dc 45 . thickness dy ca cc nt vn bn lineType Loi ca t qu, xem m t Line Hm khi to cu trc ch c th c thng qua vi Hm v ch. cvInitLineIterator Khi to bin lp dng. int cvInitLineIterator( const CvArr* image, CvPoint pt1, CvPoint pt2, CvLineIterator* line iterator, int connectivity=8, int left to right=0 ); image Hnh nh mu dng t 91

pt1 u tin kt thc im ca on thng pt2 im kt thc th hai ca on ng line_iterator tr cu trc dng trng thi lp connectivity kt ni dng qut, 4 hoc 8. left_to_right(left_to_right = 0) sau dng c qut trong th t quy nh, t pt1 n pt2. Nu (left_to_right!= 0) dng c qut t tri im bn phi. Hm khi to bin lp v tr v s lng im nh gia hai kt thc im. C hai im ny phi c bn trong hnh nh. Sau khi lp c khi to, tt c cc im trn dng raster kt ni hai im kt thc c th c ly ra bi cc cuc gi k tip ca CV_NEXT_LINE_POINT im. Cc im trn ng dy c tnh bng cch s dng 4 kt ni hoc kt ni 8-Bresen ham thut ton. V d: S dng dng lp tnh ton tng cc gi tr im nh dc theo dng mu. CvScalar sum_line_pixels( IplImage* image, CvPoint pt1, CvPoint pt2 ) { CvLineIterator iterator; int blue_sum = 0, green_sum = 0, red_sum = 0; int count = cvInitLineIterator( image, pt1, pt2, &iterator, 8, 0 ); for( int i = 0; i < count; i++ ){ blue_sum += iterator.ptr[0]; green_sum += iterator.ptr[1]; red_sum += iterator.ptr[2]; CV_NEXT_LINE_POINT(iterator); /* print the pixel coordinates: demonstrates how to calculate the coordinates */ { int offset, x, y; /* assume that ROI is not set, otherwise need to take it into account. */ offset = iterator.ptr - (uchar*)(image->imageData); y = offset/image->widthStep; x = (offset - y*image->widthStep)/(3*sizeof(uchar) /* size of pixel */); printf("(%d,%d)\n", x, y ); } } return cvScalar( blue_sum, green_sum, red_sum ); }

cvLine V mt on thng ni hai im. void cvLine( CvArr* img, CvPoint pt1, CvPoint pt2, 92

CvScalar color, int thickness=1, int lineType=8, int shift=0 ); img Hnh nh pt1 im u tin ca on ng pt2 im th hai ca on ng Mu ng k mu thickness dy dng lineType Loi ca dng: 8 (hoc b qua) 8-kt ni dng. 4 4 kt ni dng. CV_AA antialiased. shift s bit phn on ti im ta Hm rt ra on ng t pt1 v pt2 im trong hnh nh. Dng c ct bt hnh nh hoc hnh ch nht ROI. i vi dng vi khng antialiased s nguyn ta 8-kt ni hoc thut ton Bresenham 4 kt ni c s dng. ng dy dy c rt ra vi lm trn kt thc. Dng Antialiased c rt ra bng cch s dng Gaussian lc. xc nh mu ng, ngi s dng c th s dng v m CV_RGB( r, g, b ). cvPolyLine Rt ra cc a gic n gin hay dy. void cvPolyLine( CvArr* img, CvPoint** pts, int* npts, int contours, int is closed, CvScalar color, int thickness=1, int lineType=8, int shift=0 ); pts mng ca con tr cho cc a gic npts mng ca cc quy nh a gic contours S ng nt lin kt khu vc y img Hnh nh is_closed Ch xem cc polylines v phi c ng li. Nu ng ca, Hm rt ra ng t nh cui cng ca tt c cc ng vin nh u tin. color Polyline color thickness dy ca cc cnh polyline lineType Loi ca cc on ng thng, xem m t Line shift s bit phn on trong cc ta nh Hm rt ra mt hoc nhiu ng cong a gic. cvPutText V mt chui vn bn. void cvPutText( 93

CvArr* img, const char* text, CvPoint org, const CvFont* font, CvScalar color ); img nh u vo text chui in org Ta ca gc di bn tri ca ch ci u tin font con tr n cu trc phng ch color mu ch Hm lm cho vn bn trong hnh nh vi phng ch v mu sc. Vn bn in ct bt bi hnh ch nht ROI. Biu tng khng thuc v phng ch c thay th bng biu tng cho mt hnh ch nht. cvRectangle V mt hnh ch nht n gin, dy, y. void cvRectangle( CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int lineType=8, int shift=0 ); img Hnh nh pt1 Mt trong cc nh ca hnh ch nht pt2 i din hnh ch nht nh color mu nt v (RGB) hoc sng (hnh nh mu xm ) thickness dy ca ng to ra hnh ch nht. Tiu cc gi tr, v d nh, CV IN, gy ra cc Hm v mt hnh ch nht y. lineType Loi ca cc dng, xem Line thay i s bit phn on ti im ta Hm v mt hnh ch nht vi hai gc i din pt1 v pt2. CV_RGB Xy dng mt gi tr mu. #define CV RGB( r, g, b ) cvScalar( (b), (g), (r) ) red thnh phn grn xanh thnh phn blu xanh thnh phn 1.5 XML/YAML Persistence CvFileStorage Tp tin lu tr. typedef struct CvFileStorage { 94

// cc trng n } CvFileStorage; Cu trc CvFileStorage l mt "hp en" i din ca cc tp tin lu tr c lin quan vi mt tp tin trn a. Mt s Hm c m t di y CvFileStorage nh l u vo v cho php theuser lu hoc ti cc b su tp th bc bao gm cc gi tr v hng, tiu chun CXCore i tng (chng hn nh ma trn, trnh t, th), v cc i tng ngi dng nh ngha. CXCore c th c v ghi d liu trong XML (http://www.w3c.org/XML) hoc YAML (http://www.yaml.org) nh dng. Di y l mt v d v 33 du chm ng nhn dng ma trn A, c lu tr trong XML v YAML cc tp tin bng cch s dng cc Hm CXCore: XML: <?xml version="1.0"> <opencv_storage> <A type_id="opencv-matrix"> <rows>3</rows> <cols>3</cols> <dt>f</dt> <data>1. 0. 0. 0. 1. 0. 0. 0. 1.</data> </A> </opencv_storage> YAML: %YAML:1.0 A: !!opencv-matrix rows: 3 cols: 3 dt: f data: [ 1., 0., 0., 0., 1., 0., 0., 0., 1.] V n c th c nhn thy t cc v d, XML s dng cc th lng nhau i din cho h thng phn cp, trong khi YAML s dng tht u dng cho mc ch (tng t nh ngn ng lp trnh Python). Cc Hm CXCore tng t c th c v ghi d liu trong c hai nh dng, nh dng c bit l xc nh bi phn m rng ca tp tin m, xml cho cc tp tin XML. yml hoc yaml YAML.. CvFileNode Node lu tr tp tin. /* Loi nt tp tin */ #define CV_NODE_NONE 0 #define CV_NODE_INT 1 #define CV_NODE_INTEGER CV_NODE_INT #define CV_NODE_REAL 2 #define CV_NODE_FLOAT CV_NODE_REAL #define CV_NODE_STR 3 #define CV_NODE_STRING CV_NODE_STR #define CV_NODE_REF 4 /* khng c s dng */ #define CV_NODE_SEQ 5 #define CV_NODE_MAP 6 #define CV_NODE_TYPE_MASK 7 95

/* C ty chn no */ #define CV_NODE_USER 16 #define CV_NODE_EMPTY 32 #define CV_NODE_NAMED 64 #define CV_NODE_TYPE(tag) ((tag) & CV_NODE_TYPE_MASK) #define CV_NODE_IS_INT(tag) (CV_NODE_TYPE(tag) == CV_NODE_INT) #define CV_NODE_IS_REAL(tag) (CV_NODE_TYPE(tag) == CV_NODE_REAL) #define CV_NODE_IS_STRING(tag) (CV_NODE_TYPE(tag) == CV_NODE_STRING) #define CV_NODE_IS_SEQ(tag) (CV_NODE_TYPE(tag) == CV_NODE_SEQ) #define CV_NODE_IS_MAP(tag) (CV_NODE_TYPE(tag) == CV_NODE_MAP) #define CV_NODE_IS_COLLECTION(tag) (CV_NODE_TYPE(tag) >= CV_NODE_SEQ) #define CV_NODE_IS_FLOW(tag) (((tag) & CV_NODE_FLOW) != 0) #define CV_NODE_IS_EMPTY(tag) (((tag) & CV_NODE_EMPTY) != 0) #define CV_NODE_IS_USER(tag) (((tag) & CV_NODE_USER) != 0) #define CV_NODE_HAS_NAME(tag) (((tag) & CV_NODE_NAMED) != 0) #define CV_NODE_SEQ_SIMPLE 256 #define CV_NODE_SEQ_IS_SIMPLE(seq) (((seq)->flags & CV_NODE_SEQ_SIMPLE) != 0) typedef struct CvString { int len; char* ptr; } CvString; /* Tt c cc phm (tn) ca cc yu t trong lu tr tp tin readed c lu tr trong bm tng tc hot ng tra cu */ typedef struct CvStringHashNode { unsigned hashval; CvString str; struct CvStringHashNode* next; } CvStringHashNode; /* Yu t c bn ca tp tin lu tr - v hng hoc b su tp */ typedef struct CvFileNode { int tag; struct CvTypeInfo* info; /* loi thng tin (Ch for i tng ngi dng c xc nh, for i vi nhng ngi khc n l 0) */ union { double f; /* v hng ni im s */ int i; /* V hng s lng s nguyn */ CvString str; /* chui vn bn */ CvSeq * seq; /* chui (ra lnh cho b su tp ca cc nt tp tin) */ struct CvMap * map; /* bn (b su tp ca cc nt tp tin c tn) */ } data; 96

} CvFileNode; Cu trc ny ch c s dng ly d liu t tp tin lu tr (tc l, cho cc d liu ti t tp tin). Khi d liu c ghi vo mt tp tin, n c thc hin tun t, vi m ti thiu. Khng c d liu c lu tr trong lu tr tp tin. Ngc li, khi d liu c c t mt tp tin, ton b tp tin c phn tch c php v i din trong b nh nh mt cy. Mi nt ca cy c i din bi CvFileNode. Loi N nt tp tin c th c ly nh CV_NODE_TYPE(N->tag). Mt s cc nt tp tin (l) l v hng: cc chui vn bn, s nguyn, s du chm ng. Cc nt tp tin khc l cc b su tp ca cc nt tp tin, c th c v hng hoc cc b su tp ln lt ca h. C hai loi b su tp: trnh t v bn (chng ti s dng k hiu YAML, tuy nhin, iu ny cng ng cho dng XML). Trnh t (khng trn chng vi CvSeq) c sp xp b su tp ca cc nt tp tin giu tn, bn l khng c th t cc b su tp ca tn l cc nt tp tin. V vy, cc yu t ca chui c truy cp bi ch s (GetSeqElem), trong khi cc yu t ca bn c truy cp bi tn (GetFileNodeByName). Bng di y m t cc cc loi khc nhau ca cc nt tp tin: /////////// table Type CV_NODE_TYPE(node->tag) Value Integer CV_NODE_INT node->data.i Floating-point CV_NODE_INT node->data.f Text string CV NODE STR node->data.str.ptr Trnh t CV NODE SEQ node->data.seq Map CV NODE MAP node->data.map(xem di y) Khng cn phi truy cp vo cc lnh vc map trc tip (bng cch ny, CvMap l mt cu trc n). Cc yu t ca bn c th c ly ra vi Hm GetFileNodeByName mt mt con tr n nt tp tin "bn " . Mt i tng ngi dng (ty chnh) l mt th hin hoc l mt trong cc loi CxCore tiu chun, chng hn nh CvMat, CvSeq vv, hoc bt k loi hnh ng k vi RegisterTypeInfo. Mt i tng ban u l i din. bc bi trong mt tp tin nh l mt bn (nh th hin trong XML v cc tp tin v d YAML trn) sau khi lu tr tp tin c m ra v phn tch c php. Sau i tng c th c gii m (chuyn i sang i din bn a) theo yu cu - khi ngi dng gi cc Hm Read hoc ReadByName. CvAttrList Danh sch cc thuc tnh. typedef struct CvAttrList { const char** attr; /* mng NULL-chm dt (thuc tnh \ _name, thuc tnh \ _value) cp */ struct CvAttrList tip theo; / * con tr n on tip theo ca danh sch cc thuc tnh * / } 97

CvAttrList; /* Khi to cu trc CvAttrList */ inline CvAttrList cvAttrList( const char** attr=NULL, CvAttrList* next=NULL ); /* Tr v thuc tnh gi tr hoc 0 (NULL) nu khng c thuc tnh nh vy */ const char* cvAttrValue( const CvAttrList* attr, const char* attr\_name ); Trong vic thc hin hin ti, thuc tnh c s dng vt qua cc thng s khi vit ngi dng i tng (xem Write). Thuc tnh XML bn trong cc th khng c h tr, ngoi cc loi i tng c im k thut (type_id thuc tnh). CvTypeInfo in thng tin. typedef int (CV_CDECL *CvIsInstanceFunc)( const void* structPtr ); typedef void (CV_CDECL *CvReleaseFunc)( void** structDblPtr ); typedef void* (CV_CDECL *CvReadFunc)( CvFileStorage* storage, CvFileNode* node ); typedef void (CV_CDECL *CvWriteFunc)( CvFileStorage* storage, const char* name, const void* structPtr, CvAttrList attributes ); typedef void* (CV_CDECL *CvCloneFunc)( const void* structPtr ); typedef struct CvTypeInfo { int flags; /* khng c s dng */ int header_size;; /* sizeof (CvTypeInfo) */ struct CvTypeInfo* prev; /* loi trc ng k trong danh sch */ struct CvTypeInfo* next; /* tip theo ng k loi trong danh sch */ const char* type_name; /* tn loi, c vit lu tr tp tin */ /* Phng php */ CvIsInstanceFunc is_instance; /* kim tra nu i tng thng qua thuc loi */ CvReleaseFunc release; /* i tng pht hnh (b nh ...) */ CvReadFunc read; /* i tng c t tp tin lu tr */ CvWriteFunc write; /* vit i tng lu tr tp tin */ CvCloneFunc clone; /* to ra mt bn sao ca i tng */ } CvTypeInfo; CvTypeInfo cu trc cha thng tin v mt trong nhng tiu chun hoc ngi dng nh ngha cc loi. Trng hp ca loi c th hoc khng c th cha mt con tr vo CvTypeInfo tng ng cu trc. Trong bt k trng hp no, c mt cch tm thy nhng cu trc thng tin loi cho mt i tng bng cch s dng Typeof Hm. Aternatively, thng tin loi c th c tm thy theo tn loi s dng FindType, c s dng khi mt i tng c c t tp tin lu tr. Ngi dng c th ng k mt loi mi vi RegisterType rng cho bit thm cc cu trc thng tin nh vo u ca danh sch loi. V vy, n c th to ra loi chuyn ngnh t cc loi tiu chun chung v ghi ln cc phng php c bn. cvClone 98

Lm cho mt bn sao ca mt i tng. void* cvClone( const void* structPtr ); structPtr Cc i tng nhn bn Hm tm thy cc loi ca mt i tng nht nh v cc cuc gi clone vi cc i tng c thng qua. cvEndWriteStruct Kt thc bng vn bn ca mt cu trc. void cvEndWriteStruct(CvFileStorage* fs); fs tp tin lu tr Hm hon thnh cu trc hin bng vn bn. cvFindType Tm thy mt loi theo tn ca n. CvTypeInfo* cvFindType(const char* typeName); typeName Tn Loi Hm tm thy mt loi ng k theo tn ca n. N tr v NULL nu khng c loi vi quy nh tn. cvFirstType Tr v khi u ca mt danh sch loi. CvTypeInfo* cvFirstType(void); Hm tr v cc loi u tin trong danh sch cc loi ng k. Chuyn hng thng qua danh sch c th c thc hin thng qua prev v cc trng next ca cu trc CvTypeInfo. cvGetFileNode Tm mt nt trong mt bn hoc lu tr tp tin. CvFileNode* cvGetFileNode( CvFileStorage* fs, CvFileNode* map, const CvStringHashNode* key, int createMissing=0 ); fs tp tin lu tr map Bn cha. Nu n l NULL, Hm tm kim mt nt cp cao nht. Nu c hai map v key l NULLs, hm tr v nt tp tin gc - mt bn c cha cc nt cp cao nht. key quan trng duy nht con tr n cc tn nt, ly vi GetHashedKey createMissing Flag xc nh liu mt nt vng mt nn c thm vo bn Hm tm thy mt nt tp tin. N l mt phin bn nhanh hn ca GetFileNodeByName (xem GetHashedKey). Ngoi ra, cc Hm c th chn mt nt mi, nu n khng c trong bn . cvGetFileNodeByName Tm mt nt trong mt bn hoc lu tr tp tin. CvFileNode* cvGetFileNodeByName( const CvFileStorage* fs, const CvFileNode* map, const char* name); 99

fs tp tin lu tr map Bn cha. Nu n l NULL, tm kim Hm trong tt c cc nt cp cao nht (streams) bt u vi mt trong nhng ngi u tin. name tn tp tin nt Hm tm thy mt nt tp tin theo name. Nt hoc l tm kim bn , nu con tr NULL, cc nt lu tr tp tin gia cc cp cao nht. S dng Hm ny cho cc bn v GetSeqElem (Hay trnh t c) cho cc trnh t, n c th nagivate thng qua cc tp tin lu tr. tng tc truy vn lp nhiu cho mt phm no (v d, trong trng hp ca mt mng ca cu trc), ngi ta c th s dng mt s kt hp ca GetHashedKey v GetFileNode. cvGetFileNodeName Tr v tn ca mt nt tp tin. const char* cvGetFileNodeName( const CvFileNode* node ); node file nt Hm tr v tn ca mt nt tp tin hoc NULL, nu nt tp tin khng c tn hoc nu node l NULL. cvGetHashedKey Tr v mt con tr duy nht cho mt tn cho. CvStringHashNode* cvGetHashedKey( CvFileStorage* fs, const char* name, int len=-1, int createMissing=0 ); fs tp tin lu tr name nt tn Literal len Chiu di ca tn (nu n c bit n apriori), hoc -1 nu n cn phi c tnh ton createMissing Flag ch nh, cho d mt kho vng mt nn c b sung vo bng bm Hm tr v mt con tr duy nht cho mi tn file nt c th. Con tr ny c th c sau thng qua Hm GetFileNode nhanh hn GetFileNodeByName bi v n so snh cc chui vn bn bng cch so snh con tr ch khng phi l ni dung ca chui. Hy xem xt v d sau y mt lot cc im c m ha nh l mt chui nhp cnh-2 bn : %YAML:1.0 points: - { x: 10, y: 10 } - { x: 20, y: 20 } - { x: 30, y: 30 } # ... Sau , n c th c c bm "x" v "y" con tr tng tc gii m ca cc im. #include "cxcore.h" int main( int argc, char** argv ) { CvFileStorage* fs = cvOpenFileStorage( "points.yml", 0, CV\_STORAGE\_READ ) CvStringHashNode* x\_key = cvGetHashedNode( fs, "x", -1, 1 ); CvStringHashNode* y\_key = cvGetHashedNode( fs, "y", -1, 1 ); CvFileNode* points = cvGetFileNodeByName( fs, 0, "points" ); 100

if( CV\_NODE\_IS\_SEQ(points->tag) ) { CvSeq* seq = points->data.seq; int i, total = seq->total; CvSeqReader reader; cvStartReadSeq( seq, &reader, 0 ); for( i = 0; i < total; i++ ) { CvFileNode* pt = (CvFileNode*)reader.ptr; #if 1 /* nhanh hn phin bn */ CvFileNode* xnode = cvGetFileNode( fs, pt, x\_key, 0 ); CvFileNode* ynode = cvGetFileNode( fs, pt, y\_key, 0 ); assert( xnode && CV\_NODE\_IS\_INT(xnode->tag) && ynode && CV\_NODE\_IS\_INT(ynode->tag)); int x = xnode->data.i; // or x = cvReadInt( xnode, 0 ); int y = ynode->data.i; // or y = cvReadInt( ynode, 0 ); # Elif 1 / * chm bin th, khng s dng x \ _key & y \ _key * / CvFileNode* xnode = cvGetFileNodeByName( fs, pt, "x" ); CvFileNode* ynode = cvGetFileNodeByName( fs, pt, "y" ); assert( xnode && CV\_NODE\_IS\_INT(xnode->tag) && ynode && CV\_NODE\_IS\_INT(ynode->tag)); int x = xnode->data.i; // or x = cvReadInt( xnode, 0 ); int y = ynode->data.i; // or y = cvReadInt( ynode, 0 ); #else /* the slowest yet the easiest to use variant */ int x = cvReadIntByName( fs, pt, "x", 0 /* default value */ ); int y = cvReadIntByName( fs, pt, "y", 0 /* default value */ ); #endif CV\_NEXT\_SEQ\_ELEM( seq->elem\_size, reader ); printf("%d: (%d, %d)\n", i, x, y ); } } cvReleaseFileStorage( &fs ); return 0; } Xin lu rng bt c phng php truy cp vo mt bn m bn ang s dng, n vn cn chm hn nhiu hn bng cch s dng cc trnh t n gin, v d, trong v d trn, n l hiu qu hn m ha cc im nh cp s nguyn trong mt chui s duy nht. cvGetRootFileNode Ly mt trong cc nt cp cao nht ca tp tin lu tr. CvFileNode* cvGetRootFileNode( const CvFileStorage* fs, int stream index=0 ); fs tp tin lu tr stream_index Zero-index ca dng sui. Xem StartNextStream. Trong hu ht trng hp, c 101

ch c mt dng trong tp tin, tuy nhin, c th c nhiu. Hm tr v mt trong cc nt tp tin trn cp. Cc nt cp cao nht khng c tn, chng tng ng vi cc dng c lu tr sau khi mt trong lu tr tp tin. Nu ch s l ra khi phm vi, Hm tr v mt con tr NULL, do tt c cc nt cp cao nht c th c lp cc cuc gi tip theo Hm vi stream_index=0,1,..., cho n khi con tr NULL c tr v. Hm ny c th c s dng nh mt c s cho quy traversal lu tr tp tin. cvLoad Np mt i tng t mt tp tin. void* cvLoad( const char* filename, CvMemStorage* storage=NULL, const char* name=NULL, const char** realName=NULL ); filename tn tp tin storage B nh lu tr cho cc cu trc nng ng, chng hn nh CvSeq hoc CvGraph. N khng c s dng cho ma trn, hnh nh. name tn i tng ty chn. Nu n l NULL, i tng cp cao u tin trong lu tr s c np. realName tham s ty chn u ra s cha tn ca i tng c ti (hu ch nu name = NULL) Hm ti mt i tng t mt tp tin. N cung cp mt giao din n gin cvRead. Sau khi i tng c ti, lu tr tp tin c ng ca v tt c cc b m tm thi c xa. V vy, ti mt cu trc nng ng, chng hn nh l mt chui, ng vin, hoc biu , ta nn thng qua mt b nh hp l a im lu tr Hm. cvOpenFileStorage M tp tin lu tr cho vic c hoc ghi d liu. CvFileStorage* cvOpenFileStorage( const char* filename, CvMemStorage* memstorage, int flags); filename Tn ca tp tin kt hp vi lu tr memstorage b nh lu tr c s dng cho d liu tm thi v lu tr cc cu trc nng ng, nh nh CvSeq hoc CvGraph. Nu n l NULL, mt b nh lu tr tm thi c to ra v s dng. flags c th l mt trong nhng iu sau y: CV_STORAGE_READ lu tr c m ra c CV_STORAGE_WRITE lu tr c m ra vit Hm m tp tin lu tr cho vic c hoc ghi d liu. Trong trng hp th hai, mt tp tin mi to ra hoc mt tp tin hin c vit li. Cc loi tp tin c c hoc bng vn bn c xc nh bi tn tp tin m rng: xml cho XML v yml hoc yaml cho YAML. Hm tr v mt con tr cu trc CvFileStorage. cvRead Mt i tng gii m v tr v mt con tr n n. void* cvRead( 102

CvFileStorage* fs, CvFileNode* node, CvAttrList* attributes=NULL ); fs tp tin lu tr node nt i tng gc attributes biu tng thng s cha s dng Cc Hm gii m ngi s dng mt i tng (to ra mt i tng trong mt i din c ngun gc t tp tin lu tr cy con) v tr v n. Cc i tng c gii m phi l mt th hin ca mt loi ng k h tr phng php read (xem CvTypeInfo). Cc loi i tng c xc nh bi loi tn c m ha trong tp tin. Nu i tng l mt cu trc nng ng, n c to ra hoc trong b nh lu tr v thng qua OpenFileStorage hoc, nu mt con tr NULL c thng qua, trong tm thi b nh lu tr, c pht hnh khi ReleaseFileStorage c gi. Nu khng, nu i tng khng phi l mt cu trc nng ng, n c to ra trong b nh v s c pht hnh vi mt Hm chuyn ngnh hoc bng cch s dng thng co Release. cvReadByName Tm thy mt i tng tn v gii m n. void* cvReadByName( CvFileStorage* fs, const CvFileNode* map, const char* name, CvAttrList* attributes=NULL ); fs tp tin lu tr map Bn cha m. Nu n l NULL, Hm tm kim mt nt cp cao nht. name tn nt attributes biu tng thng s cha s dng Cc Hm c mt s chng cht n gin ca GetFileNodeByName v Read. cvReadInt Ly mt gi tr s nguyn t mt nt tp tin. int cvReadInt( const CvFileNode* node, int defaultValue=0 ); node file nt defaultValue Cc gi tr c tr v nu node l NULL Hm tr v mt s nguyn c i din bi cc nt tp tin. Nu nt tp tin l NULL, defaultValue c tr li (do , n l thun tin gi ng Hm sau khi GetFileNode m khng cn kim tra mt con tr NULL). Nu nt tp tin c kiu CV_NODE_INT, sau node>data.i c tr v. Nu nt tp tin c loi CV_NODE_REAL, sau node->data.f l chuyn i thnh mt s nguyn v tr v. Nu khng kt qu l khng xc nh. cvReadIntByName Tm mt nt tp tin v tr v gi tr ca n. int cvReadIntByName( const CvFileStorage* fs, const CvFileNode* map, 103

const char* name, int defaultValue=0 ); fs tp tin lu tr map Bn cha m. Nu n l NULL, Hm tm kim mt nt cp cao nht. name tn nt defaultValue Cc gi tr c tr v nu cc nt tp tin l khng tm thy Hm l mt s chng cht n gin GetFileNodeByName v ReadInt. cvReadRawData c s lng nhiu. void cvReadRawData( const CvFileStorage* fs, const CvFileNode* src, void* dst, const char* dt); fs tp tin lu tr src nt tp tin (mt chui) c s t dst con tr n mng ch dt c im k thut ca mi phn t mng. N c nh dng tng t nh trong WriteRawData. Hm c cc yu t t mt nt tp tin i din cho mt chui v hng. cvReadRawDataSlice Khi u c file dy nt. void cvReadRawDataSlice( const CvFileStorage* fs, CvSeqReader* reader, int count, void* dst, const char* dt ); fs tp tin lu tr reader trnh t c. Khi to n StartReadRawData. count s lng cc yu t c dst con tr n mng ch dt c im k thut ca mi phn t mng. N c nh dng tng t nh trong WriteRawData. Hm c mt hoc nhiu yu t t nt tp tin, i din cho mt chui, vi mt ngi dng ch nh mng. Tng s cc yu t trnh t c l mt sn phm ca total v s lng cc thnh phn trong mi phn t mng. V d, nu dt = 2if, Hm s c total 3 yu t chui. Nh vi bt k chui, mt s phn ca chui nt tp tin c th b b qua hoc c nhiu ln bng cch ti nh v cho ngi c s dng SetSeqReaderPos. cvReadReal Ly mt gi tr ni-im t mt nt tp tin. double cvReadReal( const CvFileNode* node, double defaultValue=0. ); 104

node file nt defaultValue Cc gi tr c tr v nu node l NULL Hm tr v mt gi tr ni im c i din bi cc nt tp tin. Nu nt tp tin NULL, defaultValue c tr li (do , n l thun tin gi Hm ngay sau khi GetFileNode m khng cn kim tra mt con tr NULL). Nu nt tp tin c kiu CV_NODE_REAL, sau node->data.f c tr v. Nu nt tp tin c kiu CV_NODE_INT, sau node->data.f l chuyn i du chm ng v quay tr li. Nu khng kt qu l khng xc nh. cvReadRealByName Tm mt nt tp tin v tr v gi tr ca n. double cvReadRealByName( const CvFileStorage* fs, const CvFileNode* map, const char* name, double defaultValue=0.); fs tp tin lu tr map Bn cha m. Nu n l NULL, Hm tm kim mt nt cp cao nht. name tn nt defaultValue Cc gi tr c tr v nu cc nt tp tin l khng tm thy Hm l mt s chng cht n gin GetFileNodeByName v ReadReal. cvReadString Ly mt chui vn bn t mt nt tp tin. const char* cvReadString( const CvFileNode* node, const char* defaultValue=NULL ); node file nt defaultValue Cc gi tr c tr v nu node l NULL Hm tr v mt chui vn bn c i din bi cc nt tp tin. Nu nt tp tin NULL, defaultValue c tr li (do , n l thun tin gi Hm ngay sau khi GetFileNode m khng cn kim tra mt con tr NULL). Nu nt tp tin c kiu CV_NODE_STR, sau node->data.str.ptr c tr v. Nu khng kt qu l khng xc nh. cvReadStringByName Tm mt nt tp tin theo tn ca n v tr v gi tr ca n. const char* cvReadStringByName( const CvFileStorage* fs, const CvFileNode* map, const char* name, const char* defaultValue=NULL ); fs tp tin lu tr map Bn cha m. Nu n l NULL, Hm tm kim mt nt cp cao nht. name tn nt defaultValue Cc gi tr c tr v nu cc nt tp tin l khng tm thy Hm l mt s chng cht n gin GetFileNodeByName v ReadString. cvRegisterType 105

ng k mt loi mi. void cvRegisterType(const CvTypeInfo* info); info Loi thng tin cu trc Hm ng k mt loi mi, c m t bi info. Hm to ra mt bn sao ca cu trc, v vy ngi dng nn xa n sau khi gi Hm. cvRelease Pht hnh mt i tng. void cvRelease( void** structPtr ); structPtr i con tr n i tng Hm tm thy cc loi ca mt i tng nht nh v cc cuc gi pht hnh vi hai con tr. cvReleaseFileStorage Phin bn lu tr tp tin. void cvReleaseFileStorage(CvFileStorage** fs); fs i con tr lu tr tp tin pht hnh Hm ng ca cc tp tin lin quan n lu tr v pht hnh tt c cc tm thi cu tures. N phi c gi sau khi tt c cc hot ng I / O vi lu tr kt thc. cvSave Lu mt i tng vo mt tp tin. void cvSave( const char* filename, const void* structPtr, const char* name=NULL, const char* comment=NULL, CvAttrList attributes=cvAttrList()); filename tn tp tin structPtr i tng tit kim name tn i tng ty chn. Nu n l NULL, tn s c hnh thnh t filename comment ty chn a vo u ca tp tin attributes thuc tnh ty chn c thng qua Write Hm tit kim mt i tng vo mt tp tin. N cung cp mt giao din n gin vit. cvStartNextStream Bt u dng tip theo. void cvStartNextStream(CvFileStorage* fs); fs tp tin lu tr Hm bt u cc dng tip theo trong tp tin lu tr. C hai YAML v nhiu h tr XML "streams". iu ny rt hu ch cho concatenating cc tp tin hoc khi phc cc qu trnh vit. cvStartReadRawData Khi to cc nt c tp tin trnh t. void cvStartReadRawData( const CvFileStorage* fs, const CvFileNode* src, 106

CvSeqReader* reader); fs tp tin lu tr src nt tp tin (mt chui) c s t reader Pointer ngi c trnh t Hm khi to cho ngi c trnh t c d liu t mt nt tp tin. c khi to c th sau c truyn n ReadRawDataSlice. cvStartWriteStruct Bt u vit mt cu trc mi. void cvStartWriteStruct( CvFileStorage* fs, const char* name, int struct flags, const char* typeName=NULL, CvAttrList attributes=cvAttrList() ); fs tp tin lu tr name tn ca cu trc bng vn bn. Cu trc ny c th c truy cp bi tn ny khi lu tr c c. struct_flags mt s kt hp ca cc gi tr sau: CV_NODE_SEQ cu trc bng vn bn l mt chui (xem tho lun ca CvFileStorage), l, cc yu t ca n khng c mt tn. CV_NODE_MAP cu trc vn bn mt bn (xem tho lun ca CvFileStorage), c ngha l, tt c cc phn t ca n c tn. Mt v ch mt trong hai l c trn phi c xc nh CV_NODE_FLOW c ty chn c ngha cho cc dng YAML. N c ngha l cu trc c vit nh l mt dng chy (khng phi l mt khi), m l nh gn hn. l khuyn co s dng l c ny cho cc cu trc hoc cc mng m cc thnh phn ca tt c cc v hng. typeName tham s ty chn - tn loi i tng. Trong trng hp ca XML, n c vit nh l mt type_id thuc tnh ca th m cu trc. Trong trng hp ca YAML, n c vit sau khi sau du hai chm tn cu trc (xem v d trong bn m t CvFileStorage). Ch yu l n c s dng vi ngi s dng i tng. Khi lu tr c c, tn loi m ha c s dng xc nh loi i tng (xem CvTypeInfo v FindTypeInfo). attributes tham s ny khng c s dng trong vic thc hin hin ti Hm bt u vit mt cu trc hp cht (thu) c th l mt chui hoc mt bn . Sau khi tt c cc lnh vc c cu, c th l v hng hoc cu trc, c vit, EndWriteStruct nn c gi l. Hm ny c th c s dng nhm mt s i tng hoc thc hin cc write Hm cho mt s i tng ngi dng (xem CvTypeInfo). cvTypeOf Tr v loi ca mt i tng. CvTypeInfo* cvTypeOf( const void* structPtr ); structPtr Cc con tr i tng Hm tm thy cc loi ca mt i tng nht nh. N lp qua danh sch cc loi ng k v gi l is_instance Hm/phng php cho mi cu trc thng tin loi vi i tng cho n khi mt trong nhng h khng tr v s khng hoc cho n khi ton b danh sch c i qua. Trong trng hp th hai, Hm tr v NULL. 107

cvUnregisterType Unregisters loi. void cvUnregisterType( const char* typeName ); typeName Tn ca mt loi ng k Hm unregisters mt loi vi mt tn c ch nh. Nu tn cha c bit, n c th xc nh v tr cc thng tin loi bi mt th hin ca cc loi bng cch s dng typeof hoc bng cch lp li cc danh sch loi, bt u t FirstType, v sau gi cvUnregisterType(info>typeName). cvWrite Vit mt i tng ngi dng. void cvWrite( CvFileStorage* fs, const char* name, const void* ptr, CvAttrList attributes=cvAttrList() ); fs tp tin lu tr name tn ca i tng bng vn bn. Nu c NULL nu v ch nu cu trc m l mt chui. ptr con tr n i tng attributes Cc thuc tnh ca i tng. H l nhng c th i vi tng loi c th (xem dicsussion di y). Hm vit mt i tng lu tr tp tin. Th nht, thng tin loi thch hp c tm thy bng cch s dng Typeof. Sau , phng php write lin quan n thng tin loi c gi l. Cc thuc tnh c s dng ty chnh cc th tc bng vn bn. Cc loi tiu chun h tr cc cc thuc tnh (tt c cc thuc tnh *dt nh dng ging nh trong WriteRawData): 1. CvSeq header_dt m t ca cc lnh vc ngi s dng ca tiu trnh t theo CvSeq, hoc CvChain (Nu trnh t l mt chui Freeman) hoc CvContour (nu trnh t l mt ng vin hoc im trnh t) dt m t cc yu t trnh t. recursive nu thuc tnh l hin ti v khng phi l bng "0" hoc "false", c cy trnh t (ng vin) c lu tr. 2. Cvgraph header_dt m t tiu ca cc lnh vc ngi s dng tiu th sau CvGraph; vertex_dt m t ca cc lnh vc ngi s dng cc nh th edge_dt m t ca cc lnh vc ngi s dng ca cc cnh th (lu rng trng lng cnh lun lun l bng vn bn, v vy khng cn phi xc nh n mt cch r rng) Di y l on code m to ra cc file YAML c hin th trong phn m t CvFileStorage: #include "cxcore.h" int main( int argc, char** argv ) { CvMat* mat = cvCreateMat( 3, 3, CV\_32F ); CvFileStorage* fs = cvOpenFileStorage( "example.yml", 0, CV\_STORAGE\_WRITE 108

); cvSetIdentity( mat ); cvWrite( fs, "A", mat, cvAttrList(0,0) ); cvReleaseFileStorage( &fs ); cvReleaseMat( &mat ); return 0; } cvWriteComment Vit mt li. void cvWriteComment( CvFileStorage* fs, const char* comment, int eolComment); fs tp tin lu tr comment Cc nhn xt bng vn bn, mt dng hoc nhiu dng eolComment Nu khng phi s khng, Hm s c gng a cc bnh lun cui dng hin ti. Nu c l s khng, nu nhn xt l nhiu dng, hoc nu n khng ph hp phn cui ca dng hin ti, bnh lun bt u mt dng mi. Hm vit mt bnh lun vo lu tr tp tin. Cc kin c b qua khi lu tr c c, h c th ch c s dng g li hoc cc mc ch m t. cvWriteFileNode Vit mt nt tp tin lu tr tp tin khc. void cvWriteFileNode( CvFileStorage* fs, const char* new node name, const CvFileNode* node, int embed ); fs im n tp tin lu tr new_file_node tn tp tin mi nt mi ca cc nt tp tin trong vic lu tr tp tin ch. gi cho hin ti tn, s dng cvGetFileNodeName node Nt bng vn bn embed Nu nt bng vn bn l mt b su tp v tham s ny l khng khng, khng c thm cc hiararchy c to ra. Thay vo , tt c cc yu t ca nt c vit vo cc cu trc hin bng vn bn. Tt nhin, cc yu t bn c th c ghi vo bn , v c th c cc yu t ca chui vit ch mt chui. Hm vit mt bn sao ca mt nt tp tin lu tr tp tin. Cc ng dng c th Hm sp nhp cc kho tp tin nhau thnh mt v chuyn i gia cc nh dng XML v YAML. cvWriteInt Vit mt gi tr s nguyn. void cvWriteInt( CvFileStorage* fs, const char* name, int value); fs tp tin lu tr 109

name tn ca gi tr bng vn bn. Nu c NULL nu v ch nu cu trc m l mt chui. value gi tr bng vn bn Hm vit mt gi tr s nguyn duy nht (c hoc khng c mt tn) lu tr tp tin. cvWriteRawData Ghi s lng nhiu. void cvWriteRawData( CvFileStorage* fs, const void* src, int len, const char* dt ); fs tp tin lu tr src con tr mng bng vn bn len S ca cc phn t mng vit dt c im k thut ca mi phn t mng c nh dng sau ([count]fu|c|w|s|i|f|dg)... ni m cc nhn vt tng ng vi loi C c bn: u 8-bit unsigned s c 8-bit k s w 16-bit unsigned s s 16-bit k s i 32-bit k s f n chnh xc ni im s d tng gp i chnh xc ni im s r con tr, thp hn 32 bit trong c vit nh mt s nguyn k kt. Loi c th c s dng cu trc lu tr vi cc lin kt gia cc yu t. tnh l truy cp ty chn ca val UE ca mt loi nht nh. V d, 2if c ngha l mi phn t mng l mt cu trc ca 2 s nguyn, theo sau l mt s du chm ng chnh xc n. Tng ng vi k hiu cc c im k thut trn IIF ', '2 i1f v vv. Cc v d khc: u c ngha l mng bao gm cc byte, v 2d c ngha l mng bao gm cc cp i. Hm vit mt mng, m cc thnh phn bao gm cc con s mt hoc nhiu. -Function cuc gi c th c thay th bng mt vng lp c cha mt vi WriteInt v cc cuc gi WriteReal, nhng mt gi l hiu qu hn. Lu rng bi v khng ai trong s cc yu t c mt ci tn, h cn phi c vit mt chui ch khng phi l mt bn . cvWriteReal Vit mt gi tr du chm ng. void cvWriteReal( CvFileStorage* fs, const char* name, double value ); fs tp tin lu tr name tn ca gi tr bng vn bn. Nu c NULL nu v ch nu cu trc m l mt chui. value gi tr bng vn bn Hm vit mt gi tr ni-im duy nht (c hoc khng c mt tn) lu tr tp tin. c bit gi tr c m ha nh sau: NaN (Not A Number) l NaN, v cng nh + Inf (-. Inf)... V d sau y cho thy lm th no s dng Hm vit mc thp lu tr ty chnh cu trc, chng hn nh tiu ch chm dt, m khng cn ng k mt loi mi. 110

void write_termcriteria( CvFileStorage* fs, const char* struct_name, CvTermCriteria* termcrit ) { cvStartWriteStruct( fs, struct_name, CV_NODE_MAP, NULL, cvAttrList(0,0)); cvWriteComment( fs, "termination criteria", 1 ); // just a description if( termcrit->type & CV_TERMCRIT_ITER ) cvWriteInteger( fs, "max_iterations", termcrit->max_iter ); if( termcrit->type & CV_TERMCRIT_EPS ) cvWriteReal( fs, "accuracy", termcrit->epsilon ); cvEndWriteStruct( fs ); } cvWriteString Vit mt chui vn bn. void cvWriteString( CvFileStorage* fs, const char* name, const char* str, int quote=0 ); fs tp tin lu tr name tn ca chui bng vn bn. Nu c NULL nu v ch nu cu trc m l mt chui. str Cc chui vn bn quote Nu khng, cc chui vn bn c t trong du ngoc kp, bt k cho d h c yu cu. Nu khng, nu l c l s khng, bo gi c s dng ch khi h c yu cu (v d nh khi chui bt u bng mt ch s hoc cha khong trng). Hm vit mt chui vn bn lu tr tp tin. 1.6 Phn nhm v Tm kim trong khng gian a chiu cvKMeans2 Chia tch tp cc vect bi mt s lng nht nh cc cm. int cvKMeans2(const CvArr* samples, int nclusters, CvArr* labels, CvTermCriteria termcrit, int attempts=1, CvRNG* rng=0, int flags=0, CvArr* centers=0, double* compactness=0); samples ni ma trn im ca mu u vo, mt hng cho mi mu nclusters S cm phn chia cc thit lp bi labels u ra vector s nguyn ch s lu tr cluster cho tt c cc mu termcrit Ch nh s lng ti a ca lp i lp li v / hoc tnh chnh xc (khong cch trung tm c th di chuyn bng gia lp i lp li sau ny) attempts c gng bao nhiu ln cc thut ton c thc hin bng cch s dng labelings khc nhau ban u. Cc thut ton- tr v rithm nhn mang li nh gn tt nht (xem cc thng s Hm cui cng) rng Ty chn ngu nhin s my pht in bn ngoi, c th c s dng hon ton kim sot c Hm haviour 111

flags c th l 0 hoc CV_KMEANS_USE_INITIAL_LABELS. Cc gi tr ny c ngha l trong n lc u tin (v c th l duy nht), Hm s dng ngi dng cung cp nhn nh ban u xp x thay v to ra cc nhn ngu nhin. i vi nhng n lc th hai v tip tc, Hm s s dng cc nhn c to ra ngu nhin trong bt k trng hp no centers ty chn mng u ra ca cc trung tm cm compactness tham s u ra ty chn, c tnh nh i || samplesi - centerslabelsi ||2 sau khi mi n lc, gi tr tt nht (ti thiu) c chn v cc nhn tng ng c ti quay bng Hm. V c bn, ngi s dng ch c th s dng ct li ca Hm, thit lp s lng cc n lc n 1, khi to nhn mi thi gian s dng mt thut ton ty chnh (flags=CV_KMEAN_USE_INITIAL_L) v, da trn cht u ra hoc bt k tiu chun khc, chn phn nhm tt nht. CvKMeans2 Hm thc hin mt thut ton k-c ngha l tm thy cc trung tm ca nclusters cm, nhm cc mu u vo xung quanh cc cm. u ra, labels(i) cha mt cm ch s cho cc mu c lu tr hng th i ca samples ma trn. #include "cxcore.h" #include "highgui.h" void main( int argc, char** argv ) { #define MAX_CLUSTERS 5 CvScalar color_tab[MAX_CLUSTERS]; IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 ); CvRNG rng = cvRNG(0xffffffff); color_tab[0] = CV_RGB(255,0,0); color_tab[1] = CV_RGB(0,255,0); color_tab[2] = CV_RGB(100,100,255); color_tab[3] = CV_RGB(255,0,255); color_tab[4] = CV_RGB(255,255,0); cvNamedWindow( "clusters", 1 );for(;;) { int k, cluster_count = cvRandInt(&rng)%MAX_CLUSTERS + 1; int i, sample_count = cvRandInt(&rng)%1000 + 1; CvMat* points = cvCreateMat( sample_count, 1, CV_32FC2 ); CvMat* clusters = cvCreateMat( sample_count, 1, CV_32SC1 ); /* generate random sample from multigaussian distribution */ for( k = 0; k < cluster_count; k++ ) { CvPoint center; CvMat point_chunk; center.x = cvRandInt(&rng)%img->width; center.y = cvRandInt(&rng)%img->height; cvGetRows( points, &point_chunk, k*sample_count/cluster_count, (k == (cluster_count - 1)) ? sample_count : (k+1)*sample_count/cluster_count ); 112

cvRandArr( &rng, &point_chunk, CV_RAND_NORMAL, cvScalar(center.x,center.y,0,0), cvScalar(img->width/6, img->height/6,0,0) ); } /* shuffle samples */ for( i = 0; i < sample_count/2; i++ ) { CvPoint2D32f* pt1 = (CvPoint2D32f*)points->data.fl + cvRandInt(&rng)%sample_count; CvPoint2D32f* pt2 = (CvPoint2D32f*)points->data.fl + cvRandInt(&rng)%sample_count; CvPoint2D32f temp; CV_SWAP( *pt1, *pt2, temp ); } cvKMeans2( points, cluster_count, clusters, cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0 )); cvZero( img ); for( i = 0; i < sample_count; i++ ) { CvPoint2D32f pt = ((CvPoint2D32f*)points->data.fl)[i]; int cluster_idx = clusters->data.i[i];cvCircle( img, cvPointFrom32f(pt), 2, color_tab[cluster_idx], CV_FILLED ); } cvReleaseMat( &points ); cvReleaseMat( &clusters ); cvShowImage( "clusters", img ); int key = cvWaitKey(0); if( key == 27 ) break; } }

Chng 2: cv. X l hnh nh v th gic my tnh


2.1 Lc hnh nh Hm v cc lp hc c m t trong phn ny c s dng thc hin khc hau, tuyn tnh hoc phi tuyn tnh lc cc hot ng trn hnh nh 2D (i din l cv), c ngha l, i vi mi v tr pixel (x, y) trong ngun hnh nh mt s khu vc (thng l hnh ch nht) c xem xt v c s dng tnh ton cc phn ng. Trong trng hp ca mt b lc tuyn 113

tnh, n l mt tng trng s ca cc gi tr pixel, trong trng hp ca hnh thi hc hot ng l ... ti thiu hoc ti a phn ng tnh ton c lu tr n ch hnh nh cng mt v tr (x, y). N c ngha l, rng hnh nh u ra s c kch thc tng t nh hnh nh nhp vo. Thng thng, Hm h tr a knh mng, trong trng hp ny mi knh x l c lp, do hnh nh u ra cng s c cng s lng cc knh nh l mt u vo. Mt tnh nng ph bin ca cc Hm v cc lp hc c m t trong phn ny l, khng ging nh Hm s hc n gin, h cn phi ngoi suy cc gi tr ca mt s im nh khng tn ti. i vi c V d, nu chng ta mun lm mn hnh nh bng cch s dng mt Gaussian 3 3 b lc, sau trong lc x l cc im nh bn tri trong mi hng, chng ti cn im nh bn tri ca h, tc l bn ngoi ca hnh nh. Chng ti c th cho php nhng im nh tng t nh cc im nh hnh nh tri (tc l s dng "nhn rng bin gii" extrapolation phng php), hoc gi nh rng tt c cc im nh khng tn ti l s khng ("contant bin gii" ngoi suy phng php). IplConvKernel Mt IplConvKernel l mt ht nhn chp hnh ch nht, c to ra bi Hm CreateStructuringElementEx. cvCopyMakeBorder Bn sao mt hnh nh v lm cho mt bin gii xung quanh n. void cvCopyMakeBorder( const CvArr* src, CvArr* dst, CvPoint offset, int bordertype, CvScalar value=cvScalarAll(0) ); src Cc hnh nh ngun dst Cc hnh nh im n offset Ta ca gc trn bn tri (hoc pha di bn tri trong trng hp ca hnh nh pha di bn tri xut x) ca hnh ch nht hnh nh im n, ni ngun hnh nh (ROI) c sao chp. Kch thc ca rectanlge ph hp vi ngun hnh nh kch thc / ROI kch thc bordertype Loi bin gii to ra xung quanh hnh ch nht hnh nh sao chp m ngun, cc loi trong : IPL_BORDER_CONSTANT bin gii c lm y vi cc gi tr c nh, c thng qua nh mt tham s cui cng ca Hm. IPL_BORDER_REPLICATE cc im nh t cc hng trn v di, bn tri nht v phi hu ht cc ct c nhn rng in vo cc bin gii. (Hai loi khu khc t IPL, IPL_BORDER_REFLECT v IPL_BORDER_WRAP, hin khng c h tr) valueGi tr ca cc im nh bin gii nu bordertype l IPL_BORDER_CONSTANT 114

Hm sao chp cc mng ngun 2D thnh phn bn trong ca cc mng ch v lm cho mt bin gii ca cc loi quy nh xung quanh khu vc sao chp. Hm ny rt hu dng khi mt trong nhng nhu cu thi ua loi bin gii l khc nhau t mt trong nhng nhng vo mt thut ton c th implementation. V d, hnh thi Hm, cng nh hu ht cc Hm lc khc trong OpenCV, ni b s dng cc loi bin gii nhn rng, trong khi ngi s dng c th cn mt bin gii khng, bin gii, trn y 1 hoc 255. cvCreateStructuringElementEx To mt yu t cu trc. IplConvKernel* cvCreateStructuringElementEx( int cols, int rows, int anchorX, int anchorY, int shape, int* values=NULL ); cols S ct trong cc yu t cu trc rows S hng trong cc yu t cu trc anchorX tng i ngang b p ca cc im neo anchorY thng ng tng i b p ca cc im neo shape Hnh dng ca cc yu t cu trc, c th c gi tr sau: CV_SHAPE_RECT mt yu t hnh ch nht CV_SHAPE_CROSS mt yu t hnh ch thp CV_SHAPE_ELLIPSE mt yu t elip CV_SHAPE_CUSTOM mt yu t ngi dng nh ngha. Trong trng hp ny cc values tham s quy nh c th mt n, c ngha l, nhng ngi hng xm ca im nh phi c xem xt values Con tr tr ti cc yu t cu trc d liu, mt mng my bay, i din cho hng hng qut nguyn t ca ma trn. Cc gi tr khc khng cho thy im thuc v phn t. Nu con tr NULL, sau tt c cc gi tr c coi l khng-khng, c ngha l phn t ca mt hnh ch nht. Tham s ny c coi l ch khi hnh l CV_SHAPE_CUSTOM Hm CreateStructuringElementEx phn b v lp y IplConvKernel cu trc, m c th c s dng nh mt yu t c cu trong hot ng hnh thi. cvDilate Lm gin mt hnh nh bng cch s dng mt yu t cu trc c th. void cvDilate( const CvArr* src, CvArr* dst, IplConvKernel* element=NULL, int iterations=1 ); 115

src Ngun hnh nh dst im n hnh nh element phn t C cu s dng cho s gin n. Nu n l NULL, mt 3 3 hnh ch nht c cu thnh phn c s dng iterations S ln ca thi gian gin n c p dng Hm lm gin cc ngun hnh nh bng cch s dng cc yu t cu trc quy nh c th xc nh hnh dng ca mt khu ph im nh ti a c ly:

Hm ny h tr ch ti ch. S gin n c th c p dng nhiu (lp li) ln. i vi hnh nh mu sc, mi knh c x l c lp. cvErode Lm xi mn hnh nh bng cch s dng mt yu t cu trc c th. void cvErode( const CvArr* src, CvArr* dst, IplConvKernel* element=NULL, int iterations=1); src Ngun hnh nh dst im n hnh nh element C cu thnh phn yu t c s dng xi mn. Nu n l NULL, mt 3 3 hnh ch nht c cu thnh phn c s dng iterations s ln xi mn Hm lm xi mn ngun hnh nh bng cch s dng cc yu t cu trc quy nh c th xc nh hnh dng ca mt khu ph pixel m ti thiu l:

Hm ny h tr ch ti ch. Xi mn c th c p dng nhiu (lp li) ln. i vi hnh nh mu sc, mi knh c x l c lp. cvFilter2D Convolves mt hnh nh vi ht nhn. void cvFilter2D( const CvArr* src, CvArr* dst, const CvMat* kernel, CvPoint anchor=cvPoint(-1,-1)); src Cc hnh nh ngun dst Cc hnh nh im n 116

kernel Convolution ht nhn , mt ma trn im mt knh ni. Nu bn mun p dng khc nhau ht nhn n cc knh khc nhau, phn chia cc hnh nh vo my bay mu sc ring bit bng cch s dng cvSplit x l c nhn anchor neo ca ht nhn cho bit v tr tng i ca mt im lc trong ht nhn. Neo shoud nm trong ht nhn. Gi tr mc nh c bit (-1, -1) c ngha l n l trung tm ht nhn Hm p dng mt b lc tuyn tnh ty hnh nh. Hot ng ti ch c h tr. Khi khu l mt phn bn ngoi hnh nh, Hm ni suy cc gi tr im nh outlier t cc pixel gn nht nm bn trong hnh nh. cvLaplace Tnh Laplacian ca mt hnh nh. void cvLaplace( const CvArr* src, CvArr* dst, int apertureSize=3); src Ngun hnh nh dst im n hnh nh apertureSize kch thc m ng knh (n c ngha tng t nh cvSobel) Hm tnh ton Laplacian ca ngun hnh nh bng cch thm vo th hai x v y cc cht dn xut tnh bng cch s dng cc ton t Sobel:

Thit apertureSize = 1 cung cp cho cc phin bn nhanh nht l bng convolving cc hnh nh vi ht nhn sau y: 01 0 1 -4 1 10 1 Tng t nh Hm cvSobel, m rng quy m khng c thc hin v kt hp cng mt u vo v nh dng u ra c h tr. cvMorphologyEx Thc hin chuyn i hnh thi hc tin tin. void cvMorphologyEx( const CvArr* src, CvArr* dst, CvArr* temp, IplConvKernel* element, int operation, int iterations=1 ); src Ngun hnh nh dst im n hnh nh 117

temp thi tm thi hnh nh, cn thit trong mt s trng hp element C cu thnh phn phn t operation Loi hnh thi hot ng, mt trong nhng iu sau y: CV_MOP_OPEN m CV_MOP_CLOSE ng ca CV_MOP_GRADIENT hnh thi CV_MOP_TOPHAT "u m" CV_MOP_BLACKHAT "m en" iterations s thi gian xi mn v s gin n c p dng Hm c th thc hin chuyn i hnh thi hc tin tin bng cch s dng xi mn v s gin n l hot ng c bn. M ca: dst = open(src, element) = dilate(erode(src, element), element) Kt thc: dst = close(src, element) = erode(dilate(src, element), element) Hnh thi dc: dst = morph grad(src, element) = dilate(src, element) erode(src, element) "M": dst = tophat(src, element) = src open(src, element) "M en": dst = blackhat(src, element) = close(src, element) src Tm thi hnh nh tm thi l cn thit cho mt gradient hnh thi hc, v trong trng hp ti ch hot ng i vi "u m" v "m en". cvPyrDown Downsamples mt hnh nh. void cvPyrDown( const CvArr* src, CvArr* dst, int filter=CV GAUSSIAN 5x5 ); src Cc hnh nh ngun dst hnh nh im n, cn phi c mt na chiu rng v chiu cao ln hn so vi ngun filter Loi ca b lc c s dng cho chp, ch CV_GAUSSIAN 5x5 hin ang c h tr. Hm thc hin cc bc downsampling ca phn hy kim t thp Gaussian. u tin n convolves ngun hnh nh vi b lc c ch nh v sau downsamples hnh nh bng cch t b thm ch c hng v ct. cvReleaseStructuringElement Xo mt yu t cu trc. void cvReleaseStructuringElement( IplConvKernel** element ); 118

element con tr n cc yu t cu trc b xa Hm pht hnh IplConvKernel cu trc m khng cn cn thit. Nu *element l NULL, Hm khng c tc dng. cvSmooth Lm mm hnh nh trong mt trong nhiu cch. void cvSmooth( const CvArr* src, CvArr* dst, int smoothtype=CV GAUSSIAN, int param1=3, int param2=0, double param3=0, double param4=0); src Cc hnh nh ngun dst Cc hnh nh im n smoothtype Loi lm mn: CV_BLUR_NO_SCALE tuyn tnh convolution vi param1 param2 hp ht nhn (tt c 1). Nu bn mun lm mn im nh khc nhau vi cc ht nhn hp kch thc khc nhau, bn c th s dng tch phn hnh nh c tnh ton bng cch s dng cvIntegral CV_BLUR tuyn tnh convolution vi param1 param2 hp ht nhn (tt c 1) vi tip theo m rng quy m bng 1 / (param1 param2) CV_GAUSSIAN tuyn tnh convolution vi param1 param2 Gaussian ht nhn CV_MEDIAN trung bnh lc vi mt param1 param1 vung khu CV_BILATERAL phng song phng b lc vi mt param1 param1 vung khu , mu sc sigma = param3 v khng gian sigma = param4. Nu param1 = 0, bn khu vung c thit lp cvRound (param4 * 1.5) * 2 +1. Thng tin v vic lc song phng c th c tm thy ti http://www.dai.ed.ac.uk/CVonline/LOCAL_COPIES/MANDUCHI1/Bilateral_Filtering.html param1 tham s u tin ca hot ng lm mn, chiu rng khu . Phi tch cc s l (1, 3, 5, ...) param2 tham s th hai ca hot ng lm mn, chiu cao khu . B qua CV_MEDIAN v CV_BILATERAL. Trong trng hp n gin quy m / khng quy m v Gaussian blur nu param2 l s khng, n c thit lp param1. Nu khng, n phi l mt l tch cc s. param3 Trong trng hp ca mt tham s Gaussian tham s ny c th ch nh Gaussian (tiu chun lch). Nu n l s khng, n c tnh ton t cc kch thc ht nhn: param1 cho ht nhn ngang

119

S dng tiu chun sigma ht nhn nh (3 3-7 7) cho tc tt hn. Nu param3 l khng bng khng, trong khi param1 param2 l s khng, kch thc ht nhn c tnh t sigma ( cung cp chnh xc hot ng ). Hm lm mm mt hnh nh bng cch s dng mt trong mt s phng php. Mi ca cc phng php c mt s tnh nng v hn ch c lit k di y Blur vi quy m khng hot ng vi mt knh hnh nh v h tr tch ly ca bit-8 nh dng 16-bit (tng t cvSobel v cvLaplace) v 32-bit du chm ng 32-bit floating-point nh dng. M n gin v h tr Gaussian blur 1 hoc 3-knh, 8-bit v hnh nh 32-bit du chm ng. Hai phng php c th x l hnh nh ti ch. B lc trung bnh v song phng lm vic vi 1 hoc 3-knh hnh nh 8-bit v khng th x l hnh nh ti ch. cvSobel Tnh ton u tin th hai, cc dn xut hnh nh th ba hoc hn hp bng cch s dng mt ton t Sobel m rng. void cvSobel( const CvArr* src, CvArr* dst, int xorder, int yorder, int apertureSize=3 ); src Ngun hnh nh ca loi CvArr dst im n hnh nh xorder th t ca x phm phi sinh yorder Trnh t cc y phi sinh apertureSize Kch thc ca ht nhn Sobel m rng, phi l 1, 3, 5 hoc 7 Trong mi trng hp, ngoi tr 1, mt ht nhn apertureSize apertureSize tch s c s dng tnh o hm. i vi apertureSize = 1 a 3 1 hoc 1 x 3 mt ht nhn c s dng (Gaussian lm mn khng c thc hin). Ngoi ra cn c gi tr c bit CV_SCHARR (-1) tng ng vi mt 3 3. Scharr b lc c th cho kt qu chnh xc hn 3 3 Sobel. Scharr khu -3 0 3 -10 0 10 -3 0 3 cho x-phi sinh, hon cho y-phi sinh. Hm tnh ton cc dn xut hnh nh bng convolving hnh nh thch hp ht nhn:

120

Cc nh khai thc Sobel kt hp Gaussian lm mn v s khc bit v vy kt qu l hn hoc t hn kh nng chng ting n. Thng thng, Hm c gi l (xorder = 1, yorder = 0, apertureSize = 3) hoc (xorder = 0, yorder = 1, apertureSize = 3) tnh ton u tin x hoc y-hnh nh phi sinh. Trng hp u tin tng ng vi mt ht nhn: -1 0 1 -2 0 2 -1 0 1 v th hai tng ng vi mt ht nhn: -1 -2 -1 0 0 0 1 2 1 hoc mt ht nhn: 1 2 1 0 0 0 -1 2 -1 ty thuc vo ngun gc hnh nh (ngun gc lnh vc cu trc IplImage). M rng quy m khng c thc hin, do hnh nh im n thng c s lng ln (gi tr tuyt i) so vi ngun hnh nh. trnh trn, Hm i hi mt hnh nh im n 16-bit nu ngun hnh nh l 8-bit. Kt qu c th c chuyn i tr li-8 bit bng cch s dng cvConvertScale hoc cc cvConvertScaleAbs Hm. Bn cnh hnh nh 8-bit Hm c th x l hnh nh 32bit floating-point. C hai ngun v ch n phi l mt knh hnh nh c kch thc bng nhau hoc kch c ROI bng. 2.2 nh hnh hc bin i Cc Hm trong phn ny thc hin chuyn i hnh hc ca hnh nh 2D. l, h khng thay i ni dung hnh nh, nhng bin dng li im nh, v bn li ny b bin dng. im n hnh nh. Trong thc t, trnh cc hin vt ly mu, lp bn c thc hin theo th t ngc li, t im n ngun. l, mi im nh (x, y) ca hnh nh ch, Hm tnh ton ta ca im nh tng ng "nh ti tr" trong hnh nh ngun v sao chp cc im nh gi tr, l: dst (x, y) = src (fx (x, y), fy (x, y)) Trong trng hp khi ngi dng xc nh lp bn v pha trc : , cc OpenCV Hm u tin tnh ton lp bn tng ng vi nghch o: v sau s dng cng thc trn. Thc t trin khai ca bin i hnh hc, t cvRemap chung nht v cvResize n gin nht v nhanh nht, cn phi gii quyt 2 vn chnh vi cc bn trn cng thc: 1. ngoi suy ca cc im nh khng tn ti. Tng t nh Hm lc, c m t trong vious phn, i vi mt s (x, y) fx (x, y) hoc fy (x, y), hoc c hai, c th ri bn ngoi hnh nh, trong trng hp mt s phng php ngoi suy cn phi c s dng. OpenCV cung cp 121

vic la chn cng mt trong nhng phng php ngoi suy nh trong cc Hm lc, nhng cng l mt qung co ditional phng php BORDER_TRANSPARENT, c ngha l cc im nh tng ng trong hnh nh im n s khng c sa i tt c. 2. suy ca gi tr pixel. Thng thng fx (x, y) v fy (x, y) l s im ni (tc l fx, fy c th l mt chuyn i afin hoc quan im, hay bin dng ng knh iu chnh b tr hnh trn vv), do , mt gi tr pixel ti ta phn on cn phi c ly ra. Trong trng hp n gin cc ta c th c lm trn n ta s nguyn gn nht v tng ng cc im nh c s dng, c gi l gn nht, hng xm ni suy. Tuy nhin, mt kt qu tt hn c th th t c bng cch s dng phng php ni suy phc tp hn, ni m mt hm a thc l ph hp vi mt s vng ln cn ca im nh tnh (fx (x, y), fy (x, y)) v sau gi tr ca a thc (fx (x, y), fy (x, y)) c thc hin nh l gi tr im nh ni suy. Trong OpenCV bn c th la chn gia nhiu phng php ni suy, xem cvResize. cvGetRotationMatrix2D Tnh ma trn afin quay 2d. CvMat* cv2DRotationMatrix( CvPoint2D32f center, double angle, double scale, CvMat* mapMatrix ); center Trung tm lun chuyn ngun hnh nh angle Cc gc quay cc mc . Gi tr tch cc c ngha l quay ngc chiu kim ng h (coor- ngun gc dinate c gi nh l cc gc trn cng, bn tri) scale ng hng quy m yu t mapMatrix Con tr tr ti cc im n 2 3 ma trn Cv2DRotationMatrix Hm tnh ton ma trn sau y:

Vi = scale . cos(angle), = scale . sin(angle) Vic chuyn i bn cc trung tm lun chuyn n chnh n. Nu y khng phi l mc ch, s thay i c iu chnh. cvGetAffineTransform Tnh affine bin i t 3 im tng ng. CvMat* cvGetAffineTransform( const CvPoint2D32f* src, const CvPoint2D32f* dst, CvMat* mapMatrix ); src Ta ca 3 nh tam gic trong hnh nh ngun 122

dst Ta ca 3 nh tam gic tng ng vi hnh nh im n mapMatrix Con tr tr ti cc im n 2 3 ma trn CvGetAffineTransform Hm tnh ton ma trn ca mt affine bin i nh vy m:

ni cvGetPerspectiveTransform Tnh quan im chuyn i t 4 im tng ng. CvMat* cvGetPerspectiveTransform( const CvPoint2D32f* src, const CvPoint2D32f* dst, CvMat* mapMatrix ); src Ta 4 nh t gic trong hnh nh ngun dst Ta 4 nh t gic tng ng vi hnh nh im n mapMatrix Con tr tr ti cc im n 3 3 ma trn CvGetPerspectiveTransform Hm tnh ton mt ma trn ca quan im bin i nh vy m:

ni

cvGetQuadrangleSubPix Ly t gic im nh t mt hnh nh vi chnh xc phn im nh. void cvGetQuadrangleSubPix( const CvArr* src, CvArr* dst, const CvMat* mapMatrix ); src Ngun hnh nh dst Trch t gic mapMatrix Vic chuyn i 2 x 3 ma trn [A | b] (xem cc cuc tho lun) CvGetQuadrangleSubPix Hm chit xut t cc im nh t src chnh xc sub-pixel v lu tr chng dst nh sau: vi

v 123

Cc gi tr ca pixel ti ta s nguyn khng c ly ra bng cch s dng ni suy song tuyn tnh. Khi Hm cn im nh bn ngoi ca hnh nh, n s dng ch bin gii nhn rng ti to li gi tr. Mi knh nhiu knh hnh nh c x l c lp. cvGetRectSubPix Ly hnh ch nht pixel t mt hnh nh vi chnh xc sub-pixel. void cvGetRectSubPix( const CvArr* src, CvArr* dst, CvPoint2D32f center ); src Ngun hnh nh dst Trch hnh ch nht center ni ta im ca trung tm hnh ch nht c chit xut trong hnh nh ngun. Cc trung tm phi c bn trong hnh nh CvGetRectSubPix Hm chit xut t cc im nh t src: ni m cc gi tr ca cc im nh ti ta s nguyn khng c ly ra bng cch s dng song tuyn tnh interpo lation. Mi knh nhiu knh hnh nh c x l c lp. Trong khi hnh ch nht trung tm phi c bn trong hnh nh, cc b phn ca hnh ch nht c th c bn ngoi. Trong trng hp ny, vic nhn rng ch bin gii c s dng c c cc gi tr im nh vt ra ngoi ranh gii hnh nh. cvLogPolar Remaps mt hnh nh ng nhp khng gian cc. void cvLogPolar( const CvArr* src, CvArr* dst, CvPoint2D32f center, double M, int flags=CV INTER LINEAR+CV WARP FILL OUTLIERS ); src Ngun hnh nh dst im n hnh nh center Trung tm chuyn i, ni m chnh xc u ra l ti a M Tm quan trng quy m tham s. Xem di y flags Mt s kt hp ca phng php ni suy v nhng l c ty chn sau y: CV_WARP_FILL_OUTLIERS lp y tt c cc im nh hnh nh im n. Nu mt s trong s h corre spond Bn ngoi trong cc ngun hnh nh, h thit lp khng CV_WARP_INVERSE_MAP Xem di y CvLogPolar Hm bin i hnh nh ngun bng cch s dng vic chuyn i sau y: 124

Chuyn tip chuyn i (CV_WARP_INVERSE_MAP khng c thit lp): Inverse chuyn i (CV_WARP_INVERSE_MAP c thit lp): ni Hm gi lp tm nhn ca con ngi "foveal" v c th c s dng cho quy m nhanh v quay bt bin mu ph hp, theo di i tng v vv. Cc Hm khng th hot ng ti ch. #include <cv.h> #include <highgui.h> int main(int argc, char** argv) { IplImage* src; if( argc == 2 && (src=cvLoadImage(argv[1],1) != 0 ) { IplImage* dst = cvCreateImage( cvSize(256,256), 8, 3 ); IplImage* src2 = cvCreateImage( cvGetSize(src), 8, 3 ); cvLogPolar( src, dst, cvPoint2D32f(src->width/2,src->height/2), 40, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS ); cvLogPolar( dst, src2, cvPoint2D32f(src->width/2,src->height/2), 40, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS+CV_WARP_INVERSE_MAP ); cvNamedWindow( "log-polar", 1 ); cvShowImage( "log-polar", dst ); cvNamedWindow( "inverse log-polar", 1 ); cvShowImage( "inverse log-polar", src2 ); cvWaitKey(); } return 0; }

V y l nhng g chng trnh s hin th khi opencv/samples/c/fruits.jpg c thng qua n

125

cvRemap p dng mt chuyn i hnh hc chung cho hnh nh. void cvRemap( const CvArr* src, CvArr* dst, const CvArr* mapx, const CvArr* mapy, int flags=CV INTER LINEAR+CV WARP FILL OUTLIERS, CvScalar fillval=cvScalarAll(0) ); src Ngun hnh nh dst im n hnh nh MapX Cc bn ca x-ta (CV_32FC1 hnh nh) mapy bn ca y-ta (CV_32FC1 hnh nh) flags Mt s kt hp ca phng php ni suy v c ty chn sau y (s): CV_WARP_FILL_OUTLIERS lp y tt c cc im nh hnh nh im n. Nu mt s trong s h correspond Bn ngoi trong cc ngun hnh nh, h thit lp fillval fillval Mt gi tr s dng in Bn ngoi CvRemap Hm bin i hnh nh ngun bng cch s dng bn quy nh: dst(x; y) = src(mapx(x; y), mapy(x; y)) Tng t nh bin i hnh hc khc, mt s phng php ni suy (quy nh bi ngi s dng) c s dng trch xut cc pixel vi cc ta s nguyn khng. Lu rng cc Hm khng th hot ng ti ch. cvResize Thay i kch thc hnh nh. void cvResize( const CvArr* src, 126

CvArr* dst, int interpolation=CV INTER LINEAR ); src Ngun hnh nh dst im n hnh nh interpolation Phng php ni suy : CV_INTER_NN gn nht-neigbor ni suy CV_INTER_LINEAR ni suy song tuyn tnh (s dng mc nh) CV_INTER_AREA ly mu li bng cch s dng mi quan h khu vc im nh. y l phng php a thch cho im tui decimation cho kt qu khng gn song. Trong iu kin ca phng to n cng tng t nh CV_INTER_NN phng php CV_INTER_CUBIC bicubic suy CvResize Hm thay i kch thc mt src hnh nh n ph hp vi chnh xc vo dst. Nu ROI c thit lp, Hm xem xt t l hon vn nh h tr. cvWarpAffine p dng mt chuyn i afin mt hnh nh. void cvWarpAffine( const CvArr* src, CvArr* dst, const CvMat* mapMatrix, int flags=CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, CvScalar fillval=cvScalarAll(0) ); src Ngun hnh nh dst im n hnh nh mapMatrix 2 3 chuyn i ma trn flags Mt s kt hp ca phng php ni suy v nhng l c ty chn sau y: CV_WARP_FILL_OUTLIERS lp y tt c cc im nh hnh nh im n, nu mt s ngi trong s h correspond Bn ngoi trong cc ngun hnh nh, h thit lp fillval CV_WARP_INVERSE_MAP cho thy rng ma trn nghch chuyn i t im n hnh nh ngun, v do , c th c s dng trc tip cho ni suy im nh. Nu khng, Hm tm thy cc bin i ngc t mapMatrix fillval Mt gi tr s dng in Bn ngoi CvWarpAffine Hm bin i hnh nh ngun bng cch s dng ma trn xc nh: ni

127

Hm ny tng t nh cvGetQuadrangleSubPix nhng h khng chnh xc nh nhau. cvWarpAffine yu cu u vo v u ra hnh nh c cng mt kiu d liu, c chi ph ln hn (do , n khng phi l kh ph hp i vi hnh nh nh) v c th li mt phn ca hnh nh ch khng thay i. Trong khi cvGetQuad rangleSubPix c th trch xut t t 8-bit hnh nh vo b m im ni, nh hn trn u v lun lun thay i ni dung im n ton b hnh nh. Lu rng Hm c th khng hot ng ti ch. chuyn i mt tp hp tha tht im, s dng Hm cvTransform t cxcore. cvWarpPerspective p dng mt bin i quan im cho mt hnh nh. void cvWarpPerspective( const CvArr* src, CvArr* dst, const CvMat* mapMatrix, int flags=CV INTER LINEAR+CV WARP FILL OUTLIERS, CvScalar fillval=cvScalarAll(0) ); src Ngun hnh nh dst im n hnh nh mapMatrix 3 3 chuyn i ma trn flags Mt s kt hp ca phng php ni suy v nhng l c ty chn sau y: CV_WARP_FILL_OUTLIERS lp y tt c cc im nh hnh nh im n, nu mt s ngi trong s h correspond Bn ngoi trong cc ngun hnh nh, h thit lp fillval CV_WARP_INVERSE_MAP cho thy rng ma trn nghch chuyn i t im n hnh nh ngun, v do , c th c s dng trc tip cho ni suy im nh. Nu khng, Hm tm thy cc bin i ngc t mapMatrix fillval Mt gi tr s dng in Bn ngoi CvWarpPerspective Hm bin i hnh nh ngun bng cch s dng ma trn xc nh:

Lu rng cc Hm khng th hot ng ti ch. i vi mt tp hp tha tht ca cc im s dng cvPerspectiveTransform Hm t CxCore. 2.3 Hnh nh hn hp bin i cvAdaptiveThreshold p dng mt ngng thch nghi vo mt mng. void cvAdaptiveThreshold( const CvArr* src, 128

CvArr* dst, double maxValue, int adaptive method=CV ADAPTIVE THRESH MEAN C, int thresholdType=CV THRESH BINARY, int blockSize=3, double param1=5 ); src Ngun hnh nh dst im n hnh nh maxValue ti a gi tr c s dng vi CV_THRESH_BINARY v CV_THRESH BINARY_INV adaptive_method thch ng ngng thut ton s dng: CV_ADAPTIVE_THRESH_MEAN_C hoc CV_ADAPTIVE_THRESH_GAUSSIAN_C (xem cc cuc tho lun) thresholdType ngng loi; phi l mt trong CV_THRESH_BINARY xxx CV_THRESH_BINARY_INV xxx blockSize Cc kch thc ca mt khu ph im nh c s dng tnh ton mt gi tr ngng im nh: 3, 5, 7, v nh vy param1 ph thuc vo phng php tham s. i vi cc phng php thch nghi CV_ADAPTIVE_THRESH_MEAN_C v CV_ADAPTIVE_THRESH_GAUSSIAN_C l mt hng s c tr t trung bnh hoc trng trung bnh (xem cc cuc tho lun), mc d n c th c tiu cc Hm bin i mt mu xm hnh nh vo mt nh nh phn theo cng thc: CV_THRESH_BINARY

CV_THRESH_BINARY_INV

T (x, y) l mt ngng tnh ring bit cho mi im nh. i vi phng php CV_ADAPTIVE_THRESH_MEAN_C n l blockSize blockSize im nh ln cn, tr param1. i vi phng php CV_ADAPTIVE_THRESH_GAUSSIAN_C l tng trng s (gaussian) ca mt blockSize blockSize im nh kch c khi, tr param1. cvCvtColor Chuyn i mt hnh nh t mt khng gian mu khc. void cvCvtColor( const CvArr* src, CvArr* dst, int code ); 129

src Ngun 8-bit (8u), hnh nh 16-bit (16u) hoc duy nht chnh xc floating-point (32F) dst hnh nh im n ca cng mt kiu d liu nh l ngun gc. S lng cc knh c th c khc nhau code mu chuyn i hot ng c th c specifed s dng CV_src_color_space 2 dst_color_space hng s (xem di y) Hm chuyn i hnh nh u vo t mt khng gian mu khc. Hm b qua cc lnh vc colorModel v channelSeq ca tiu IplImage, v vy ngun hnh nh mu sc khng gian c th quy nh mt cch chnh xc (bao gm c th t ca cc knh trong trng hp khng gian RGB. V d, BGR c ngha l nh dng 24-bit B0, G0, R0, B1, G1, R1, ... b tr trong khi RGB c ngha l 24 nh dng vi R0, G0, B0, R1, G1, B1, ... b tr). Phm vi thng thng cho R, G, B knh gi tr l: 0 n 255 cho hnh nh 8-bit 0 n 65535 cho hnh nh 16-bit v 0-1 cho hnh nh ni-im. Tt nhin, trong trng hp ca bin i tuyn tnh, phm vi c th c c th, nhng c c kt qu chnh xc trong trng hp bin i phi tuyn tnh, hnh nh u vo c thu nh. Cc Hm c th lm bin i sau y: Bin i trong khng gian RGB nh thm / g b cc knh alpha, o chiu knh trt t, chuyn i n / t 16-bit mu RGB (R5: G6: B5 hoc R5: G5: B5), cng nh chuyn i n / t mu xm bng cch s dng: V Vic chuyn i t mt hnh nh RGB mu xm c thc hin vi: cvCvtColor(src ,bwsrc, CV_RGB2GRAY)
RGB CIE XYZ.Rec 709 with D65 white point (CV_BGR2XYZ, CV_RGB2XYZ, CV_XYZ2BGR, CV_XYZ2RGB):

X, Y v Z bao gm cc phm vi ton b gi tr (trong trng hp hnh nh ni im Z c th vt qu 1).


RGB YCrCb JPEG (a.k.a. YCC) (CV_BGR2YCrCb, CV_RGB2YCrCb, CV_YCrCb2BGR, CV_YCrCb2RGB)

130

Vi

Y, Cr v Cb bao gm ton b phm vi gi tr. RGBHSV (CV_BGR2HSV, CV_RGB2HSV, CV_HSV2BGR, CV_HSV2RGB) trong trng hp hnh nh 8-bit v 16-bit R, G v B c chuyn i sang nh dng du chm ng v quy m ph hp vi khong 0-1

nu H < 0 th H <- H + 360 Ngy u ra 0 <= V <= 1.0 <= S <= 1.0 <= H <= 360. Cc gi tr ny sau c chuyn i sang cc loi d liu im n: nh 8-bit Hnh nh 16-bit (hin khng c h tr) Hnh nh 32-bit H, S, V tri nh l RGBHLS (CV_BGR2HLS, CV_RGB2HLS, CV_HLS2BGR, CV_HLS2RGB). trong trng hp hnh nh 8-bit v 16-bit R, G v B c chuyn i sang nh dng du chm ng v quy m ph hp vi phm vi t 0 n 1.

131

if then On output , , Cc gi tr ny sau c chuyn i sang cc loi d liu im n: nh 8-bit Hnh nh 16-bit (hin khng c h tr) Hnh nh 32-bit H, S, V tri nh l

RGBCIE L*a*b*(CV_BGR2Lab,CV_RGB2Lab,CV_Lab2BGR,CV_Lab2RGB) trong trng hp hnh nh-8 bit v 16-bit R, G v B c chuyn i nh dng du chm ng v quy m ph hp vi khong 0-1

Vi

u ra 0 <= L <= 100, -127 <= a<= 127, -127 <= b <= 127 Cc gi tr ny sau c chuyn i sang cc loi d liu im n: nh 8-bit Hnh nh 16-bit khng c h tr Hnh nh 32-bit L, a, b, cn li l

132

RGBCIE L*u*v* (CV_BGR2Luv, CV_RGB2Luv, CV_Luv2BGR, CV_Luv2RGB) trong trng hp hnh nh-8 bit v 16-bit R, G v B c chuyn i nh dng du chm ng v quy m ph hp vi 0 n 1 phm vi

Ngy u ra 0 <= L <= 100, -134 <= u <= 220, -140 <= v <= 122. Cc gi tr ny sau c chuyn i sang cc loi d liu im n: nh 8-bit Hnh nh 16-bit khng c h tr Hnh nh 32-bit L, u, v tri nh l Cc cng thc trn chuyn i RGB / t khng gian mu khc nhau c ly t nhiu ngun trn trang web, ch yu l t Ford98 ti trang web Charles Poynton. BayerRGB (CV_BayerBG2BGR, CV_BayerGB2BGR, CV_BayerRG2BGR,
CV_BayerGR2BGR, CV_BayerBG2RGB, CV_BayerGB2RGB, CV_BayerRG2RGB, CV_BayerGR2RGB) Cc Bayer m hnh s dng rng ri trong cc my nh CCD v

CMOS. N cho php mt c c hnh nh mu sc t mt my bay duy nht, ni pixels R, G v B (cm bin ca mt thnh phn c th) c xen k nh th ny:

Sn lng cc thnh phn RGB ca mt im nh c ni suy t 1, 2 hoc 4 ngi hng xm ca im nh c cng mt mu sc. C mt s c sa i ca m hnh trn c th c t c bng cch dch chuyn bn tri m hnh im nh v / hoc mt trong nhng im nh ln. Hai ch C1 v C2 trong cc hng s chuyn i CV_Bayer C1C2 2BGR v CV_Bayer C1C2 2RGB ch cc loi m hnh c th - y l nhng thnh phn t hng th hai, th hai v th ba ct, tng ng. V d, m hnh trn rt ph bin loi "BG". cvDistTransform 133

Tnh khong cch n cc im nh khng gn gi nht cho tt c cc im nh khc khng ca hnh nh ngun. void cvDistTransform( const CvArr* src, CvArr* dst, int distance type=CV DIST L2, int mask size=3, const float* mask=NULL, CvArr* labels=NULL ); src 8-bit, mt knh (nh phn) ngun hnh nh dst u ra hnh nh vi khong cch tnh (32-bit floating-point, single-knh) distance_type v khong cch, can be CV_DIST_L1, CV_DIST_L2, CV_DIST_C or CV_DIST_USER mask_size Kch thc ca mt n khong cch bin i, c th l 3 hoc 5. trong trng hp ca CV_DIST_L1 hoc CV_DIST_C tham s ny l buc phi n 3, v 3 x 3 mt n cung cp cho cng mt kt qu nh mt 5 5 nhng n l nhanh hn mask mt n ngi dng nh ngha trong trng hp ca mt khong cch ngi dng nh ngha, n bao gm 2 con s (hor izontal / chi ph thay i theo chiu dc, ng cho thay i chi ph) trong trng hp OFA 3 3 mt n v 3 s in thoi (Ngang / dc thay i chi ph, ng cho thay i chi ph, chi ph di chuyn ca nhng hip s) trong trng hp ca mt 5 5 mt n labels Cc mng u ra ty chn 2d nhn kiu s nguyn, kch thc ging nh src v dst Hm tnh ton khong cch xp x t mi im nh hnh nh nh phn gn nht khng im nh. i vi khng pixels Hm thit lp khong cch bng khng i vi nhng ngi khc m n tm thy con ng ngn nht bao gm nhng thay i c bn: di chuyn ngang, dc, cho hoc ca nhng hip s (mi nht hin c sn 5 5 mt n). Khong cch tng th c tnh nh l mt tng hp ca nhng khong cch c bn. Bi v khong cch Hm nn c i xng, tt c trong s chuyn dch theo chiu ngang v dc phi c cng mt chi ph (c k hiu l a), tt c cc thay i ng cho phi c cng chi ph (k hiu l b), v tt c cc ca nhng hip s di chuyn phi c cng chi ph (k hiu l c). i vi CV_DIST_C v CV_DIST_L1 khong cch c tnh ton chnh xc, trong khi i vi CV_DIST_L2 (clt khong cch), khong cch c th c tnh ch vi mt s li tng i (5 5 mt n cho kt qu chnh xc hn), OpenCV s dng cc gi tr ngh trong [4]: CV_DIST_C (3 x 3) a=1 CV_DIST_L1 (3 x 3) a = 1, b = 2 CV_DIST_L2 CV_DIST_L2 (3 x 3) (5 5) a=0.955, b=1.3693
a=1, b=1.4, c=2.1969

V di y l mu ca trng khong cch (en (0) im nh l gia mu trng vung) trng hp ca mt khong cch ngi dng nh ngha: Ngi dng nh ngha 3 3 mt n (a = 1, b = 1,5) 134

4.5
4 3.5 3 3.5 4 4.5

4
3 2.5 2 2.5 3 4

3.5
2.5 1.5 1 1.5 2.5 3.5

3
2 1

3.5
2.5 1.5 1

4
3 2.5 2 2.5 3 4

4.5
4 3.5 3 3.5 4 4.5

1 2 3

1.5 2.5 3.5

Ngi dng nh ngha 5 5 mt n (a = 1, b = 1,5, c = 2)


4.5 3.5 3 3 3 3.5 4 3.5 3 2 2 2 3 3.5 3 2 1.5 1 1.5 2 3 1 2 3 3 2 1 3 2 1.5 1 1.5 2 3 3.5 3 2 2 2 3 3.5 4.5 3.5 3 3 3 3.5 4

Thng thng, i vi mt khong cch, nhanh chng th d ton CV_DIST_L2, 33 mt n c s dng, v cho mt CV d ton khong cch chnh xc hn CV_DIST_L2, 5 5 mt n c s dng. Khi sn lng labels tham s khng phi l NULL, cho mi im nh khc khng Hm cng tm thy cc thnh phn kt ni gn nht bao gm khng pixels. Kt ni cc thnh phn t c tm thy nh ng vin trong u ca hm. Trong ch ny thi gian x l l O (N), trong N l s lng im nh. Nh vy, Hm cung cp mt cch rt nhanh tnh ton s Voronoi gn ng cho hnh nh nh phn. CvConnectedComp typedef struct CvConnectedComp { double area; /* area of the segmented component */ CvScalar value; /* average color of the connected component */ CvRect rect; /* ROI of the segmented component */ CvSeq* contour; /* optional component boundary (the contour might have child contours corresponding to the holes) */ } CvConnectedComp; cvFloodFill in vo cc mt thnh phn kt ni vi cc mu sc nht nh. void cvFloodFill( CvArr* image, 135

CvPoint seed point, CvScalar new val, CvScalar lo diff=cvScalarAll(0), CvScalar up diff=cvScalarAll(0), CvConnectedComp* comp=NULL, int flags=4, CvArr* mask=NULL ); image u vo - 1 hoc 3-knh, hnh nh 8-bit floating-point. N c sa i bi cc Hm tr khi CV_FLOODFILL_MASK_ONLY ch c thit lp (xem di y) seed_point im khi u new_val gi tr ca cc im nh min sn li lo_diff ti a thp hn sng / mu sc khc bit gia cc im nh hin ang quan st v cc nc lng ging thuc cc thnh phn, hoc im nh mt ht ging c b sung vo thnh phn. Trong trng hp cc hnh nh mu sc 8-bit, n l mt gi tr ng gi up_diff s khc bit ti a sng / mu sc trn gia cc im nh hin ang quan st v mt cc nc lng ging thuc cc thnh phn, hoc im nh mt ht ging c b sung vo thnh phn. Trong trng hp cc hnh nh mu sc 8-bit, n l mt gi tr ng gi comp Con tr tr ti cu trc Hm lp y vi nhng thng tin v sn li lm- chnh. Lu rng cc Hm khng in vo comp-> lnh vc ng vin. Ranh gii ca y thnh phn c th c ly t hnh nh mt n u ra bng cch s dng cvFindContours flags Cc hot ng c. Bit thp cha gi tr kt ni, 4 (theo mc nh) hoc 8, c s dng trong Hm. Kt ni xc nh ngi hng xm ca mt im nh c coi l. Trn bit c th l 0 hoc s kt hp ca nhng l c sau y: CV_FLOODFILL_FIXED_RANGE nu thit lp, s khc bit gia cc im nh hin ti v ht ging Cc im nh c coi l, nu khng s khc bit gia cc im nh ln cn c xem l ( phm vi ni) CV_FLOODFILL_MASK_ONLY, nu thit lp cc Hm khng in vo cc hnh nh (new_val l b qua), nhng y mt n (phi l khng NULL trong trng hp ny) mask mt n hot ng, nn l mt knh hnh nh 8-bit, 2 pixel rng hn v 2 pixel cao hn image. Nu khng phi l NULL, Hm s dng v cp nht cc mt n, v vy ngi s dng c li sponsibility khi to ni dung mask. Floodfilling khng th i qua cc im nh khc khng trong mt n, v d, mt u ra cnh my d c th c s dng nh mt mt n in cnh. C th s dng mt n ging nhau trong nhiu cuc gi n Hm m bo cc y khu vc khng chng cho ln nhau. Lu : v mt n ln hn so vi hnh nh y, mt im nh trong mask tng ng vi (x, y) im nh trong image s c ta (x + 1, y + 1) Hm lp y mt thnh phn kt ni bt u t im ging vi mu quy nh. Kt ni c xc nh bi s gn gi ca cc gi tr im nh. Cc im nh (x, y) c coi l thuc v min sn li nu: mu xm hnh nh, phm vi ni mu xm hnh nh, phm vi c nh 136

mu sc hnh nh, phm vi ni

mu sc hnh nh, phm vi c nh

src (x, y) l gi tr ca mt nc lng ging ca im nh. l, c thm vo cc kt ni thnh phn, mu sc / sng ca im nh nn c gn : Mu sc / sng ca mt trong cc nc lng ging gi cc thnh phn c kt ni trong trng hp ca phm vi ni Mu sc / sng ca im ht ging trong trng hp ca phm vi c nh. cvInpaint Inpaints khu vc c la chn trong hnh nh. void cvInpaint( const CvArr* src, const CvArr* mask, CvArr* dst, double inpaintRadius, int flags); src 8-bit u vo 1 knh hoc hnh nh 3-knh. mask mt n inpainting, 8-bit 1 knh hnh nh. Cc im nh khc khng cho thy khu vc c nhu cu c inpainted. dst hnh nh u ra ca nh dng v kch thc ging nh u vo. inpaintRadius bn knh ca khu ph circlular ca mi im inpainted c coi l bng cc thut ton. flags inpainting phng php, mt trong nhng iu sau y: CV_INPAINT_NS phng trnh Navier-Stokes da trn phng php. CV_INPAINT_TELEA phng php Alexandru Telea [21] Hm ti cu trc khu vc hnh nh c la chn t cc im nh gn ranh gii khu vc. Hm c th c s dng loi b bi v vt try xc t mt bc nh qut, hoc loi b i tng khng mong mun t cc hnh nh tnh hoc video. cvIntegral Tnh tch phn ca mt hnh nh. 137

void cvIntegral( const CvArr* image, CvArr* sum, CvArr* sqsum=NULL, CvArr* tiltedSum=NULL ); Nhng hnh nh hnh nh ngun, W H, 8-bit floating-point (32F hoc 64f) sum hp hnh nh khng th tch ri, (W + 1) (H + 1), s nguyn hoc tng gp i chnh xc 32-bit floating-point (64f) sqsum Nhng hnh nh khng th thiu i vi cc gi tr pixel vung, (W + 1) (H + 1), gp i chnh xc ni im (64f) tiltedSum th thiu cho cc nh xoay 45 , (W + 1) (H + 1), cng mt d liu loi nh sum Hm tnh ton mt hoc nhiu hnh nh tch ri cho hnh nh ngun nh sau:

S dng nhng hnh nh tch ri, ngi ta c th tnh ton tng hp, trung bnh v lch tiu chun trn mt c th, ln bn phi hoc xoay khu vc hnh ch nht ca hnh nh trong mt thi gian lin tc, v d:

N lm cho c th lm m nhanh hoc tng quan khi nhanh chng vi kch thc ca s bin, v d. Trong trng hp ca hnh nh a knh, cc khon tin cho mi knh c tch ly c lp. cvPyrMeanShiftFiltering Khng phn chia nh hnh nh meanshift void cvPyrMeanShiftFiltering( const CvArr* src, CvArr* dst, double sp, double sr, int max level=1, CvTermCriteria termcrit= cvTermCriteria(CV TERMCRIT ITER+CV TERMCRIT EPS,5,1)); src Ngun 8-bit, hnh nh 3-knh. dst hnh nh im n ca cng mt nh dng v kch thc ging nh ngun. 138

sp Bn knh ca s khng gian. sr Cc ca s mu sc bn knh. max_level ti a ca kim t thp cho cc phn khc. termcrit tiu ch chm dt khi no phi ngng lp i lp li meanshift. Cc Hm thc hin cc giai on lc ca phn khc meanshift, l, u ra ca Hm l lc "posterized" hnh nh vi gradient mu sc v kt cu tt, ht dt. Ti mi im nh (X, Y) hnh nh u vo (hoc hnh nh u vo vi quy m nh hn, xem di y) Hm thc hin meanshift lp i lp li, c ngha l, cc im nh (X, Y) khu ph trong HyperSpace khng gian mu sc chung l xem xt: (R, G, B) v (r, g, b) l cc vect ca cc thnh phn mu (X, Y) v (x, y), (mc d, cc thut ton khng ph thuc vo khng gian mu sc c s dng, v vy bt k thnh phn 3khng gian mu sc c th c s dng thay th). Trong khu vc khng gian gi tr trung bnh (X ', Y') v vector mu trung bnh (R, G, B) c tm thy v h hnh ng nh l trung tm khu ph k tip lp i lp li: Sau lp i lp li trn, cc thnh phn mu sc ca cc im nh ban u (c ngha l, cc im nh t ni lp i lp li bt u) c thit lp cc gi tr cui cng (mu sc trung bnh ti phin cui cng): Sau , max_level> 0, kim t thp gaussian max_level + cp 1 c xy dng, v trn th tc chy trn cc lp nh nht. Sau , kt qu c tuyn truyn cc lp ln hn v lp i lp li l chy mt ln na ch vo nhng im nh m mu sc lp khc nhau nhiu (sr) t lp c phn gii thp hn, l ranh gii ca khu vc mu c lm r. Lu , rng Cc kt qu s c thc s khc nhau t nhng ngi thu c bng cch chy cc th tc meanshift cc hnh nh ban u ton b (tc l khi max_level == 0). cvPyrSegmentation Thc hin phn khc hnh nh ca kim t thp. void cvPyrSegmentation( IplImage* src, IplImage* dst, CvMemStorage* storage, CvSeq** comp, int level, double threshold1, double threshold2 ); src Cc hnh nh ngun dst Cc hnh nh im n storage lu tr , lu tr cc chui kt qu ca cc thnh phn kt ni comp con tr n cc chui u ra ca cc thnh phn phn on 139

level ti a mc ca kim t thp cho phn on threshold1 Li ngng cho vic thit lp cc lin kt threshold2 Li ngng cho cc phn nhm phn on Hm thc hin phn chia nh hnh nh bng kim t thp. Kim t thp ny c xy dng ln n mc cp. Cc lin kt gia bt k im nh trn mt mc i v cha ng c vin b im nh trn lin k mc c thnh lp nu p (c (a), c (b)) <threshold1. Sau khi cc thnh phn c kt ni c nh ngha, h c tham gia vo mt s cm. Bt k hai phn on A v B thuc cng mt cm, nu p (c (A), c (B)) <threshold2. Nu hnh nh u vo ch c mt knh, sau xanh), sau . Nu hnh nh u vo c ba knh (, xanh l cy v mu

C th c nhiu hn mt thnh phn kt ni cho mi mt cluster. Src hnh nh v dst nn l mt knh 8-bit hoc 3-knh hnh nh hoc kch thc bng nhau. cvThreshold p dng mt ngng c nh mc cc phn t mng. double cvThreshold( const CvArr* src, CvArr* dst, double threshold, double maxValue, int thresholdType ); src Ngun mng (single-knh, 8-bit hoc 32-bit du chm ng) dst im n mng ; phi l cng loi nh src hoc 8 bitthreshold ngng ngng gi tr maxValue ti a gi tr s dng vi CV_THRESH_BINARY and CV_THRESH_BINARY_INV loi thresholdType ngng loi (xem cc cuc tho lun) Hm p dng mc ngng c nh n mt mng duy nht knh. Cc Hm c typically s dng c c mt hnh nh bi-cp (nh phn) ca mu xm hnh nh (cvCmpS c th cng c s dng cho mc ch ny) hoc loi b mt ting, tc l lc ra nhng im nh qu nh hoc qu ln valUE. C mt s loi ca ngng h tr Hm c xc nh bi thresholdType: CV_THRESH_BINARY

CV_THRESH_BINARY_INV

CV_THRESH_TRUNC 140

CV_THRESH_TOZERO

CV_THRESH_TOZERO_INV

Ngoi ra, gi tr c bit CV_THRESH_OTSU c th c kt hp vi mt trong cc gi tr trn. Trong trng hp ny, Hm xc nh gi tr ngng ti u bng cch s dng thut ton Otsu v s dng n thay v quy nh thresh. Hm tr v gi tr ngng tnh ton. Hin nay, Otsu ca phng php ch c thc hin cho hnh nh 8-bit.

141

2.4 Biu CvHistogram Biu a chiu. typedef struct CvHistogram { int type; CvArr* bins; float thresh[CV_MAX_DIM][2]; / * cho biu thng nht * / float** thresh2;/ * khng thng nht biu * / CvMatND mat;/ * nhng vo ma trn tiu cho biu mng * / 142

} CvHistogram; cvCalcBackProject Tnh chiu tr li. void cvCalcBackProject( IplImage** image, CvArr* back project, const CvHistogram* hist ); image Ngun hnh nh (mc d bn c th vt qua CvMat** cng) back_project im n chiu li hnh nh ca cng mt loi vi hnh nh ngun hist Histogram Hm tnh ton d n tr li ca biu . i vi mi tuple ca im nh ti cng mt v tr ca tt c cc hnh nh u vo duy nht knh Hm t gi tr ca bin biu , tng ng vi cc tuple trong hnh nh im n. V thng k, gi tr sn lng mi hnh nh pixel l xc sut ca cc tuple quan st c phn phi (biu ). V d, tm thy mt i tng mu trong hnh, ta c th lm nh sau: 1. Tnh ton mt biu mu sc cho cc i tng mu gi hnh nh ch cha cc i tng ny. Biu c th c ti a mnh m, tng ng vi mu . 2. Tnh ton li chiu ca mt chic my bay mu sc ca hnh nh u vo i tng l tm kim, s dng biu . Ngng hnh nh. 3. Tm cc thnh phn kt ni trong hnh kt qu v la chn cc thnh phn quyn s dng mt s tiu ch b sung, v d, thnh phn kt ni ln nht. l cc thut ton gn ng ca i tng theo di Camshift mu sc, ngoi tr cho bc th 3, thay v , thut ton CAMSHIFT c s dng xc nh v tr cc i tng trn chiu tr li v tr i tng trc .

cvCalcBackProjectPatch t mt mu trong mt hnh nh bng cch s dng mt biu so snh. void cvCalcBackProjectPatch( IplImage** images, CvArr* dst, CvSize patch size, CvHistogram* hist, int method, float factor ); images Hnh nh Ngun (tuy nhin, bn c th vt qua CvMat ** cng) dst im n hnh nh patch_size kch thc ca cc ming v trt mc d ngun hnh nh hist Histogram 143

method Compasion phng php, c thng qua cvCompareHist (xem m t v Hm ) factor Bnh thng ha yu t cho biu , s nh hng n quy m bnh thng ca ch n hnh nh, vt qua 1 nu khng chc chn Hm tnh ton chiu tr li bng cch so snh biu ca hnh nh ngun cc bn v li vi biu nht nh. Ly kt qu o lng t mt s hnh nh ti mi a im trn thu nhp t u to ra mt image mng. Nhng kt qu ny c th l mt hoc nhiu mu sc, phi sinh x, y phi sinh, Laplacian lc, nh hng Gabor b lc, vv Mi sn lng o lng c thu thp thnh ring ca mnh hnh nh ring bit. Cc mng hnh nh image l mt b su tp nhng hnh nh ny o lng. Mt biu a chiu hist c xy dng bng cch ly mu t cc mng hnh nh image. Cc biu cui cng l bnh thng. Cc biu hist c nhiu kch thc nh s lng cc yu t trong mng hnh nh. Mi hnh nh mi c o v sau chuyn i thnh mt mng hnh nh image trong mt cho sen ROI. Biu c ly t hnh nh ny image trong mt khu vc c bao ph bi mt "ming v" vi mt neo ti trung tm nh trong hnh di y. Biu c chun ha bng cch s dng cc tham s norm_factor n c th c so snh vi hist. Cc biu c tnh l compared biu m hnh, hist s dng cvCompareHist Hm so snh phng php = method). Kt qu u ra c t v tr tng ng vi bn v mt Chor dst hnh nh xc sut. Qu trnh ny c lp i lp li khi bn v c trt trn thu nhp t u. Lp i lp li cp nht biu bng cch tr i pixels du c bao ph bi cc ming v v thm mi pixels ph biu c th tit kim rt nhiu hot ng, mc d n khng c thc hin. Tnh ton d n tr li bi bn v

144

cvCalcHist Tnh ton biu ca hnh nh (s). void cvCalcHist( IplImage** image, CvHistogram* hist, int accumulate=0, const CvArr* mask=NULL ); image Ngun hnh nh (mc d bn c th vt qua CvMat** cng) hist con tr n biu accumulate c tch lu. Nu n c thit lp, biu l khng c xa trong u. iu ny tnh nng cho php ngi s dng tnh ton mt biu duy nht t mt s hnh nh, hoc cp nht cc biu trc tuyn mask mt n hot ng, xc nh nhng im nh ca hnh nh ngun c tnh Hm tnh ton biu ca mt hoc nhiu knh hnh nh n. Cc yu t mt b c s dng tng bin biu c chp ti cng mt v tr t hnh nh u vo tng ng. #include <cv.h> #include <highgui.h> int main( int argc, char** argv ) { IplImage* src; if( argc == 2 && (src=cvLoadImage(argv[1], 1))!= 0) { IplImage* h_plane = cvCreateImage( cvGetSize(src), 8, 1 ); IplImage* s_plane = cvCreateImage( cvGetSize(src), 8, 1 ); IplImage* v_plane = cvCreateImage( cvGetSize(src), 8, 1 ); IplImage* planes[] = { h_plane, s_plane }; IplImage* hsv = cvCreateImage( cvGetSize(src), 8, 3 ); int h_bins = 30, s_bins = 32; int hist_size[] = {h_bins, s_bins}; /* hue varies from 0 (0 deg red) to 180 (360 deg red again) */ float h_ranges[] = { 0, 180 }; /* saturation varies from 0 (black-gray-white) to 255 (pure spectrum color) */ float s_ranges[] = { 0, 255 }; float* ranges[] = { h_ranges, s_ranges }; int scale = 10; IplImage* hist_img = cvCreateImage( cvSize(h_bins*scale,s_bins*scale), 8, 3 ); CvHistogram* hist; float max_value = 0; int h, s; cvCvtColor( src, hsv, CV_BGR2HSV ); cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 ); hist = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 ); 145

cvCalcHist( planes, hist, 0, 0 ); cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 ); cvZero( hist_img ); for( h = 0; h < h_bins; h++ ) { for( s = 0; s < s_bins; s++ ) { float bin_val = cvQueryHistValue\_2D( hist, h, s ); int intensity = cvRound(bin_val*255/max_value); cvRectangle( hist_img, cvPoint( h*scale, s*scale ), cvPoint( (h+1)*scale - 1, (s+1)*scale - 1), CV_RGB(intensity,intensity,intensity), CV_FILLED ); } } cvNamedWindow( "Source", 1 ); cvShowImage( "Source", src ); cvNamedWindow( "H-S Histogram", 1 ); cvShowImage( "H-S Histogram", hist_img ); cvWaitKey(0); } } cvCalcProbDensity Chia mt biu bng cch khc. void cvCalcProbDensity( const CvHistogram* hist1, const CvHistogram* hist2, CvHistogram* dst hist, double scale=255 ); hist1 biu u tin (s chia) hist2 th hai biu dst_hist im n biu scale yu t quy m cho cc biu im n Hm tnh ton mt xc sut i tng t hai biu nh:

V vy, cc biu im n thng trong vng t hn so vi quy m. cvClearHist Xa biu . void cvClearHist( CvHistogram* hist ); hist Histogram 146

Hm thit lp tt c cc thng biu 0 trong trng hp ca mt biu dy c v loi b tt c cc thng biu trong trng hp ca mt mng tha tht. cvCompareHist So snh hai biu dy c. double cvCompareHist( const CvHistogram* hist1, const CvHistogram* hist2, int method ); hist1 Cc biu dy c u tin hist2 Cc biu th hai dy c method so snh phng php, mt trong nhng iu sau y: CV_COMP_CORREL tng quan CV_COMP_CHISQR Chi-Qung trng CV_COMP_INTERSECT Intersection CV_COMP_BHATTACHARYYA khong cch Hm so snh hai biu dy c bng cch s dng cc phng php quy nh (H1 biu th u tin biu , H2 th hai): Correlation (method=CV_COMP_CORREL) *

where

where N is the number of histogram bins.

Chi-Square (method=CV_COMP_CHISQR) *

Intersection (method=CV_COMP_INTERSECT) *

Bhattacharyya distance (method=CV_COMP_BHATTACHARYYA) *

Hm tr d (H1, H2). 147

Lu : phng php CV_COMP_BHATTACHARYYA ch lm vic vi biu bnh thng. so snh mt biu tha tht hoc tha tht cu hnh chung ca cc im trng, xem xt s dng Hm cvCalcEMD2. cvCopyHist Sao chp mt biu . void cvCopyHist( const CvHistogram* src, CvHistogram** dst ); src Ngun biu dst Pointer biu im n Hm ny lm cho mt bn sao ca biu . Nu con tr biu th hai * dst l NULL, biu mi c cng kch thc nh src c to ra. Nu khng, c hai biu phi c bng loi v kch c. Sau , cc Hm sao chp cc gi tr bin ca biu ngun n ch biu v t gi tr bin cng phm vi nh trong src. cvCreateHist To mt biu . CvHistogram* cvCreateHist( int dims, int* sizes, int type, float** ranges=NULL, int uniform=1 ); dims S kch thc biu sizes mng ca cc kch thc kch thc biu type nh dng biu i din: CV_HIST_ARRAY c ngha l cc d liu biu l i din- bc bi nh mt CvMatND mng a chiu dy c; CV_HIST_SPARSE c ngha l tha tht ca anh d liu togram c biu din nh l mt mng tha tht a chiu CvSparseMat ranges mng phm vi cho cc thng biu . ngha ca n ph thuc vo uniform parameter gi tr. Nhng dy c s dng cho khi biu c tnh ton hoc backprojected xc nh tng ng vi bin biu gi tr / tuple cc gi tr t u vo image (s) uniform c ng nht, nu khng phi 0, biu ngang bng vi nhau thng v cho mi 0 <= i < cDims phm vi [i] l mt mng ca hai con s thp hn v trn ranh gii th-i-ng togram kch thc. Ton b phm vi [thp hn, pha trn sau c chia thnh lm m [i] bng phn xc nh phm vi gi tr u vo th i-tuple cho mi thng biu . V nu thng nht = 0, sau yu t th i ca cc phm vi mng cha m [i] 1 yu t: lower0, upper0, lower1, upper1 lower2, ... upperdims [i] -1 lowerj v upperj thp hn v trn ranh gii uple gi tr u vo th j-bin, th i tng ng. Trong c hai trng hp, u vo gi tr vt ra ngoi phm vi ch nh cho mt bin biu khng c tnh cvCalcHist v y 0 trong bi cvCalcBackProject 148

Hm to ra mt biu ca kch thc quy nh v tr v mt con tr n vic to ra biu . Nu mng ranges l 0, phm vi bin biu phi c xc nh thng qua Hm cvSetHistBinRanges. Mc d cvCalcHist v cvCalcBackProject c th x l 8-bit hnh nh m khng thit lp phm vi bin, h gi nh l ngi cch u nhau trong 0 n 255 thng. cvGetHistValue * D Tr v mt con tr n thng biu . float cvGetHistValue 1D(hist, idx0) float cvGetHistValue 2D(hist, idx0, idx1) float cvGetHistValue 3D(hist, idx0, idx1, idx2) float cvGetHistValue nD(hist, idx) hist Histogram idx0, idx1, idx2, idx3 Ch s thng rc idx mng ca cc ch s #define cvGetHistValue_1D( hist, idx0 ) ((float*)(cvPtr1D( (hist)->bins, (idx0), 0 )) #define cvGetHistValue_2D( hist, idx0, idx1 ) ((float*)(cvPtr2D( (hist)->bins, (idx0), (idx1), 0 ))) #define cvGetHistValue_3D( hist, idx0, idx1, idx2 ) ((float*)(cvPtr3D( (hist)->bins, (idx0), (idx1), (idx2), 0 ))) #define cvGetHistValue_nD( hist, idx ) ((float*)(cvPtrND( (hist)->bins, (idx), 0 ))) GetHistValue macro tr v mt con tr vo thng quy nh ca 1D, 2D, 3D hoc ND biu . Trong trng hp ca mt biu tha tht Hm to ra mt bin mi v t n l 0, tr khi n tn ti. cvGetMinMaxHistValue Tm cc thng ti thiu v ti a biu . void cvGetMinMaxHistValue( const CvHistogram* hist, float* min value, float* max value, int* min idx=NULL, int* max idx=NULL ); hist Histogram min_value Con tr tr ti gi tr ti thiu ca biu max_value Con tr tr ti gi tr ti a ca biu min_idx Con tr tr ti cc mng ca cc ta cho ti thiu max_idx Con tr tr ti cc mng ca cc ta cho ti a 149

Hm tm thy cc thng ti thiu v ti a biu v v tr ca h. Tt c sn lng i s l ty chn. Trong s extremas nhau vi cng mt gi tr ti thiu ch s (trong t in ) c tr v. Trong trng hp mc ti a hoc ti thiu mt s, u tin trong t in (extrema a im) c tr v. cvMakeHistHeaderForArray Lm cho mt trong biu ca mt mng. CvHistogram* cvMakeHistHeaderForArray( int dims, int* sizes, CvHistogram* hist, float* data, float** ranges=NULL, int uniform=1 ); dims kch thc biu sizes mng ca cc kch thc kch thc biu hist tiu biu c khi to bi Hm data mng s c s dng lu tr cc thng biu ranges bin phm vi biu , xem cvCreateHist uniform tnh ng nht c, xem cvCreateHist Hm khi to biu , c tiu v cc thng c phn b bi ngi s dng th. cvReleaseHist khng cn phi c gi sau. Ch c biu dy c c th c khi to cch. Hm tr v hist. cvNormalizeHist Bnh thng ha cc biu . void cvNormalizeHist( CvHistogram* hist, double factor ); hist con tr n biu factor yu t chun ha Hm bnh thng ha cc thng biu bng cch m rng quy m, chng hn l s tin ca thng tr nn bng cc factor. cvQueryHistValue*D Truy vn gi tr ca bin biu . float QueryHistValue_1D(CvHistogram hist, int idx0) hist Histogram idx0, idx1, idx2, idx3 Ch s thng rc idx mng ca cc ch s #define cvQueryHistValue\_1D( hist, idx0 ) \ cvGetReal1D( (hist)->bins, (idx0) ) 150

#define cvQueryHistValue\_2D( hist, idx0, idx1 ) \ cvGetReal2D( (hist)->bins, (idx0), (idx1) ) #define cvQueryHistValue\_3D( hist, idx0, idx1, idx2 ) \ cvGetReal3D( (hist)->bins, (idx0), (idx1), (idx2) ) #define cvQueryHistValue\_nD( hist, idx ) \ cvGetRealND( (hist)->bins, (idx) ) Cc macro tr v gi tr ca bin quy nh ca 1D, 2D, 3D, biu ND. Trong trng hp ca mt biu tha tht Hm tr v 0, nu bin khng c mt trong biu khng c bi mi bin c to ra. cvReleaseHist Pht hnh cc biu . void cvReleaseHist( CvHistogram** hist ); hist p con tr n biu pht hnh Hm pht hnh (tiu v d liu) biu . Con tr n biu xa bi Hm. Nu *hist con tr c NULL, Hm khng c g. cvSetHistBinRanges Thit lp cc gii hn ca cc thng biu . void cvSetHistBinRanges( CvHistogram* hist, float** ranges, int uniform=1 ); hist Histogram ranges mng ca cc mng phm vi bin, xem cvCreateHist uniform tnh ng nht c, xem cvCreateHist Hm l mt Hm c lp thit lp phm vi bin trong biu . Cho bit thm mt m t chi tit ranges cc thng s v uniform xem Hm cvCalcHist, c th khi to cc phm vi nh. Phm vi cho cc thng biu phi c thit lp trc khi biu c tnh hoc backproject ca biu c tnh ton. cvThreshHist Ngng biu . void cvThreshHist( CvHistogram* hist, double threshold ); hist con tr n biu threshold Cp ngng Hm xa cc thng biu di ngng quy nh.

151

Chng 3: cvaux. Cc hm m rng ca Th Gic My Tnh


3.1 Pht hin v m t i tng

152

Chng 4: highgui. Cao cp giao din v I/O


Trong khi OpenCV c thit k s dng trong cc ng dng quy m y v c th c s dng trong phm vi Hm giu khun kh giao din ngi dng (chng hn nh Qt, WinForms hoc ca cao) hoc khng c bt k giao din ngi dng tt c, i khi c mt cn phi th mt s Hm mt cch nhanh chng v hin th cc kt qu. y l nhng g cc m-un HighGUI c thit k cho. N cung cp giao din d dng : To ra v thao tc cc ca s c th hin th hnh nh v "nh" ni dung ca h (khng c cn phi x l sn li cc s kin t h iu hnh) Thm trackbars cc ca s, x l cc s kin chut n gin cng nh commmands bn phm c v ghi cc hnh nh n / t a hoc b nh. c video t my nh hoc tp tin v ghi video vo mt tp tin. 4.1 Giao din ngi dng cvConvertImage Chuyn i mt hnh nh khc vi mt lt dc ty chn. void cvConvertImage( const CvArr* src, CvArr* dst, int flags=0 ); src Ngun hnh nh. dst im n hnh nh. Phi l mt knh hoc 3-knh 8-bit hnh nh. flags Cc c hot ng: CV_CVTIMG_FLIP_lt cc hnh nh theo chiu dc CV_CVTIMG_SWAP_RB Hon i cc knh mu v mu xanh. Trong hnh nh mu sc OpenCV BGR knh t, tuy nhin trn mt s h thng trt t cn phi c o ngc trc khi hin th hnh nh (ShowImage lm iu ny t ng). CvConvertImage Hm chuyn i mt hnh nh khc v lm bt kt qu theo chiu dc nu mong mun. Hm c s dng bi ShowImage. cvCreateTrackbar To ra mt trackbar v gn n vo ca s ch nh int cvCreateTrackbar( const char* trackbarName, const char* windowName, int* value, int count, CvTrackbarCallback onChange ); trackbarName Tn ca trackbar c to ra. windowName Tn ca ca s s c s dng nh mt ph huynh cho trackbar to ra.

153

value con tr n mt bin s nguyn, c gi tr s phn nh v tr ca thanh trt. Khi sng to, v tr thanh trt c xc nh bi bin ny. count v tr ti a ca thanh trt. Ti thiu v tr lun lun l 0. onChange Pointer Hm c gi l mi khi thay i v tr thanh trt. Hm ny nn c prototyped nh void Foo(int); c th c NULL nu gi li l khng cn thit. CvCreateTrackbar Hm to ra mt (hay cn gi l thanh trt hoc phm vi kim sot) trackbar vi quy nh tn v phm vi, ch nh mt bin c syncronized vi trackbar v tr v Specidoanh nghip FDI mt Hm gi li c gi l thay i v tr trackbar. Trackbar to ra c hin th trn nh ca ca s. CV_EXTERN_C_FUNCPTR( void (*CvTrackbarCallback)(int pos) ); cvDestroyAllWindows Ph hy tt c cc ca s HighGUI. void cvDestroyAllWindows(void); CvDestroyAllWindows Hm ph hy tt c cc HighGUI ca s m. cvDestroyWindow Ph hy mt ca s. void cvDestroyWindow( const char* name ); name Tn ca ca s b ph hy. CvDestroyWindow Hm ph hy cc ca s vi tn gi. cvGetTrackbarPos Tr v v tr trackbar. int cvGetTrackbarPos( const char* trackbarName, const char* windowName ); trackbarName Tn ca trackbar. windowName Tn ca ca s l cha m ca trackbar. CvGetTrackbarPos Hm tr v v tr hin ti ca cc trackbar quy nh. cvGetWindowHandle c x l ca ca s theo tn ca n. void* cvGetWindowHandle( const char* name ); name tn ca ca s. Cc cvGetWindowHandle Hm tr v ca s x l bn a (HWND trong trng hp Win32 v GtkWidget trong trng hp ca GTK+). cvGetWindowName Ly tn ca ca s bng cch x l ca n. 154

const char* cvGetWindowName( void* windowHandle ); windowHandle Handle ca ca s. CvGetWindowName Hm tr v tn ca ca s x l ngun gc ca n (HWND trong trng hp ca Win32 v GtkWidget trong trng hp ca GTK +). cvInitSystem Khi to HighGUI. int cvInitSystem( int argc, char** argv ); argc S ca cc i s dng lnh argv mng ca cc i s dng lnh CvInitSystem Hm khi to HighGUI. Nu n khng c gi mt cch r rng bi ngi s dng trc khi ca s u tin c to ra, n c gi l ngm sau vi argc = 0, argv = NULL. Di Win32 khng c cn phi gi n mt cch r rng. Theo X Window cc i s c th c s dng ty chnh mt nhn ca HighGUI ca s v kim sot. cvMoveWindow Thit lp v tr ca ca s. void cvMoveWindow( const char* name, int x, int y ); name tn ca ca s c di chuyn. x x phi hp ca cc gc trn cng, bn tri y y phi hp ca cc gc trn cng, bn tri CvMoveWindow Hm thay i v tr ca ca s. cvNamedWindow To mt ca s. int cvNamedWindow( const char* name, int flags ); name tn ca ca s trong ca s ch thch c th c s dng nh mt nh danh ca s. flags c ca ca s. Hin nay, l c duy nht c h tr l CV_WINDOW_AUTOSIZE. Nu y l thit lp, kch thc ca s c t ng iu chnh ph hp vi hnh nh hin th (xem ShowImage), v ngi dng khng th thay i kch thc ca s bng tay. CvNamedWindow Hm to ra mt ca s m c th c s dng nh mt trnh gi ch cho la tui v trackbars. Ca s to ra c gi bng tn ca h. Nu mt ca s vi cng tn tn ti, Hm khng c g. cvResizeWindow Thit lp kch thc ca s. void cvResizeWindow( const char* name, int width, int height ); name tn ca ca s thay i kch c. width chiu rng height chiu cao 155

CvResizeWindow Hm thay i kch thc ca ca s. cvSetMouseCallback Gn gi li cho cc s kin chut. void cvSetMouseCallback( const char* windowName, CvMouseCallback onMouse, void* param=NULL ); windowName Tn ca ca s. onMouse con tr n cc Hm c gi l mi khi mt s kin chut xy ra trong cc quy nh ca s. Hm ny nn c prototyped nh void Foo(int event, int x, int y, int flags, void* param); ni event l mt trong CV_EVENT_*, x v y l ng phi ca con tr chut trong h ta hnh nh (khng phi ca s ta ), flags l mt s kt hp ca CV_EVENT_FLAG_*, v param l ngi dng nh ngha tham s c truyn cho cc cvSetMouseCallback Hm cuc gi. param ngi s dng xc nh tham s c thng qua vi Hm gi li. CvSetMouseCallback Hm thit lp cc Hm gi li cho cc s kin chut xy ra trong ca s ch nh. Tham s s kin l mt trong nhng: CV_EVENT_MOUSEMOVE chut phong tro CV_EVENT_LBUTTONDOWN li nt xung CV_EVENT_RBUTTONDOWN phi xung CV_EVENT_MBUTTONDOWN Trung nt xung CV_EVENT_LBUTTONUP li nt ln CV_EVENT_RBUTTONUP phi nt ln CV_EVENT_MBUTTONUP Trung nt ln CV_EVENT_LBUTTONDBLCLK Cn li bm vo nt tng gp i CV_EVENT_RBUTTONDBLCLK nt phi nhn p chut CV_EVENT_MBUTTONDBLCLK Trung nt nhp p chut vo Cc tham s flags l mt s kt hp ca: CV_EVENT_FLAG_LBUTTON tri nt nhn CV_EVENT_FLAG_RBUTTON phi nt nhn CV_EVENT_FLAG_MBUTTON Trung nt nhn CV_EVENT_FLAG_CTRLKEY iu khin phm nhn CV_EVENT_FLAG_SHIFTKEY phm Shift p CV_EVENT_FLAG_ALTKEY Alt phm c nhn cvSetTrackbarPos Thit lp v tr trackbar. void cvSetTrackbarPos( const char* trackbarName, const char* windowName, int pos ); 156

trackbarName Tn ca trackbar. windowName Tn ca ca s l m ca trackbar. pos v tr mi. CvSetTrackbarPos Hm thit lp v tr ca cc trackbar quy nh. cvShowImage Hin th hnh nh trong ca s ch nh void cvShowImage( const char* name, const CvArr* image ); name tn ca ca s. image hnh nh c hin th. CvShowImage Hm hin th hnh nh trong ca s ch nh. Nu ca s c to ra vi c CV WINDOW AUTOSIZE sau hnh nh c hin th vi kch thc ban u ca n, khn ngoan l hnh nh thu nh ph hp vi ca s. Hm c th quy m hnh nh, ty thuc vo ca n su: Nu hnh nh 8-bit khng du, n c hin th nh l. Nu hnh nh l 16-bit s nguyn khng du 32-bit, cc im nh c chia cho 256. l, phm vi gi tr [0255 * 256] c nh x ti [0255]. Nu hnh nh l 32-bit floating-point, cc gi tr pixel c nhn vi 255. l, gi tr phm vi [0,1] l nh x ti [0255]. cvWaitKey Ch i cho mt phm c nhn. int cvWaitKey( int delay=0 ); delay dng trong mili giy. CvWaitKey Hm ch i cho s kin quan trng v hn (delay<= 0) hoc delay phn nghn giy. Tr v m ca phm c nhn hoc -1 nu khng c phm no c nhn trc khi thi gian quy nh tri qua. Lu : Hm ny l phng php duy nht trong HighGUI c th ly v x l cc s kin, v vy n cn c gi l nh k cho x l s kin bnh thng, tr khi HighGUI c s dng trong mt s mi trng chm sc x l s kin. 4.2 c / ghi hnh nh v video cvLoadImage Ti mt hnh nh t mt tp tin nh mt IplImage. IplImage* cvLoadImage( const char* filename, int iscolor=CV LOAD IMAGE COLOR ); filename tin Tn ca tp tin c np. iscolor loi mu sc c th ca hnh nh c ti: 157

CV_LOAD_IMAGE_COLOR cc hnh nh c ti buc phi l mt hnh nh 3-knh mu CV_LOAD_IMAGE_GRAYSCALE hnh nh c ti buc phi l mu xm CV_LOAD_IMAGE_UNCHANGED hnh nh c ti s c np nh l. CvLoadImage Hm ti hnh nh t cc tp tin c ch nh v tr v con tr n hnh nh c ti. Hin ti, nh dng tp tin sau y c h tr: Windows bitmap - BMP, DIB . JPEG file JPEG, JPG, JPE Thit b ha - PNG Thit b hnh nh nh dng - PBM, PGM, PPM . Sun rasters - SR, RAS . TIFF file TIFF, TIF Lu rng trong vic thc hin hin ti, cc knh alpha, nu c, c tc t u ra hnh nh, v d: 4-knh hnh nh RGBA s c np l RGB. cvLoadImageM Ti mt hnh nh t mt tp tin nh mt CvMat. CvMat* cvLoadImageM( const char* filename, int iscolor=CV LOAD IMAGE COLOR ); filename Tn ca tp tin c np. iscolor loi mu sc c th ca hnh nh c ti: CV_LOAD_IMAGE_COLOR cc hnh nh c ti buc phi l mt hnh nh 3-knh mu CV_LOAD_IMAGE_GRAYSCALE hnh nh c ti buc phi l mu xm CV_LOAD_IMAGE_UNCHANGED hnh nh c ti s c np nh l. CvLoadImageM Hm ti hnh nh t cc tp tin c ch nh v tr v con tr hnh nh c ti. urrently cc nh dng tp tin sau y c h tr: Windows bitmap - BMP, DIB . JPEG file JPEG, JPG, JPE Thit b ha - PNG Thit b hnh nh nh dng - PBM, PGM, PPM . Sun rasters - SR, RAS . TIFF file TIFF, TIF Lu rng trong vic thc hin hin ti, cc knh alpha, nu c, c tc t u ra hnh nh, v d: 4-knh hnh nh RGBA s c np l RGB. cvSaveImage Tit kim mt hnh nh vo mt tp tin c ch nh. int cvSaveImage( const char* filename, const CvArr* image ); filename Tn ca tp tin. 158

image hnh nh c lu li. CvSaveImage Hm lu li hnh nh cc tp tin c ch nh. Cc nh dng hnh nh c chn da trn phn m rng filename, xem LoadImage. Ch c duy nht 8-bit knh, 3 knh (vi BGR 'knh theo th t) hnh nh c th c lu bng cch s dng Hm ny. Nu nh dng, su hoc knh th t l khc nhau, s dng cvCvtScale v cvCvtColor chuyn i n trc khi lu, hoc s dng ph qut cvSave lu cc hnh nh nh dng XML hoc YAML. CvCapture Quay video cu trc. typedef struct CvCapture CvCapture; Cc CvCapture cu trc khng c mt giao din cng chng v ch c s dng nh mt tham s cho cc Hm quay video. cvCaptureFromCAM Khi to thu gi mt on video t my nh. CvCapture* cvCaptureFromCAM( int index ); index ch s ca my nh c s dng. Nu ch c mt my nh hoc n khng c vn g my nh c s dng -1 c th c thng qua. CvCaptureFromCAM Hm phn b v khi to cu trc CvCapture c hnh nh video t my nh. Hin nay, hai giao din my nh c th c s dng trn Windows: Video cho Windows (VFW) v hnh nh Th vin Matrox (MIL) v hai trn Linux: V4L v FireWire (IEEE1394). pht hnh cu trc, s dng ReleaseCapture. cvCaptureFromFile Khi to thu gi mt on video t mt tp tin. CvCapture* cvCaptureFromFile( const char* filename ); filename Tn ca file video. CvCaptureFromFile Hm phn b v khi to cc cu trc CvCapture c cc dng video t cc tp tin c ch nh. Codec v cc nh dng file c h tr ph thuc th vin kt thc tr li. Ngy ca Windows HighGui s dng video cho Windows (VFW), trn Linux ffmpeg c s dng v trn Mac OS X kt thc tr li QuickTime. Xem VideoCodecs i vi mt s tho lun v nhng g mong i v lm th no chun b cc tp tin video ca bn. Sau khi c cu phn b khng c s dng na, n nn c pht hnh bi cc ReleaseCapture Hm. cvGetCaptureProperty c tnh cht quay video. double cvGetCaptureProperty( CvCapture* capture, int property id ); capture cu trc quay video. 159

property_id ti sn s hu. C th mt trong nhng iu sau y: CV_CAP_PROP_POS_MSEC mili giy Phim v tr hin ti trong mili giy hoc du thi gian quay video CV_CAP_PROP_POS_FRAMES 0-da trn ch s ca khung hnh c gii m / bt tip theo CV_CAP_PROP_POS_AVI_RATIO tng i v tr ca file video (0 - bt u ca b phim, 1 kt thc ca b phim) CV_CAP_PROP_FRAME_WIDTH Chiu rng ca khung hnh trong dng video CV_CAP_PROP_FRAME_HEIGHT Chiu cao ca khung hnh trong dng video CV_CAP_PROP_FPS Frame rate CV_CAP_PROP_FOURCC 4-m k t ca cc codec CV_CAP_PROP_FRAME_COUNT S khung hnh trong cc file video CV_CAP_PROP_BRIGHTNESS sng ca hnh nh (ch dnh cho my nh) CV_CAP_PROP_CONTRAST tng phn ca hnh nh (ch dnh cho my nh) CV_CAP_PROP_SATURATION bo ha ca hnh nh (ch dnh cho my nh) CV_CAP_PROP_HUE HUE ca hnh nh (ch dnh cho my nh) CvGetCaptureProperty Hm ly ti sn quy nh ca my nh hoc video tp tin. cvGrabFrame Ly cc khung hnh t my nh hoc tp tin. int cvGrabFrame( CvCapture* capture ); capture cu trc quay video. CvGrabFrame Hm ly khung hnh t my nh hoc tp tin. Khung nm ly lu tr ni b. Mc ch ca Hm ny l ly khung hnh mt cch nhanh chng syncronization c th xy ra nu n c c t mt s my nh cng mt lc. Cc khung hnh nm ly khng tip xc bi v h c th c lu tr trong mt nh dng nn (nh c nh ngha bi cc my nh / driver). ly khung nm ly, RetrieveFrame nn c s dng. cvQueryFrame Ly v tr v mt khung hnh t my nh hoc tp tin. IplImage* cvQueryFrame( CvCapture* capture ); capture cu trc quay video. CvQueryFrame Hm ly mt khung hnh t mt file my nh hoc video, gii nn n v tr v n. Hm ny ch l mt s kt hp ca GrabFrame v RetrieveFrame, nhng trong mt cuc gi. Nhng hnh nh tr v khng nn c pht hnh hoc sa i bi ngi s dng. Trong trng hp li, tr li gi tr c th c NULL. cvReleaseCapture Pht hnh cc cu trc CvCapture. void cvReleaseCapture( CvCapture** capture ); capture con tr cu trc chp video. 160

Hm cvReleaseCapture pht hnh cu trc CvCapture giao CaptureFromFile hoc CaptureFromCAM. cvRetrieveFrame c cc hnh nh chp ly vi cvGrabFrame. IplImage* cvRetrieveFrame( CvCapture* capture ); capture cu trc quay video. CvRetrieveFrame Hm tr v con tr n hnh nh nm ly vi GrabFrame Hm. Nhng hnh nh tr v khng nn c pht hnh hoc sa i bi ngi s dng. Trong trng hp ca mt li, gi tr tr v c th l NULL. cvSetCaptureProperty Thit lp thuc tnh quay video. int cvSetCaptureProperty( CvCapture* capture, int property id, double value ); capture cu trc quay video. property_id ti sn s hu. C th mt trong nhng iu sau y: CV_CAP_PROP_POS_MSEC mili giy Phim v tr hin ti trong mili giy hoc du thi gian quay video CV_CAP_PROP_POS_FRAMES 0-da trn ch s ca khung hnh c gii m / bt tip theo CV_CAP_PROP_POS_AVI_RATIO tng i v tr ca file video (0 - bt u ca b phim, 1 kt thc ca b phim) CV_CAP_PROP_FRAME_WIDTH Chiu rng ca khung hnh trong dng video CV_CAP_PROP_FRAME_HEIGHT Chiu cao ca khung hnh trong dng video CV_CAP_PROP_FPS Frame rate CV_CAP_PROP_FOURCC 4-m k t ca cc codec CV_CAP_PROP_BRIGHTNESS sng ca hnh nh (ch dnh cho my nh) CV_CAP_PROP_CONTRAST tng phn ca hnh nh (ch dnh cho my nh) CV_CAP_PROP_SATURATION bo ha ca hnh nh (ch dnh cho my nh) CV_CAP_PROP_HUE HUE ca hnh nh (ch dnh cho my nh) value gi tr ca ti sn. CvSetCaptureProperty Hm thit lp cc ti sn quy nh thu video. Currently Hm h tr cc file video ch: CV_CAP_PROP_POS_MSEC , CV_CAP_PROP_POS_FRAMES, CV_CAP_PROP_POS_AVI_RATIO. NB Hm ny hin ang khng lm g khi s dng ti v CVS mi nht trn Linux FFMPEG (cc ni dung Hm n nu 0 c s dng v tr li). cvCreateVideoWriter 161

To ra cc nh vn tp tin video. typedef struct CvVideoWriter CvVideoWriter; CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc, double fps, CvSize frame size, int is color=1 ); filename Tn ca cc tp tin video u ra. fourcc 4-m k t ca cc codec c s dng nn cc khung hnh. V d, CV_FOURCC ('P', 'I', 'M', '1') l mt MPEG-1 codec, CV FOURCC ('M', 'J', 'P', 'G') l mt chuyn ng-jpeg m ha ... Theo Win32 c th vt qua -1 la chn phng php nn v thm compression cc thng s t hp thoi. Di Win32, nu 0 c thng qua trong khi s dng mt tn tp tin avi n s to ra mt nh vn video to ra mt file avi khng nn. fps khung hnh ca cc dng video to ra. frame_size kch thc ca khung hnh video. is_color Nu n khng phi l khng, cc b m ha s mong i v m ha cc khung mu, nu khng n s lm vic vi mu xm khung (l c hin ang c h tr trn ch dnh cho Windows). CvCreateVideoWriter Hm to ra cc cu trc nh vn video. Codec v cc nh dng file c h tr ph thuc vo cc th vin kt thc tr li. Trn Windows HighGui s dng Video cho Windows (VFW), trn Linux ffmpeg c s dng v trn Mac OS X kt thc tr li l QuickTime. Xem VideoCodecs i vi mt s tho lun v nhng g mong i. cvReleaseVideoWriter Pht hnh cc nh vn AVI. void cvReleaseVideoWriter( CvVideoWriter** writer ); writer Pointer cu trc nh vn tp tin video. CvReleaseVideoWriter Hm kt thc bng vn bn cho tp tin video v pht hnh, cu truc. cvWriteFrame Vit mt khung vo mt tp tin video. int cvWriteFrame( CvVideoWriter* writer, const IplImage* image ); writer Video ca cu trc image Khung bng vn bn CvWriteFrame Hm vit / gn thm mt khung hnh vo mt tp tin video.

162

163