1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
3 <link rel="STYLESHEET" href="opencvref.css" charset="ISO-8859-1" type="text/css">
4 <title>CXCORE Reference Manual</title>
7 <h1>CXCORE Reference Manual</h1>
10 <li><a href="#cxcore_basic_structures">Basic Structures</a>
11 <li><a href="#cxcore_arrays">Operations on Arrays</a>
13 <li><a href="#cxcore_arrays_alloc_free">Initialization</a>
14 <li><a href="#cxcore_arrays_get_set">Accessing Elements and sub-Arrays</a>
15 <li><a href="#cxcore_arrays_copying">Copying and Filling</a>
16 <li><a href="#cxcore_arrays_permute">Transforms and Permutations</a>
17 <li><a href="#cxcore_arrays_arithm_logic">Arithmetic, Logic and Comparison</a>
18 <li><a href="#cxcore_arrays_stat">Statistics</a>
19 <li><a href="#cxcore_arrays_matrix">Linear Algebra</a>
20 <li><a href="#cxcore_arrays_math">Math Functions</a>
21 <li><a href="#cxcore_arrays_rng">Random Number Generation</a>
22 <li><a href="#cxcore_arrays_dxt">Discrete Transforms</a>
24 <li><a href="#cxcore_ds">Dynamic Structures</a>
26 <li><a href="#cxcore_ds_storages">Memory Storages</a>
27 <li><a href="#cxcore_ds_sequences">Sequences</a>
28 <li><a href="#cxcore_ds_sets">Sets</a>
29 <li><a href="#cxcore_ds_graphs">Graphs</a>
30 <li><a href="#cxcore_ds_trees">Trees</a>
32 <li><a href="#cxcore_drawing">Drawing Functions</a>
34 <li><a href="#cxcore_drawing_shapes">Curves and Shapes</a>
35 <li><a href="#cxcore_drawing_text">Text</a>
36 <li><a href="#cxcore_drawing_seq">Point Sets and Contours</a>
38 <li><a href="#cxcore_persistence">Data Persistence and RTTI</a>
40 <li><a href="#cxcore_persistence_ds">File Storage</a>
41 <li><a href="#cxcore_persistence_writing">Writing Data</a>
42 <li><a href="#cxcore_persistence_reading">Reading Data</a>
43 <li><a href="#cxcore_persistence_rtti">RTTI and Generic Functions</a>
45 <li><a href="#cxcore_misc">Miscellaneous Functions</a>
46 <li><a href="#cxcore_system">Error Handling and System Functions</a>
48 <li><a href="#cxcore_system_error">Error Handling</a>
49 <li><a href="#cxcore_system_sys">System Functions</a>
51 <li><a href="#cxcore_func_index">Alphabetical List of Functions</a>
52 <li><a href="#cxcore_sample_index">List of Examples</a>
56 <!-- *****************************************************************************************
57 *****************************************************************************************
58 ***************************************************************************************** -->
60 <hr><h1><a name="cxcore_basic_structures">Basic Structures</a></h1>
62 <hr><h3><a name="decl_CvPoint">CvPoint</a></h3>
63 <p class="Blurb">2D point with integer coordinates</p>
65 typedef struct CvPoint
67 int x; /* x-coordinate, usually zero-based */
68 int y; /* y-coordinate, usually zero-based */
72 /* the constructor function */
73 inline CvPoint cvPoint( int x, int y );
75 /* conversion from CvPoint2D32f */
76 inline CvPoint cvPointFrom32f( CvPoint2D32f point );
79 <hr><h3><a name="decl_CvPoint2D32f">CvPoint2D32f</a></h3>
80 <p class="Blurb">2D point with floating-point coordinates</p>
82 typedef struct CvPoint2D32f
84 float x; /* x-coordinate, usually zero-based */
85 float y; /* y-coordinate, usually zero-based */
89 /* the constructor function */
90 inline CvPoint2D32f cvPoint2D32f( double x, double y );
92 /* conversion from CvPoint */
93 inline CvPoint2D32f cvPointTo32f( CvPoint point );
97 <hr><h3><a name="decl_CvPoint3D32f">CvPoint3D32f</a></h3>
98 <p class="Blurb">3D point with floating-point coordinates</p>
100 typedef struct CvPoint3D32f
102 float x; /* x-coordinate, usually zero-based */
103 float y; /* y-coordinate, usually zero-based */
104 float z; /* z-coordinate, usually zero-based */
108 /* the constructor function */
109 inline CvPoint3D32f cvPoint3D32f( double x, double y, double z );
113 <hr><h3><a name="decl_CvPoint2D64f">CvPoint2D64f</a></h3>
114 <p class="Blurb">2D point with double precision floating-point coordinates</p>
116 typedef struct CvPoint2D64f
118 double x; /* x-coordinate, usually zero-based */
119 double y; /* y-coordinate, usually zero-based */
123 /* the constructor function */
124 inline CvPoint2D64f cvPoint2D64f( double x, double y );
126 /* conversion from CvPoint */
127 inline CvPoint2D64f cvPointTo64f( CvPoint point );
131 <hr><h3><a name="decl_CvPoint3D64f">CvPoint3D64f</a></h3>
132 <p class="Blurb">3D point with double precision floating-point coordinates</p>
134 typedef struct CvPoint3D64f
136 double x; /* x-coordinate, usually zero-based */
137 double y; /* y-coordinate, usually zero-based */
138 double z; /* z-coordinate, usually zero-based */
142 /* the constructor function */
143 inline CvPoint3D64f cvPoint3D64f( double x, double y, double z );
147 <hr><h3><a name="decl_CvSize">CvSize</a></h3>
148 <p class="Blurb">pixel-accurate size of a rectangle</p>
150 typedef struct CvSize
152 int width; /* width of the rectangle */
153 int height; /* height of the rectangle */
157 /* the constructor function */
158 inline CvSize cvSize( int width, int height );
162 <hr><h3><a name="decl_CvSize2D32f">CvSize2D32f</a></h3>
163 <p class="Blurb">sub-pixel accurate size of a rectangle</p>
165 typedef struct CvSize2D32f
167 float width; /* width of the box */
168 float height; /* height of the box */
172 /* the constructor function */
173 inline CvSize2D32f cvSize2D32f( double width, double height );
177 <hr><h3><a name="decl_CvRect">CvRect</a></h3>
178 <p class="Blurb">offset and size of a rectangle</p>
180 typedef struct CvRect
182 int x; /* x-coordinate of the left-most rectangle corner[s] */
183 int y; /* y-coordinate of the top-most or bottom-most
184 rectangle corner[s] */
185 int width; /* width of the rectangle */
186 int height; /* height of the rectangle */
190 /* the constructor function */
191 inline CvRect cvRect( int x, int y, int width, int height );
195 <hr><h3><a name="decl_CvScalar">CvScalar</a></h3>
196 <p class="Blurb">A container for 1-,2-,3- or 4-tuples of numbers</p>
198 typedef struct CvScalar
204 /* the constructor function: initializes val[0] with val0, val[1] with val1 etc. */
205 inline CvScalar cvScalar( double val0, double val1=0,
206 double val2=0, double val3=0 );
207 /* the constructor function: initializes val[0]...val[3] with val0123 */
208 inline CvScalar cvScalarAll( double val0123 );
210 /* the constructor function: initializes val[0] with val0, val[1]...val[3] with zeros */
211 inline CvScalar cvRealScalar( double val0 );
215 <hr><h3><a name="decl_CvTermCriteria">CvTermCriteria</a></h3>
216 <p class="Blurb">Termination criteria for iterative algorithms</p>
218 #define CV_TERMCRIT_ITER 1
219 #define CV_TERMCRIT_NUMBER CV_TERMCRIT_ITER
220 #define CV_TERMCRIT_EPS 2
222 typedef struct CvTermCriteria
224 int type; /* a combination of CV_TERMCRIT_ITER and CV_TERMCRIT_EPS */
225 int max_iter; /* maximum number of iterations */
226 double epsilon; /* accuracy to achieve */
230 /* the constructor function */
231 inline CvTermCriteria cvTermCriteria( int type, int max_iter, double epsilon );
233 /* check termination criteria and transform it so that type=CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,
234 and both max_iter and epsilon are valid */
235 CvTermCriteria cvCheckTermCriteria( CvTermCriteria criteria,
237 int default_max_iters );
241 <!-- *****************************************************************************************
242 *****************************************************************************************
243 ***************************************************************************************** -->
245 <!-- <hr><h2><a name="ch1_array_structs">Array structures</a></h2> -->
247 <hr><h3><a name="decl_CvMat">CvMat</a></h3>
248 <p class="Blurb">Multi-channel matrix</p>
252 int type; /* CvMat signature (CV_MAT_MAGIC_VAL), element type and flags */
253 int step; /* full row length in bytes */
255 int* refcount; /* underlying data reference counter */
264 } data; /* data pointers */
279 int rows; /* number of rows */
280 int cols; /* number of columns */
287 <hr><h3><a name="decl_CvMatND">CvMatND</a></h3>
288 <p class="Blurb">Multi-dimensional dense multi-channel array</p>
290 typedef struct CvMatND
292 int type; /* CvMatND signature (CV_MATND_MAGIC_VAL), element type and flags */
293 int dims; /* number of array dimensions */
295 int* refcount; /* underlying data reference counter */
304 } data; /* data pointers */
306 /* pairs (number of elements, distance between elements in bytes) for
319 <hr><h3><a name="decl_CvSparseMat">CvSparseMat</a></h3>
320 <p class="Blurb">Multi-dimensional sparse multi-channel array</p>
322 typedef struct CvSparseMat
324 int type; /* CvSparseMat signature (CV_SPARSE_MAT_MAGIC_VAL), element type and flags */
325 int dims; /* number of dimensions */
326 int* refcount; /* reference counter - not used */
327 struct CvSet* heap; /* a pool of hashtable nodes */
328 void** hashtable; /* hashtable: each entry has a list of nodes
329 having the same "hashvalue modulo hashsize" */
330 int hashsize; /* size of hashtable */
331 int total; /* total number of sparse array nodes */
332 int valoffset; /* value offset in bytes for the array nodes */
333 int idxoffset; /* index offset in bytes for the array nodes */
334 int size[CV_MAX_DIM]; /* arr of dimension sizes */
340 <hr><h3><a name="decl_IplImage">IplImage</a></h3>
341 <p class="Blurb">IPL image header</p>
343 typedef struct _IplImage
345 int nSize; /* sizeof(IplImage) */
346 int ID; /* version (=0)*/
347 int nChannels; /* Most of OpenCV functions support 1,2,3 or 4 channels */
348 int alphaChannel; /* ignored by OpenCV */
349 int depth; /* pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16U,
350 IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported */
351 char colorModel[4]; /* ignored by OpenCV */
352 char channelSeq[4]; /* ditto */
353 int dataOrder; /* 0 - interleaved color channels, 1 - separate color channels.
354 cvCreateImage can only create interleaved images */
355 int origin; /* 0 - top-left origin,
356 1 - bottom-left origin (Windows bitmaps style) */
357 int align; /* Alignment of image rows (4 or 8).
358 OpenCV ignores it and uses widthStep instead */
359 int width; /* image width in pixels */
360 int height; /* image height in pixels */
361 struct _IplROI *roi;/* image ROI. when it is not NULL, this specifies image region to process */
362 struct _IplImage *maskROI; /* must be NULL in OpenCV */
363 void *imageId; /* ditto */
364 struct _IplTileInfo *tileInfo; /* ditto */
365 int imageSize; /* image data size in bytes
366 (=image->height*image->widthStep
367 in case of interleaved data)*/
368 char *imageData; /* pointer to aligned image data */
369 int widthStep; /* size of aligned image row in bytes */
370 int BorderMode[4]; /* border completion mode, ignored by OpenCV */
371 int BorderConst[4]; /* ditto */
372 char *imageDataOrigin; /* pointer to a very origin of image data
373 (not necessarily aligned) -
374 it is needed for correct image deallocation */
379 The structure <code>IplImage</code> came from <em>Intel Image Processing Library</em> where
380 the format is native. OpenCV supports only a subset of possible <code>IplImage</code> formats:
382 <li><code>alphaChannel</code> is ignored by OpenCV.
383 <li><code>colorModel</code> and <code>channelSeq</code> are ignored by OpenCV. The single OpenCV function
384 <a href="#decl_cvCvtColor">cvCvtColor</a> working with color spaces takes the source and destination color spaces
386 <li><code>dataOrder</code> must be IPL_DATA_ORDER_PIXEL (the color channels are interleaved), however
387 selected channels of planar images can be processed as well if COI is set.
388 <li><code>align</code> is ignored by OpenCV, while <code>widthStep</code> is used to access to subsequent image rows.
389 <li><code>maskROI</code> is not supported. The function that can work with mask take it as a
390 separate parameter. Also the mask in OpenCV is 8-bit, whereas in IPL it is 1-bit.
391 <li><code>tileInfo</code> is not supported.
392 <li><code>BorderMode</code> and <code>BorderConst</code> are not supported. Every OpenCV function
393 working with a pixel neighborhood uses a single hard-coded border mode (most often, replication).
395 Besides the above restrictions, OpenCV handles ROI differently. It requires that the sizes or ROI sizes of
396 all source and destination images match exactly (according to the operation, e.g. for <a href="#decl_cvPyrDown">cvPyrDown</a>
397 destination width(height) must be equal to source width(height) divided by 2 ±1),
398 whereas IPL processes the intersection area - that is, the sizes or ROI sizes of all images may
403 <hr><h3><a name="decl_CvArr">CvArr</a></h3>
404 <p class="Blurb">Arbitrary array</p>
409 The metatype <code>CvArr*</code> is used <em>only</em> as a function parameter to
410 specify that the function accepts arrays of more than a
411 single type, for example IplImage*, CvMat* or even CvSeq*. The particular array
412 type is determined at runtime by analysing the first 4 bytes of the header.
415 <!-- *****************************************************************************************
416 *****************************************************************************************
417 ***************************************************************************************** -->
419 <hr><h1><a name="cxcore_arrays">Operations on Arrays</a></h1>
420 <hr><h2><a name="cxcore_arrays_alloc_free">Initialization</a></h2>
422 <hr><h3><a name="decl_cvCreateImage">CreateImage</a></h3>
423 <p class="Blurb">Creates header and allocates data</p>
425 IplImage* cvCreateImage( CvSize size, int depth, int channels );
427 <dt>size<dd>Image width and height.
428 <dt>depth<dd>Bit depth of image elements. Can be one of:<br>
429 IPL_DEPTH_8U - unsigned 8-bit integers<br>
430 IPL_DEPTH_8S - signed 8-bit integers<br>
431 IPL_DEPTH_16U - unsigned 16-bit integers<br>
432 IPL_DEPTH_16S - signed 16-bit integers<br>
433 IPL_DEPTH_32S - signed 32-bit integers<br>
434 IPL_DEPTH_32F - single precision floating-point numbers<br>
435 IPL_DEPTH_64F - double precision floating-point numbers<br>
436 <dt>channels<dd>Number of channels per element(pixel). Can be 1, 2, 3 or 4.
437 The channels are interleaved, for example the
438 usual data layout of a color image is:<br>
439 b0 g0 r0 b1 g1 r1 ...<br>
440 Although in general IPL image format can store
441 non-interleaved images as well and some of OpenCV
442 can process it, this function can create interleaved images
445 The function <code>cvCreateImage</code> creates the header and allocates data. This call is a
448 header = cvCreateImageHeader(size,depth,channels);
449 cvCreateData(header);
454 <hr><h3><a name="decl_cvCreateImageHeader">CreateImageHeader</a></h3>
455 <p class="Blurb">Allocates, initializes, and returns structure IplImage</p>
457 IplImage* cvCreateImageHeader( CvSize size, int depth, int channels );
459 <dt>size<dd>Image width and height.
460 <dt>depth<dd>Image depth (see CreateImage).
461 <dt>channels<dd>Number of channels (see CreateImage).
463 The function <code>cvCreateImageHeader</code> allocates, initializes, and returns the structure
464 <code>IplImage</code>. This call is an analogue of
466 iplCreateImageHeader( channels, 0, depth,
467 channels == 1 ? "GRAY" : "RGB",
468 channels == 1 ? "GRAY" : channels == 3 ? "BGR" :
469 channels == 4 ? "BGRA" : "",
470 IPL_DATA_ORDER_PIXEL, IPL_ORIGIN_TL, 4,
471 size.width, size.height,
474 though it does not use IPL functions by default
475 (see also <code>CV_TURN_ON_IPL_COMPATIBILITY</code> macro)
479 <hr><h3><a name="decl_cvReleaseImageHeader">ReleaseImageHeader</a></h3>
480 <p class="Blurb">Releases header</p>
482 void cvReleaseImageHeader( IplImage** image );
484 <dt>image<dd>Double pointer to the deallocated header.
486 The function <code>cvReleaseImageHeader</code> releases the header.
487 This call is an analogue of
491 iplDeallocate( *image, IPL_IMAGE_HEADER | IPL_IMAGE_ROI );
495 though it does not use IPL functions by default
496 (see also <code>CV_TURN_ON_IPL_COMPATIBILITY</code>)
500 <hr><h3><a name="decl_cvReleaseImage">ReleaseImage</a></h3>
501 <p class="Blurb">Releases header and image data</p>
503 void cvReleaseImage( IplImage** image );
505 <dt>image<dd>Double pointer to the header of the deallocated image.
507 The function <code>cvReleaseImage</code> releases the header and the image data. This call is a
512 cvReleaseData( *image );
513 cvReleaseImageHeader( image );
519 <hr><h3><a name="decl_cvInitImageHeader">InitImageHeader</a></h3>
520 <p class="Blurb">Initializes allocated by user image header</p>
522 IplImage* cvInitImageHeader( IplImage* image, CvSize size, int depth,
523 int channels, int origin=0, int align=4 );
525 <dt>image<dd>Image header to initialise.
526 <dt>size<dd>Image width and height.
527 <dt>depth<dd>Image depth (see CreateImage).
528 <dt>channels<dd>Number of channels (see CreateImage).
529 <dt>origin<dd><code>IPL_ORIGIN_TL</code> or <code>IPL_ORIGIN_BL</code>.
530 <dt>align<dd>Alignment for image rows, typically 4 or 8 bytes.
532 The function <code>cvInitImageHeader</code> initializes the image header structure,
533 pointer to which is passed by the user, and returns the pointer.
537 <hr><h3><a name="decl_cvCloneImage">CloneImage</a></h3>
538 <p class="Blurb">Makes a full copy of image</p>
540 IplImage* cvCloneImage( const IplImage* image );
542 <dt>image<dd>Original image.
544 The function <code>cvCloneImage</code> makes a full copy of the image including
549 <hr><h3><a name="decl_cvSetImageCOI">SetImageCOI</a></h3>
550 <p class="Blurb">Sets channel of interest to given value</p>
552 void cvSetImageCOI( IplImage* image, int coi );
554 <dt>image<dd>Image header.
555 <dt>coi<dd>Channel of interest.
557 The function <code>cvSetImageCOI</code> sets the channel of interest
558 to a given value. Value 0 means that all channels are selected,
559 1 means that the first channel is selected etc.
560 If ROI is <code>NULL</code> and <code>coi != 0</code>, ROI is allocated.
561 Note that most of OpenCV functions do not support COI, so
562 to process separate image/matrix channel one may copy (via <a href="#decl_cvCopy">cvCopy</a> or
563 <a href="#decl_cvSplit">cvSplit</a>) the channel to separate image/matrix,
564 process it and copy the result back (via <a href="#decl_cvCopy">cvCopy</a> or <a href="#decl_cvCvtPlaneToPix">cvCvtPlaneToPix</a>)
568 <hr><h3><a name="decl_cvGetImageCOI">GetImageCOI</a></h3>
569 <p class="Blurb">Returns index of channel of interest</p>
571 int cvGetImageCOI( const IplImage* image );
573 <dt>image<dd>Image header.
575 The function <code>cvGetImageCOI</code> returns channel of interest of
576 the image (it returns 0 if all the channels are selected).</p>
579 <hr><h3><a name="decl_cvSetImageROI">SetImageROI</a></h3>
580 <p class="Blurb">Sets image ROI to given rectangle</p>
582 void cvSetImageROI( IplImage* image, CvRect rect );
584 <dt>image<dd>Image header.
585 <dt>rect<dd>ROI rectangle.
587 The function <code>cvSetImageROI</code> sets the image ROI to a given rectangle. If ROI is <code>NULL</code>
588 and the value of the parameter <code>rect</code> is not equal to the whole image, ROI is
589 allocated. Unlike COI, most of OpenCV functions do support ROI and treat
590 it in a way as it would be a separate image (for example, all the pixel
591 coordinates are counted from top-left or bottom-left
592 (depending on the image origin) corner of ROI)</p>
595 <hr><h3><a name="decl_cvResetImageROI">ResetImageROI</a></h3>
596 <p class="Blurb">Releases image ROI</p>
598 void cvResetImageROI( IplImage* image );
600 <dt>image<dd>Image header.
602 The function <code>cvResetImageROI</code> releases image ROI. After that the whole image
603 is considered selected. The similar result can be achieved by</p>
605 cvSetImageROI( image, cvRect( 0, 0, image->width, image->height ));
606 cvSetImageCOI( image, 0 );
609 But the latter variant does not deallocate <code>image->roi</code>.
613 <hr><h3><a name="decl_cvGetImageROI">GetImageROI</a></h3>
614 <p class="Blurb">Returns image ROI coordinates</p>
616 CvRect cvGetImageROI( const IplImage* image );
618 <dt>image<dd>Image header.
620 The function <code>cvGetImageROI</code> returns image ROI coordinates.
621 The rectangle <a href="#decl_cvRect">cvRect</a>(0,0,image->width,image->height) is returned if
625 <hr><h3><a name="decl_cvCreateMat">CreateMat</a></h3>
626 <p class="Blurb">Creates new matrix</p>
628 CvMat* cvCreateMat( int rows, int cols, int type );
630 <dt>rows<dd>Number of rows in the matrix.
631 <dt>cols<dd>Number of columns in the matrix.
632 <dt>type<dd>Type of the matrix elements.
633 Usually it is specified in form
634 <code>CV_<bit_depth>(S|U|F)C<number_of_channels></code>, for example:<br>
635 <code>CV_8UC1</code> means an 8-bit unsigned single-channel matrix,
636 <code>CV_32SC2</code> means a 32-bit signed matrix with two channels.
638 The function <code>cvCreateMat</code> allocates header for the new matrix and underlying data,
639 and returns a pointer to the created matrix. It is a short form for:</p>
641 CvMat* mat = cvCreateMatHeader( rows, cols, type );
644 Matrices are stored row by row. All the rows are aligned by 4 bytes.
648 <hr><h3><a name="decl_cvCreateMatHeader">CreateMatHeader</a></h3>
649 <p class="Blurb">Creates new matrix header</p>
651 CvMat* cvCreateMatHeader( int rows, int cols, int type );
653 <dt>rows<dd>Number of rows in the matrix.
654 <dt>cols<dd>Number of columns in the matrix.
655 <dt>type<dd>Type of the matrix elements (see <a href="#decl_cvCreateMat">cvCreateMat</a>).
657 The function <code>cvCreateMatHeader</code> allocates new matrix header and returns pointer to
658 it. The matrix data can further be allocated using <a href="#decl_cvCreateData">cvCreateData</a> or set
659 explicitly to user-allocated data via <a href="#decl_cvSetData">cvSetData</a>.
663 <hr><h3><a name="decl_cvReleaseMat">ReleaseMat</a></h3>
664 <p class="Blurb">Deallocates matrix</p>
666 void cvReleaseMat( CvMat** mat );
668 <dt>mat<dd>Double pointer to the matrix.
670 The function <code>cvReleaseMat</code> decrements the matrix data reference counter and
671 releases matrix header:</p>
674 cvDecRefData( *mat );
675 cvFree( (void**)mat );
680 <hr><h3><a name="decl_cvInitMatHeader">InitMatHeader</a></h3>
681 <p class="Blurb">Initializes matrix header</p>
683 CvMat* cvInitMatHeader( CvMat* mat, int rows, int cols, int type,
684 void* data=NULL, int step=CV_AUTOSTEP );
686 <dt>mat<dd>Pointer to the matrix header to be initialised.
687 <dt>rows<dd>Number of rows in the matrix.
688 <dt>cols<dd>Number of columns in the matrix.
689 <dt>type<dd>Type of the matrix elements.
690 <dt>data<dd>Optional data pointer assigned to the matrix header.
691 <dt>step<dd>Full row width in bytes of the data assigned. By default, the minimal
692 possible step is used, i.e., no gaps is assumed between subsequent rows of the
695 The function <code>cvInitMatHeader</code> initializes already allocated <a href="#decl_CvMat">CvMat</a> structure. It can
696 be used to process raw data with OpenCV matrix functions.
698 For example, the following code computes matrix product of two matrices, stored
702 <font color=blue size=4>Calculating Product of Two Matrices</font></p>
704 double a[] = { 1, 2, 3, 4
708 double b[] = { 1, 5, 9,
716 cvInitMatHeader( &Ma, 3, 4, CV_64FC1, a );
717 cvInitMatHeader( &Mb, 4, 3, CV_64FC1, b );
718 cvInitMatHeader( &Mc, 3, 3, CV_64FC1, c );
720 cvMatMulAdd( &Ma, &Mb, 0, &Mc );
721 // c array now contains product of a(3x4) and b(4x3) matrices
725 <hr><h3><a name="decl_cvMat">Mat</a></h3>
726 <p class="Blurb">Initializes matrix header (light-weight variant)</p>
728 CvMat cvMat( int rows, int cols, int type, void* data=NULL );
730 <dt>rows<dd>Number of rows in the matrix.
731 <dt>cols<dd>Number of columns in the matrix.
732 <dt>type<dd>Type of the matrix elements (see CreateMat).
733 <dt>data<dd>Optional data pointer assigned to the matrix header.
735 The function <code>cvMat</code> is a fast inline substitution for <a href="#decl_cvInitMatHeader">cvInitMatHeader</a>.
736 Namely, it is equivalent to:
739 cvInitMatHeader( &mat, rows, cols, type, data, CV_AUTOSTEP );
744 <hr><h3><a name="decl_cvCloneMat">CloneMat</a></h3>
745 <p class="Blurb">Creates matrix copy</p>
747 CvMat* cvCloneMat( const CvMat* mat );
749 <dt>mat<dd>Input matrix.
751 The function <code>cvCloneMat</code> creates a copy of input matrix and returns the pointer to
756 <hr><h3><a name="decl_cvCreateMatND">CreateMatND</a></h3>
757 <p class="Blurb">Creates multi-dimensional dense array</p>
759 CvMatND* cvCreateMatND( int dims, const int* sizes, int type );
761 <dt>dims<dd>Number of array dimensions. It must not exceed CV_MAX_DIM (=32 by default,
762 though it may be changed at build time)
763 <dt>sizes<dd>Array of dimension sizes.
764 <dt>type<dd>Type of array elements. The same as for <a href="#decl_CvMat">CvMat</a>
766 The function <code>cvCreateMatND</code> allocates header for multi-dimensional dense array
767 and the underlying data, and returns pointer to the created array. It is a short form for:</p>
769 CvMatND* mat = cvCreateMatNDHeader( dims, sizes, type );
772 Array data is stored row by row. All the rows are aligned by 4 bytes.
776 <hr><h3><a name="decl_cvCreateMatNDHeader">CreateMatNDHeader</a></h3>
777 <p class="Blurb">Creates new matrix header</p>
779 CvMatND* cvCreateMatNDHeader( int dims, const int* sizes, int type );
781 <dt>dims<dd>Number of array dimensions.
782 <dt>sizes<dd>Array of dimension sizes.
783 <dt>type<dd>Type of array elements. The same as for CvMat
785 The function <code>cvCreateMatND</code> allocates header for multi-dimensional dense array.
786 The array data can further be allocated using <a href="#decl_cvCreateData">cvCreateData</a> or set
787 explicitly to user-allocated data via <a href="#decl_cvSetData">cvSetData</a>.
791 <hr><h3><a name="decl_cvReleaseMatND">ReleaseMatND</a></h3>
792 <p class="Blurb">Deallocates multi-dimensional array</p>
794 void cvReleaseMatND( CvMatND** mat );
796 <dt>mat<dd>Double pointer to the array.
798 The function <code>cvReleaseMatND</code> decrements the array data reference counter and
799 releases the array header:</p>
802 cvDecRefData( *mat );
803 cvFree( (void**)mat );
808 <hr><h3><a name="decl_cvInitMatNDHeader">InitMatNDHeader</a></h3>
809 <p class="Blurb">Initializes multi-dimensional array header</p>
811 CvMatND* cvInitMatNDHeader( CvMatND* mat, int dims, const int* sizes, int type, void* data=NULL );
813 <dt>mat<dd>Pointer to the array header to be initialized.
814 <dt>dims<dd>Number of array dimensions.
815 <dt>sizes<dd>Array of dimension sizes.
816 <dt>type<dd>Type of array elements. The same as for CvMat
817 <dt>data<dd>Optional data pointer assigned to the matrix header.
819 The function <code>cvInitMatNDHeader</code> initializes <a href="#decl_CvMatND">CvMatND</a> structure allocated by
824 <hr><h3><a name="decl_cvCloneMatND">CloneMatND</a></h3>
825 <p class="Blurb">Creates full copy of multi-dimensional array</p>
827 CvMatND* cvCloneMatND( const CvMatND* mat );
829 <dt>mat<dd>Input array.
831 The function <code>cvCloneMatND</code> creates a copy of input array and returns pointer to
836 <hr><h3><a name="decl_cvDecRefData">DecRefData</a></h3>
837 <p class="Blurb">Decrements array data reference counter</p>
839 void cvDecRefData( CvArr* arr );
841 <dt>arr<dd>array header.
843 The function <code>cvDecRefData</code> decrements <a href="#decl_CvMat">CvMat</a> or <a href="#decl_CvMatND">CvMatND</a> data reference counter if the
844 reference counter pointer is not NULL and deallocates the data if the counter reaches zero.
845 In the current implementation the reference counter is not NULL only if the data was allocated using
846 <a href="#decl_cvCreateData">cvCreateData</a> function, in other cases such as:<br>
847 external data was assigned to the header using <a href="#decl_cvSetData">cvSetData</a><br>
848 the matrix header presents a part of a larger matrix or image<br>
849 the matrix header was converted from image or n-dimensional matrix header<br>
851 the reference counter is set to NULL and thus it is not decremented.
852 Whenever the data is deallocated or not,
853 the data pointer and reference counter pointers are cleared by the function.
857 <hr><h3><a name="decl_cvIncRefData">IncRefData</a></h3>
858 <p class="Blurb">Increments array data reference counter</p>
860 int cvIncRefData( CvArr* arr );
862 <dt>arr<dd>array header.
864 The function <code>cvIncRefData</code> increments <a href="#decl_CvMat">CvMat</a> or <a href="#decl_CvMatND">CvMatND</a> data reference counter and
865 returns the new counter value if the reference counter pointer is not NULL, otherwise it returns zero.
869 <hr><h3><a name="decl_cvCreateData">CreateData</a></h3>
870 <p class="Blurb">Allocates array data</p>
872 void cvCreateData( CvArr* arr );
874 <dt>arr<dd>Array header.
876 The function <code>cvCreateData</code> allocates image, matrix or multi-dimensional array data.
877 Note that in case of matrix types OpenCV allocation functions are used and
878 in case of IplImage they are used too unless <code>CV_TURN_ON_IPL_COMPATIBILITY</code> was called.
879 In the latter case IPL functions are used to allocate the data
883 <hr><h3><a name="decl_cvReleaseData">ReleaseData</a></h3>
884 <p class="Blurb">Releases array data</p>
886 void cvReleaseData( CvArr* arr );
888 <dt>arr<dd>Array header
890 The function <code>cvReleaseData</code> releases the array data.
891 In case of <a href="#decl_CvMat">CvMat</a> or <a href="#decl_CvMatND">CvMatND</a> it simply calls cvDecRefData(), that is
892 the function can not deallocate external data. See also the note to <a href="#decl_cvCreateData">cvCreateData</a>.
896 <hr><h3><a name="decl_cvSetData">SetData</a></h3>
897 <p class="Blurb">Assigns user data to the array header</p>
899 void cvSetData( CvArr* arr, void* data, int step );
901 <dt>arr<dd>Array header.
902 <dt>data<dd>User data.
903 <dt>step<dd>Full row length in bytes.
905 The function <code>cvSetData</code> assigns user data to the array header.
906 Header should be initialized before using cvCreate*Header,
907 cvInit*Header or <a href="#decl_cvMat">cvMat</a> (in case of matrix) function.
911 <hr><h3><a name="decl_cvGetRawData">GetRawData</a></h3>
912 <p class="Blurb">Retrieves low-level information about the array</p>
914 void cvGetRawData( const CvArr* arr, uchar** data,
915 int* step=NULL, CvSize* roi_size=NULL );
917 <dt>arr<dd>Array header.
918 <dt>data<dd>Output pointer to the whole image origin or ROI origin if
920 <dt>step<dd>Output full row length in bytes.
921 <dt>roi_size<dd>Output ROI size.
923 The function <code>cvGetRawData</code> fills output variables with low-level
924 information about the array data.
925 All output parameters are optional, so some of the pointers
926 may be set to <code>NULL</code>.
927 If the array is <code>IplImage</code> with ROI set,
928 parameters of ROI are returned.
930 <p>The following example shows how to get access to array elements using
933 <font color=blue size=4>Using GetRawData to calculate absolute value of elements of
934 a single-channel floating-point array.</font>
942 cvGetRawData( array, (uchar**)&data, &step, &size );
943 step /= sizeof(data[0]);
945 for( y = 0; y < size.height; y++, data += step )
946 for( x = 0; x < size.width; x++ )
947 data[x] = (float)fabs(data[x]);
951 <hr><h3><a name="decl_cvGetMat">GetMat</a></h3>
952 <p class="Blurb">Returns matrix header for arbitrary array</p>
954 CvMat* cvGetMat( const CvArr* arr, CvMat* header, int* coi=NULL, int allowND=0 );
956 <dt>arr<dd>Input array.
957 <dt>header<dd>Pointer to <a href="#decl_CvMat">CvMat</a> structure used as a temporary buffer.
958 <dt>coi<dd>Optional output parameter for storing COI.
959 <dt>allowND<dd>If non-zero, the function accepts multi-dimensional dense
960 arrays (CvMatND*) and returns 2D (if CvMatND has two dimensions)
961 or 1D matrix (when CvMatND has 1 dimension or more than 2 dimensions).
962 The array must be continuous.
964 The function <code>cvGetMat</code> returns matrix header for the input array that can be
965 matrix - <a href="#decl_CvMat">CvMat</a>, image - <code>IplImage</code> or multi-dimensional dense array - <a href="#decl_CvMatND*">CvMatND*</a>
966 (latter case is allowed only if <code>allowND != 0</code>) .
967 In the case of matrix the function simply returns the input pointer.
968 In the case of <code>IplImage*</code> or <a href="#decl_CvMatND*">CvMatND*</a> it initializes <code>header</code> structure
969 with parameters of the current image ROI and returns pointer to this temporary
970 structure. Because COI is not supported by <a href="#decl_CvMat">CvMat</a>, it is returned separately.
972 The function provides an easy way to handle both types of array - <code>IplImage</code> and
973 <a href="#decl_CvMat">CvMat</a> -, using the same code. Reverse transform from <a href="#decl_CvMat">CvMat</a> to <code>IplImage</code> can be
974 done using <a href="#decl_cvGetImage">cvGetImage</a> function.
976 Input array must have underlying data allocated or attached, otherwise the
979 If the input array is <code>IplImage</code> with planar data layout and COI set, the function
980 returns pointer to the selected plane and COI = 0. It enables per-plane
981 processing of multi-channel images with planar data layout using OpenCV
985 <hr><h3><a name="decl_cvGetImage">GetImage</a></h3>
986 <p class="Blurb">Returns image header for arbitrary array</p>
988 IplImage* cvGetImage( const CvArr* arr, IplImage* image_header );
990 <dt>arr<dd>Input array.
991 <dt>image_header<dd>Pointer to <code>IplImage</code> structure used as a temporary buffer.
993 The function <code>cvGetImage</code> returns image header for the input array that can be
994 matrix - <a href="#decl_CvMat*">CvMat*</a>, or image - <code>IplImage*</code>. In the case of image the function simply
995 returns the input pointer. In the case of <a href="#decl_CvMat*">CvMat*</a> it initializes <code>image_header</code> structure
996 with parameters of the input matrix. Note that if we transform <code>IplImage</code> to <a href="#decl_CvMat">CvMat</a> and then transform
997 CvMat back to IplImage, we can get different headers if the ROI is set, and thus some IPL functions
998 that calculate image stride from its width and align may fail on the resultant image.</p>
1001 <hr><h3><a name="decl_cvCreateSparseMat">CreateSparseMat</a></h3>
1002 <p class="Blurb">Creates sparse array</p>
1004 CvSparseMat* cvCreateSparseMat( int dims, const int* sizes, int type );
1006 <dt>dims<dd>Number of array dimensions. As opposite to the dense matrix, the number
1007 of dimensions is practically unlimited (up to 2<sup>16</sup>).
1008 <dt>sizes<dd>Array of dimension sizes.
1009 <dt>type<dd>Type of array elements. The same as for CvMat
1011 The function <code>cvCreateSparseMat</code> allocates multi-dimensional sparse array.
1012 Initially the array contains no elements, that is <a href="#decl_cvGet*D">cvGet*D</a> or
1013 <a href="#decl_cvGetReal*D">cvGetReal*D</a> return zero for every index</p>
1016 <hr><h3><a name="decl_cvReleaseSparseMat">ReleaseSparseMat</a></h3>
1017 <p class="Blurb">Deallocates sparse array</p>
1019 void cvReleaseSparseMat( CvSparseMat** mat );
1021 <dt>mat<dd>Double pointer to the array.
1023 The function <code>cvReleaseSparseMat</code> releases the sparse array and clears the array pointer upon exit</p>
1026 <hr><h3><a name="decl_cvCloneSparseMat">CloneSparseMat</a></h3>
1027 <p class="Blurb">Creates full copy of sparse array</p>
1029 CvSparseMat* cvCloneSparseMat( const CvSparseMat* mat );
1031 <dt>mat<dd>Input array.
1033 The function <code>cvCloneSparseMat</code> creates a copy of the input array and returns pointer to the copy.</p>
1036 <hr><h2><a name="cxcore_arrays_get_set">Accessing Elements and sub-Arrays</a></h2>
1038 <hr><h3><a name="decl_cvGetSubRect">GetSubRect</a></h3>
1039 <p class="Blurb">Returns matrix header corresponding to the rectangular sub-array of input image or matrix</p>
1041 CvMat* cvGetSubRect( const CvArr* arr, CvMat* submat, CvRect rect );
1043 <dt>arr<dd>Input array.
1044 <dt>submat<dd>Pointer to the resultant sub-array header.
1045 <dt>rect<dd>Zero-based coordinates of the rectangle of interest.
1047 The function <code>cvGetSubRect</code> returns header, corresponding
1048 to a specified rectangle of the input array.
1049 In other words, it allows the user to treat a rectangular part
1050 of input array as a stand-alone array. ROI is taken into account by the function
1051 so the sub-array of ROI is actually extracted.</p>
1054 <hr><h3><a name="decl_cvGetRow">GetRow, GetRows</a></h3>
1055 <p class="Blurb">Returns array row or row span</p>
1057 CvMat* cvGetRow( const CvArr* arr, CvMat* submat, int row );
1058 CvMat* cvGetRows( const CvArr* arr, CvMat* submat, int start_row, int end_row, int delta_row=1 );
1060 <dt>arr<dd>Input array.
1061 <dt>submat<dd>Pointer to the resulting sub-array header.
1062 <dt>row<dd>Zero-based index of the selected row.
1063 <dt>start_row<dd>Zero-based index of the starting row (inclusive) of the span.
1064 <dt>end_row<dd>Zero-based index of the ending row (exclusive) of the span.
1065 <dt>delta_row<dd>Index step in the row span. That is, the function extracts every <code>delta_row</code>-th
1066 row from <code>start_row</code> and up to (but not including) <code>end_row</code>.
1068 The functions <code>GetRow</code> and <code>GetRows</code> return the header, corresponding to a specified
1069 row/row span of the input array. Note that <code>GetRow</code> is a shortcut for <a href="#decl_cvGetRows">cvGetRows</a>:
1071 cvGetRow( arr, submat, row ) ~ cvGetRows( arr, submat, row, row + 1, 1 );
1075 <hr><h3><a name="decl_cvGetCol">GetCol, GetCols</a></h3>
1076 <p class="Blurb">Returns array column or column span</p>
1078 CvMat* cvGetCol( const CvArr* arr, CvMat* submat, int col );
1079 CvMat* cvGetCols( const CvArr* arr, CvMat* submat, int start_col, int end_col );
1081 <dt>arr<dd>Input array.
1082 <dt>submat<dd>Pointer to the resulting sub-array header.
1083 <dt>col<dd>Zero-based index of the selected column.
1084 <dt>start_col<dd>Zero-based index of the starting column (inclusive) of the span.
1085 <dt>end_col<dd>Zero-based index of the ending column (exclusive) of the span.
1087 The functions <code>GetCol</code> and <code>GetCols</code> return the header, corresponding to a specified
1088 column/column span of the input array. Note that <code>GetCol</code> is a shortcut for <a href="#decl_cvGetCols">cvGetCols</a>:
1090 cvGetCol( arr, submat, col ); // ~ cvGetCols( arr, submat, col, col + 1 );
1094 <hr><h3><a name="decl_cvGetDiag">GetDiag</a></h3>
1095 <p class="Blurb">Returns one of array diagonals</p>
1097 CvMat* cvGetDiag( const CvArr* arr, CvMat* submat, int diag=0 );
1099 <dt>arr<dd>Input array.
1100 <dt>submat<dd>Pointer to the resulting sub-array header.
1101 <dt>diag<dd>Array diagonal. Zero corresponds to the main diagonal, -1 corresponds to the diagonal above
1102 the main etc., 1 corresponds to the diagonal below the main etc.
1104 The function <code>cvGetDiag</code> returns the header, corresponding to a specified
1105 diagonal of the input array.</p>
1108 <hr><h3><a name="decl_cvGetSize">GetSize</a></h3>
1109 <p class="Blurb">Returns size of matrix or image ROI</p>
1111 CvSize cvGetSize( const CvArr* arr );
1113 <dt>arr<dd>array header.
1115 The function <code>cvGetSize</code> returns number of rows (CvSize::height) and number of columns
1116 (CvSize::width) of the input matrix or image. In case of image the size of ROI is returned.</p>
1119 <hr><h3><a name="decl_cvInitSparseMatIterator">InitSparseMatIterator</a></h3>
1120 <p class="Blurb">Initializes sparse array elements iterator</p>
1122 CvSparseNode* cvInitSparseMatIterator( const CvSparseMat* mat,
1123 CvSparseMatIterator* mat_iterator );
1125 <dt>mat<dd>Input array.
1126 <dt>mat_iterator<dd>Initialized iterator.
1128 The function <code>cvInitSparseMatIterator</code> initializes iterator of sparse array elements and
1129 returns pointer to the first element, or NULL if the array is empty.</p>
1132 <hr><h3><a name="decl_cvGetNextSparseNode">GetNextSparseNode</a></h3>
1133 <p class="Blurb">Initializes sparse array elements iterator</p>
1135 CvSparseNode* cvGetNextSparseNode( CvSparseMatIterator* mat_iterator );
1137 <dt>mat_iterator<dd>Sparse array iterator.
1139 The function <code>cvGetNextSparseNode</code> moves iterator to the next sparse matrix element and returns
1140 pointer to it. In the current version there is no any particular order of the elements, because they
1141 are stored in hash table. The sample below demonstrates how to iterate through the sparse matrix:</p>
1143 <font color=blue size=4>Using <a href="#decl_cvInitSparseMatIterator">cvInitSparseMatIterator</a> and <a href="#decl_cvGetNextSparseNode">cvGetNextSparseNode</a> to calculate sum of
1144 floating-point sparse array.</font>
1147 int i, dims = cvGetDims( array );
1148 CvSparseMatIterator mat_iterator;
1149 CvSparseNode* node = cvInitSparseMatIterator( array, &mat_iterator );
1151 for( ; node != 0; node = cvGetNextSparseNode( &mat_iterator ))
1153 const int* idx = CV_NODE_IDX( array, node ); /* get pointer to the element indices */
1154 float val = *(float*)CV_NODE_VAL( array, node ); /* get value of the element
1155 (assume that the type is CV_32FC1) */
1157 for( i = 0; i < dims; i++ )
1158 printf( "%4d%s", idx[i], i < dims - 1 "," : "): " );
1159 printf( "%g\n", val );
1164 printf( "\nTotal sum = %g\n", sum );
1167 <hr><h3><a name="decl_cvGetElemType">GetElemType</a></h3>
1168 <p class="Blurb">Returns type of array elements</p>
1170 int cvGetElemType( const CvArr* arr );
1172 <dt>arr<dd>Input array.
1174 The functions <code>GetElemType</code> returns type of the array elements as it is described in
1175 cvCreateMat discussion: <pre>CV_8UC1 ... CV_64FC4</pre></p>
1178 <hr><h3><a name="decl_cvGetDims">GetDims, GetDimSize</a></h3>
1179 <p class="Blurb">Return number of array dimensions and their sizes or the size of particular dimension</p>
1181 int cvGetDims( const CvArr* arr, int* sizes=NULL );
1182 int cvGetDimSize( const CvArr* arr, int index );
1184 <dt>arr<dd>Input array.
1185 <dt>sizes<dd>Optional output vector of the array dimension sizes. For 2d arrays the number of rows (height)
1186 goes first, number of columns (width) next.
1187 <dt>index<dd>Zero-based dimension index (for matrices 0 means number of rows, 1 means number of columns;
1188 for images 0 means height, 1 means width).
1190 The function <code>cvGetDims</code> returns number of array dimensions and their sizes.
1191 In case of <code>IplImage</code> or <a href="#decl_CvMat">CvMat</a> it always returns 2 regardless of number of image/matrix rows.
1192 The function <code>cvGetDimSize</code> returns the particular dimension size (number of elements per that dimension).
1193 For example, the following code calculates total number of array elements in two ways:<pre>
1196 int sizes[CV_MAX_DIM];
1198 int dims = cvGetDims( arr, size );
1199 for( i = 0; i < dims; i++ )
1202 // via cvGetDims() and cvGetDimSize()
1204 int dims = cvGetDims( arr );
1205 for( i = 0; i < dims; i++ )
1206 total *= cvGetDimsSize( arr, i );
1211 <hr><h3><a name="decl_cvPtr*D">Ptr*D</a></h3>
1212 <p class="Blurb">Return pointer to the particular array element</p>
1214 uchar* cvPtr1D( const CvArr* arr, int idx0, int* type=NULL );
1215 uchar* cvPtr2D( const CvArr* arr, int idx0, int idx1, int* type=NULL );
1216 uchar* cvPtr3D( const CvArr* arr, int idx0, int idx1, int idx2, int* type=NULL );
1217 uchar* cvPtrND( const CvArr* arr, const int* idx, int* type=NULL, int create_node=1, unsigned* precalc_hashval=NULL );
1219 <dt>arr<dd>Input array.
1220 <dt>idx0<dd>The first zero-based component of the element index
1221 <dt>idx1<dd>The second zero-based component of the element index
1222 <dt>idx2<dd>The third zero-based component of the element index
1223 <dt>idx<dd>Array of the element indices
1224 <dt>type<dd>Optional output parameter: type of matrix elements
1225 <dt>create_node<dd>Optional input parameter for sparse matrices. Non-zero value of the parameter means that
1226 the requested element is created if it does not exist already.
1227 <dt>precalc_hashval<dd>Optional input parameter for sparse matrices. If the pointer is not NULL, the function
1228 does not recalculate the node hash value, but takes it from the specified location. It is useful for speeding
1229 up pair-wise operations (TODO: provide an example)
1231 The functions <code>>cvPtr*D</code> return pointer to the particular array element.
1232 Number of array dimension should match to the number of indices passed to the function except
1233 for <code>cvPtr1D</code> function that can be used for sequential access to 1D, 2D or nD dense arrays.
1235 The functions can be used for sparse arrays as well - if the requested node does not exist they
1236 create it and set it to zero.</p><p>
1237 All these as well as other functions accessing array elements
1238 (<a href="#decl_cvGet*D">cvGet*D</a>, <a href="#decl_cvGetReal*D">cvGetReal*D</a>,
1239 <a href="#decl_cvSet*D">cvSet*D</a>, <a href="#decl_cvSetReal*D">cvSetReal*D</a>)
1240 raise an error in case if the element index is out of range.
1244 <hr><h3><a name="decl_cvGet*D">Get*D</a></h3>
1245 <p class="Blurb">Return the particular array element</p>
1247 CvScalar cvGet1D( const CvArr* arr, int idx0 );
1248 CvScalar cvGet2D( const CvArr* arr, int idx0, int idx1 );
1249 CvScalar cvGet3D( const CvArr* arr, int idx0, int idx1, int idx2 );
1250 CvScalar cvGetND( const CvArr* arr, const int* idx );
1252 <dt>arr<dd>Input array.
1253 <dt>idx0<dd>The first zero-based component of the element index
1254 <dt>idx1<dd>The second zero-based component of the element index
1255 <dt>idx2<dd>The third zero-based component of the element index
1256 <dt>idx<dd>Array of the element indices
1258 The functions <code>cvGet*D</code> return the particular array element. In case of sparse array
1259 the functions return 0 if the requested node does not exist (no new node is created
1260 by the functions)</p>
1263 <hr><h3><a name="decl_cvGetReal*D">GetReal*D</a></h3>
1264 <p class="Blurb">Return the particular element of single-channel array</p>
1266 double cvGetReal1D( const CvArr* arr, int idx0 );
1267 double cvGetReal2D( const CvArr* arr, int idx0, int idx1 );
1268 double cvGetReal3D( const CvArr* arr, int idx0, int idx1, int idx2 );
1269 double cvGetRealND( const CvArr* arr, const int* idx );
1271 <dt>arr<dd>Input array. Must have a single channel.
1272 <dt>idx0<dd>The first zero-based component of the element index
1273 <dt>idx1<dd>The second zero-based component of the element index
1274 <dt>idx2<dd>The third zero-based component of the element index
1275 <dt>idx<dd>Array of the element indices
1277 The functions <code>cvGetReal*D</code> return the particular element of single-channel array.
1278 If the array has multiple channels, runtime error is raised. Note that <a href="#decl_cvGet*D">cvGet*D</a> function
1279 can be used safely for both single-channel and multiple-channel arrays though they are
1280 a bit slower.</p><p>In case of sparse array
1281 the functions return 0 if the requested node does not exist (no new node is created
1282 by the functions)</p>
1285 <hr><h3><a name="decl_cvmGet">mGet</a></h3>
1286 <p class="Blurb">Return the particular element of single-channel floating-point matrix</p>
1288 double cvmGet( const CvMat* mat, int row, int col );
1290 <dt>mat<dd>Input matrix.
1291 <dt>row<dd>The zero-based index of row.
1292 <dt>col<dd>The zero-based index of column.
1294 The function <code>cvmGet</code> is a fast replacement for <a href="#decl_cvGetReal2D">cvGetReal2D</a> in case of
1295 single-channel floating-point matrices. It is faster because it is inline,
1296 it does less checks for array type and array element type and it
1297 checks for the row and column ranges only in debug mode.</p>
1300 <hr><h3><a name="decl_cvSet*D">Set*D</a></h3>
1301 <p class="Blurb">Change the particular array element</p>
1303 void cvSet1D( CvArr* arr, int idx0, CvScalar value );
1304 void cvSet2D( CvArr* arr, int idx0, int idx1, CvScalar value );
1305 void cvSet3D( CvArr* arr, int idx0, int idx1, int idx2, CvScalar value );
1306 void cvSetND( CvArr* arr, const int* idx, CvScalar value );
1308 <dt>arr<dd>Input array.
1309 <dt>idx0<dd>The first zero-based component of the element index
1310 <dt>idx1<dd>The second zero-based component of the element index
1311 <dt>idx2<dd>The third zero-based component of the element index
1312 <dt>idx<dd>Array of the element indices
1313 <dt>value<dd>The assigned value
1315 The functions <code>cvSet*D</code> assign the new value to the particular element of array.
1316 In case of sparse array the functions create the node if it does not exist yet</p>
1319 <hr><h3><a name="decl_cvSetReal*D">SetReal*D</a></h3>
1320 <p class="Blurb">Change the particular array element</p>
1322 void cvSetReal1D( CvArr* arr, int idx0, double value );
1323 void cvSetReal2D( CvArr* arr, int idx0, int idx1, double value );
1324 void cvSetReal3D( CvArr* arr, int idx0, int idx1, int idx2, double value );
1325 void cvSetRealND( CvArr* arr, const int* idx, double value );
1327 <dt>arr<dd>Input array.
1328 <dt>idx0<dd>The first zero-based component of the element index
1329 <dt>idx1<dd>The second zero-based component of the element index
1330 <dt>idx2<dd>The third zero-based component of the element index
1331 <dt>idx<dd>Array of the element indices
1332 <dt>value<dd>The assigned value
1334 The functions <code>cvSetReal*D</code> assign the new value to the particular element of single-channel array.
1335 If the array has multiple channels, runtime error is raised. Note that <a href="#decl_cvSet*D">cvSet*D</a> function
1336 can be used safely for both single-channel and multiple-channel arrays though they are
1338 <p>In case of sparse array the functions create the node if it does not exist yet</p>
1341 <hr><h3><a name="decl_cvmSet">mSet</a></h3>
1342 <p class="Blurb">Return the particular element of single-channel floating-point matrix</p>
1344 void cvmSet( CvMat* mat, int row, int col, double value );
1346 <dt>mat<dd>The matrix.
1347 <dt>row<dd>The zero-based index of row.
1348 <dt>col<dd>The zero-based index of column.
1349 <dt>value<dd>The new value of the matrix element
1351 The function <code>cvmSet</code> is a fast replacement for <a href="#decl_cvSetReal2D">cvSetReal2D</a> in case of
1352 single-channel floating-point matrices. It is faster because it is inline,
1353 it does less checks for array type and array element type and it
1354 checks for the row and column ranges only in debug mode.</p>
1357 <hr><h3><a name="decl_cvClearND">ClearND</a></h3>
1358 <p class="Blurb">Clears the particular array element</p>
1360 void cvClearND( CvArr* arr, const int* idx );
1362 <dt>arr<dd>Input array.
1363 <dt>idx<dd>Array of the element indices
1365 The function <a href="#decl_cvClearND">cvClearND</a> clears (sets to zero)
1366 the particular element of dense array or deletes the element of sparse array.
1367 If the element does not exists, the function does nothing.</p>
1370 <hr><h2><a name="cxcore_arrays_copying">Copying and Filling</a></h2>
1373 <hr><h3><a name="decl_cvCopy">Copy</a></h3>
1374 <p class="Blurb">Copies one array to another</p>
1376 void cvCopy( const CvArr* src, CvArr* dst, const CvArr* mask=NULL );
1378 <dt>src<dd>The source array.
1379 <dt>dst<dd>The destination array.
1380 <dt>mask<dd>Operation mask, 8-bit single channel array; specifies elements of
1381 destination array to be changed.
1383 The function <code>cvCopy</code> copies selected elements from input array to output array:</p>
1385 dst(I)=src(I) if mask(I)!=0.
1386 </p><p>If any of the passed arrays is of <code>IplImage</code> type, then its ROI and COI fields are
1387 used. Both arrays must have the same type, the same number of dimensions and the same size.
1388 The function can also copy sparse arrays (mask is not supported in this case).</p>
1391 <hr><h3><a name="decl_cvSet">Set</a></h3>
1392 <p class="Blurb">Sets every element of array to given value</p>
1394 void cvSet( CvArr* arr, CvScalar value, const CvArr* mask=NULL );
1396 <dt>arr<dd>The destination array.
1397 <dt>value<dd>Fill value.
1398 <dt>mask<dd>Operation mask, 8-bit single channel array; specifies elements of
1399 destination array to be changed.
1401 The function <code>cvSet</code> copies scalar <code>value</code> to every selected element of the destination
1403 <pre>arr(I)=value if mask(I)!=0</pre>
1404 <p>If array <code>arr</code> is of <code>IplImage</code> type, then is ROI used, but COI must not be
1408 <hr><h3><a name="decl_cvSetZero">SetZero</a></h3>
1409 <p class="Blurb">Clears the array</p>
1411 void cvSetZero( CvArr* arr );
1412 #define cvZero cvSetZero
1414 <dt>arr<dd>array to be cleared.
1416 The function <code>cvSetZero</code> clears the array. In case of dense arrays
1417 (CvMat, CvMatND or IplImage) cvZero(array) is equivalent to cvSet(array,cvScalarAll(0),0),
1418 in case of sparse arrays all the elements are removed.</p>
1421 <hr><h3><a name="decl_cvSetIdentity">SetIdentity</a></h3>
1422 <p class="Blurb">Initializes scaled identity matrix</p>
1424 void cvSetIdentity( CvArr* mat, CvScalar value=cvRealScalar(1) );
1426 <dt>arr<dd>The matrix to initialize (not necessarily square).
1427 <dt>value<dd>The value to assign to the diagonal elements.
1429 The function <code>cvSetIdentity</code> initializes scaled identity matrix:</p>
1431 arr(i,j)=value if i=j,
1436 <hr><h3><a name="decl_cvRange">Range</a></h3>
1437 <p class="Blurb">Fills matrix with given range of numbers</p>
1439 void cvRange( CvArr* mat, double start, double end );
1441 <dt>mat<dd>The matrix to initialize. It should be single-channel 32-bit, integer or floating-point.
1442 <dt>start<dd>The lower inclusive boundary of the range.
1443 <dt>end<dd>The upper exclusive boundary of the range.
1445 The function <code>cvRange</code> initializes the matrix as following:</p>
1447 arr(i,j)=(end-start)*(i*cols(arr)+j)/(cols(arr)*rows(arr))
1449 For example, the following code will initialize 1D vector with subsequent integer numbers.
1451 CvMat* A = cvCreateMat( 1, 10, CV_32S );
1452 cvRange( A, 0, A->cols ); // A will be initialized as [0,1,2,3,4,5,6,7,8,9]
1455 <!-- *****************************************************************************************
1456 *****************************************************************************************
1457 ***************************************************************************************** -->
1459 <hr><h2><a name="cxcore_arrays_permute">Transforms and Permutations</a></h2>
1461 <hr><h3><a name="decl_cvReshape">Reshape</a></h3>
1462 <p class="Blurb">Changes shape of matrix/image without copying data</p>
1464 CvMat* cvReshape( const CvArr* arr, CvMat* header, int new_cn, int new_rows=0 );
1466 <dt>arr<dd>Input array.
1467 <dt>header<dd>Output header to be filled.
1468 <dt>new_cn<dd>New number of channels. <code>new_cn = 0</code> means that number of channels remains unchanged.
1469 <dt>new_rows<dd>New number of rows. <code>new_rows = 0</code> means that number of rows remains unchanged unless
1470 it needs to be changed according to <code>new_cn</code> value.
1471 destination array to be changed.
1473 The function <code>cvReshape</code> initializes CvMat header so that it points to the same data as
1474 the original array but has different shape - different number of channels,
1475 different number of rows or both.</p><p>For example, the following code creates one image buffer and
1476 two image headers, first is for 320x240x3 image and the second is for 960x240x1 image:</p>
1478 IplImage* color_img = cvCreateImage( cvSize(320,240), IPL_DEPTH_8U, 3 );
1480 IplImage gray_img_hdr, *gray_img;
1481 cvReshape( color_img, &gray_mat_hdr, 1 );
1482 gray_img = cvGetImage( &gray_mat_hdr, &gray_img_hdr );
1484 <p>And the next example converts 3x3 matrix to a single 1x9 vector</p>
1486 CvMat* mat = cvCreateMat( 3, 3, CV_32F );
1487 CvMat row_header, *row;
1488 row = cvReshape( mat, &row_header, 0, 1 );
1492 <hr><h3><a name="decl_cvReshapeMatND">ReshapeMatND</a></h3>
1493 <p class="Blurb">Changes shape of multi-dimensional array w/o copying data</p>
1495 CvArr* cvReshapeMatND( const CvArr* arr,
1496 int sizeof_header, CvArr* header,
1497 int new_cn, int new_dims, int* new_sizes );
1499 #define cvReshapeND( arr, header, new_cn, new_dims, new_sizes ) \
1500 cvReshapeMatND( (arr), sizeof(*(header)), (header), \
1501 (new_cn), (new_dims), (new_sizes))
1504 <dt>arr<dd>Input array.
1505 <dt>sizeof_header<dd>Size of output header to distinguish between IplImage, CvMat and CvMatND output headers.
1506 <dt>header<dd>Output header to be filled.
1507 <dt>new_cn<dd>New number of channels. <code>new_cn = 0</code> means that number of channels remains unchanged.
1508 <dt>new_dims<dd>New number of dimensions. <code>new_dims = 0</code> means that number of dimensions remains the same.
1509 <dt>new_sizes<dd>Array of new dimension sizes. Only <code>new_dims-1</code> values are used, because the total number of
1510 elements must remain the same. Thus, if <code>new_dims = 1</code>, <code>new_sizes</code> array is not used
1512 The function <code>cvReshapeMatND</code> is an advanced version of <a href="#decl_cvReshape">cvReshape</a> that can work
1513 with multi-dimensional arrays as well (though, it can work with ordinary images and matrices)
1514 and change the number of dimensions. Below are the two samples
1515 from the <a href="#decl_cvReshape">cvReshape</a> description rewritten using <a href="#decl_cvReshapeMatND">cvReshapeMatND</a>:</p>
1517 IplImage* color_img = cvCreateImage( cvSize(320,240), IPL_DEPTH_8U, 3 );
1518 IplImage gray_img_hdr, *gray_img;
1519 gray_img = (IplImage*)cvReshapeND( color_img, &gray_img_hdr, 1, 0, 0 );
1523 /* second example is modified to convert 2x2x2 array to 8x1 vector */
1524 int size[] = { 2, 2, 2 };
1525 CvMatND* mat = cvCreateMatND( 3, size, CV_32F );
1526 CvMat row_header, *row;
1527 row = cvReshapeND( mat, &row_header, 0, 1, 0 );
1531 <hr><h3><a name="decl_cvRepeat">Repeat</a></h3>
1532 <p class="Blurb">Fill destination array with tiled source array</p>
1534 void cvRepeat( const CvArr* src, CvArr* dst );
1536 <dt>src<dd>Source array, image or matrix.
1537 <dt>dst<dd>Destination array, image or matrix.
1539 The function <code>cvRepeat</code> fills the destination array with source array tiled:</p>
1540 <pre>dst(i,j)=src(i mod rows(src), j mod cols(src))</pre>
1542 So the destination array may be as larger as well as smaller than
1547 <hr><h3><a name="decl_cvFlip">Flip</a></h3>
1548 <p class="Blurb">Flip a 2D array around vertical, horizontal or both axises</p>
1550 void cvFlip( const CvArr* src, CvArr* dst=NULL, int flip_mode=0);
1551 #define cvMirror cvFlip
1554 <dt>src<dd>Source array.
1555 <dt>dst<dd>Destination array. If <code>dst = NULL</code> the flipping is done in-place.
1556 <dt>flip_mode<dd>Specifies how to flip the array.<br>
1557 flip_mode = 0 means flipping around x-axis,
1558 flip_mode > 0 (e.g. 1) means flipping around y-axis
1559 and flip_mode < 0 (e.g. -1) means flipping around both axises.
1560 See also the discussion below for the formulas
1562 The function <code>cvFlip</code> flips the array in one of different 3 ways
1563 (row and column indices are 0-based):</p>
1564 <pre>dst(i,j)=src(rows(src)-i-1,j) if flip_mode = 0</pre>
1565 <pre>dst(i,j)=src(i,cols(src1)-j-1) if flip_mode > 0</pre>
1566 <pre>dst(i,j)=src(rows(src)-i-1,cols(src)-j-1) if flip_mode < 0</pre>
1567 <p>The example scenario of the function use are:
1570 vertical flipping of the image (flip_mode > 0) to switch between top-left
1571 and bottom-left image origin, which is typical operation in video processing under Win32 systems.</li>
1573 horizontal flipping of the image with subsequent horizontal shift and absolute difference calculation
1574 to check for a vertical-axis symmetry (flip_mode > 0)</li>
1576 simultaneous horizontal and vertical flipping of the image with subsequent shift and
1577 absolute difference calculation to check for a central symmetry (flip_mode < 0)</li>
1578 <li>reversing the order of 1d point arrays(flip_mode > 0)</li>
1582 <hr><h3><a name="decl_cvSplit">Split</a></h3>
1583 <p class="Blurb">Divides multi-channel array into several single-channel arrays or extracts
1584 a single channel from the array</p>
1586 void cvSplit( const CvArr* src, CvArr* dst0, CvArr* dst1,
1587 CvArr* dst2, CvArr* dst3 );
1588 #define cvCvtPixToPlane cvSplit
1590 <dt>src<dd>Source array.
1591 <dt>dst0...dst3<dd>Destination channels.
1593 The function <code>cvSplit</code> divides a multi-channel array into separate single-channel arrays.
1594 Two modes are available for the operation. If the source array has N channels then if
1595 the first N destination channels are not NULL, all they are extracted from the source array,
1596 otherwise if only a single destination channel of the first N is not NULL, this particular channel
1597 is extracted, otherwise an error is raised. Rest of destination channels (beyond the first N)
1598 must always be NULL.
1599 For IplImage <a href="#decl_cvCopy">cvCopy</a> with COI set can be also used to extract a single channel from the image.</p>
1602 <hr><h3><a name="decl_cvMerge">Merge</a></h3>
1603 <p class="Blurb">Composes multi-channel array from several single-channel arrays or inserts a
1604 single channel into the array</p>
1606 void cvMerge( const CvArr* src0, const CvArr* src1,
1607 const CvArr* src2, const CvArr* src3, CvArr* dst );
1608 #define cvCvtPlaneToPix cvMerge
1610 <dt>src0... src3<dd>Input channels.
1611 <dt>dst<dd>Destination array.
1613 The function <code>cvMerge</code> is the opposite to the previous.
1614 If the destination array has N channels then if
1615 the first N input channels are not NULL, all they are copied to the destination array,
1616 otherwise if only a single source channel of the first N is not NULL, this particular channel is copied
1617 into the destination array, otherwise an error is raised. Rest of source channels (beyond the first N)
1618 must always be NULL.
1619 For IplImage <a href="#decl_cvCopy">cvCopy</a> with COI set can be also used to insert a single channel into the image.
1623 <hr><h3><a name="decl_cvMixChannels">MixChannels</a></h3>
1624 <p class="Blurb">Copies several channels from input arrays to
1625 certain channels of output arrays</p>
1627 void cvMixChannels( const CvArr** src, int src_count,
1628 CvArr** dst, int dst_count,
1629 const int* from_to, int pair_count );
1631 <dt>src<dd>The array of input arrays.
1632 <dt>src_count<dd>The number of input arrays.
1633 <dt>dst<dd>The array of output arrays.
1634 <dt>dst_count<dd>The number of output arrays.
1635 <dt>from_to<dd>The array of pairs of indices of the planes copied.
1636 <code>from_to[k*2]</code> is the 0-based index of the input plane,
1637 and <code>from_to[k*2+1]</code> is the index of the output plane, where the continuous numbering
1638 of the planes over all the input and over all the output arrays is used.
1639 When <code>from_to[k*2]</code> is negative, the corresponding output plane is filled with 0's.
1640 <dt>pair_count<dd>The number of pairs in <code>from_to</code>, or the number of the planes copied.
1642 The function <code>cvMixChannels</code> is a generalized form of <a href="#decl_cvSplit">cvSplit</a> and
1643 <a href="#decl_cvMerge">cvMerge</a> and some forms of <a href="opencvref_cv.htm#decl_cvCvtColor">cvCvtColor</a>.
1644 It can be used to change the order of the planes, add/remove alpha
1645 channel, extract or insert a single plane or multiple planes etc.
1646 Below is the example, how to split 4-channel RGBA image into 3-channel BGR (i.e. with R&B swapped) and
1647 separate alpha channel images:</p>
1649 CvMat* rgba = cvCreateMat( 100, 100, CV_8UC4 );
1650 CvMat* bgr = cvCreateMat( rgba->rows, rgba->cols, CV_8UC3 );
1651 CvMat* alpha = cvCreateMat( rgba->rows, rgba->cols, CV_8UC1 );
1652 CvArr* out[] = { bgr, alpha };
1653 int from_to[] = { 0, 2, 1, 1, 2, 0, 3, 3 };
1654 cvSet( rgba, cvScalar(1,2,3,4) );
1655 cvMixChannels( (const CvArr**)&rgba, 1, out, 2, from_to, 4 );
1659 <hr><h3><a name="decl_cvRandShuffle">RandShuffle</a></h3>
1660 <p class="Blurb">Randomly shuffles the array elements</p>
1662 void cvRandShuffle( CvArr* mat, CvRNG* rng, double iter_factor=1. );
1664 <dt>mat<dd>The input/output matrix. It is shuffled in-place.
1665 <dt>rng<dd>The <a href="#cxcore_arrays_rng">Random Number Generator</a>
1666 used to shuffle the elements. When the pointer is NULL,
1667 a temporary RNG will be created and used.
1668 <dt>iter_factor<dd>The relative parameter that characterizes intensity of the shuffling performed.
1669 See the description below.
1671 The function <code>cvRandShuffle</code> shuffles the matrix by swapping randomly chosen pairs
1672 of the matrix elements on each iteration (where each element may contain several components in case of multi-channel arrays).
1673 The number of iterations (i.e. pairs swapped) is <code>round(iter_factor*rows(mat)*cols(mat))</code>, so
1674 <code>iter_factor=0</code> means that no shuffling is done,
1675 <code>iter_factor=1</code> means that the function swaps
1676 <code>rows(mat)*cols(mat)</code> random pairs etc.
1680 <hr><h2><a name="cxcore_arrays_arithm_logic">Arithmetic, Logic and Comparison</a></h2>
1683 <hr><h3><a name="decl_cvLUT">LUT</a></h3>
1684 <p class="Blurb">Performs look-up table transform of array</p>
1686 void cvLUT( const CvArr* src, CvArr* dst, const CvArr* lut );
1688 <dt>src<dd>Source array of 8-bit elements.
1689 <dt>dst<dd>Destination array of arbitrary depth and of the same number of channels as the
1691 <dt>lut<dd>Look-up table of 256 elements; should have the same depth as the
1692 destination array. In case of multi-channel source and destination arrays, the table
1693 should either have a single-channel (in this case the same table is used for all channels),
1694 or the same number of channels as the source/destination array.
1696 The function <code>cvLUT</code> fills the destination array with values
1697 from the look-up table. Indices of the entries are taken from the source array. That is, the
1698 function processes each element of <code>src</code> as following:</p>
1700 dst(I)=lut[src(I)+DELTA]
1702 where <code>DELTA=0</code> if <code>src</code> has depth <code>CV_8U</code>, and
1703 <code>DELTA=128</code> if <code>src</code> has depth <code>CV_8S</code>.
1707 <hr><h3><a name="decl_cvConvertScale">ConvertScale</a></h3>
1708 <p class="Blurb">Converts one array to another with optional linear transformation</p>
1710 void cvConvertScale( const CvArr* src, CvArr* dst, double scale=1, double shift=0 );
1712 #define cvCvtScale cvConvertScale
1713 #define cvScale cvConvertScale
1714 #define cvConvert( src, dst ) cvConvertScale( (src), (dst), 1, 0 )
1716 <dt>src<dd>Source array.
1717 <dt>dst<dd>Destination array.
1718 <dt>scale<dd>Scale factor.
1719 <dt>shift<dd>Value added to the scaled source array elements.
1721 The function <code>cvConvertScale</code> has several different purposes and thus has several synonyms.
1722 It copies one array to another with optional scaling, which is performed first, and/or optional type conversion, performed after:</p>
1723 <pre>dst(I)=src(I)*scale + (shift,shift,...)</pre>
1724 <p>All the channels of multi-channel arrays are processed independently.</p><p>
1725 The type conversion is done with rounding and saturation, that is if a result of scaling + conversion can not
1726 be represented exactly by a value of destination array element type, it is set to the nearest
1727 representable value on the real axis.</p>
1728 <p>In case of <code>scale=1, shift=0</code> no pre-scaling is done. This is a specially optimized case and it
1729 has the appropriate <a href="#decl_cvConvert">cvConvert</a> synonym. If source and destination array types have
1730 equal types, this is also a special case that can be used to scale and shift a matrix or an image and
1731 that fits to <a href="#decl_cvScale">cvScale</a> synonym.</p>
1734 <hr><h3><a name="decl_cvConvertScaleAbs">ConvertScaleAbs</a></h3>
1735 <p class="Blurb">Converts input array elements to 8-bit unsigned integer another with optional linear transformation</p>
1737 void cvConvertScaleAbs( const CvArr* src, CvArr* dst, double scale=1, double shift=0 );
1738 #define cvCvtScaleAbs cvConvertScaleAbs
1741 <dt>src<dd>Source array.
1742 <dt>dst<dd>Destination array (should have 8u depth).
1743 <dt>scale<dd>ScaleAbs factor.
1744 <dt>shift<dd>Value added to the scaled source array elements.
1746 The function <code>cvConvertScaleAbs</code> is similar to the previous one, but it stores absolute values
1747 of the conversion results:</p>
1748 <pre>dst(I)=abs(src(I)*scale + (shift,shift,...))</pre>
1750 The function supports only destination arrays of 8u (8-bit unsigned integers) type, for
1751 other types the function can be emulated by combination of <a href="#decl_cvConvertScale">cvConvertScale</a> and <a href="#decl_cvAbsDiffS">cvAbs</a>
1755 <hr><h3><a name="decl_cvAdd">Add</a></h3>
1756 <p class="Blurb">Computes per-element sum of two arrays</p>
1758 void cvAdd( const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask=NULL );
1761 <dt>src1<dd>The first source array.
1762 <dt>src2<dd>The second source array.
1763 <dt>dst<dd>The destination array.
1764 <dt>mask<dd>Operation mask, 8-bit single channel array; specifies elements of
1765 destination array to be changed.
1767 The function <code>cvAdd</code> adds one array to another one:</p>
1768 <pre>dst(I)=src1(I)+src2(I) if mask(I)!=0</pre>
1769 <p>All the arrays must have the same type, except the mask, and the same size (or ROI size)</p>
1772 </p><hr><h3><a name="decl_cvAddS">AddS</a></h3>
1773 <p class="Blurb">Computes sum of array and scalar</p>
1775 void cvAddS( const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL );
1777 <dt>src<dd>The source array.
1778 <dt>value<dd>Added scalar.
1779 <dt>dst<dd>The destination array.
1780 <dt>mask<dd>Operation mask, 8-bit single channel array; specifies elements of
1781 destination array to be changed.
1783 The function <code>cvAddS</code> adds scalar <code>value</code> to every element in the source array <code>src1</code> and
1784 stores the result in <code>dst</code><p>
1785 <pre>dst(I)=src(I)+value if mask(I)!=0</pre>
1786 <p>All the arrays must have the same type, except the mask, and the same size (or ROI size)</p>
1789 <hr><h3><a name="decl_cvAddWeighted">AddWeighted</a></h3>
1790 <p class="Blurb">Computes weighted sum of two arrays</p>
1792 void cvAddWeighted( const CvArr* src1, double alpha,
1793 const CvArr* src2, double beta,
1794 double gamma, CvArr* dst );
1796 <dt>src1<dd>The first source array.
1797 <dt>alpha<dd>Weight of the first array elements.
1798 <dt>src2<dd>The second source array.
1799 <dt>beta<dd>Weight of the second array elements.
1800 <dt>dst<dd>The destination array.
1801 <dt>gamma<dd>Scalar, added to each sum.
1803 The function <code>cvAddWeighted</code> calculated weighted sum
1804 of two arrays as following:</p>
1805 <pre>dst(I)=src1(I)*alpha+src2(I)*beta+gamma</pre>
1806 <p>All the arrays must have the same type and the same size (or ROI size)</p>
1809 <hr><h3><a name="decl_cvSub">Sub</a></h3>
1810 <p class="Blurb">Computes per-element difference between two arrays</p>
1812 void cvSub( const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask=NULL );
1814 <dt>src1<dd>The first source array.
1815 <dt>src2<dd>The second source array.
1816 <dt>dst<dd>The destination array.
1817 <dt>mask<dd>Operation mask, 8-bit single channel array; specifies elements of
1818 destination array to be changed.
1820 The function <code>cvSub</code> subtracts one array from another one:</p>
1821 <pre>dst(I)=src1(I)-src2(I) if mask(I)!=0</pre>
1822 <p>All the arrays must have the same type, except the mask, and the same size (or ROI size)</p>
1825 </p><hr><h3><a name="decl_cvSubS">SubS</a></h3>
1826 <p class="Blurb">Computes difference between array and scalar</p>
1828 void cvSubS( const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL );
1830 <dt>src<dd>The source array.
1831 <dt>value<dd>Subtracted scalar.
1832 <dt>dst<dd>The destination array.
1833 <dt>mask<dd>Operation mask, 8-bit single channel array; specifies elements of
1834 destination array to be changed.
1836 The function <code>cvSubS</code> subtracts a scalar from every element of the source array:<p>
1837 <pre>dst(I)=src(I)-value if mask(I)!=0</pre>
1838 <p>All the arrays must have the same type, except the mask, and the same size (or ROI size)</p>
1841 <hr><h3><a name="decl_cvSubRS">SubRS</a></h3>
1842 <p class="Blurb">Computes difference between scalar and array</p>
1844 void cvSubRS( const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL );
1846 <dt>src<dd>The first source array.
1847 <dt>value<dd>Scalar to subtract from.
1848 <dt>dst<dd>The destination array.
1849 <dt>mask<dd>Operation mask, 8-bit single channel array; specifies elements of
1850 destination array to be changed.
1852 The function <code>cvSubRS</code> subtracts every element of source array from a scalar:</p>
1853 <pre>dst(I)=value-src(I) if mask(I)!=0</pre>
1854 <p>All the arrays must have the same type, except the mask, and the same size (or ROI size)</p>
1857 <hr><h3><a name="decl_cvMul">Mul</a></h3>
1858 <p class="Blurb">Calculates per-element product of two arrays</p>
1860 void cvMul( const CvArr* src1, const CvArr* src2, CvArr* dst, double scale=1 );
1862 <dt>src1<dd>The first source array.
1863 <dt>src2<dd>The second source array.
1864 <dt>dst<dd>The destination array.
1865 <dt>scale<dd>Optional scale factor
1867 The function <code>cvMul</code> calculates per-element product of two arrays:</p>
1868 <pre>dst(I)=scale•src1(I)•src2(I)</pre>
1869 <p>All the arrays must have the same type, and the same size (or ROI size)</p>
1872 <hr><h3><a name="decl_cvDiv">Div</a></h3>
1873 <p class="Blurb">Performs per-element division of two arrays</p>
1875 void cvDiv( const CvArr* src1, const CvArr* src2, CvArr* dst, double scale=1 );
1877 <dt>src1<dd>The first source array. If the pointer is NULL, the array is assumed to be all 1’s.
1878 <dt>src2<dd>The second source array.
1879 <dt>dst<dd>The destination array.
1880 <dt>scale<dd>Optional scale factor
1882 The function <code>cvDiv</code> divides one array by another:</p>
1884 dst(I)=scale•src1(I)/src2(I), if src1!=NULL
1885 dst(I)=scale/src2(I), if src1=NULL
1887 <p>All the arrays must have the same type, and the same size (or ROI size)</p>
1890 <hr><h3><a name="decl_cvAnd">And</a></h3>
1891 <p class="Blurb">Calculates per-element bit-wise conjunction of two arrays</p>
1893 void cvAnd( const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask=NULL );
1895 <dt>src1<dd>The first source array.
1896 <dt>src2<dd>The second source array.
1897 <dt>dst<dd>The destination array.
1898 <dt>mask<dd>Operation mask, 8-bit single channel array; specifies elements of
1899 destination array to be changed.
1901 The function <code>cvAnd</code> calculates per-element bit-wise logical conjunction of two arrays:</p>
1902 <pre>dst(I)=src1(I)&src2(I) if mask(I)!=0</pre>
1903 <p>In the case of floating-point arrays their bit representations are used for the
1904 operation. All the arrays must have the same type, except the mask, and the same size</p>
1907 <hr><h3><a name="decl_cvAndS">AndS</a></h3>
1908 <p class="Blurb">Calculates per-element bit-wise conjunction of array and scalar</p>
1910 void cvAndS( const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL );
1912 <dt>src<dd>The source array.
1913 <dt>value<dd>Scalar to use in the operation.
1914 <dt>dst<dd>The destination array.
1915 <dt>mask<dd>Operation mask, 8-bit single channel array; specifies elements of
1916 destination array to be changed.
1918 The function AndS calculates per-element bit-wise conjunction of array and scalar:</p>
1919 <pre>dst(I)=src(I)&value if mask(I)!=0</pre>
1920 <p>Prior to the actual operation the scalar is converted to the same type as
1921 the arrays. In the case of floating-point arrays their bit representations are used for the
1922 operation. All the arrays must have the same type, except the mask, and the same size</p>
1924 <p>The following sample demonstrates how to calculate absolute value of floating-point array elements
1925 by clearing the most-significant bit:</p>
1927 float a[] = { -1, 2, -3, 4, -5, 6, -7, 8, -9 };
1928 CvMat A = cvMat( 3, 3, CV_32F, &a );
1929 int i, abs_mask = 0x7fffffff;
1930 cvAndS( &A, cvRealScalar(*(float*)&abs_mask), &A, 0 );
1931 for( i = 0; i < 9; i++ )
1932 printf("%.1f ", a[i] );
1934 <p>The code should print:</p>
1935 <pre>1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0</pre>
1938 <hr><h3><a name="decl_cvOr">Or</a></h3>
1939 <p class="Blurb">Calculates per-element bit-wise disjunction of two arrays</p>
1941 void cvOr( const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask=NULL );
1943 <dt>src1<dd>The first source array.
1944 <dt>src2<dd>The second source array.
1945 <dt>dst<dd>The destination array.
1946 <dt>mask<dd>Operation mask, 8-bit single channel array; specifies elements of
1947 destination array to be changed.
1949 The function <code>cvOr</code> calculates per-element bit-wise disjunction of two arrays:</p>
1950 <pre>dst(I)=src1(I)|src2(I)</pre>
1951 <p>In the case of floating-point arrays their bit representations are used for the
1952 operation. All the arrays must have the same type, except the mask, and the same size</p>
1955 <hr><h3><a name="decl_cvOrS">OrS</a></h3>
1956 <p class="Blurb">Calculates per-element bit-wise disjunction of array and scalar</p>
1958 void cvOrS( const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL );
1960 <dt>src1<dd>The source array.
1961 <dt>value<dd>Scalar to use in the operation.
1962 <dt>dst<dd>The destination array.
1963 <dt>mask<dd>Operation mask, 8-bit single channel array; specifies elements of
1964 destination array to be changed.
1966 The function OrS calculates per-element bit-wise disjunction of array and scalar:</p>
1967 <pre>dst(I)=src(I)|value if mask(I)!=0</pre>
1968 <p>Prior to the actual operation the scalar is converted to the same type as
1969 the arrays. In the case of floating-point arrays their bit representations are used for the
1970 operation. All the arrays must have the same type, except the mask, and the same size</p>
1973 <hr><h3><a name="decl_cvXor">Xor</a></h3>
1974 <p class="Blurb">Performs per-element bit-wise "exclusive or" operation on two arrays</p>
1976 void cvXor( const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask=NULL );
1978 <dt>src1<dd>The first source array.
1979 <dt>src2<dd>The second source array.
1980 <dt>dst<dd>The destination array.
1981 <dt>mask<dd>Operation mask, 8-bit single channel array; specifies elements of
1982 destination array to be changed.
1984 The function <code>cvXor</code> calculates per-element bit-wise logical conjunction of two arrays:</p>
1985 <pre>dst(I)=src1(I)^src2(I) if mask(I)!=0</pre>
1986 <p>In the case of floating-point arrays their bit representations are used for the
1987 operation. All the arrays must have the same type, except the mask, and the same size</p>
1990 <hr><h3><a name="decl_cvXorS">XorS</a></h3>
1991 <p class="Blurb">Performs per-element bit-wise "exclusive or" operation on array and scalar</p>
1993 void cvXorS( const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL );
1995 <dt>src<dd>The source array.
1996 <dt>value<dd>Scalar to use in the operation.
1997 <dt>dst<dd>The destination array.
1998 <dt>mask<dd>Operation mask, 8-bit single channel array; specifies elements of
1999 destination array to be changed.
2001 The function XorS calculates per-element bit-wise conjunction of array and scalar:</p>
2002 <pre>dst(I)=src(I)^value if mask(I)!=0</pre>
2003 <p>Prior to the actual operation the scalar is converted to the same type as
2004 the arrays. In the case of floating-point arrays their bit representations are used for the
2005 operation. All the arrays must have the same type, except the mask, and the same size</p>
2007 <p>The following sample demonstrates how to conjugate complex vector
2008 by switching the most-significant bit of imaging part:</p>
2010 float a[] = { 1, 0, 0, 1, -1, 0, 0, -1 }; /* 1, j, -1, -j */
2011 CvMat A = cvMat( 4, 1, CV_32FC2, &a );
2012 int i, neg_mask = 0x80000000;
2013 cvXorS( &A, cvScalar( 0, *(float*)&neg_mask, 0, 0 ), &A, 0 );
2014 for( i = 0; i < 4; i++ )
2015 printf("(%.1f, %.1f) ", a[i*2], a[i*2+1] );
2017 <p>The code should print:</p>
2018 <pre>(1.0,0.0) (0.0,-1.0) (-1.0,0.0) (0.0,1.0)</pre>
2021 <hr><h3><a name="decl_cvNot">Not</a></h3>
2022 <p class="Blurb">Performs per-element bit-wise inversion of array elements</p>
2024 void cvNot( const CvArr* src, CvArr* dst );
2026 <dt>src1<dd>The source array.
2027 <dt>dst<dd>The destination array.
2029 The function Not inverses every bit of every array element:</p>
2030 <pre>dst(I)=~src(I)</pre>
2033 <hr><h3><a name="decl_cvCmp">Cmp</a></h3>
2034 <p class="Blurb">Performs per-element comparison of two arrays</p>
2036 void cvCmp( const CvArr* src1, const CvArr* src2, CvArr* dst, int cmp_op );
2038 <dt>src1<dd>The first source array.
2039 <dt>src2<dd>The second source array. Both source array must have a single channel.
2040 <dt>dst<dd>The destination array, must have 8u or 8s type.
2041 <dt>cmp_op<dd>The flag specifying the relation between the elements to be checked:<br>
2042 CV_CMP_EQ - src1(I) "equal to" src2(I)<br>
2043 CV_CMP_GT - src1(I) "greater than" src2(I)<br>
2044 CV_CMP_GE - src1(I) "greater or equal" src2(I)<br>
2045 CV_CMP_LT - src1(I) "less than" src2(I)<br>
2046 CV_CMP_LE - src1(I) "less or equal" src2(I)<br>
2047 CV_CMP_NE - src1(I) "not equal to" src2(I)<br>
2049 The function <code>cvCmp</code> compares the corresponding elements of two arrays and
2050 fills the destination mask array:</p>
2052 dst(I)=src1(I) op src2(I),
2054 <p>where <code>op</code> is '=', '>', '>=', '<', '<=' or '!='.</p>
2055 <p><code>dst(I)</code> is set to 0xff (all '1'-bits) if the particular relation between the elements
2056 is true and 0 otherwise.
2057 All the arrays must have the same type, except the destination, and the same size (or ROI size)</p>
2060 <hr><h3><a name="decl_cvCmpS">CmpS</a></h3>
2061 <p class="Blurb">Performs per-element comparison of array and scalar</p>
2063 void cvCmpS( const CvArr* src, double value, CvArr* dst, int cmp_op );
2065 <dt>src<dd>The source array, must have a single channel.
2066 <dt>value<dd>The scalar value to compare each array element with.
2067 <dt>dst<dd>The destination array, must have 8u or 8s type.
2068 <dt>cmp_op<dd>The flag specifying the relation between the elements to be checked:<br>
2069 CV_CMP_EQ - src1(I) "equal to" value<br>
2070 CV_CMP_GT - src1(I) "greater than" value<br>
2071 CV_CMP_GE - src1(I) "greater or equal" value<br>
2072 CV_CMP_LT - src1(I) "less than" value<br>
2073 CV_CMP_LE - src1(I) "less or equal" value<br>
2074 CV_CMP_NE - src1(I) "not equal" value<br>
2076 The function <code>cvCmpS</code> compares the corresponding elements of array and scalar
2077 and fills the destination mask array:</p>
2079 dst(I)=src(I) op scalar,
2081 <p>where <code>op</code> is '=', '>', '>=', '<', '<=' or '!='.</p>
2082 <p><code>dst(I)</code> is set to 0xff (all '1'-bits) if the particular relation between the elements
2083 is true and 0 otherwise.
2084 All the arrays must have the same size (or ROI size)</p>
2088 <hr><h3><a name="decl_cvInRange">InRange</a></h3>
2089 <p class="Blurb">Checks that array elements lie between elements of two other arrays</p>
2091 void cvInRange( const CvArr* src, const CvArr* lower, const CvArr* upper, CvArr* dst );
2093 <dt>src<dd>The first source array.
2094 <dt>lower<dd>The inclusive lower boundary array.
2095 <dt>upper<dd>The exclusive upper boundary array.
2096 <dt>dst<dd>The destination array, must have 8u or 8s type.
2098 The function <code>cvInRange</code> does the range check for every element of the input array:</p>
2100 dst(I)=lower(I)<sub>0</sub> <= src(I)<sub>0</sub> < upper(I)<sub>0</sub>
2102 <p>for single-channel arrays,</p>
2104 dst(I)=lower(I)<sub>0</sub> <= src(I)<sub>0</sub> < upper(I)<sub>0</sub> &&
2105 lower(I)<sub>1</sub> <= src(I)<sub>1</sub> < upper(I)<sub>1</sub>
2106 </pre><p>for two-channel arrays etc.</p>
2107 <p><code>dst(I)</code> is set to 0xff (all '1'-bits) if <code>src(I)</code> is within the range and 0 otherwise.
2108 All the arrays must have the same type, except the destination, and the same size (or ROI size)</p>
2111 <hr><h3><a name="decl_cvInRangeS">InRangeS</a></h3>
2112 <p class="Blurb">Checks that array elements lie between two scalars</p>
2114 void cvInRangeS( const CvArr* src, CvScalar lower, CvScalar upper, CvArr* dst );
2116 <dt>src<dd>The first source array.
2117 <dt>lower<dd>The inclusive lower boundary.
2118 <dt>upper<dd>The exclusive upper boundary.
2119 <dt>dst<dd>The destination array, must have 8u or 8s type.
2121 The function <code>cvInRangeS</code> does the range check for every element of the input array:</p>
2123 dst(I)=lower<sub>0</sub> <= src(I)<sub>0</sub> < upper<sub>0</sub>
2125 <p>for a single-channel array,</p>
2127 dst(I)=lower<sub>0</sub> <= src(I)<sub>0</sub> < upper<sub>0</sub> &&
2128 lower<sub>1</sub> <= src(I)<sub>1</sub> < upper<sub>1</sub>
2129 </pre><p>for a two-channel array etc.</p>
2131 <p><code>dst(I)</code> is set to 0xff (all '1'-bits) if <code>src(I)</code> is within the range and 0 otherwise.
2132 All the arrays must have the same size (or ROI size)</p>
2135 <hr><h3><a name="decl_cvMax">Max</a></h3>
2136 <p class="Blurb">Finds per-element maximum of two arrays</p>
2138 void cvMax( const CvArr* src1, const CvArr* src2, CvArr* dst );
2140 <dt>src1<dd>The first source array.
2141 <dt>src2<dd>The second source array.
2142 <dt>dst<dd>The destination array.
2144 The function <code>cvMax</code> calculates per-element maximum of two arrays:</p>
2145 <pre>dst(I)=max(src1(I), src2(I))</pre>
2146 <p>All the arrays must have a single channel, the same data type
2147 and the same size (or ROI size).</p>
2150 <hr><h3><a name="decl_cvMaxS">MaxS</a></h3>
2151 <p class="Blurb">Finds per-element maximum of array and scalar</p>
2153 void cvMaxS( const CvArr* src, double value, CvArr* dst );
2155 <dt>src<dd>The first source array.
2156 <dt>value<dd>The scalar value.
2157 <dt>dst<dd>The destination array.
2159 The function <code>cvMaxS</code> calculates per-element maximum of array and scalar:</p>
2160 <pre>dst(I)=max(src(I), value)</pre>
2161 <p>All the arrays must have a single channel, the same data type
2162 and the same size (or ROI size).</p>
2165 <hr><h3><a name="decl_cvMin">Min</a></h3>
2166 <p class="Blurb">Finds per-element minimum of two arrays</p>
2168 void cvMin( const CvArr* src1, const CvArr* src2, CvArr* dst );
2170 <dt>src1<dd>The first source array.
2171 <dt>src2<dd>The second source array.
2172 <dt>dst<dd>The destination array.
2174 The function <code>cvMin</code> calculates per-element minimum of two arrays:</p>
2176 dst(I)=min(src1(I),src2(I))
2178 <p>All the arrays must have a single channel, the same data type
2179 and the same size (or ROI size).</p>
2183 <hr><h3><a name="decl_cvMinS">MinS</a></h3>
2184 <p class="Blurb">Finds per-element minimum of array and scalar</p>
2186 void cvMinS( const CvArr* src, double value, CvArr* dst );
2188 <dt>src<dd>The first source array.
2189 <dt>value<dd>The scalar value.
2190 <dt>dst<dd>The destination array.
2192 The function <code>cvMinS</code> calculates minimum of array and scalar:</p>
2193 <pre>dst(I)=min(src(I), value)</pre>
2194 <p>All the arrays must have a single channel, the same data type
2195 and the same size (or ROI size).</p>
2198 <hr><h3><a name="decl_cvAbsDiff">AbsDiff</a></h3>
2199 <p class="Blurb">Calculates absolute difference between two arrays</p>
2201 void cvAbsDiff( const CvArr* src1, const CvArr* src2, CvArr* dst );
2203 <dt>src1<dd>The first source array.
2204 <dt>src2<dd>The second source array.
2205 <dt>dst<dd>The destination array.
2207 The function <code>cvAbsDiff</code> calculates absolute difference between two arrays.</p>
2208 <pre>dst(I)<sub>c</sub> = abs(src1(I)<sub>c</sub> - src2(I)<sub>c</sub>).</pre>
2209 <p>All the arrays must have the same data type and the same size (or ROI size).</p>
2212 <hr><h3><a name="decl_cvAbsDiffS">AbsDiffS</a></h3>
2213 <p class="Blurb">Calculates absolute difference between array and scalar</p>
2215 void cvAbsDiffS( const CvArr* src, CvArr* dst, CvScalar value );
2216 #define cvAbs(src, dst) cvAbsDiffS(src, dst, cvScalarAll(0))
2218 <dt>src<dd>The source array.
2219 <dt>dst<dd>The destination array.
2220 <dt>value<dd>The scalar.
2222 The function <code>cvAbsDiffS</code> calculates absolute difference between array and scalar.</p>
2223 <pre>dst(I)<sub>c</sub> = abs(src(I)<sub>c</sub> - value<sub>c</sub>).</pre>
2224 <p>All the arrays must have the same data type and the same size (or ROI size).</p>
2227 <hr><h2><a name="cxcore_arrays_stat">Statistics</a></h2>
2229 <hr><h3><a name="decl_cvCountNonZero">CountNonZero</a></h3>
2230 <p class="Blurb">Counts non-zero array elements</p>
2232 int cvCountNonZero( const CvArr* arr );
2234 <dt>arr<dd>The array, must be single-channel array or multi-channel image with COI set.
2236 The function <code>cvCountNonZero</code> returns the number of non-zero elements in src1:</p>
2238 result = sum<sub>I</sub> arr(I)!=0
2240 In case of <code>IplImage</code> both ROI and COI are supported.</p>
2243 <hr><h3><a name="decl_cvSum">Sum</a></h3>
2244 <p class="Blurb">Summarizes array elements</p>
2246 CvScalar cvSum( const CvArr* arr );
2248 <dt>arr<dd>The array.
2250 The function <code>cvSum</code> calculates sum <code>S</code> of array elements, independently for each channel:</p>
2252 S<sub>c</sub> = sum<sub>I</sub> arr(I)<sub>c</sub>
2254 If the array is <code>IplImage</code> and COI is set, the function processes the selected channel only
2255 and stores the sum to the first scalar component (S<sub>0</sub>).</p>
2258 <hr><h3><a name="decl_cvAvg">Avg</a></h3>
2259 <p class="Blurb">Calculates average (mean) of array elements</p>
2261 CvScalar cvAvg( const CvArr* arr, const CvArr* mask=NULL );
2263 <dt>arr<dd>The array.
2264 <dt>mask<dd>The optional operation mask.
2266 The function <code>cvAvg</code> calculates the average value <code>M</code> of array elements, independently for each channel:</p>
2268 N = sum<sub>I</sub> mask(I)!=0
2270 M<sub>c</sub> = 1/N • sum<sub>I,mask(I)!=0</sub> arr(I)<sub>c</sub>
2272 If the array is <code>IplImage</code> and COI is set, the function processes the selected channel only
2273 and stores the average to the first scalar component (S<sub>0</sub>).</p>
2276 <hr><h3><a name="decl_cvAvgSdv">AvgSdv</a></h3>
2277 <p class="Blurb">Calculates average (mean) of array elements</p>
2279 void cvAvgSdv( const CvArr* arr, CvScalar* mean, CvScalar* std_dev, const CvArr* mask=NULL );
2281 <dt>arr<dd>The array.
2282 <dt>mean<dd>Pointer to the mean value, may be NULL if it is not needed.
2283 <dt>std_dev<dd>Pointer to the standard deviation.
2284 <dt>mask<dd>The optional operation mask.
2286 The function <code>cvAvgSdv</code> calculates the average value and
2287 standard deviation of array elements, independently for each channel:</p>
2289 N = sum<sub>I</sub> mask(I)!=0
2291 mean<sub>c</sub> = 1/N • sum<sub>I,mask(I)!=0</sub> arr(I)<sub>c</sub>
2293 std_dev<sub>c</sub> = sqrt(1/N • sum<sub>I,mask(I)!=0</sub> (arr(I)<sub>c</sub> - M<sub>c</sub>)<sup>2</sup>)
2295 If the array is <code>IplImage</code> and COI is set, the function processes the selected channel only
2296 and stores the average and standard deviation to the first components of output scalars (M<sub>0</sub>
2297 and S<sub>0</sub>).</p>
2301 <hr><h3><a name="decl_cvMinMaxLoc">MinMaxLoc</a></h3>
2302 <p class="Blurb">Finds global minimum and maximum in array or subarray</p>
2304 void cvMinMaxLoc( const CvArr* arr, double* min_val, double* max_val,
2305 CvPoint* min_loc=NULL, CvPoint* max_loc=NULL, const CvArr* mask=NULL );
2307 <dt>arr<dd>The source array, single-channel or multi-channel with COI set.
2308 <dt>min_val<dd>Pointer to returned minimum value.
2309 <dt>max_val<dd>Pointer to returned maximum value.
2310 <dt>min_loc<dd>Pointer to returned minimum location.
2311 <dt>max_loc<dd>Pointer to returned maximum location.
2312 <dt>mask<dd>The optional mask that is used to select a subarray.
2314 The function <code>MinMaxLoc</code> finds minimum and maximum element values and their
2315 positions. The extremums are searched over the whole array, selected <code>ROI</code> (in case
2316 of <code>IplImage</code>) or, if <code>mask</code> is not <code>NULL</code>, in the specified array region.
2317 If the array has more than one channel, it must be <code>IplImage</code> with <code>COI</code> set.
2318 In case if multi-dimensional arrays <code>min_loc->x</code> and <code>max_loc->x</code> will contain raw (linear)
2319 positions of the extremums.</p>
2322 <hr><h3><a name="decl_cvNorm">Norm</a></h3>
2323 <p class="Blurb">Calculates absolute array norm, absolute difference norm or relative difference norm</p>
2325 double cvNorm( const CvArr* arr1, const CvArr* arr2=NULL, int norm_type=CV_L2, const CvArr* mask=NULL );
2327 <dt>arr1<dd>The first source image.
2328 <dt>arr2<dd>The second source image. If it is NULL, the absolute norm of <code>arr1</code> is calculated, otherwise
2329 absolute or relative norm of <code>arr1</code>-<code>arr2</code> is calculated.
2330 <dt>normType<dd>Type of norm, see the discussion.
2331 <dt>mask<dd>The optional operation mask.
2333 The function <code>cvNorm</code> calculates the absolute norm of <code>arr1</code> if <code>arr2</code> is NULL:</p>
2335 norm = ||arr1||<sub>C</sub> = max<sub>I</sub> abs(arr1(I)), if <code>normType</code> = CV_C
2337 norm = ||arr1||<sub>L1</sub> = sum<sub>I</sub> abs(arr1(I)), if <code>normType</code> = CV_L1
2339 norm = ||arr1||<sub>L2</sub> = sqrt( sum<sub>I</sub> arr1(I)<sup>2</sup>), if <code>normType</code> = CV_L2
2342 <p>And the function calculates absolute or relative difference norm if <code>arr2</code> is not NULL:</p>
2344 norm = ||arr1-arr2||<sub>C</sub> = max<sub>I</sub> abs(arr1(I)-arr2(I)), if <code>normType</code> = CV_C
2346 norm = ||arr1-arr2||<sub>L1</sub> = sum<sub>I</sub> abs(arr1(I)-arr2(I)), if <code>normType</code> = CV_L1
2348 norm = ||arr1-arr2||<sub>L2</sub> = sqrt( sum<sub>I</sub> (arr1(I)-arr2(I))<sup>2</sup> ), if <code>normType</code> = CV_L2
2352 norm = ||arr1-arr2||<sub>C</sub>/||arr2||<sub>C</sub>, if <code>normType</code> = CV_RELATIVE_C
2354 norm = ||arr1-arr2||<sub>L1</sub>/||arr2||<sub>L1</sub>, if <code>normType</code> = CV_RELATIVE_L1
2356 norm = ||arr1-arr2||<sub>L2</sub>/||arr2||<sub>L2</sub>, if <code>normType</code> = CV_RELATIVE_L2
2360 The function<code> Norm</code> returns the calculated norm.
2361 The multiple-channel array are treated as single-channel, that is, the results for all channels
2366 <hr><h3><a name="decl_cvReduce">Reduce</a></h3>
2367 <p class="Blurb">Reduces matrix to a vector</p>
2369 void cvReduce( const CvArr* src, CvArr* dst, int op=CV_REDUCE_SUM );
2371 <dt>src<dd>The input matrix.
2372 <dt>dst<dd>The output single-row/single-column vector that accumulates
2373 somehow all the matrix rows/columns.
2374 <dt>dim<dd>The dimension index along which the matrix is reduce.
2375 0 means that the matrix is reduced to a single row,
2376 1 means that the matrix is reduced to a single column.
2377 -1 means that the dimension is chosen automatically by analysing the <code>dst</code> size.
2378 <dt>op<dd>The reduction operation. It can take of the following values:<br>
2379 <code>CV_REDUCE_SUM</code> - the output is the sum of all the matrix rows/columns.<br>
2380 <code>CV_REDUCE_AVG</code> - the output is the mean vector of all the matrix rows/columns.<br>
2381 <code>CV_REDUCE_MAX</code> - the output is the maximum (column/row-wise) of all the matrix rows/columns.<br>
2382 <code>CV_REDUCE_MIN</code> - the output is the minimum (column/row-wise) of all the matrix rows/columns.<br>
2384 The function <code>cvReduce</code> reduces matrix to a vector by treating the matrix rows/columns as a set
2385 of 1D vectors and performing the specified operation on the vectors until a single row/column is obtained.
2386 For example, the function can be used to compute horizontal and vertical projections of an raster image.
2387 In case of <code>CV_REDUCE_SUM</code> and <code>CV_REDUCE_AVG</code> the output may have a larger
2388 element bit-depth to preserve accuracy. And multi-channel arrays are also supported in these two reduction modes.
2392 <!-- *****************************************************************************************
2393 *****************************************************************************************
2394 ***************************************************************************************** -->
2396 <hr><h2><a name="cxcore_arrays_matrix">Linear Algebra</a></h2>
2398 <hr><h3><a name="decl_cvDotProduct">DotProduct</a></h3>
2399 <p class="Blurb">Calculates dot product of two arrays in Euclidean metrics</p>
2401 double cvDotProduct( const CvArr* src1, const CvArr* src2 );
2403 <dt>src1<dd>The first source array.
2404 <dt>src2<dd>The second source array.
2406 The function <code>cvDotProduct</code> calculates and returns the Euclidean dot product of two
2408 <pre>src1•src2 = sum<sub>I</sub>(src1(I)*src2(I))
2410 <p>In case of multiple channel arrays the results for all channels are
2411 accumulated. In particular, <code><a href="#decl_cvDotProduct">cvDotProduct</a>(a,a)</code>,
2412 where <code>a</code> is a complex vector, will return <code>||a||<sup>2</sup></code>.
2413 The function can process multi-dimensional arrays, row by row, layer by layer and so on.</p>
2416 <hr><h3><a name="decl_cvNormalize">Normalize</a></h3>
2417 <p class="Blurb">Normalizes array to a certain norm or value range</p>
2419 void cvNormalize( const CvArr* src, CvArr* dst,
2420 double a=1, double b=0, int norm_type=CV_L2,
2421 const CvArr* mask=NULL );
2423 <dt>src<dd>The input array.
2424 <dt>dst<dd>The output array; in-place operation is supported.
2425 <dt>a<dd>The minimum/maximum value of the output array or the norm of output array.
2426 <dt>b<dd>The maximum/minimum value of the output array.
2427 <dt>norm_type<dd>The normalization type. It can take one of the following values:<br>
2428 <code>CV_C</code> - the C-norm (maximum of absolute values) of the array is normalized.<br>
2429 <code>CV_L1</code> - the L<sub>1</sub>-norm (sum of absolute values) of the array is normalized.<br>
2430 <code>CV_L2</code> - the (Euclidean) L<sub>2</sub>-norm of the array is normalized.<br>
2431 <code>CV_MINMAX</code> - the array values are scaled and shifted to the specified range.<br>
2432 <dt>mask<dd>The operation mask. Makes the function consider and normalize only certain array elements.
2434 The function <code>cvNormalize</code> normalizes the input array so that it's norm or value
2435 range takes the certain value(s).</p>
2436 <p>When <code>norm_type==CV_MINMAX</code>:
2438 dst(i,j)=(src(i,j)-min(src))*(b'-a')/(max(src)-min(src)) + a', if mask(i,j)!=0
2439 dst(i,j)=src(i,j) otherwise
2441 where <code>b'=MAX(a,b)</code>, <code>a'=MIN(a,b)</code>;<br>
2442 <code>min(src)</code> and <code>max(src)</code> are the global minimum and maximum, respectively,
2443 of the input array, computed over the whole array or the specified subset of it.
2444 <p>When <code>norm_type!=CV_MINMAX</code>:
2446 dst(i,j)=src(i,j)*a/<a href="#decl_cvNorm">cvNorm</a>(src,0,norm_type,mask), if mask(i,j)!=0
2447 dst(i,j)=src(i,j) otherwise
2449 <p>Here is the short example:</p>
2451 float v[3] = { 1, 2, 3 };
2452 CvMat V = cvMat( 1, 3, CV_32F, v );
2454 // make vector v unit-length;
2456 // for(int i=0;i<3;i++) v[i]/=sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
2457 cvNormalize( &V, &V );
2460 <hr><h3><a name="decl_cvCrossProduct">CrossProduct</a></h3>
2461 <p class="Blurb">Calculates cross product of two 3D vectors</p>
2463 void cvCrossProduct( const CvArr* src1, const CvArr* src2, CvArr* dst );
2465 <dt>src1<dd>The first source vector.
2466 <dt>src2<dd>The second source vector.
2467 <dt>dst<dd>The destination vector.
2469 The function <code>cvCrossProduct</code> calculates the cross product of two 3D vectors:</p>
2470 <pre>dst = src1 × src2, (dst<sub>1</sub> = src1<sub>2</sub>src2<sub>3</sub> - src1<sub>3</sub>src2<sub>2</sub> , dst<sub>2</sub> = src1<sub>3</sub>src2<sub>1</sub> - src1<sub>1</sub>src2<sub>3</sub> , dst<sub>3</sub> = src1<sub>1</sub>src2<sub>2</sub> - src1<sub>2</sub>src2<sub>1</sub>).</pre>
2473 <hr><h3><a name="decl_cvScaleAdd">ScaleAdd</a></h3>
2474 <p class="Blurb">Calculates sum of scaled array and another array</p>
2476 void cvScaleAdd( const CvArr* src1, CvScalar scale, const CvArr* src2, CvArr* dst );
2477 #define cvMulAddS cvScaleAdd
2479 <dt>src1<dd>The first source array.
2480 <dt>scale<dd>Scale factor for the first array.
2481 <dt>src2<dd>The second source array.
2482 <dt>dst<dd>The destination array
2484 The function <code>cvScaleAdd</code> calculates sum of scaled array and another array:</p>
2485 <pre>dst(I)=src1(I)*scale + src2(I)</pre>
2486 <p>All array parameters should have the same type and the same size.</p>
2489 <hr><h3><a name="decl_cvGEMM">GEMM</a></h3>
2490 <p class="Blurb">Performs generalized matrix multiplication</p>
2492 void cvGEMM( const CvArr* src1, const CvArr* src2, double alpha,
2493 const CvArr* src3, double beta, CvArr* dst, int tABC=0 );
2494 #define cvMatMulAdd( src1, src2, src3, dst ) cvGEMM( src1, src2, 1, src3, 1, dst, 0 )
2495 #define cvMatMul( src1, src2, dst ) cvMatMulAdd( src1, src2, 0, dst )
2497 <dt>src1<dd>The first source array.
2498 <dt>src2<dd>The second source array.
2499 <dt>src3<dd>The third source array (shift). Can be NULL, if there is no shift.
2500 <dt>dst<dd>The destination array.
2501 <dt>tABC<dd>The operation flags that can be 0 or combination of the following values:<br>
2502 CV_GEMM_A_T - transpose src1<br>
2503 CV_GEMM_B_T - transpose src2<br>
2504 CV_GEMM_C_T - transpose src3<br>
2505 for example, CV_GEMM_A_T+CV_GEMM_C_T corresponds to
2506 <pre>alpha*src1<sup>T</sup>*src2 + beta*src<sup>T</sup></pre>
2508 The function <code>cvGEMM</code> performs generalized matrix multiplication:</p>
2509 <pre>dst = alpha*op(src1)*op(src2) + beta*op(src3), where op(X) is X or X<sup>T</sup></pre>
2511 All the matrices should have the same data type and the coordinated sizes.
2512 Real or complex floating-point matrices are supported</p>
2515 <hr><h3><a name="decl_cvTransform">Transform</a></h3>
2516 <p class="Blurb">Performs matrix transform of every array element</p>
2518 void cvTransform( const CvArr* src, CvArr* dst, const CvMat* transmat, const CvMat* shiftvec=NULL );
2520 <dt>src<dd>The first source array.
2521 <dt>dst<dd>The destination array.
2522 <dt>transmat<dd>Transformation matrix.
2523 <dt>shiftvec<dd>Optional shift vector.
2525 The function <code>cvTransform</code> performs matrix transformation of every element of array <code>src</code>
2526 and stores the results in <code>dst</code>:</p>
2527 <pre>dst(I)=transmat*src(I) + shiftvec or dst(I)<sub>k</sub>=sum<sub>j</sub>(transmat(k,j)*src(I)<sub>j</sub>) + shiftvec(k)</pre>
2528 <p>That is every element of <code>N</code>-channel array <code>src</code> is considered as <code>N</code>-element vector, which is
2529 transformed using matrix <code>M</code>×<code>N</code> matrix <code>transmat</code> and shift vector <code>shiftvec</code>
2530 into an element of <code>M</code>-channel array <code>dst</code>.
2531 There is an option to embed <code>shiftvec</code> into <code>transmat</code>. In this case <code>transmat</code> should be
2532 <code>M</code>×<code>N+1</code> matrix and the right-most column is treated as the shift vector.</p>
2534 Both source and destination arrays should have the same depth and the same size or selected ROI
2535 size. <code>transmat</code> and <code>shiftvec</code> should be real floating-point matrices.
2536 <p>The function may be used for geometrical transformation of <code>N</code>D point set,
2537 arbitrary linear color space transformation, shuffling the channels etc.
2540 <hr><h3><a name="decl_cvPerspectiveTransform">PerspectiveTransform</a></h3>
2541 <p class="Blurb">Performs perspective matrix transform of vector array</p>
2543 void cvPerspectiveTransform( const CvArr* src, CvArr* dst, const CvMat* mat );
2545 <dt>src<dd>The source three-channel floating-point array.
2546 <dt>dst<dd>The destination three-channel floating-point array.
2547 <dt>mat<dd>3×3 or 4×4 transformation matrix.
2549 The function <code>cvPerspectiveTransform</code> transforms every element of <code>src</code>
2550 (by treating it as 2D or 3D vector) in the following way:</p>
2552 (x, y, z) -> (x’/w, y’/w, z’/w) or
2553 (x, y) -> (x’/w, y’/w),
2556 (x’, y’, z’, w’) = mat4x4*(x, y, z, 1) or
2557 (x’, y’, w’) = mat3x3*(x, y, 1)
2559 and w = w’ if w’!=0,
2564 <hr><h3><a name="decl_cvMulTransposed">MulTransposed</a></h3>
2565 <p class="Blurb">Calculates product of array and transposed array</p>
2567 void cvMulTransposed( const CvArr* src, CvArr* dst, int order, const CvArr* delta=NULL );
2569 <dt>src<dd>The source matrix.
2570 <dt>dst<dd>The destination matrix.
2571 <dt>order<dd>Order of multipliers.
2572 <dt>delta<dd>An optional array, subtracted from <code>src</code> before multiplication.
2574 The function <code>cvMulTransposed</code> calculates the product of src and its transposition.</p>
2575 <p>The function evaluates</p>
2576 <pre>dst=(src-delta)*(src-delta)<sup>T</sup></pre>
2578 <pre>dst=(src-delta)<sup>T</sup>*(src-delta)</pre>
2582 <hr><h3><a name="decl_cvTrace">Trace</a></h3>
2583 <p class="Blurb">Returns trace of matrix</p>
2585 CvScalar cvTrace( const CvArr* mat );
2587 <dt>mat<dd>The source matrix.
2589 The function <code>cvTrace</code> returns sum of diagonal elements of the matrix <code>src1</code>.
2591 tr(src1)=sum<sub>i</sub>mat(i,i)
2595 <hr><h3><a name="decl_cvTranspose">Transpose</a></h3>
2596 <p class="Blurb">Transposes matrix</p>
2598 void cvTranspose( const CvArr* src, CvArr* dst );
2599 #define cvT cvTranspose
2601 <dt>src<dd>The source matrix.
2602 <dt>dst<dd>The destination matrix.
2604 The function <code>cvTranspose</code> transposes matrix <code>src1</code>:</p>
2608 <p>Note that no complex conjugation is done in case of complex matrix. Conjugation
2609 should be done separately: look at the sample code in <a href="#decl_cvXorS">cvXorS</a> for example</p>
2612 <hr><h3><a name="decl_cvDet">Det</a></h3>
2613 <p class="Blurb">Returns determinant of matrix</p>
2615 double cvDet( const CvArr* mat );
2617 <dt>mat<dd>The source matrix.
2619 The function <code>cvDet</code> returns determinant of the square matrix <code>mat</code>.
2620 The direct method is used for small matrices and Gaussian elimination is used for larger matrices.
2621 For symmetric positive-determined matrices it is also possible to run <a href="#decl_cvSVD">SVD</a>
2622 with <code>U=V=NULL</code> and then calculate determinant as a product of the diagonal elements of <code>W</code></p>
2625 <hr><h3><a name="decl_cvInvert">Invert</a></h3>
2626 <p class="Blurb">Finds inverse or pseudo-inverse of matrix</p>
2628 double cvInvert( const CvArr* src, CvArr* dst, int method=CV_LU );
2629 #define cvInv cvInvert
2631 <dt>src<dd>The source matrix.
2632 <dt>dst<dd>The destination matrix.
2633 <dt>method<dd>Inversion method:<br>
2634 CV_LU - Gaussian elimination with optimal pivot element chose<br>
2635 CV_SVD - Singular value decomposition (SVD) method<br>
2636 CV_SVD_SYM - SVD method for a symmetric positively-defined matrix<br>
2638 The function <code>cvInvert</code> inverts matrix <code>src1</code> and stores the result in <code>src2</code></p>
2639 <p>In case of <code>LU</code> method the function returns <code>src1</code> determinant (src1 must be square).
2640 If it is 0, the matrix is not inverted and <code>src2</code> is filled with zeros.</p>
2641 <p>In case of <code>SVD</code> methods the function returns the inverted condition number of <code>src1</code>
2642 (ratio of the smallest singular value to the largest singular value) and 0 if <code>src1</code> is all zeros.
2643 The SVD methods calculate a pseudo-inverse matrix if <code>src1</code> is singular</p>
2646 <hr><h3><a name="decl_cvSolve">Solve</a></h3>
2647 <p class="Blurb">Solves linear system or least-squares problem</p>
2649 int cvSolve( const CvArr* A, const CvArr* B, CvArr* X, int method=CV_LU );
2651 <dt>A<dd>The source matrix.
2652 <dt>B<dd>The right-hand part of the linear system.
2653 <dt>X<dd>The output solution.
2654 <dt>method<dd>The solution (matrix inversion) method:<br>
2655 CV_LU - Gaussian elimination with optimal pivot element chose<br>
2656 CV_SVD - Singular value decomposition (SVD) method<br>
2657 CV_SVD_SYM - SVD method for a symmetric positively-defined matrix.
2659 The function <code>cvSolve</code> solves linear system or least-squares problem (the latter
2660 is possible with SVD methods):</p>
2662 dst = arg min<sub>X</sub>||A*X-B||
2664 <p>If <code>CV_LU</code> method is used, the function returns 1 if <code>src1</code> is non-singular and
2665 0 otherwise, in the latter case <code>dst</code> is not valid</p>
2668 <hr><h3><a name="decl_cvSVD">SVD</a></h3>
2669 <p class="Blurb">Performs singular value decomposition of real floating-point matrix</p>
2671 void cvSVD( CvArr* A, CvArr* W, CvArr* U=NULL, CvArr* V=NULL, int flags=0 );
2674 <dt>A<dd>Source <code>M</code>×<code>N</code> matrix.
2675 <dt>W<dd>Resulting singular value matrix (<code>M</code>×<code>N</code> or <code>N</code>×<code>N</code>) or
2676 vector (<code>N</code>×<code>1</code>).
2677 <dt>U<dd>Optional left orthogonal matrix (<code>M</code>×<code>M</code> or <code>M</code>×<code>N</code>).
2678 If CV_SVD_U_T is specified, the number of rows and columns in the sentence above should be swapped.
2679 <dt>V<dd>Optional right orthogonal matrix (<code>N</code>×<code>N</code>)
2680 <dt>flags<dd>Operation flags; can be 0 or combination of the following values:
2682 <li> <code>CV_SVD_MODIFY_A</code> enables modification of matrix <code>src1</code> during the operation. It
2683 speeds up the processing.
2684 <li> <code>CV_SVD_U_T</code> means that the transposed matrix <code>U</code> is returned.
2685 Specifying the flag speeds up the processing.
2686 <li> <code>CV_SVD_V_T</code> means that the transposed matrix <code>V</code> is returned.
2687 Specifying the flag speeds up the processing.
2690 The function <code>cvSVD</code> decomposes matrix <code>A</code> into a product of a diagonal matrix and two
2691 orthogonal matrices:</p>
2695 <p>Where <code>W</code> is diagonal matrix of singular values that can be coded as a 1D
2696 vector of singular values and <code>U</code> and <code>V</code>. All the singular values are non-negative and sorted (together with <code>U</code> and
2697 and <code>V</code> columns) in descending order.</p>
2698 <p>SVD algorithm is numerically robust and its typical applications include:
2700 <li>accurate eigenvalue problem solution when matrix <code>A</code> is square, symmetric and positively
2701 defined matrix, for example, when it is a covariation matrix.
2702 <code>W</code> in this case will be a vector of eigenvalues, and <code>U</code>=<code>V</code> is matrix of
2703 eigenvectors (thus, only one of <code>U</code> or <code>V</code> needs to be calculated if
2704 the eigenvectors are required)</li>
2705 <li>accurate solution of poor-conditioned linear systems</li>
2706 <li>least-squares solution of overdetermined linear systems. This and previous is done by <a href="#decl_cvSolve">cvSolve</a>
2707 function with <code>CV_SVD</code> method</li>
2708 <li>accurate calculation of different matrix characteristics such as rank
2709 (number of non-zero singular values),
2710 condition number (ratio of the largest singular value to the smallest one),
2711 determinant (absolute value of determinant is equal to the product of singular values).
2712 All the things listed in this item do not require calculation of <code>U</code>
2713 and <code>V</code> matrices.</li>
2718 <hr><h3><a name="decl_cvSVBkSb">SVBkSb</a></h3>
2719 <p class="Blurb">Performs singular value back substitution</p>
2721 void cvSVBkSb( const CvArr* W, const CvArr* U, const CvArr* V,
2722 const CvArr* B, CvArr* X, int flags );
2725 <dt>W<dd>Matrix or vector of singular values.
2726 <dt>U<dd>Left orthogonal matrix (transposed, perhaps)
2727 <dt>V<dd>Right orthogonal matrix (transposed, perhaps)
2728 <dt>B<dd>The matrix to multiply the pseudo-inverse of the original matrix <code>A</code> by.
2729 This is the optional parameter. If it is omitted then it is assumed to be
2730 an identity matrix of an appropriate size (So <code>X</code> will be the reconstructed
2731 pseudo-inverse of <code>A</code>).
2732 <dt>X<dd>The destination matrix: result of back substitution.
2733 <dt>flags<dd>Operation flags, should match exactly to the <code>flags</code> passed to <a href="#decl_cvSVD">cvSVD</a>.
2735 The function <code>cvSVBkSb</code> calculates back substitution for decomposed matrix <code>A</code> (see
2736 <a href="#decl_cvSVD">cvSVD</a> description) and matrix <code>B</code>:</p>
2738 X=V*W<sup>-1</sup>*U<sup>T</sup>*B
2742 W<sup>-1</sup>(i,i)=1/W(i,i) if W(i,i) > epsilon•sum<sub>i</sub>W(i,i),
2745 <p>And <code>epsilon</code> is a small number that depends on the matrix data type.
2747 This function together with <a href="#decl_cvSVD">cvSVD</a> is used inside <a href="#decl_cvInvert">cvInvert</a> and <a href="#decl_cvSolve">cvSolve</a>,
2748 and the possible reason to use these (svd & bksb) "low-level" function is to avoid
2749 temporary matrices allocation inside the high-level counterparts (inv & solve).</p>
2752 <hr><h3><a name="decl_cvEigenVV">EigenVV</a></h3>
2753 <p class="Blurb">Computes eigenvalues and eigenvectors of symmetric matrix</p>
2755 void cvEigenVV( CvArr* mat, CvArr* evects, CvArr* evals, double eps=0 );
2757 <dt>mat<dd>The input symmetric square matrix. It is modified during the processing.
2758 <dt>evects<dd>The output matrix of eigenvectors, stored as a subsequent rows.
2759 <dt>evals<dd>The output vector of eigenvalues, stored in the descending order (order of
2760 eigenvalues and eigenvectors is synchronized, of course).
2761 <dt>eps<dd>Accuracy of diagonalization (typically, DBL_EPSILON=≈10<sup>-15</sup> is enough).
2763 The function <code>cvEigenVV</code> computes the eigenvalues and eigenvectors of the matrix <code>A</code>:</p>
2764 <pre>mat*evects(i,:)' = evals(i)*evects(i,:)' (in MATLAB notation)</pre>
2765 <p>The contents of matrix <code>A</code> is destroyed by the function.</p>
2766 <p>Currently the function is slower than <a href="#decl_cvSVD">cvSVD</a> yet less accurate,
2767 so if <code>A</code> is known to be positively-defined (for example, it is a covariation matrix),
2768 it is recommended to use <a href="#decl_cvSVD">cvSVD</a> to find eigenvalues and eigenvectors of <code>A</code>,
2769 especially if eigenvectors are not required. That is, instead of
2771 cvEigenVV(mat, eigenvals, eigenvects);
2775 cvSVD(mat, eigenvals, eigenvects, 0, CV_SVD_U_T + CV_SVD_MODIFY_A);
2780 <hr><h3><a name="decl_cvCalcCovarMatrix">CalcCovarMatrix</a></h3>
2781 <p class="Blurb">Calculates covariation matrix of the set of vectors</p>
2783 void cvCalcCovarMatrix( const CvArr** vects, int count, CvArr* cov_mat, CvArr* avg, int flags );
2785 <dt>vects<dd>The input vectors. They all must have the same type and the same size.
2786 The vectors do not have to be 1D, they can be 2D (e.g. images) etc.
2787 <dt>count<dd>The number of input vectors.
2788 <dt>cov_mat<dd>The output covariation matrix that should be floating-point and square.
2789 <dt>avg<dd>The input or output (depending on the flags) array - the mean (average) vector of the input vectors.
2790 <dt>flags<dd>The operation flags, a combination of the following values:<br>
2791 <code>CV_COVAR_SCRAMBLED</code> - the output covariation matrix is calculated as:<br>
2792 <code>scale*[vects[0]-avg,vects[1]-avg,...]<sup>T</sup>*[vects[0]-avg,vects[1]-avg,...]</code>,<br>
2793 that is, the covariation matrix is <code>count</code>×<code>count</code>. Such an unusual
2794 covariation matrix is used for fast PCA of a set of very large vectors
2795 (see, for example, Eigen Faces technique for face recognition). Eigenvalues of this
2796 "scrambled" matrix will match to the eigenvalues of the true covariation matrix and
2797 the "true" eigenvectors can be easily calculated from the eigenvectors of the "scrambled" covariation
2799 <code>CV_COVAR_NORMAL</code> - the output covariation matrix is calculated as:<br>
2800 <code>scale*[vects[0]-avg,vects[1]-avg,...]*[vects[0]-avg,vects[1]-avg,...]<sup>T</sup></code>,<br>
2801 that is, <code>cov_mat</code> will be a usual covariation matrix with the same linear size as the total number of
2802 elements in every input vector. One and only one of <code>CV_COVAR_SCRAMBLED</code> and <code>CV_COVAR_NORMAL</code> must be specified<br>
2803 <code>CV_COVAR_USE_AVG</code> - if the flag is specified, the function does not calculate <code>avg</code> from
2804 the input vectors, but, instead, uses the passed <code>avg</code> vector. This is useful if <code>avg</code>
2805 has been already calculated somehow, or if the covariation matrix is calculated by parts - in this case,
2806 <code>avg</code> is not a mean vector of the input sub-set of vectors, but rather the mean vector of
2808 <code>CV_COVAR_SCALE</code> - if the flag is specified, the covariation matrix is scaled by
2809 the number of input vectors.
2810 <code>CV_COVAR_ROWS</code> - Means that all the input vectors are stored as rows of a single matrix, <code>vects[0]</code>.
2811 <code>count</code> is ignored in this case, and <code>avg</code> should be a single-row vector of an appropriate size.
2812 <code>CV_COVAR_COLS</code> - Means that all the input vectors are stored as columns of a single matrix, <code>vects[0]</code>.
2813 <code>count</code> is ignored in this case, and <code>avg</code> should be a single-column vector of an appropriate size.
2815 The function <code>cvCalcCovarMatrix</code> calculates the covariation matrix and, optionally,
2816 mean vector of the set of input vectors. The function can be used for PCA, for comparing vectors using Mahalanobis distance etc.
2820 <hr><h3><a name="decl_cvMahalonobis">Mahalonobis</a></h3>
2821 <p class="Blurb">Calculates Mahalonobis distance between two vectors</p>
2823 double cvMahalanobis( const CvArr* vec1, const CvArr* vec2, CvArr* mat );
2825 <dt>vec1<dd>The first 1D source vector.
2826 <dt>vec2<dd>The second 1D source vector.
2827 <dt>mat<dd>The inverse covariation matrix.
2829 The function <code>cvMahalonobis</code> calculates the weighted distance between two vectors
2832 d(vec1,vec2)=sqrt( sum<sub>i,j</sub> {mat(i,j)*(vec1(i)-vec2(i))*(vec1(j)-vec2(j))} )
2834 <p>The covariation matrix may be calculated using <a href="#decl_cvCalcCovarMatrix">cvCalcCovarMatrix</a> function and further
2835 inverted using <a href="#decl_cvInvert">cvInvert</a> function (CV_SVD method is the preferred one, because the matrix
2836 might be singular).</p>
2839 <hr><h3><a name="decl_cvCalcPCA">CalcPCA</a></h3>
2840 <p class="Blurb">Performs Principal Component Analysis of a vector set</p>
2842 void cvCalcPCA( const CvArr* data, CvArr* avg,
2843 CvArr* eigenvalues, CvArr* eigenvectors, int flags );
2845 <dt>data<dd>The input data; each vector is either a single row (<code>CV_PCA_DATA_AS_ROW</code>)
2846 or a single column (<code>CV_PCA_DATA_AS_COL</code>).
2847 <dt>avg<dd>The mean (average) vector, computed inside the function or provided by user.
2848 <dt>eigenvalues<dd>The output eigenvalues of covariation matrix.
2849 <dt>eigenvectors<dd>The output eigenvectors of covariation matrix (i.e. principal components);
2851 <dt>flags<dd>The operation flags, a combination of the following values:<br>
2852 <code>CV_PCA_DATA_AS_ROW</code> - the vectors are stored as rows
2853 (i.e. all the components of a certain vector are stored continuously)<br>
2854 <code>CV_PCA_DATA_AS_COL</code> - the vectors are stored as columns
2855 (i.e. values of a certain vector component are stored continuously)<br>
2856 (the above two flags are mutually exclusive)<br>
2857 <code>CV_PCA_USE_AVG</code> - use pre-computed average vector<br>
2859 The function <code>cvCalcPCA</code> performs PCA analysis of the vector set.
2860 First, it uses <a href="#decl_cvCalcCovarMatrix">cvCalcCovarMatrix</a> to compute covariation matrix
2861 and then it finds its eigenvalues and eigenvectors. The output number of eigenvalues/eigenvectors
2862 should be less than or equal to <code>MIN(rows(data),cols(data))</code>.
2865 <hr><h3><a name="decl_cvProjectPCA">ProjectPCA</a></h3>
2866 <p class="Blurb">Projects vectors to the specified subspace</p>
2868 void cvProjectPCA( const CvArr* data, const CvArr* avg,
2869 const CvArr* eigenvectors, CvArr* result )
2871 <dt>data<dd>The input data; each vector is either a single row
2873 <dt>avg<dd>The mean (average) vector. If it is a single-row vector,
2874 it means that the input vectors are stored as rows of <code>data</code>;
2875 otherwise, it should be a single-column vector,
2876 then the vectors are stored as columns of <code>data</code>.
2877 <dt>eigenvectors<dd>The eigenvectors (principal components); one vector per row.
2878 <dt>result<dd>The output matrix of decomposition coefficients. The number of rows must be
2879 the same as the number of vectors, the number of columns must
2880 be less than or equal to the number of rows in <code>eigenvectors</code>.
2881 That it is less, the input vectors are projected into subspace
2882 of the first <code>cols(result)</code> principal components.
2884 The function <code>cvProjectPCA</code> projects input vectors to the subspace represented by the
2885 orthonormal basis (<code>eigenvectors</code>). Before computing the dot products, <code>avg</code>
2886 vector is subtracted from the input vectors:</p>
2888 result(i,:)=(data(i,:)-avg)*eigenvectors' // for CV_PCA_DATA_AS_ROW layout.
2892 <hr><h3><a name="decl_cvBackProjectPCA">BackProjectPCA</a></h3>
2893 <p class="Blurb">Reconstructs the original vectors from the projection coefficients</p>
2895 void cvBackProjectPCA( const CvArr* proj, const CvArr* avg,
2896 const CvArr* eigenvects, CvArr* result );
2898 <dt>proj<dd>The input data; in the same format as <code>result</code> in
2899 <a href="#decl_cvProjectPCA">cvProjectPCA</a>.
2900 <dt>avg<dd>The mean (average) vector. If it is a single-row vector,
2901 it means that the output vectors are stored as rows of <code>result</code>;
2902 otherwise, it should be a single-column vector,
2903 then the vectors are stored as columns of <code>result</code>.
2904 <dt>eigenvectors<dd>The eigenvectors (principal components); one vector per row.
2905 <dt>result<dd>The output matrix of reconstructed vectors.
2907 The function <code>cvBackProjectPCA</code> reconstructs the vectors from the projection coefficients:</p>
2909 result(i,:)=proj(i,:)*eigenvectors + avg // for CV_PCA_DATA_AS_ROW layout.
2913 <hr><h2><a name="cxcore_arrays_math">Math Functions</a></h2>
2915 <hr><h3><a name="decl_cvRound">Round, Floor, Ceil</a></h3>
2916 <p class="Blurb">Converts floating-point number to integer</p>
2918 int cvRound( double value );
2919 int cvFloor( double value );
2920 int cvCeil( double value );
2922 <dt>value<dd>The input floating-point value
2924 The functions <code>cvRound, cvFloor</code> and <code>cvCeil</code> convert input floating-point number to
2925 integer using one of the rounding modes. <code>cvRound</code> returns the nearest integer value to
2927 <code>cvFloor</code> returns the maximum integer value that is not larger than the argument.
2928 <code>cvCeil</code> returns the minimum integer value that is not smaller than the argument.
2929 On some architectures the functions work <em>much</em> faster than the standard cast operations in C.
2930 If absolute value of the argument is greater than 2<sup>31</sup>, the result is not determined.
2931 Special values (±Inf, NaN) are not handled.
2935 <hr><h3><a name="decl_cvSqrt">Sqrt</a></h3>
2936 <p class="Blurb">Calculates square root</p>
2938 float cvSqrt( float value );
2940 <dt>value<dd>The input floating-point value
2942 The function <code>cvSqrt</code> calculates square root of the argument.
2943 If the argument is negative, the result is not determined.
2947 <hr><h3><a name="decl_cvInvSqrt">InvSqrt</a></h3>
2948 <p class="Blurb">Calculates inverse square root</p>
2950 float cvInvSqrt( float value );
2952 <dt>value<dd>The input floating-point value
2954 The function <code>cvInvSqrt</code> calculates inverse square root of the argument, and
2955 normally it is faster than <code>1./sqrt(value)</code>. If the argument is zero or negative, the result is
2956 not determined. Special values (±Inf, NaN) are not handled.
2960 <hr><h3><a name="decl_cvCbrt">Cbrt</a></h3>
2961 <p class="Blurb">Calculates cubic root</p>
2963 float cvCbrt( float value );
2965 <dt>value<dd>The input floating-point value
2967 The function <code>cvCbrt</code> calculates cubic root of the argument, and
2968 normally it is faster than <code>pow(value,1./3)</code>. Besides, negative arguments are handled properly.
2969 Special values (±Inf, NaN) are not handled.
2973 <hr><h3><a name="decl_cvFastArctan">FastArctan</a></h3>
2974 <p class="Blurb">Calculates angle of 2D vector</p>
2976 float cvFastArctan( float y, float x );
2978 <dt>x<dd>x-coordinate of 2D vector
2979 <dt>y<dd>y-coordinate of 2D vector
2981 The function <code>cvFastArctan</code> calculates full-range angle of input 2D vector.
2982 The angle is measured in degrees and varies from 0° to 360°. The accuracy is ~0.1°
2986 <hr><h3><a name="decl_cvIsNaN">IsNaN</a></h3>
2987 <p class="Blurb">Determines if the argument is Not A Number</p>
2989 int cvIsNaN( double value );
2991 <dt>value<dd>The input floating-point value
2993 The function <code>cvIsNaN</code> returns 1 if the argument is Not A Number (as defined
2994 by IEEE754 standard), 0 otherwise.
2997 <hr><h3><a name="decl_cvIsInf">IsInf</a></h3>
2998 <p class="Blurb">Determines if the argument is Infinity</p>
3000 int cvIsInf( double value );
3002 <dt>value<dd>The input floating-point value
3004 The function <code>cvIsInf</code> returns 1 if the argument is ±Infinity (as defined
3005 by IEEE754 standard), 0 otherwise.
3009 <hr><h3><a name="decl_cvCartToPolar">CartToPolar</a></h3>
3010 <p class="Blurb">Calculates magnitude and/or angle of 2d vectors</p>
3012 void cvCartToPolar( const CvArr* x, const CvArr* y, CvArr* magnitude,
3013 CvArr* angle=NULL, int angle_in_degrees=0 );
3015 <dt>x<dd>The array of x-coordinates
3016 <dt>y<dd>The array of y-coordinates
3017 <dt>magnitude<dd>The destination array of magnitudes, may be set to NULL if it is not needed
3018 <dt>angle<dd>The destination array of angles, may be set to NULL if it is not needed.
3019 The angles are measured in radians (0..2π) or in degrees (0..360°).
3020 <dt>angle_in_degrees<dd>The flag indicating whether the angles are measured in radians,
3021 which is default mode, or in degrees.
3023 The function <code>cvCartToPolar</code> calculates either magnitude, angle,
3024 or both of every 2d vector <code>(x(I),y(I))</code>:</p>
3026 magnitude(I)=sqrt( x(I)<sup>2</sup>+y(I)<sup>2</sup> ),
3027 angle(I)=atan( y(I)/x(I) )
3029 <p>The angles are calculated with ≈0.1° accuracy. For (0,0) point the angle
3033 <hr><h3><a name="decl_cvPolarToCart">PolarToCart</a></h3>
3034 <p class="Blurb">Calculates Cartesian coordinates of 2d vectors represented in polar form</p>
3036 void cvPolarToCart( const CvArr* magnitude, const CvArr* angle,
3037 CvArr* x, CvArr* y, int angle_in_degrees=0 );
3039 <dt>magnitude<dd>The array of magnitudes. If it is NULL, the magnitudes are assumed all 1’s.
3040 <dt>angle<dd>The array of angles, whether in radians or degrees.
3041 <dt>x<dd>The destination array of x-coordinates, may be set to NULL if it is not needed.
3042 <dt>y<dd>The destination array of y-coordinates, may be set to NULL if it is not needed.
3043 <dt>angle_in_degrees<dd>The flag indicating whether the angles are measured in radians,
3044 which is default mode, or in degrees.
3046 The function <code>cvPolarToCart</code> calculates either x-coordinate, y-coordinate
3047 or both of every vector <code>magnitude(I)*exp(angle(I)*j), j=sqrt(-1)</code>:</p>
3049 x(I)=magnitude(I)*cos(angle(I)),
3050 y(I)=magnitude(I)*sin(angle(I))
3054 <hr><h3><a name="decl_cvPow">Pow</a></h3>
3055 <p class="Blurb">Raises every array element to power</p>
3057 void cvPow( const CvArr* src, CvArr* dst, double power );
3059 <dt>src<dd>The source array.
3060 <dt>dst<dd>The destination array, should be the same type as the source.
3061 <dt>power<dd>The exponent of power.
3063 The function <code>cvPow</code> raises every element of input array to <code>p</code>:</p>
3065 dst(I)=src(I)<sup>p</sup>, if <code>p</code> is integer
3066 dst(I)=abs(src(I))<sup>p</sup>, otherwise
3068 <p>That is, for non-integer power exponent the absolute values of input
3069 array elements are used. However, it is possible to get true values
3070 for negative values using some extra operations, as the following sample,
3071 computing cube root of array elements, shows:</p>
3073 CvSize size = cvGetSize(src);
3074 CvMat* mask = cvCreateMat( size.height, size.width, CV_8UC1 );
3075 cvCmpS( src, 0, mask, CV_CMP_LT ); /* find negative elements */
3076 cvPow( src, dst, 1./3 );
3077 cvSubRS( dst, cvScalarAll(0), dst, mask ); /* negate the results of negative inputs */
3078 cvReleaseMat( &mask );
3080 <p>For some values of <code>power</code>, such as integer values, 0.5 and -0.5,
3081 specialized faster algorithms are used.</p>
3084 <hr><h3><a name="decl_cvExp">Exp</a></h3>
3085 <p class="Blurb">Calculates exponent of every array element</p>
3087 void cvExp( const CvArr* src, CvArr* dst );
3089 <dt>src<dd>The source array.
3090 <dt>dst<dd>The destination array, it should have <code>double</code> type or
3091 the same type as the source.
3093 The function <code>cvExp</code> calculates exponent of every element of input array:</p>
3097 <p>Maximum relative error is ≈7e-6. Currently,
3098 the function converts denormalized values to zeros on output.</p>
3101 <hr><h3><a name="decl_cvLog">Log</a></h3>
3102 <p class="Blurb">Calculates natural logarithm of every array element absolute value</p>
3104 void cvLog( const CvArr* src, CvArr* dst );
3106 <dt>src<dd>The source array.
3107 <dt>dst<dd>The destination array, it should have <code>double</code> type or
3108 the same type as the source.
3110 The function <code>cvLog</code> calculates natural logarithm
3111 of absolute value of every element of input array:</p>
3113 dst(I)=log(abs(src(I))), src(I)!=0
3116 Where <code>C</code> is large negative number (≈-700 in the current implementation)</p>
3119 <hr><h3><a name="decl_cvSolveCubic">SolveCubic</a></h3>
3120 <p class="Blurb">Finds real roots of a cubic equation</p>
3122 int cvSolveCubic( const CvMat* coeffs, CvMat* roots );
3124 <dt>coeffs<dd>The equation coefficients, array of 3 or 4 elements.
3125 <dt>roots<dd>The output array of real roots. Should have 3 elements.
3127 The function <code>cvSolveCubic</code> finds real roots of a cubic equation:</p>
3129 coeffs[0]*x<sup>3</sup> + coeffs[1]*x<sup>2</sup> + coeffs[2]*x + coeffs[3] = 0
3130 (if coeffs is 4-element vector)
3134 x<sup>3</sup> + coeffs[0]*x<sup>2</sup> + coeffs[1]*x + coeffs[2] = 0
3135 (if coeffs is 3-element vector)
3138 <p>The function returns the number of real roots found. The roots are stored
3139 to <code>root</code> array, which is padded with zeros if there is only one root.</p>
3141 <hr><h2><a name="cxcore_arrays_rng">Random Number Generation</a></h2>
3143 <hr><h3><a name="decl_cvRNG">RNG</a></h3>
3144 <p class="Blurb">Initializes random number generator state</p>
3146 CvRNG cvRNG( int64 seed=-1 );
3148 <dt>seed<dd>64-bit value used to initiate a random sequence.
3150 The function <code>cvRNG</code> initializes random number generator
3151 and returns the state. Pointer to the state can be then passed to <a href="#decl_cvRandInt">cvRandInt</a>,
3152 <a href="#decl_cvRandReal">cvRandReal</a> and <a href="#decl_cvRandArr">cvRandArr</a> functions.
3153 In the current implementation a multiply-with-carry generator is used.</p>
3156 <hr><h3><a name="decl_cvRandArr">RandArr</a></h3>
3157 <p class="Blurb">Fills array with random numbers and updates the RNG state</p>
3159 void cvRandArr( CvRNG* rng, CvArr* arr, int dist_type, CvScalar param1, CvScalar param2 );
3161 <dt>rng<dd>RNG state initialized by <a href="#decl_cvRNG">cvRNG</a>.
3162 <dt>arr<dd>The destination array.
3163 <dt>dist_type<dd>Distribution type:<br>
3164 CV_RAND_UNI - uniform distribution<br>
3165 CV_RAND_NORMAL - normal or Gaussian distribution<br>
3166 <dt>param1<dd>The first parameter of distribution. In case of uniform distribution it is
3167 the inclusive lower boundary of random numbers range. In case of
3168 normal distribution it is the mean value of random numbers.
3169 <dt>param2<dd>The second parameter of distribution. In case of uniform distribution it is
3170 the exclusive upper boundary of random numbers range. In case of
3171 normal distribution it is the standard deviation of random numbers.
3173 The function <code>cvRandArr</code> fills the destination array with
3174 uniformly or normally distributed random numbers. In the sample below
3175 the function is used to add a few normally distributed
3176 floating-point numbers to random locations within a 2d array</p>
3178 /* let noisy_screen be the floating-point 2d array that is to be "crapped" */
3179 CvRNG rng_state = cvRNG(0xffffffff);
3180 int i, pointCount = 1000;
3181 /* allocate the array of coordinates of points */
3182 CvMat* locations = cvCreateMat( pointCount, 1, CV_32SC2 );
3183 /* arr of random point values */
3184 CvMat* values = cvCreateMat( pointCount, 1, CV_32FC1 );
3185 CvSize size = cvGetSize( noisy_screen );
3187 cvRandInit( &rng_state,
3188 0, 1, /* use dummy parameters now and adjust them further */
3189 0xffffffff /* just use a fixed seed here */,
3190 CV_RAND_UNI /* specify uniform type */ );
3192 /* initialize the locations */
3193 cvRandArr( &rng_state, locations, CV_RAND_UNI, cvScalar(0,0,0,0), cvScalar(size.width,size.height,0,0) );
3195 /* modify RNG to make it produce normally distributed values */
3196 rng_state.disttype = CV_RAND_NORMAL;
3197 cvRandSetRange( &rng_state,
3199 100 /* average point brightness */,
3200 -1 /* initialize all the dimensions */ );
3201 /* generate values */
3202 cvRandArr( &rng_state, values, CV_RAND_NORMAL,
3203 cvRealScalar(100), // average intensity
3204 cvRealScalar(30) // deviation of the intensity
3207 /* set the points */
3208 for( i = 0; i < pointCount; i++ )
3210 CvPoint pt = *(CvPoint*)cvPtr1D( locations, i, 0 );
3211 float value = *(float*)cvPtr1D( values, i, 0 );
3212 *((float*)cvPtr2D( noisy_screen, pt.y, pt.x, 0 )) += value;
3215 /* not to forget to release the temporary arrays */
3216 cvReleaseMat( &locations );
3217 cvReleaseMat( &values );
3219 /* RNG state does not need to be deallocated */
3223 <hr><h3><a name="decl_cvRandInt">RandInt</a></h3>
3224 <p class="Blurb">Returns 32-bit unsigned integer and updates RNG</p>
3226 unsigned cvRandInt( CvRNG* rng );
3228 <dt>rng<dd>RNG state initialized by <code>RandInit</code> and, optionally,
3229 customized by <code>RandSetRange</code> (though, the latter function does not
3230 affect on the discussed function outcome).
3232 The function <code>cvRandInt</code> returns uniformly-distributed random
3233 32-bit unsigned integer and updates RNG state. It is similar to rand() function from C runtime library,
3234 but it always generates 32-bit number whereas rand() returns a number in between 0
3235 and <code>RAND_MAX</code> which is 2**16 or 2**32, depending on the platform.</p><p>
3236 The function is useful for generating scalar random numbers, such as points, patch sizes,
3237 table indices etc, where integer numbers of a certain range can be generated using
3238 modulo operation and floating-point numbers can be generated by scaling to
3239 0..1 of any other specific range. Here is the example from the previous function discussion
3240 rewritten using <a href="#decl_cvRandInt">cvRandInt</a>:</p>
3242 /* the input and the task is the same as in the previous sample. */
3243 CvRNG rng_state = cvRNG(0xffffffff);
3244 int i, pointCount = 1000;
3245 /* ... - no arrays are allocated here */
3246 CvSize size = cvGetSize( noisy_screen );
3247 /* make a buffer for normally distributed numbers to reduce call overhead */
3248 #define bufferSize 16
3249 float normalValueBuffer[bufferSize];
3250 CvMat normalValueMat = cvMat( bufferSize, 1, CV_32F, normalValueBuffer );
3253 for( i = 0; i < pointCount; i++ )
3256 /* generate random point */
3257 pt.x = cvRandInt( &rng_state ) % size.width;
3258 pt.y = cvRandInt( &rng_state ) % size.height;
3260 if( valuesLeft <= 0 )
3262 /* fulfill the buffer with normally distributed numbers if the buffer is empty */
3263 cvRandArr( &rng_state, &normalValueMat, CV_RAND_NORMAL, cvRealScalar(100), cvRealScalar(30) );
3264 valuesLeft = bufferSize;
3266 *((float*)cvPtr2D( noisy_screen, pt.y, pt.x, 0 ) = normalValueBuffer[--valuesLeft];
3269 /* there is no need to deallocate normalValueMat because we have
3270 both the matrix header and the data on stack. It is a common and efficient
3271 practice of working with small, fixed-size matrices */
3275 <hr><h3><a name="decl_cvRandReal">RandReal</a></h3>
3276 <p class="Blurb">Returns floating-point random number and updates RNG</p>
3278 double cvRandReal( CvRNG* rng );
3280 <dt>rng<dd>RNG state initialized by <a href="#decl_cvRNG">cvRNG</a>.
3282 The function <code>cvRandReal</code> returns uniformly-distributed random
3283 floating-point number from 0..1 range (1 is not included).</p>
3286 <hr><h2><a name="cxcore_arrays_dxt">Discrete Transforms</a></h2>
3288 <hr><h3><a name="decl_cvDFT">DFT</a></h3>
3289 <p class="Blurb">Performs forward or inverse Discrete Fourier transform of 1D or 2D floating-point array</p>
3291 #define CV_DXT_FORWARD 0
3292 #define CV_DXT_INVERSE 1
3293 #define CV_DXT_SCALE 2
3294 #define CV_DXT_ROWS 4
3295 #define CV_DXT_INV_SCALE (CV_DXT_SCALE|CV_DXT_INVERSE)
3296 #define CV_DXT_INVERSE_SCALE CV_DXT_INV_SCALE
3298 void cvDFT( const CvArr* src, CvArr* dst, int flags, int nonzero_rows=0 );
3300 <dt>src<dd>Source array, real or complex.
3301 <dt>dst<dd>Destination array of the same size and same type as the source.
3302 <dt>flags<dd>Transformation flags, a combination of the following values:<br>
3303 <code>CV_DXT_FORWARD</code> - do forward 1D or 2D transform. The result is not scaled.<br>
3304 <code>CV_DXT_INVERSE</code> - do inverse 1D or 2D transform. The result is not scaled.
3305 <code>CV_DXT_FORWARD</code> and <code>CV_DXT_INVERSE</code> are mutually exclusive,
3307 <code>CV_DXT_SCALE</code> - scale the result: divide it by the number of array elements.
3308 Usually, it is combined with <code>CV_DXT_INVERSE</code>, and one
3309 may use a shortcut <code>CV_DXT_INV_SCALE</code>.<br>
3310 <code>CV_DXT_ROWS</code> - do forward or inverse transform of every individual row of the input
3311 matrix. This flag allows user to transform multiple vectors simultaneously and
3312 can be used to decrease the overhead (which is sometimes several times
3313 larger than the processing itself), to do 3D and higher-dimensional
3315 <dt>nonzero_rows<dd>Number of nonzero rows to in the source array (in case of forward 2d transform),
3316 or a number of rows of interest in the destination array (in case of inverse 2d transform).
3317 If the value is negative, zero, or greater than the total number of rows, it is ignored.
3318 The parameter can be used to speed up 2d convolution/correlation when computing them via DFT.
3319 See the sample below.
3321 The function <code>cvDFT</code> performs forward or inverse transform of
3322 1D or 2D floating-point array:
3324 Forward Fourier transform of 1D vector of N elements:
3325 y = F<sup>(N)</sup>•x, where F<sup>(N)</sup><sub>jk</sub>=exp(-i•2Pi•j•k/N), i=sqrt(-1)
3327 Inverse Fourier transform of 1D vector of N elements:
3328 x'= (F<sup>(N)</sup>)<sup>-1</sup>•y = conj(F<sup>(N)</sup>)•y
3331 Forward Fourier transform of 2D vector of M×N elements:
3332 Y = F<sup>(M)</sup>•X•F<sup>(N)</sup>
3334 Inverse Fourier transform of 2D vector of M×N elements:
3335 X'= conj(F<sup>(M)</sup>)•Y•conj(F<sup>(N)</sup>)
3336 X = (1/(M•N))•X'
3339 <p>In case of real (single-channel) data, the packed format, borrowed from IPL, is used to
3340 to represent a result of forward Fourier transform or input for inverse Fourier transform:
3343 Re Y<sub>0,0</sub> Re Y<sub>0,1</sub> Im Y<sub>0,1</sub> Re Y<sub>0,2</sub> Im Y<sub>0,2</sub> ... Re Y<sub>0,N/2-1</sub> Im Y<sub>0,N/2-1</sub> Re Y<sub>0,N/2</sub>
3344 Re Y<sub>1,0</sub> Re Y<sub>1,1</sub> Im Y<sub>1,1</sub> Re Y<sub>1,2</sub> Im Y<sub>1,2</sub> ... Re Y<sub>1,N/2-1</sub> Im Y<sub>1,N/2-1</sub> Re Y<sub>1,N/2</sub>
3345 Im Y<sub>1,0</sub> Re Y<sub>2,1</sub> Im Y<sub>2,1</sub> Re Y<sub>2,2</sub> Im Y<sub>2,2</sub> ... Re Y<sub>2,N/2-1</sub> Im Y<sub>2,N/2-1</sub> Im Y<sub>2,N/2</sub>
3346 ............................................................................................
3347 Re Y<sub>M/2-1,0</sub> Re Y<sub>M-3,1</sub> Im Y<sub>M-3,1</sub> Re Y<sub>M-3,2</sub> Im Y<sub>M-3,2</sub> ... Re Y<sub>M-3,N/2-1</sub> Im Y<sub>M-3,N/2-1</sub> Re Y<sub>M-3,N/2</sub>
3348 Im Y<sub>M/2-1,0</sub> Re Y<sub>M-2,1</sub> Im Y<sub>M-2,1</sub> Re Y<sub>M-2,2</sub> Im Y<sub>M-2,2</sub> ... Re Y<sub>M-2,N/2-1</sub> Im Y<sub>M-2,N/2-1</sub> Im Y<sub>M-2,N/2</sub>
3349 Re Y<sub>M/2,0</sub> Re Y<sub>M-1,1</sub> Im Y<sub>M-1,1</sub> Re Y<sub>M-1,2</sub> Im Y<sub>M-1,2</sub> ... Re Y<sub>M-1,N/2-1</sub> Im Y<sub>M-1,N/2-1</sub> Im Y<sub>M-1,N/2</sub>
3352 <p>Note: the last column is present if <code>N</code> is even, the last row is present if <code>M</code> is even.</p>
3353 <p>In case of 1D real transform the result looks like the first row of the above matrix</p>
3355 <font color=blue size=4>Computing 2D Convolution using DFT</font></p>
3357 CvMat* A = cvCreateMat( M1, N1, CV_32F );
3358 CvMat* B = cvCreateMat( M2, N2, A->type );
3360 // it is also possible to have only abs(M2-M1)+1×abs(N2-N1)+1
3361 // part of the full convolution result
3362 CvMat* conv = cvCreateMat( A->rows + B->rows - 1, A->cols + B->cols - 1, A->type );
3364 // initialize A and B
3367 int dft_M = cvGetOptimalDFTSize( A->rows + B->rows - 1 );
3368 int dft_N = cvGetOptimalDFTSize( A->cols + B->cols - 1 );
3370 CvMat* dft_A = cvCreateMat( dft_M, dft_N, A->type );
3371 CvMat* dft_B = cvCreateMat( dft_M, dft_N, B->type );
3374 // copy A to dft_A and pad dft_A with zeros
3375 cvGetSubRect( dft_A, &tmp, cvRect(0,0,A->cols,A->rows));
3377 cvGetSubRect( dft_A, &tmp, cvRect(A->cols,0,dft_A->cols - A->cols,A->rows));
3379 // no need to pad bottom part of dft_A with zeros because of
3380 // use nonzero_rows parameter in cvDFT() call below
3382 cvDFT( dft_A, dft_A, CV_DXT_FORWARD, A->rows );
3384 // repeat the same with the second array
3385 cvGetSubRect( dft_B, &tmp, cvRect(0,0,B->cols,B->rows));
3387 cvGetSubRect( dft_B, &tmp, cvRect(B->cols,0,dft_B->cols - B->cols,B->rows));
3389 // no need to pad bottom part of dft_B with zeros because of
3390 // use nonzero_rows parameter in cvDFT() call below
3392 cvDFT( dft_B, dft_B, CV_DXT_FORWARD, B->rows );
3394 cvMulSpectrums( dft_A, dft_B, dft_A, 0 /* or CV_DXT_MUL_CONJ to get correlation
3395 rather than convolution */ );
3397 cvDFT( dft_A, dft_A, CV_DXT_INV_SCALE, conv->rows ); // calculate only the top part
3398 cvGetSubRect( dft_A, &tmp, cvRect(0,0,conv->cols,conv->rows) );
3400 cvCopy( &tmp, conv );
3404 <hr><h3><a name="decl_cvGetOptimalDFTSize">GetOptimalDFTSize</a></h3>
3405 <p class="Blurb">Returns optimal DFT size for given vector size</p>
3407 int cvGetOptimalDFTSize( int size0 );
3409 <dt>size0<dd>Vector size.
3411 The function <code>cvGetOptimalDFTSize</code> returns the minimum
3412 number <code>N</code> that is greater to equal to <code>size0</code>, such that DFT of a vector
3413 of size <code>N</code> can be computed fast. In the current implementation
3414 <code>N=2<sup>p</sup>×3<sup>q</sup>×5<sup>r</sup></code> for some <code>p, q, r</code>.
3416 <p>The function returns a negative number if <code>size0</code> is too large (very close to <code>INT_MAX</code>)</p>
3419 <hr><h3><a name="decl_cvMulSpectrums">MulSpectrums</a></h3>
3420 <p class="Blurb">Performs per-element multiplication of two Fourier spectrums</p>
3422 void cvMulSpectrums( const CvArr* src1, const CvArr* src2, CvArr* dst, int flags );
3424 <dt>src1<dd>The first source array.
3425 <dt>src2<dd>The second source array.
3426 <dt>dst<dd>The destination array of the same type and the same size of the sources.
3427 <dt>flags<dd>A combination of the following values:<br>
3428 <code>CV_DXT_ROWS</code> - treat each row of the arrays as a separate spectrum
3429 (see <a href="#decl_cvDFT">cvDFT</a> parameters description).<br>
3430 <code>CV_DXT_MUL_CONJ</code> - conjugate the second source array before the multiplication.<br>
3432 The function <code>cvMulSpectrums</code> performs per-element multiplication
3433 of the two CCS-packed or complex matrices that are results of real or complex Fourier transform.
3435 The function, together with <a href="#decl_cvDFT">cvDFT</a>, may be used to calculate convolution
3439 <hr><h3><a name="decl_cvDCT">DCT</a></h3>
3440 <p class="Blurb">Performs forward or inverse Discrete Cosine transform of 1D or 2D floating-point array</p>
3442 #define CV_DXT_FORWARD 0
3443 #define CV_DXT_INVERSE 1
3444 #define CV_DXT_ROWS 4
3446 void cvDCT( const CvArr* src, CvArr* dst, int flags );
3448 <dt>src<dd>Source array, real 1D or 2D array.
3449 <dt>dst<dd>Destination array of the same size and same type as the source.
3450 <dt>flags<dd>Transformation flags, a combination of the following values:<br>
3451 <code>CV_DXT_FORWARD</code> - do forward 1D or 2D transform.<br>
3452 <code>CV_DXT_INVERSE</code> - do inverse 1D or 2D transform.<br>
3453 <code>CV_DXT_ROWS</code> - do forward or inverse transform of every individual row of the input
3454 matrix. This flag allows user to transform multiple vectors simultaneously and
3455 can be used to decrease the overhead (which is sometimes several times
3456 larger than the processing itself), to do 3D and higher-dimensional
3459 The function <code>cvDCT</code> performs forward or inverse transform of
3460 1D or 2D floating-point array:
3462 Forward Cosine transform of 1D vector of N elements:
3463 y = C<sup>(N)</sup>•x, where C<sup>(N)</sup><sub>jk</sub>=sqrt((j==0?1:2)/N)•cos(Pi•(2k+1)•j/N)
3465 Inverse Cosine transform of 1D vector of N elements:
3466 x = (C<sup>(N)</sup>)<sup>-1</sup>•y = (C<sup>(N)</sup>)<sup>T</sup>•y
3468 Forward Cosine transform of 2D vector of M×N elements:
3469 Y = (C<sup>(M)</sup>)•X•(C<sup>(N)</sup>)<sup>T</sup>
3471 Inverse Cosine transform of 2D vector of M×N elements:
3472 X = (C<sup>(M)</sup>)<sup>T</sup>•Y•C<sup>(N)</sup>
3476 <hr><h1><a name="cxcore_ds">Dynamic Structures</a></h1>
3478 <hr><h2><a name="cxcore_ds_storages">Memory Storages</a></h2>
3480 <hr><h3><a name="decl_CvMemStorage">CvMemStorage</a></h3>
3481 <p class="Blurb">Growing memory storage</p>
3483 typedef struct CvMemStorage
3485 struct CvMemBlock* bottom;/* first allocated block */
3486 struct CvMemBlock* top; /* the current memory block - top of the stack */
3487 struct CvMemStorage* parent; /* borrows new blocks from */
3488 int block_size; /* block size */
3489 int free_space; /* free space in the <code>top</code> block (in bytes) */
3493 Memory storage is a low-level structure used to store dynamically growing data structures such as
3494 sequences, contours, graphs, subdivisions etc. It is organized as a list of memory blocks of
3495 equal size - <code>bottom</code> field is the beginning of the list of blocks</code>
3496 and <code>top</code> is the currently used block, but not necessarily the last block of the list. All blocks
3497 between <code>bottom</code> and <code>top</code>, not including the latter, are considered fully occupied;
3498 and all blocks between <code>top</code> and the last block, not including <code>top</code>,
3499 are considered free and <code>top</code> block itself is partly occupied - <code>free_space</code>
3500 contains the number of free bytes left in the end of <code>top</code>.
3501 </p><p>New memory buffer that may be allocated explicitly by <a href="#decl_cvMemStorageAlloc">cvMemStorageAlloc</a> function
3503 higher-level functions, such as <a href="#decl_cvSeqPush">cvSeqPush</a>, <a href="#decl_cvGraphAddEdge">cvGraphAddEdge</a> etc., <code>always</code>
3504 starts in the end of the current block if it fits there. After allocation <code>free_space</code>
3505 is decremented by the size of the allocated buffer plus some padding to keep the proper alignment.
3506 When the allocated buffer does not fit into the available part of <code>top</code>, the next storage
3507 block from the list is taken as <code>top</code> and <code>free_space</code> is reset to the
3508 whole block size prior to the allocation.</p><p>
3509 If there is no more free blocks, a new block is allocated (or borrowed from parent, see
3510 <a href="#decl_cvCreateChildMemStorage">cvCreateChildMemStorage</a>) and added to the end of list. Thus, the storage behaves as a stack
3511 with <code>bottom</code> indicating bottom of the stack and the pair (<code>top</code>, <code>free_space</code>)
3512 indicating top of the stack. The stack top may be saved via <a href="#decl_cvSaveMemStoragePos">cvSaveMemStoragePos</a>,
3513 restored via <a href="#decl_cvRestoreMemStoragePos">cvRestoreMemStoragePos</a> or reset via <a href="#decl_cvClearStorage">cvClearStorage</a>.
3517 <hr><h3><a name="decl_CvMemBlock">CvMemBlock</a></h3>
3518 <p class="Blurb">Memory storage block</p>
3520 typedef struct CvMemBlock
3522 struct CvMemBlock* prev;
3523 struct CvMemBlock* next;
3527 The structure <a href="#decl_CvMemBlock">CvMemBlock</a> represents a single block of memory storage.
3528 Actual data of the memory blocks follows the header, that is, the <code>i-th</code> byte of
3529 the memory block can be retrieved with the expression <code>((char*)(mem_block_ptr+1))[i]</code>.
3530 However, normally there is no need to access the storage structure fields directly.
3534 <hr><h3><a name="decl_CvMemStoragePos">CvMemStoragePos</a></h3>
3535 <p class="Blurb">Memory storage position</p>
3537 typedef struct CvMemStoragePos
3544 The structure described below stores the position of the stack top that can be
3545 saved via <a href="#decl_cvSaveMemStoragePos">cvSaveMemStoragePos</a> and restored via <a href="#decl_cvRestoreMemStoragePos">cvRestoreMemStoragePos</a>.</p>
3548 <hr><h3><a name="decl_cvCreateMemStorage">CreateMemStorage</a></h3>
3549 <p class="Blurb">Creates memory storage</p>
3551 CvMemStorage* cvCreateMemStorage( int block_size=0 );
3553 <dt>block_size<dd>Size of the storage blocks in bytes. If it is 0, the block size is set to default value
3554 - currently it is ≈64K.
3556 The function <code>cvCreateMemStorage</code> creates a memory storage and returns pointer
3557 to it. Initially the storage is empty. All fields of the header, except the <code>block_size</code>,
3561 <hr><h3><a name="decl_cvCreateChildMemStorage">CreateChildMemStorage</a></h3>
3562 <p class="Blurb">Creates child memory storage</p>
3564 CvMemStorage* cvCreateChildMemStorage( CvMemStorage* parent );
3566 <dt>parent<dd>Parent memory storage.
3568 The function <code>cvCreateChildMemStorage</code> creates a child memory storage that is similar
3569 to simple memory storage except for the differences in the memory
3570 allocation/deallocation mechanism. When a child storage needs a new block to
3571 add to the block list, it tries to get this block from the parent. The first
3572 unoccupied parent block available is taken and excluded from the parent block
3573 list. If no blocks are available, the parent either allocates a block or borrows
3574 one from its own parent, if any. In other words, the chain, or a more complex
3575 structure, of memory storages where every storage is a child/parent of another
3576 is possible. When a child storage is released or even cleared, it returns all
3577 blocks to the parent. In other aspects, the child storage is the same as the simple storage.</p>
3579 The children storages are useful in the following situation.
3580 Imagine that user needs to process dynamical data resided in some storage and
3581 put the result back to the same storage. With the simplest approach, when temporary
3582 data is resided in the same storage as the input and output data, the storage
3583 will look as following after processing:</p>
3585 <font color=blue>Dynamic data processing without using child storage</font>
3588 <img align="center" src="pics/memstorage1.png" >
3591 That is, garbage appears in the middle of the storage.
3592 However, if one creates a child memory storage in the beginning of the processing,
3593 writes temporary data there and releases the child storage in the end, no garbage will
3594 appear in the source/destination storage:</p>
3595 <p><font color=blue>Dynamic data processing using a child storage</font>
3597 <img align="center" src="pics/memstorage2.png" >
3601 <hr><h3><a name="decl_cvReleaseMemStorage">ReleaseMemStorage</a></h3>
3602 <p class="Blurb">Releases memory storage</p>
3604 void cvReleaseMemStorage( CvMemStorage** storage );
3606 <dt>storage<dd>Pointer to the released storage.
3608 The function <code>cvReleaseMemStorage</code> deallocates all storage memory blocks or returns
3609 them to the parent, if any. Then it deallocates the storage header and clears
3610 the pointer to the storage. All children of the storage must be released before
3611 the parent is released.</p>
3614 <hr><h3><a name="decl_cvClearMemStorage">ClearMemStorage</a></h3>
3615 <p class="Blurb">Clears memory storage</p>
3617 void cvClearMemStorage( CvMemStorage* storage );
3619 <dt>storage<dd>Memory storage.
3621 The function <code>cvClearMemStorage</code> resets the top (free space boundary) of the storage
3622 to the very beginning. This function does not deallocate any memory. If the
3623 storage has a parent, the function returns all blocks to the parent.</p>
3626 <hr><h3><a name="decl_cvMemStorageAlloc">MemStorageAlloc</a></h3>
3627 <p class="Blurb">Allocates memory buffer in the storage</p>
3629 void* cvMemStorageAlloc( CvMemStorage* storage, size_t size );
3631 <dt>storage<dd>Memory storage.
3632 <dt>size<dd>Buffer size.
3634 The function <code>cvMemStorageAlloc</code> allocates memory buffer in the storage. The buffer size
3635 must not exceed the storage block size, otherwise runtime error is raised. The buffer address is
3636 aligned by <code>CV_STRUCT_ALIGN</code> (=<code>sizeof(double)</code> for the moment) bytes.
3640 <hr><h3><a name="decl_cvMemStorageAllocString">MemStorageAllocString</a></h3>
3641 <p class="Blurb">Allocates text string in the storage</p>
3643 typedef struct CvString
3650 CvString cvMemStorageAllocString( CvMemStorage* storage, const char* ptr, int len=-1 );
3652 <dt>storage<dd>Memory storage.
3653 <dt>ptr<dd>The string.
3654 <dt>len<dd>Length of the string (not counting the ending '\0'). If the parameter is negative,
3655 the function computes the length.
3657 The function <code>cvMemStorageAllocString</code> creates copy of the
3658 string in the memory storage. It returns the structure that contains user-passed or computed length
3659 of the string and pointer to the copied string.
3663 <hr><h3><a name="decl_cvSaveMemStoragePos">SaveMemStoragePos</a></h3>
3664 <p class="Blurb">Saves memory storage position</p>
3666 void cvSaveMemStoragePos( const CvMemStorage* storage, CvMemStoragePos* pos );
3668 <dt>storage<dd>Memory storage.
3669 <dt>pos<dd>The output position of the storage top.
3671 The function <code>cvSaveMemStoragePos</code> saves the current position of the storage top to
3672 the parameter <code>pos</code>. The function <code>cvRestoreMemStoragePos</code> can further retrieve
3676 <hr><h3><a name="decl_cvRestoreMemStoragePos">RestoreMemStoragePos</a></h3>
3677 <p class="Blurb">Restores memory storage position</p>
3679 void cvRestoreMemStoragePos( CvMemStorage* storage, CvMemStoragePos* pos );
3681 <dt>storage<dd>Memory storage.
3682 <dt>pos<dd>New storage top position.
3684 The function <code>cvRestoreMemStoragePos</code> restores the position of the storage top from
3685 the parameter <code>pos</code>. This function and The function <code>cvClearMemStorage</code> are the
3686 only methods to release memory occupied in memory blocks.
3687 Note again that there is no way to free memory in the middle of the occupied part of the storage.
3691 <hr><h2><a name="cxcore_ds_sequences">Sequences</a></h2>
3693 <hr><h3><a name="decl_CvSeq">CvSeq</a></h3>
3694 <p class="Blurb">Growable sequence of elements</p>
3696 #define CV_SEQUENCE_FIELDS() \
3697 int flags; /* miscellaneous flags */ \
3698 int header_size; /* size of sequence header */ \
3699 struct CvSeq* h_prev; /* previous sequence */ \
3700 struct CvSeq* h_next; /* next sequence */ \
3701 struct CvSeq* v_prev; /* 2nd previous sequence */ \
3702 struct CvSeq* v_next; /* 2nd next sequence */ \
3703 int total; /* total number of elements */ \
3704 int elem_size;/* size of sequence element in bytes */ \
3705 char* block_max;/* maximal bound of the last block */ \
3706 char* ptr; /* current write pointer */ \
3707 int delta_elems; /* how many elements allocated when the sequence grows (sequence granularity) */ \
3708 CvMemStorage* storage; /* where the seq is stored */ \
3709 CvSeqBlock* free_blocks; /* free blocks list */ \
3710 CvSeqBlock* first; /* pointer to the first sequence block */
3713 typedef struct CvSeq
3715 CV_SEQUENCE_FIELDS()
3719 The structure <a href="#decl_CvSeq">CvSeq</a> is a base for all of OpenCV dynamic data structures.</p>
3720 <p>Such an unusual definition via a helper macro simplifies the extension of the structure
3721 <a href="#decl_CvSeq">CvSeq</a> with additional parameters.
3722 To extend <a href="#decl_CvSeq">CvSeq</a> the user may define a new structure and
3723 put user-defined fields after all <a href="#decl_CvSeq">CvSeq</a> fields that are included via the macro
3724 <code>CV_SEQUENCE_FIELDS()</code>.</p>
3725 <p>There are two types of sequences - dense and sparse. Base type for dense sequences is <a href="#decl_CvSeq">CvSeq</a>
3726 and such sequences are used to represent growable 1d arrays - vectors, stacks, queues, deques.
3727 They have no gaps in the middle - if an element is removed from the middle or inserted into the middle
3728 of the sequence the elements from the closer end are shifted.
3729 Sparse sequences have <a href="#decl_CvSet">CvSet</a> base
3730 class and they are discussed later in more details. They are sequences of nodes each of those may be
3731 either occupied or free as indicated by the node flag. Such sequences are used for unordered data
3732 structures such as sets of elements, graphs, hash tables etc.</p>
3734 <p>The field <code>header_size</code> contains the actual size of the
3735 sequence header and should be greater or equal to <code>sizeof(CvSeq)</code>.</p><p>The fields
3736 <code>h_prev, h_next, v_prev, v_next</code> can be used to create hierarchical structures
3737 from separate sequences. The fields <code>h_prev</code> and <code>h_next</code> point to the previous and
3738 the next sequences on the same hierarchical level while the fields <code>v_prev</code> and
3739 <code>v_next</code> point to the previous and the next sequence in the vertical direction,
3740 that is, parent and its first child. But these are just names and the pointers
3741 can be used in a different way.</p><p>The field <code>first</code> points to the first sequence
3742 block, whose structure is described below.</p>
3743 <p>The field <code>total</code> contains the actual number of dense sequence elements and
3744 number of allocated nodes in sparse sequence.</p>
3745 <p>The field <code>flags</code>contain the particular dynamic type
3746 signature (<code>CV_SEQ_MAGIC_VAL</code> for dense sequences and <code>CV_SET_MAGIC_VAL</code> for sparse sequences)
3747 in the highest 16 bits and miscellaneous information about the sequence.
3748 The lowest <code>CV_SEQ_ELTYPE_BITS</code> bits contain the ID of the
3749 element type. Most of sequence processing functions do not use element type
3750 but element size stored in <code>elem_size</code>.
3751 If sequence contains the numeric data of one of <a href="#decl_CvMat">CvMat</a> type
3752 then the element type matches to the corresponding <a href="#decl_CvMat">CvMat</a> element type, e.g.
3753 CV_32SC2 may be used for sequence of 2D points, CV_32FC1 for sequences of floating-point values etc.
3754 <code>CV_SEQ_ELTYPE(seq_header_ptr)</code> macro retrieves the type of sequence elements.
3755 Processing function that work with numerical sequences check that <code>elem_size</code> is equal
3756 to the calculated from the type element size.
3757 Besides <a href="#decl_CvMat">CvMat</a> compatible types, there are few extra element types defined
3758 in <a href="#decl_cvtypes.h">cvtypes.h</a> header:</p>
3759 <p><font color=blue>Standard Types of Sequence Elements</font></p>
3761 #define CV_SEQ_ELTYPE_POINT CV_32SC2 /* (x,y) */
3762 #define CV_SEQ_ELTYPE_CODE CV_8UC1 /* freeman code: 0..7 */
3763 #define CV_SEQ_ELTYPE_GENERIC 0 /* unspecified type of sequence elements */
3764 #define CV_SEQ_ELTYPE_PTR CV_USRTYPE1 /* =6 */
3765 #define CV_SEQ_ELTYPE_PPOINT CV_SEQ_ELTYPE_PTR /* &elem: pointer to element of other sequence */
3766 #define CV_SEQ_ELTYPE_INDEX CV_32SC1 /* #elem: index of element of some other sequence */
3767 #define CV_SEQ_ELTYPE_GRAPH_EDGE CV_SEQ_ELTYPE_GENERIC /* &next_o, &next_d, &vtx_o, &vtx_d */
3768 #define CV_SEQ_ELTYPE_GRAPH_VERTEX CV_SEQ_ELTYPE_GENERIC /* first_edge, &(x,y) */
3769 #define CV_SEQ_ELTYPE_TRIAN_ATR CV_SEQ_ELTYPE_GENERIC /* vertex of the binary tree */
3770 #define CV_SEQ_ELTYPE_CONNECTED_COMP CV_SEQ_ELTYPE_GENERIC /* connected component */
3771 #define CV_SEQ_ELTYPE_POINT3D CV_32FC3 /* (x,y,z) */
3774 The next <code>CV_SEQ_KIND_BITS</code> bits specify the kind of the sequence:</p>
3775 <p><font color=blue>Standard Kinds of Sequences</font></p>
3777 /* generic (unspecified) kind of sequence */
3778 #define CV_SEQ_KIND_GENERIC (0 << CV_SEQ_ELTYPE_BITS)
3780 /* dense sequence subtypes */
3781 #define CV_SEQ_KIND_CURVE (1 << CV_SEQ_ELTYPE_BITS)
3782 #define CV_SEQ_KIND_BIN_TREE (2 << CV_SEQ_ELTYPE_BITS)
3784 /* sparse sequence (or set) subtypes */
3785 #define CV_SEQ_KIND_GRAPH (3 << CV_SEQ_ELTYPE_BITS)
3786 #define CV_SEQ_KIND_SUBDIV2D (4 << CV_SEQ_ELTYPE_BITS)
3789 The remaining bits are used to identify different features specific to certain
3790 sequence kinds and element types. For example, curves made of points (
3791 <code>CV_SEQ_KIND_CURVE|CV_SEQ_ELTYPE_POINT</code> ), together with the flag
3792 <code>CV_SEQ_FLAG_CLOSED</code> belong to the type <code>CV_SEQ_POLYGON</code> or, if other flags are
3793 used, to its subtype. Many contour processing functions check the type of the input
3794 sequence and report an error if they do not support this type. The file
3795 <a href="#decl_cvtypes.h">cvtypes.h</a> stores the complete list of all supported predefined sequence types
3796 and helper macros designed to get the sequence type of other properties.
3797 Below follows the definition of the building block of sequences.</p>
3800 <hr><h3><a name="decl_CvSeqBlock">CvSeqBlock</a></h3>
3801 <p class="Blurb">Continuous sequence block</p>
3803 typedef struct CvSeqBlock
3805 struct CvSeqBlock* prev; /* previous sequence block */
3806 struct CvSeqBlock* next; /* next sequence block */
3807 int start_index; /* index of the first element in the block +
3808 sequence->first->start_index */
3809 int count; /* number of elements in the block */
3810 char* data; /* pointer to the first element of the block */
3814 Sequence blocks make up a circular double-linked list, so the pointers <code>prev</code> and
3815 <code>next</code> are never <code>NULL</code> and point to the previous and the next sequence blocks
3816 within the sequence. It means that <code>next</code> of the last block is the first block and
3817 <code>prev</code> of the first block is the last block. The fields <code>start_index</code> and <code>count</code> help
3818 to track the block location within the sequence. For example, if the sequence
3819 consists of 10 elements and splits into three blocks of 3, 5, and 2 elements,
3820 and the first block has the parameter <code>start_index = 2</code>, then pairs <code>(start_index, count)</code>
3821 for the sequence blocks are <code>(2,3), (5, 5)</code>, and <code>(10, 2)</code>
3822 correspondingly. The parameter <code>start_index</code> of the first block is usually <code>0</code>
3823 unless some elements have been inserted at the beginning of the sequence.
3827 <hr><h3><a name="decl_CvSlice">CvSlice</a></h3>
3828 <p class="Blurb">A sequence slice</p>
3830 typedef struct CvSlice
3836 inline CvSlice cvSlice( int start, int end );
3837 #define CV_WHOLE_SEQ_END_INDEX 0x3fffffff
3838 #define CV_WHOLE_SEQ cvSlice(0, CV_WHOLE_SEQ_END_INDEX)
3840 /* calculates the sequence slice length */
3841 int cvSliceLength( CvSlice slice, const CvSeq* seq );
3844 Some of functions that operate on sequences take <code>CvSlice slice</code> parameter that is
3845 often set to the whole sequence (CV_WHOLE_SEQ) by default. Either of the <code>start_index</code>
3846 and <code>end_index</code> may be negative or exceed the sequence length, <code>start_index</code>
3847 is inclusive, <code>end_index</code> is exclusive boundary. If they are equal, the slice is considered
3848 empty (i.e. contains no elements). Because sequences are treated as circular structures,
3849 the slice may select a few elements in the end of a sequence followed by a few elements in the beginning
3850 of the sequence, for example, <code>cvSlice(-2, 3)</code> in case of 10-element sequence will select 5-element slice,
3851 containing pre-last (8th), last (9th), the very first (0th), second (1th) and third (2nd) elements.
3852 The functions normalize the slice argument in the following way: first, <a href="#decl_CvSlice">cvSliceLength</a>
3853 is called to determine the length of the slice, then, <code>start_index</code> of the slice is
3854 normalized similarly to the argument of <a href="#decl_cvGetSeqElem">cvGetSeqElem</a>
3855 (i.e. negative indices are allowed).
3856 The actual slice to process starts at the normalized <code>start_index</code>
3857 and lasts <a href="#decl_CvSlice">cvSliceLength</a> elements
3858 (again, assuming the sequence is a circular structure).
3861 <p>If a function does not take slice argument, but you want to process only a part of the sequence,
3862 the sub-sequence may be extracted using <a href="#decl_cvSeqSlice">cvSeqSlice</a> function,
3863 or stored as into a continuous buffer with <a href="#decl_cvCvtSeqToArray">cvCvtSeqToArray</a>
3864 (optionally, followed by <a href="#decl_cvMakeSeqHeaderForArray">cvMakeSeqHeaderForArray</a>.
3868 <hr><h3><a name="decl_cvCreateSeq">CreateSeq</a></h3>
3869 <p class="Blurb">Creates sequence</p>
3871 CvSeq* cvCreateSeq( int seq_flags, int header_size,
3872 int elem_size, CvMemStorage* storage );
3874 <dt>seq_flags<dd>Flags of the created sequence. If the sequence is not passed to any
3875 function working with a specific type of sequences, the sequence value may be
3876 set to 0, otherwise the appropriate type must be selected from the list of
3877 predefined sequence types.
3878 <dt>header_size<dd>Size of the sequence header; must be greater or equal to
3879 <code>sizeof(CvSeq)</code>. If a specific type or its extension is indicated, this type must
3880 fit the base type header.
3881 <dt>elem_size<dd>Size of the sequence elements in bytes. The size must be consistent
3882 with the sequence type. For example, for a sequence of points to be created, the
3883 element type <code>CV_SEQ_ELTYPE_POINT</code> should be specified and the parameter <code>elem_size</code>
3884 must be equal to <code>sizeof(CvPoint)</code>.
3885 <dt>storage<dd>Sequence location.
3887 The function <code>cvCreateSeq</code> creates a sequence and returns the pointer to it. The
3888 function allocates the sequence header in the storage block as one continuous
3889 chunk and sets the structure fields <code>flags</code>, <code>elem_size</code>, <code>header_size</code> and <code>storage</code> to
3890 passed values, sets <code>delta_elems</code> to the default value (that may be reassigned using
3891 <a href="#decl_cvSetSeqBlockSize">cvSetSeqBlockSize</a> function), and clears other header fields,
3892 including the space after the first <code>sizeof(CvSeq)</code> bytes.</p>
3895 <hr><h3><a name="decl_cvSetSeqBlockSize">SetSeqBlockSize</a></h3>
3896 <p class="Blurb">Sets up sequence block size</p>
3898 void cvSetSeqBlockSize( CvSeq* seq, int delta_elems );
3900 <dt>seq<dd>Sequence.
3901 <dt>delta_elems<dd>Desirable sequence block size in elements.
3903 The function <code>cvSetSeqBlockSize</code> affects memory allocation granularity.
3904 When the free space in the sequence buffers has run out, the function allocates the space
3905 for <code>delta_elems</code> sequence elements. If this block immediately follows the one
3906 previously allocated, the two blocks are concatenated, otherwise, a new sequence
3907 block is created. Therefore, the bigger the parameter is, the lower the possible sequence
3908 fragmentation, but the more space in the storage is wasted. When the
3909 sequence is created, the parameter <code>delta_elems</code> is set to the default value ≈1K.
3910 The function can be called any time after the sequence is created and affects
3911 future allocations. The function can modify the passed value of the parameter to
3912 meet the memory storage constraints.
3916 <hr><h3><a name="decl_cvSeqPush">SeqPush</a></h3>
3917 <p class="Blurb">Adds element to sequence end</p>
3919 char* cvSeqPush( CvSeq* seq, void* element=NULL );
3921 <dt>seq<dd>Sequence.
3922 <dt>element<dd>Added element.
3924 The function <code>cvSeqPush</code> adds an element to the end of sequence and returns pointer
3925 to the allocated element. If the input <code>element</code> is NULL,
3926 the function simply allocates a space for one more element.</p>
3927 <p>The following code demonstrates how to create a new sequence using this function:</p>
3929 CvMemStorage* storage = cvCreateMemStorage(0);
3930 CvSeq* seq = cvCreateSeq( CV_32SC1, /* sequence of integer elements */
3931 sizeof(CvSeq), /* header size - no extra fields */
3932 sizeof(int), /* element size */
3933 storage /* the container storage */ );
3935 for( i = 0; i < 100; i++ )
3937 int* added = (int*)cvSeqPush( seq, &i );
3938 printf( "%d is added\n", *added );
3942 /* release memory storage in the end */
3943 cvReleaseMemStorage( &storage );
3946 The function <code>cvSeqPush</code> has O(1) complexity, but there is a faster method for
3947 writing large sequences (see <a href="#decl_cvStartWriteSeq">cvStartWriteSeq</a> and related functions).
3950 <hr><h3><a name="decl_cvSeqPop">SeqPop</a></h3>
3951 <p class="Blurb">Removes element from sequence end</p>
3953 void cvSeqPop( CvSeq* seq, void* element=NULL );
3955 <dt>seq<dd>Sequence.
3956 <dt>element<dd>Optional parameter. If the pointer is not zero, the function copies the
3957 removed element to this location.
3959 The function <code>cvSeqPop</code> removes an element from the sequence. The function reports
3960 an error if the sequence is already empty. The function has O(1) complexity. </p>
3963 <hr><h3><a name="decl_cvSeqPushFront">SeqPushFront</a></h3>
3964 <p class="Blurb">Adds element to sequence beginning</p>
3966 char* cvSeqPushFront( CvSeq* seq, void* element=NULL );
3968 <dt>seq<dd>Sequence.
3969 <dt>element<dd>Added element.
3971 The function <code>cvSeqPushFront</code> is similar to <a href="#decl_cvSeqPush">cvSeqPush</a> but it adds the new element
3972 to the beginning of the sequence. The function has O(1) complexity.
3976 <hr><h3><a name="decl_cvSeqPopFront">SeqPopFront</a></h3>
3977 <p class="Blurb">Removes element from sequence beginning</p>
3979 void cvSeqPopFront( CvSeq* seq, void* element=NULL );
3981 <dt>seq<dd>Sequence.
3982 <dt>element<dd>Optional parameter. If the pointer is not zero, the function copies the
3983 removed element to this location.
3985 The function <code>cvSeqPopFront</code> removes an element from the beginning of the sequence.
3986 The function reports an error if the sequence is already empty. The function has O(1) complexity. </p>
3989 <hr><h3><a name="decl_cvSeqPushMulti">SeqPushMulti</a></h3>
3990 <p class="Blurb">Pushes several elements to the either end of sequence</p>
3992 void cvSeqPushMulti( CvSeq* seq, void* elements, int count, int in_front=0 );
3994 <dt>seq<dd>Sequence.
3995 <dt>elements<dd>Added elements.
3996 <dt>count<dd>Number of elements to push.
3997 <dt>in_front<dd>The flags specifying the modified sequence end:<br>
3998 CV_BACK (=0) - the elements are added to the end of sequence<br>
3999 CV_FRONT(!=0) - the elements are added to the beginning of sequence<br>
4001 The function <code>cvSeqPushMulti</code> adds several elements to either end of the sequence.
4002 The elements are added to the sequence in the same order as they are arranged in the
4003 input array but they can fall into different sequence blocks.</p>
4006 <hr><h3><a name="decl_cvSeqPopMulti">SeqPopMulti</a></h3>
4007 <p class="Blurb">Removes several elements from the either end of sequence</p>
4009 void cvSeqPopMulti( CvSeq* seq, void* elements, int count, int in_front=0 );
4011 <dt>seq<dd>Sequence.
4012 <dt>elements<dd>Removed elements.
4013 <dt>count<dd>Number of elements to pop.
4014 <dt>in_front<dd>The flags specifying the modified sequence end:<br>
4015 CV_BACK (=0) - the elements are removed from the end of sequence<br>
4016 CV_FRONT(!=0) - the elements are removed from the beginning of sequence<br>
4018 The function <code>cvSeqPopMulti</code> removes several elements from either end of the sequence.
4019 If the number of the elements to be removed exceeds the total number of elements
4020 in the sequence, the function removes as many elements as possible.</p>
4023 <hr><h3><a name="decl_cvSeqInsert">SeqInsert</a></h3>
4024 <p class="Blurb">Inserts element in sequence middle</p>
4026 char* cvSeqInsert( CvSeq* seq, int before_index, void* element=NULL );
4028 <dt>seq<dd>Sequence.
4029 <dt>before_index<dd>Index before which the element is inserted. Inserting before 0 (the minimal allowed value
4030 of the parameter) is equal to <a href="#decl_cvSeqPushFront">cvSeqPushFront</a> and inserting before <code>seq->total</code> (the maximal
4031 allowed value of the parameter) is equal to <a href="#decl_cvSeqPush">cvSeqPush</a>.
4032 <dt>element<dd>Inserted element.
4034 The function <code>cvSeqInsert</code> shifts the sequence elements from the inserted position
4035 to the nearest end of the sequence and copies the <code>element</code> content there if
4036 the pointer is not NULL. The function returns pointer to the inserted element.</p>
4039 <hr><h3><a name="decl_cvSeqRemove">SeqRemove</a></h3>
4040 <p class="Blurb">Removes element from sequence middle</p>
4042 void cvSeqRemove( CvSeq* seq, int index );
4044 <dt>seq<dd>Sequence.
4045 <dt>index<dd>Index of removed element.
4047 The function <code>cvSeqRemove</code> removes elements with the given index. If the index is
4048 out of range the function reports an error.
4049 An attempt to remove an element from an empty sequence is a
4050 partial case of this situation. The function removes an element by shifting the
4051 sequence elements between the nearest end of the sequence and the <code>index</code>-th position, not
4052 counting the latter.</p>
4055 <hr><h3><a name="decl_cvClearSeq">ClearSeq</a></h3>
4056 <p class="Blurb">Clears sequence</p>
4058 void cvClearSeq( CvSeq* seq );
4060 <dt>seq<dd>Sequence.
4062 The function <code>cvClearSeq</code> removes all elements from the sequence. The function does not return the
4063 memory to the storage, but this memory is reused later when new elements are added
4064 to the sequence. This function time complexity is <code>O(1)</code>.
4067 <hr><h3><a name="decl_cvGetSeqElem">GetSeqElem</a></h3>
4068 <p class="Blurb">Returns pointer to sequence element by its index</p>
4070 char* cvGetSeqElem( const CvSeq* seq, int index );
4071 #define CV_GET_SEQ_ELEM( TYPE, seq, index ) (TYPE*)cvGetSeqElem( (CvSeq*)(seq), (index) )
4073 <dt>seq<dd>Sequence.
4074 <dt>index<dd>Index of element.
4076 The function <code>cvGetSeqElem</code> finds the element with the given index in the sequence
4077 and returns the pointer to it. If the element is not found,
4078 the function returns 0. The function supports negative indices, where -1 stands
4079 for the last sequence element, -2 stands for the one before last, etc. If the
4080 sequence is most likely to consist of a single sequence block or the desired
4081 element is likely to be located in the first block, then the macro
4082 <code>CV_GET_SEQ_ELEM( elemType, seq, index )</code> should be used, where the parameter
4083 <code>elemType</code> is the type of sequence elements ( <a href="#decl_CvPoint">CvPoint</a> for example), the parameter
4084 <code>seq</code> is a sequence, and the parameter <code>index</code> is the index of the desired element.
4085 The macro checks first whether the desired element belongs to the first block of
4086 the sequence and returns it if it does, otherwise the macro calls the main
4087 function <code>GetSeqElem</code>. Negative indices always cause the <a href="#decl_cvGetSeqElem">cvGetSeqElem</a> call.
4088 The function has O(1) time complexity assuming that number of blocks is much smaller than the
4089 number of elements.</p>
4092 <hr><h3><a name="decl_cvSeqElemIdx">SeqElemIdx</a></h3>
4093 <p class="Blurb">Returns index of concrete sequence element</p>
4095 int cvSeqElemIdx( const CvSeq* seq, const void* element, CvSeqBlock** block=NULL );
4097 <dt>seq<dd>Sequence.
4098 <dt>element<dd>Pointer to the element within the sequence.
4099 <dt>block<dd>Optional argument. If the pointer is not <code>NULL</code>, the address of the
4100 sequence block that contains the element is stored in this location.
4102 The function <code>cvSeqElemIdx</code> returns the index of a sequence element or a negative
4103 number if the element is not found.</p>
4106 <hr><h3><a name="decl_cvCvtSeqToArray">CvtSeqToArray</a></h3>
4107 <p class="Blurb">Copies sequence to one continuous block of memory</p>
4109 void* cvCvtSeqToArray( const CvSeq* seq, void* elements, CvSlice slice=CV_WHOLE_SEQ );
4111 <dt>seq<dd>Sequence.
4112 <dt>elements<dd>Pointer to the destination array that must be large enough.
4113 It should be a pointer to data, not a matrix header.
4114 <dt>slice<dd>The sequence part to copy to the array.
4116 The function <code>cvCvtSeqToArray</code> copies the entire sequence or subsequence to the
4117 specified buffer and returns the pointer to the buffer.</p>
4120 <hr><h3><a name="decl_cvMakeSeqHeaderForArray">MakeSeqHeaderForArray</a></h3>
4121 <p class="Blurb">Constructs sequence from array</p>
4123 CvSeq* cvMakeSeqHeaderForArray( int seq_type, int header_size, int elem_size,
4124 void* elements, int total,
4125 CvSeq* seq, CvSeqBlock* block );
4127 <dt>seq_type<dd>Type of the created sequence.
4128 <dt>header_size<dd>Size of the header of the sequence. Parameter sequence must point to
4129 the structure of that size or greater size.
4130 <dt>elem_size<dd>Size of the sequence element.
4131 <dt>elements<dd>Elements that will form a sequence.
4132 <dt>total<dd>Total number of elements in the sequence. The number of array elements
4133 must be equal to the value of this parameter.
4134 <dt>seq<dd>Pointer to the local variable that is used as the sequence header.
4135 <dt>block<dd>Pointer to the local variable that is the header of the single sequence
4138 The function <code>cvMakeSeqHeaderForArray</code> initializes sequence header for array.
4139 The sequence header as well as the sequence block are allocated by the user (for example, on stack).
4140 No data is copied by the function. The resultant sequence will consists of a single block and have
4141 NULL storage pointer, thus, it is possible to read its elements, but the attempts to
4142 add elements to the sequence will raise an error in most cases.</p>
4145 <hr><h3><a name="decl_cvSeqSlice">SeqSlice</a></h3>
4146 <p class="Blurb">Makes separate header for the sequence slice</p>
4148 CvSeq* cvSeqSlice( const CvSeq* seq, CvSlice slice,
4149 CvMemStorage* storage=NULL, int copy_data=0 );
4151 <dt>seq<dd>Sequence.
4152 <dt>slice<dd>The part of the sequence to extract.
4153 <dt>storage<dd>The destination storage to keep the new sequence header and the copied data if any.
4154 If it is NULL, the function uses the storage containing the input sequence.
4155 <dt>copy_data<dd>The flag that indicates whether to copy the elements of the extracted slice
4156 (<code>copy_data</code>!=0) or not (<code>copy_data</code>=0)
4158 The function <code>cvSeqSlice</code> creates a sequence
4159 that represents the specified slice of the input sequence. The new sequence either shares the elements
4160 with the original sequence or has own copy of the elements.
4161 So if one needs to process a part of sequence but the processing function does not have a slice parameter,
4162 the required sub-sequence may be extracted using this function.
4166 <hr><h3><a name="decl_cvCloneSeq">CloneSeq</a></h3>
4167 <p class="Blurb">Creates a copy of sequence</p>
4169 CvSeq* cvCloneSeq( const CvSeq* seq, CvMemStorage* storage=NULL );
4171 <dt>seq<dd>Sequence.
4172 <dt>storage<dd>The destination storage to keep the new sequence header and the copied data if any.
4173 If it is NULL, the function uses the storage containing the input sequence.
4175 The function <code>cvCloneSeq</code> makes a complete copy of the input sequence and returns it.
4176 The call <code><a href="#decl_cvCloneSeq">cvCloneSeq</a>( seq, storage )</code> is equivalent to
4177 <code><a href="#decl_cvSeqSlice">cvSeqSlice</a>( seq, CV_WHOLE_SEQ, storage, 1 )</code>
4181 <hr><h3><a name="decl_cvSeqRemoveSlice">SeqRemoveSlice</a></h3>
4182 <p class="Blurb">Removes sequence slice</p>
4184 void cvSeqRemoveSlice( CvSeq* seq, CvSlice slice );
4186 <dt>seq<dd>Sequence.
4187 <dt>slice<dd>The part of the sequence to remove.
4189 The function <code>cvSeqRemoveSlice</code> removes slice from the sequence.</p>
4192 <hr><h3><a name="decl_cvSeqInsertSlice">SeqInsertSlice</a></h3>
4193 <p class="Blurb">Inserts array in the middle of sequence</p>
4195 void cvSeqInsertSlice( CvSeq* seq, int before_index, const CvArr* from_arr );
4197 <dt>seq<dd>Sequence.
4198 <dt>slice<dd>The part of the sequence to remove.
4199 <dt>from_arr<dd>The array to take elements from.
4201 The function <code>cvSeqInsertSlice</code> inserts all <code>from_arr</code>
4202 array elements at the specified position of the sequence. The array <code>from_arr</code>
4203 can be a matrix or another sequence.</p>
4206 <hr><h3><a name="decl_cvSeqInvert">SeqInvert</a></h3>
4207 <p class="Blurb">Reverses the order of sequence elements</p>
4209 void cvSeqInvert( CvSeq* seq );
4211 <dt>seq<dd>Sequence.
4213 The function <code>cvSeqInvert</code> reverses the sequence in-place - makes the first element go last,
4214 the last element go first etc.</p>
4217 <hr><h3><a name="decl_cvSeqSort">SeqSort</a></h3>
4218 <p class="Blurb">Sorts sequence element using the specified comparison function</p>
4220 /* a < b ? -1 : a > b ? 1 : 0 */
4221 typedef int (CV_CDECL* CvCmpFunc)(const void* a, const void* b, void* userdata);
4223 void cvSeqSort( CvSeq* seq, CvCmpFunc func, void* userdata=NULL );
4225 <dt>seq<dd>The sequence to sort
4226 <dt>func<dd>The comparison function that returns negative, zero or positive value depending
4227 on the elements relation (see the above declaration and the example below) -
4228 similar function is used by <code>qsort</code> from C runtime except that in the latter
4229 <code>userdata</code> is not used
4230 <dt>userdata<dd>The user parameter passed to the comparison function;
4231 helps to avoid global variables in some cases.
4233 The function <code>cvSeqSort</code> sorts the sequence in-place using the specified criteria.
4234 Below is the example of the function use:</p>
4236 /* Sort 2d points in top-to-bottom left-to-right order */
4237 static int cmp_func( const void* _a, const void* _b, void* userdata )
4239 CvPoint* a = (CvPoint*)_a;
4240 CvPoint* b = (CvPoint*)_b;
4241 int y_diff = a->y - b->y;
4242 int x_diff = a->x - b->x;
4243 return y_diff ? y_diff : x_diff;
4248 CvMemStorage* storage = cvCreateMemStorage(0);
4249 CvSeq* seq = cvCreateSeq( CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), storage );
4252 for( i = 0; i < 10; i++ )
4255 pt.x = rand() % 1000;
4256 pt.y = rand() % 1000;
4257 cvSeqPush( seq, &pt );
4260 cvSeqSort( seq, cmp_func, 0 /* userdata is not used here */ );
4262 /* print out the sorted sequence */
4263 for( i = 0; i < seq->total; i++ )
4265 CvPoint* pt = (CvPoint*)cvSeqElem( seq, i );
4266 printf( "(%d,%d)\n", pt->x, pt->y );
4269 cvReleaseMemStorage( &storage );
4274 <hr><h3><a name="decl_cvSeqSearch">SeqSearch</a></h3>
4275 <p class="Blurb">Searches element in sequence</p>
4277 /* a < b ? -1 : a > b ? 1 : 0 */
4278 typedef int (CV_CDECL* CvCmpFunc)(const void* a, const void* b, void* userdata);
4280 char* cvSeqSearch( CvSeq* seq, const void* elem, CvCmpFunc func,
4281 int is_sorted, int* elem_idx, void* userdata=NULL );
4283 <dt>seq<dd>The sequence
4284 <dt>elem<dd>The element to look for
4285 <dt>func<dd>The comparison function that returns negative, zero or positive value depending
4286 on the elements relation (see also <a href="#decl_cvSeqSort">cvSeqSort</a>).
4287 <dt>is_sorted<dd>Whether the sequence is sorted or not.
4288 <dt>elem_idx<dd>Output parameter; index of the found element.
4289 <dt>userdata<dd>The user parameter passed to the comparison function;
4290 helps to avoid global variables in some cases.
4292 The function <code>cvSeqSearch</code> searches the element in the sequence.
4293 If the sequence is sorted, binary O(log(N)) search is used, otherwise, a simple linear search is used.
4294 If the element is not found, the function returns NULL pointer and the index is set to the number of
4295 sequence elements if the linear search is used, and to the smallest index <code>i, seq(i)>elem</code>.
4299 <hr><h3><a name="decl_cvStartAppendToSeq">StartAppendToSeq</a></h3>
4300 <p class="Blurb">Initializes process of writing data to sequence</p>
4302 void cvStartAppendToSeq( CvSeq* seq, CvSeqWriter* writer );
4304 <dt>seq<dd>Pointer to the sequence.
4305 <dt>writer<dd>Writer state; initialized by the function.
4307 The function <code>cvStartAppendToSeq</code> initializes the process of writing data to the sequence.
4308 Written elements are added to the end of the sequence by <code>CV_WRITE_SEQ_ELEM( written_elem, writer )</code> macro.
4309 Note that during the writing process other operations on the sequence may yield incorrect result or
4310 even corrupt the sequence (see description of <a href="#decl_cvFlushSeqWriter">cvFlushSeqWriter</a> that helps to avoid
4311 some of these problems).</p>
4314 <hr><h3><a name="decl_cvStartWriteSeq">StartWriteSeq</a></h3>
4315 <p class="Blurb">Creates new sequence and initializes writer for it</p>
4317 void cvStartWriteSeq( int seq_flags, int header_size, int elem_size,
4318 CvMemStorage* storage, CvSeqWriter* writer );
4320 <dt>seq_flags<dd>Flags of the created sequence. If the sequence is not passed to any
4321 function working with a specific type of sequences, the sequence value may be
4322 equal to 0, otherwise the appropriate type must be selected from the list of
4323 predefined sequence types.
4324 <dt>header_size<dd>Size of the sequence header. The parameter value may not be less than
4325 <code>sizeof(CvSeq)</code>. If a certain type or extension is specified, it must fit the
4327 <dt>elem_size<dd>Size of the sequence elements in bytes; must be consistent with the
4328 sequence type. For example, if the sequence of points is created (element type
4329 <code>CV_SEQ_ELTYPE_POINT</code> ), then the parameter elem_size must be equal to
4330 <code>sizeof(CvPoint)</code>.
4331 <dt>storage<dd>Sequence location.
4332 <dt>writer<dd>Writer state; initialized by the function.
4334 The function <code>cvStartWriteSeq</code> is a composition of <a href="#decl_cvCreateSeq">cvCreateSeq</a> and <a href="#decl_cvStartAppendToSeq">cvStartAppendToSeq</a>.
4335 The pointer to the created sequence is stored at <code>writer->seq</code> and is also returned
4336 by <a href="#decl_cvEndWriteSeq">cvEndWriteSeq</a> function that should be called in the end.</p>
4339 <hr><h3><a name="decl_cvEndWriteSeq">EndWriteSeq</a></h3>
4340 <p class="Blurb">Finishes process of writing sequence</p>
4342 CvSeq* cvEndWriteSeq( CvSeqWriter* writer );
4344 <dt>writer<dd>Writer state
4346 The function <code>cvEndWriteSeq</code> finishes the writing process and returns the pointer to
4347 the written sequence. The function also truncates the last incomplete sequence block to
4348 return the remaining part of the block to the memory storage. After that the sequence
4349 can be read and modified safely.</p>
4352 <hr><h3><a name="decl_cvFlushSeqWriter">FlushSeqWriter</a></h3>
4353 <p class="Blurb">Updates sequence headers from the writer state</p>
4355 void cvFlushSeqWriter( CvSeqWriter* writer );
4357 <dt>writer<dd>Writer state
4359 The function <code>cvFlushSeqWriter</code> is intended to enable the user to read sequence
4360 elements, whenever required, during the writing process, e.g., in order to check
4361 specific conditions. The function updates the sequence headers to make reading
4362 from the sequence possible. The writer is not closed, however, so that the
4363 writing process can be continued any time. If some algorithm requires often flushes,
4364 consider using <a href="#decl_cvSeqPush">cvSeqPush</a> instead.</p>
4367 <hr><h3><a name="decl_cvStartReadSeq">StartReadSeq</a></h3>
4368 <p class="Blurb">Initializes process of sequential reading from sequence</p>
4370 void cvStartReadSeq( const CvSeq* seq, CvSeqReader* reader, int reverse=0 );
4372 <dt>seq<dd>Sequence.
4373 <dt>reader<dd>Reader state; initialized by the function.
4374 <dt>reverse<dd>Determines the direction of the sequence traversal. If <code>reverse</code> is 0,
4375 the reader is positioned at the first sequence element, otherwise it is positioned at the last
4378 The function <code>cvStartReadSeq</code> initializes the reader state. After that all the
4379 sequence elements from the first down to the last one can be read by subsequent
4380 calls of the macro <code>CV_READ_SEQ_ELEM( read_elem, reader )</code> in case of forward reading
4381 and by using <code>CV_REV_READ_SEQ_ELEM( read_elem, reader )</code> in case of reversed reading.
4382 Both macros put the sequence element to <code>read_elem</code> and move the
4383 reading pointer toward the next element.
4384 A circular structure of sequence blocks is used for the reading process, that
4385 is, after the last element has been read by the macro <code>CV_READ_SEQ_ELEM</code>, the
4386 first element is read when the macro is called again. The same applies to
4387 <code>CV_REV_READ_SEQ_ELEM </code>. There is no function to finish the reading process,
4388 since it neither changes the sequence nor creates any temporary buffers. The reader
4389 field <code>ptr</code> points to the current element of the sequence that is to be read
4390 next. The code below demonstrates how to use sequence writer and reader.</p>
4392 CvMemStorage* storage = cvCreateMemStorage(0);
4393 CvSeq* seq = cvCreateSeq( CV_32SC1, sizeof(CvSeq), sizeof(int), storage );
4398 cvStartAppendToSeq( seq, &writer );
4399 for( i = 0; i < 10; i++ )
4401 int val = rand()%100;
4402 CV_WRITE_SEQ_ELEM( val, writer );
4403 printf("%d is written\n", val );
4405 cvEndWriteSeq( &writer );
4407 cvStartReadSeq( seq, &reader, 0 );
4408 for( i = 0; i < seq->total; i++ )
4412 CV_READ_SEQ_ELEM( val, reader );
4413 printf("%d is read\n", val );
4414 #else /* alternative way, that is preferable if sequence elements are large,
4415 or their size/type is unknown at compile time */
4416 printf("%d is read\n", *(int*)reader.ptr );
4417 CV_NEXT_SEQ_ELEM( seq->elem_size, reader );
4422 cvReleaseStorage( &storage );
4426 <hr><h3><a name="decl_cvGetSeqReaderPos">GetSeqReaderPos</a></h3>
4427 <p class="Blurb">Returns the current reader position</p>
4429 int cvGetSeqReaderPos( CvSeqReader* reader );
4431 <dt>reader<dd>Reader state.
4433 The function <code>cvGetSeqReaderPos</code> returns the current reader position
4434 (within 0 ... <code>reader->seq->total</code> - 1).</p>
4437 <hr><h3><a name="decl_cvSetSeqReaderPos">SetSeqReaderPos</a></h3>
4438 <p class="Blurb">Moves the reader to specified position</p>
4440 void cvSetSeqReaderPos( CvSeqReader* reader, int index, int is_relative=0 );
4443 <dt>reader<dd>Reader state.
4444 <dt>index<dd>The destination position. If the positioning mode is used (see the next parameter)
4445 the actual position will be <code>index</code> mod <code>reader->seq->total</code>.
4446 <dt>is_relative<dd>If it is not zero, then <code>index</code> is a relative to the current position.
4448 The function <code>cvSetSeqReaderPos</code> moves the read position to the absolute position or
4449 relative to the current position.
4453 <hr><h2><a name="cxcore_ds_sets">Sets</a></h2>
4455 <hr><h3><a name="decl_CvSet">CvSet</a></h3>
4456 <p class="Blurb">Collection of nodes</p>
4458 typedef struct CvSetElem
4460 int flags; /* it is negative if the node is free and zero or positive otherwise */
4461 struct CvSetElem* next_free; /* if the node is free, the field is a
4462 pointer to next free node */
4466 #define CV_SET_FIELDS() \
4467 CV_SEQUENCE_FIELDS() /* inherits from <a href="#decl_CvSeq">CvSeq</a> */ \
4468 struct CvSetElem* free_elems; /* list of free nodes */
4470 typedef struct CvSet
4476 The structure <a href="#decl_CvSet">CvSet</a> is a base for OpenCV sparse data structures.</p>
4477 <p>As follows from the above declaration <a href="#decl_CvSet">CvSet</a> inherits from <a href="#decl_CvSeq">CvSeq</a>
4478 and it adds <code>free_elems</code> field it to, which is a list of free nodes.
4479 Every set node, whether free or not, is the element of the underlying sequence.
4480 While there is no restrictions on elements of dense sequences, the set (and derived structures)
4481 elements must start with integer field and be able to fit CvSetElem structure, because
4482 these two fields (integer followed by the pointer) are required for organization of node set with
4483 the list of free nodes. If a node is free, <code>flags</code> field is negative (the most-significant
4484 bit, or MSB, of the field is set), and <code>next_free</code>
4485 points to the next free node (the first free node is referenced by <code>free_elems</code> field of
4486 <a href="#decl_CvSet">CvSet</a>). And if a node is occupied, <code>flags</code> field is positive and contains the node index
4487 that may be retrieved using (set_elem->flags & CV_SET_ELEM_IDX_MASK) expression,
4488 the rest of the node content is determined by the user. In particular, the occupied nodes
4489 are not linked as the free nodes are, so the second field can be used for such a link as well as
4490 for some different purpose. The macro <code>CV_IS_SET_ELEM(set_elem_ptr)</code>
4491 can be used to determined whether the specified node is occupied or not.</p>
4493 Initially the set and the list are empty. When a new node is requested from the set,
4494 it is taken from the list of free nodes, which is updated then. If the list appears to be empty,
4495 a new sequence block is allocated and all the nodes within the block are joined in the list of free
4496 nodes. Thus, <code>total</code> field of the set is the total number of nodes both occupied and free.
4497 When an occupied node is released, it is added to the list of free nodes. The node released last
4498 will be occupied first.</p>
4499 <p>In OpenCV <a href="#decl_CvSet">CvSet</a> is used for representing graphs (<a href="#decl_CvGraph">CvGraph</a>),
4500 sparse multi-dimensional arrays (<a href="#decl_CvSparseMat">CvSparseMat</a>), planar subdivisions (<a href="#decl_CvSubdiv2D">CvSubdiv2D</a>) etc.</p>
4503 <hr><h3><a name="decl_cvCreateSet">CreateSet</a></h3>
4504 <p class="Blurb">Creates empty set</p>
4506 CvSet* cvCreateSet( int set_flags, int header_size,
4507 int elem_size, CvMemStorage* storage );
4509 <dt>set_flags<dd>Type of the created set.
4510 <dt>header_size<dd>Set header size; may not be less than <code>sizeof(CvSet)</code>.
4511 <dt>elem_size<dd>Set element size; may not be less than <a href="#decl_CvSetElem">CvSetElem</a>.
4512 <dt>storage<dd>Container for the set.
4514 The function <code>cvCreateSet</code> creates an empty set with a specified header size and element size, and
4515 returns the pointer to the set. The function is just a thin layer on top of <a href="#decl_cvCreateSeq">cvCreateSeq</a>.</p>
4518 <hr><h3><a name="decl_cvSetAdd">SetAdd</a></h3>
4519 <p class="Blurb">Occupies a node in the set</p>
4521 int cvSetAdd( CvSet* set_header, CvSetElem* elem=NULL, CvSetElem** inserted_elem=NULL );
4523 <dt>set_header<dd>Set.
4524 <dt>elem<dd>Optional input argument, inserted element. If not NULL, the function
4525 copies the data to the allocated node (The MSB of the first integer field is cleared after copying).
4526 <dt>inserted_elem<dd>Optional output argument; the pointer to the allocated cell.
4528 The function <code>cvSetAdd</code> allocates a new node, optionally copies input element data
4529 to it, and returns the pointer and the index to the node. The index value is
4530 taken from the lower bits of <code>flags</code> field of the node. The function has O(1) complexity,
4531 however there exists a faster function for allocating set nodes (see <a href="#decl_cvSetNew">cvSetNew</a>).
4535 <hr><h3><a name="decl_cvSetRemove">SetRemove</a></h3>
4536 <p class="Blurb">Removes element from set</p>
4538 void cvSetRemove( CvSet* set_header, int index );
4540 <dt>set_header<dd>Set.
4541 <dt>index<dd>Index of the removed element.
4543 The function <code>cvSetRemove</code> removes an element with a specified index from the set.
4544 If the node at the specified location is not occupied the function does nothing.
4545 The function has O(1) complexity, however, <a href="#decl_cvSetRemoveByPtr">cvSetRemoveByPtr</a> provides yet
4546 faster way to remove a set element if it is located already.</p>
4549 <hr><h3><a name="decl_cvSetNew">SetNew</a></h3>
4550 <p class="Blurb">Adds element to set (fast variant)</p>
4552 CvSetElem* cvSetNew( CvSet* set_header );
4554 <dt>set_header<dd>Set.
4556 The function <code>cvSetNew</code> is inline light-weight variant of <a href="#decl_cvSetAdd">cvSetAdd</a>.
4557 It occupies a new node and returns pointer to it rather than index.</p>
4560 <hr><h3><a name="decl_cvSetRemoveByPtr">SetRemoveByPtr</a></h3>
4561 <p class="Blurb">Removes set element given its pointer</p>
4563 void cvSetRemoveByPtr( CvSet* set_header, void* elem );
4565 <dt>set_header<dd>Set.
4566 <dt>elem<dd>Removed element.
4568 The function <code>cvSetRemoveByPtr</code> is inline light-weight variant of <a href="#decl_cvSetRemove">cvSetRemove</a>
4569 that takes element pointer.
4570 The function does not check whether the node is occupied or not - the user should take care of it.</p>
4573 <hr><h3><a name="decl_cvGetSetElem">GetSetElem</a></h3>
4574 <p class="Blurb">Finds set element by its index</p>
4576 CvSetElem* cvGetSetElem( const CvSet* set_header, int index );
4578 <dt>set_header<dd>Set.
4579 <dt>index<dd>Index of the set element within a sequence.
4581 The function <code>cvGetSetElem</code> finds a set element by index. The function returns the
4582 pointer to it or 0 if the index is invalid or the corresponding node is free.
4583 The function supports negative indices as it uses <a href="#decl_cvGetSeqElem">cvGetSeqElem</a> to locate the node.</p>
4587 <hr><h3><a name="decl_cvClearSet">ClearSet</a></h3>
4588 <p class="Blurb">Clears set</p>
4590 void cvClearSet( CvSet* set_header );
4592 <dt>set_header<dd>Cleared set.
4594 The function <code>cvClearSet</code> removes all elements from set. It has O(1) time complexity.</p>
4597 <hr><h2><a name="cxcore_ds_graphs">Graphs</a></h2>
4599 <hr><h3><a name="decl_CvGraph">CvGraph</a></h3>
4600 <p class="Blurb">Oriented or undirected weighted graph</p>
4602 #define CV_GRAPH_VERTEX_FIELDS() \
4603 int flags; /* vertex flags */ \
4604 struct CvGraphEdge* first; /* the first incident edge */
4606 typedef struct CvGraphVtx
4608 CV_GRAPH_VERTEX_FIELDS()
4612 #define CV_GRAPH_EDGE_FIELDS() \
4613 int flags; /* edge flags */ \
4614 float weight; /* edge weight */ \
4615 struct CvGraphEdge* next[2]; /* the next edges in the incidence lists for staring (0) */ \
4616 /* and ending (1) vertices */ \
4617 struct CvGraphVtx* vtx[2]; /* the starting (0) and ending (1) vertices */
4619 typedef struct CvGraphEdge
4621 CV_GRAPH_EDGE_FIELDS()
4625 #define CV_GRAPH_FIELDS() \
4626 CV_SET_FIELDS() /* set of vertices */ \
4627 CvSet* edges; /* set of edges */
4629 typedef struct CvGraph
4637 The structure <a href="#decl_CvGraph">CvGraph</a> is a base for graphs used in OpenCV.</p>
4638 <p>Graph structure inherits from <a href="#decl_CvSet">CvSet</a> - this part describes common graph properties and
4639 the graph vertices, and contains another set as a member - this part describes the graph edges.</p>
4640 <p>The vertex, edge and the graph header structures are declared using the same technique as other
4641 extendible OpenCV structures - via macros, that simplifies extension and customization of the structures.
4642 While the vertex and edge structures do not inherit from <a href="#decl_CvSetElem">CvSetElem</a> explicitly, they satisfy
4643 both conditions on the set elements - have an integer field in the beginning and fit CvSetElem structure.
4644 The <code>flags</code> fields are used as for indicating occupied vertices and edges as well as
4645 for other purposes, for example, for graph traversal (see <a href="#decl_cvCreateGraphScanner">cvCreateGraphScanner</a> et al.), so
4646 it is better not to use them directly.</p>
4647 <p>The graph is represented as a set of edges each of whose has the list of incident edges. The incidence
4648 lists for different vertices are interleaved to avoid information duplication as much as possible.</p>
4649 <p>The graph may be oriented or undirected. In the latter case there is no distinction between edge
4650 connecting vertex A with vertex B and the edge connecting vertex B with vertex A - only one of them
4651 can exist in the graph at the same moment and it represents both <A, B> and <B, A> edges..</p>
4655 <hr><h3><a name="decl_cvCreateGraph">CreateGraph</a></h3>
4656 <p class="Blurb">Creates empty graph</p>
4658 CvGraph* cvCreateGraph( int graph_flags, int header_size, int vtx_size,
4659 int edge_size, CvMemStorage* storage );
4661 <dt>graph_flags<dd>Type of the created graph. Usually, it is either <code>CV_SEQ_KIND_GRAPH</code>
4662 for generic undirected graphs and <code>CV_SEQ_KIND_GRAPH | CV_GRAPH_FLAG_ORIENTED</code> for generic oriented graphs.
4663 <dt>header_size<dd>Graph header size; may not be less than <code>sizeof(CvGraph).</code>
4664 <dt>vtx_size<dd>Graph vertex size; the custom vertex structure must start with <a href="#decl_CvGraphVtx">CvGraphVtx</a>
4665 (use <code>CV_GRAPH_VERTEX_FIELDS()</code>)
4666 <dt>edge_size<dd>Graph edge size; the custom edge structure must start with <a href="#decl_CvGraphEdge">CvGraphEdge</a>
4667 (use <code>CV_GRAPH_EDGE_FIELDS()</code>)
4668 <dt>storage<dd>The graph container.
4670 The function <code>cvCreateGraph</code> creates an empty graph and returns pointer to it.</p>
4673 <hr><h3><a name="decl_cvGraphAddVtx">GraphAddVtx</a></h3>
4674 <p class="Blurb">Adds vertex to graph</p>
4676 int cvGraphAddVtx( CvGraph* graph, const CvGraphVtx* vtx=NULL,
4677 CvGraphVtx** inserted_vtx=NULL );
4680 <dt>vtx<dd>Optional input argument used to initialize the added vertex (only user-defined fields
4681 beyond <code>sizeof(CvGraphVtx)</code> are copied).
4682 <dt>inserted_vertex<dd>Optional output argument. If not <code>NULL</code>, the address of the new
4683 vertex is written there.
4685 The function <code>cvGraphAddVtx</code> adds a vertex to the graph and returns the vertex
4689 <hr><h3><a name="decl_cvGraphRemoveVtx">GraphRemoveVtx</a></h3>
4690 <p class="Blurb">Removes vertex from graph</p>
4692 int cvGraphRemoveVtx( CvGraph* graph, int index );
4695 <dt>vtx_idx<dd>Index of the removed vertex.
4697 The function <code>cvGraphRemoveAddVtx</code> removes a vertex from the graph together with all
4698 the edges incident to it. The function reports an error, if the input vertex does
4699 not belong to the graph. The return value is number of edges deleted,
4700 or -1 if the vertex does not belong to the graph.</p>
4703 <hr><h3><a name="decl_cvGraphRemoveVtxByPtr">GraphRemoveVtxByPtr</a></h3>
4704 <p class="Blurb">Removes vertex from graph</p>
4706 int cvGraphRemoveVtxByPtr( CvGraph* graph, CvGraphVtx* vtx );
4709 <dt>vtx<dd>Pointer to the removed vertex.
4711 The function <code>cvGraphRemoveVtxByPtr</code> removes a vertex from the graph together with
4712 all the edges incident to it. The function reports an error, if the vertex does not belong to the graph.
4713 The return value is number of edges deleted, or -1 if the vertex does not belong to the graph.</p>
4716 <hr><h3><a name="decl_cvGetGraphVtx">GetGraphVtx</a></h3>
4717 <p class="Blurb">Finds graph vertex by index</p>
4719 CvGraphVtx* cvGetGraphVtx( CvGraph* graph, int vtx_idx );
4722 <dt>vtx_idx<dd>Index of the vertex.
4724 The function <code>cvGetGraphVtx</code> finds the graph vertex by index and returns the pointer
4725 to it or NULL if the vertex does not belong to the graph.</p>
4728 <hr><h3><a name="decl_cvGraphVtxIdx">GraphVtxIdx</a></h3>
4729 <p class="Blurb">Returns index of graph vertex</p>
4731 int cvGraphVtxIdx( CvGraph* graph, CvGraphVtx* vtx );
4734 <dt>vtx<dd>Pointer to the graph vertex.
4736 The function <code>cvGraphVtxIdx</code> returns index of the graph vertex.</p>
4739 <hr><h3><a name="decl_cvGraphAddEdge">GraphAddEdge</a></h3>
4740 <p class="Blurb">Adds edge to graph</p>
4742 int cvGraphAddEdge( CvGraph* graph, int start_idx, int end_idx,
4743 const CvGraphEdge* edge=NULL, CvGraphEdge** inserted_edge=NULL );
4746 <dt>start_idx<dd>Index of the starting vertex of the edge.
4747 <dt>end_idx<dd>Index of the ending vertex of the edge. For undirected graph the order of the vertex
4748 parameters does not matter.
4749 <dt>edge<dd>Optional input parameter, initialization data for the edge.
4750 <dt>inserted_edge<dd>Optional output parameter to contain the address of the inserted
4753 The function <code>cvGraphAddEdge</code> connects two specified vertices.
4754 The function returns 1 if the edge has been added successfully, 0 if the edge connecting
4755 the two vertices exists already and -1 if either of the vertices was not found, the starting and
4756 the ending vertex are the same or there is some other critical situation. In the latter case
4757 (i.e. when the result is negative) the function also reports an error by default.</p>
4760 <hr><h3><a name="decl_cvGraphAddEdgeByPtr">GraphAddEdgeByPtr</a></h3>
4761 <p class="Blurb">Adds edge to graph</p>
4763 int cvGraphAddEdgeByPtr( CvGraph* graph, CvGraphVtx* start_vtx, CvGraphVtx* end_vtx,
4764 const CvGraphEdge* edge=NULL, CvGraphEdge** inserted_edge=NULL );
4767 <dt>start_vtx<dd>Pointer to the starting vertex of the edge.
4768 <dt>end_vtx<dd>Pointer to the ending vertex of the edge. For undirected graph the order of the vertex
4769 parameters does not matter.
4770 <dt>edge<dd>Optional input parameter, initialization data for the edge.
4771 <dt>inserted_edge<dd>Optional output parameter to contain the address of the inserted
4772 edge within the edge set.
4774 The function <code>cvGraphAddEdge</code> connects two specified vertices.
4775 The function returns 1 if the edge has been added successfully, 0 if the edge connecting
4776 the two vertices exists already and -1 if either of the vertices was not found, the starting and
4777 the ending vertex are the same or there is some other critical situation. In the latter case
4778 (i.e. when the result is negative) the function also reports an error by default.</p>
4781 <hr><h3><a name="decl_cvGraphRemoveEdge">GraphRemoveEdge</a></h3>
4782 <p class="Blurb">Removes edge from graph</p>
4784 void cvGraphRemoveEdge( CvGraph* graph, int start_idx, int end_idx );
4787 <dt>start_idx<dd>Index of the starting vertex of the edge.
4788 <dt>end_idx<dd>Index of the ending vertex of the edge. For undirected graph the order of the vertex
4789 parameters does not matter.
4791 The function <code>cvGraphRemoveEdge</code> removes the edge connecting two specified vertices.
4792 If the vertices are not connected [in that order], the function does nothing.
4796 <hr><h3><a name="decl_cvGraphRemoveEdgeByPtr">GraphRemoveEdgeByPtr</a></h3>
4797 <p class="Blurb">Removes edge from graph</p>
4799 void cvGraphRemoveEdgeByPtr( CvGraph* graph, CvGraphVtx* start_vtx, CvGraphVtx* end_vtx );
4802 <dt>start_vtx<dd>Pointer to the starting vertex of the edge.
4803 <dt>end_vtx<dd>Pointer to the ending vertex of the edge. For undirected graph the order of the vertex
4804 parameters does not matter.
4806 The function <code>cvGraphRemoveEdgeByPtr</code> removes the edge connecting two specified vertices.
4807 If the vertices are not connected [in that order], the function does nothing.</p>
4810 <hr><h3><a name="decl_cvFindGraphEdge">FindGraphEdge</a></h3>
4811 <p class="Blurb">Finds edge in graph</p>
4813 CvGraphEdge* cvFindGraphEdge( const CvGraph* graph, int start_idx, int end_idx );
4814 #define cvGraphFindEdge cvFindGraphEdge
4817 <dt>start_idx<dd>Index of the starting vertex of the edge.
4818 <dt>end_idx<dd>Index of the ending vertex of the edge. For undirected graph the order of the vertex
4819 parameters does not matter.
4821 The function <code>cvFindGraphEdge</code> finds the graph edge connecting two specified vertices
4822 and returns pointer to it or NULL if the edge does not exists.</p>
4825 <hr><h3><a name="decl_cvFindGraphEdgeByPtr">FindGraphEdgeByPtr</a></h3>
4826 <p class="Blurb">Finds edge in graph</p>
4828 CvGraphEdge* cvFindGraphEdgeByPtr( const CvGraph* graph, const CvGraphVtx* start_vtx,
4829 const CvGraphVtx* end_vtx );
4830 #define cvGraphFindEdgeByPtr cvFindGraphEdgeByPtr
4833 <dt>start_vtx<dd>Pointer to the starting vertex of the edge.
4834 <dt>end_vtx<dd>Pointer to the ending vertex of the edge. For undirected graph the order of the vertex
4835 parameters does not matter.
4837 The function <code>cvFindGraphEdge</code> finds the graph edge connecting two specified vertices
4838 and returns pointer to it or NULL if the edge does not exists.</p>
4841 <hr><h3><a name="decl_cvGraphEdgeIdx">GraphEdgeIdx</a></h3>
4842 <p class="Blurb">Returns index of graph edge</p>
4844 int cvGraphEdgeIdx( CvGraph* graph, CvGraphEdge* edge );
4847 <dt>edge<dd>Pointer to the graph edge.
4849 The function <code>cvGraphEdgeIdx</code> returns index of the graph edge.</p>
4852 <hr><h3><a name="decl_cvGraphVtxDegree">GraphVtxDegree</a></h3>
4853 <p class="Blurb">Counts edges incident to the vertex</p>
4855 int cvGraphVtxDegree( const CvGraph* graph, int vtx_idx );
4858 <dt>vtx<dd>Index of the graph vertex.
4860 The function <code>cvGraphVtxDegree</code> returns the number of edges
4861 incident to the specified vertex, both incoming and outgoing.
4862 To count the edges, the following code is used:</p>
4864 CvGraphEdge* edge = vertex->first; int count = 0;
4867 edge = CV_NEXT_GRAPH_EDGE( edge, vertex );
4871 <p>The macro <code>CV_NEXT_GRAPH_EDGE( edge, vertex )</code> returns the edge incident to <code>vertex</code>
4872 that follows after <code>edge</code>.</p>
4875 <hr><h3><a name="decl_cvGraphVtxDegreeByPtr">GraphVtxDegreeByPtr</a></h3>
4876 <p class="Blurb">Finds edge in graph</p>
4878 int cvGraphVtxDegreeByPtr( const CvGraph* graph, const CvGraphVtx* vtx );
4881 <dt>vtx<dd>Pointer to the graph vertex.
4883 The function <code>cvGraphVtxDegree</code> returns the number of edges
4884 incident to the specified vertex, both incoming and outgoing.</p>
4887 <hr><h3><a name="decl_cvClearGraph">ClearGraph</a></h3>
4888 <p class="Blurb">Clears graph</p>
4890 void cvClearGraph( CvGraph* graph );
4894 The function <code>cvClearGraph</code> removes all vertices and edges from the graph.
4895 The function has O(1) time complexity.</p>
4898 <hr><h3><a name="decl_cvCloneGraph">CloneGraph</a></h3>
4899 <p class="Blurb">Clone graph</p>
4901 CvGraph* cvCloneGraph( const CvGraph* graph, CvMemStorage* storage );
4903 <dt>graph<dd>The graph to copy.
4904 <dt>storage<dd>Container for the copy.
4906 The function <code>cvCloneGraph</code> creates full copy of the graph. If the graph vertices
4907 or edges have pointers to some external data, it still be shared between the copies.
4908 The vertex and edge indices in the new graph may be different from the original, because
4909 the function defragments the vertex and edge sets.</p>
4912 <hr><h3><a name="decl_CvGraphScanner">CvGraphScanner</a></h3>
4913 <p class="Blurb">Graph traversal state</p>
4915 typedef struct CvGraphScanner
4917 CvGraphVtx* vtx; /* current graph vertex (or current edge origin) */
4918 CvGraphVtx* dst; /* current graph edge destination vertex */
4919 CvGraphEdge* edge; /* current edge */
4921 CvGraph* graph; /* the graph */
4922 CvSeq* stack; /* the graph vertex stack */
4923 int index; /* the lower bound of certainly visited vertices */
4924 int mask; /* event mask */
4928 <p>The structure <a href="#decl_CvGraphScanner">CvGraphScanner</a> is used for depth-first graph traversal.
4929 See discussion of the functions below.</p>
4932 <hr><h3><a name="decl_cvCreateGraphScanner">CreateGraphScanner</a></h3>
4933 <p class="Blurb">Creates structure for depth-first graph traversal</p>
4935 CvGraphScanner* cvCreateGraphScanner( CvGraph* graph, CvGraphVtx* vtx=NULL,
4936 int mask=CV_GRAPH_ALL_ITEMS );
4939 <dt>vtx<dd>Initial vertex to start from. If NULL, the traversal starts from the first vertex (a vertex with the
4940 minimal index in the sequence of vertices).
4941 <dt>mask<dd>Event mask indicating which events are interesting to the user (where <a href="#decl_cvNextGraphItem">cvNextGraphItem</a>
4942 function returns control to the user)
4943 It can be <code>CV_GRAPH_ALL_ITEMS</code> (all events are interesting)
4944 or combination of the following flags:<ul>
4945 <li>CV_GRAPH_VERTEX - stop at the graph vertices visited for the first time<br>
4946 <li>CV_GRAPH_TREE_EDGE - stop at tree edges (<code>tree edge</code> is the edge connecting the last visited vertex and
4947 the vertex to be visited next)<br>
4948 <li>CV_GRAPH_BACK_EDGE - stop at back edges (<code>back edge</code> is an edge connecting
4949 the last visited vertex with some of its ancestors in the search tree)<br>
4950 <li>CV_GRAPH_FORWARD_EDGE - stop at forward edges (<code>forward edge</code> is an edge connecting
4951 the last visited vertex with some of its descendants in the search tree).
4952 The <code>forward edges</code> are only possible during oriented graph traversal)<br>
4953 <li>CV_GRAPH_CROSS_EDGE - stop at cross edges (<code>cross edge</code> is an edge connecting different search trees or
4954 branches of the same tree.
4955 The <code>cross edges</code> are only possible during oriented graphs traversal)<br>
4956 <li>CV_GRAPH_ANY_EDGE - stop and any edge (<code>tree, back, forward</code> and <code>cross edges</code>)<br>
4957 <li>CV_GRAPH_NEW_TREE - stop in the beginning of every new search tree. When the traversal procedure
4958 visits all vertices and edges reachable from the initial vertex (the visited vertices
4959 together with tree edges make up a tree), it searches for some unvisited vertex
4960 in the graph and resumes the traversal process from that vertex.
4961 Before starting a new tree (including the very first tree
4962 when <code>cvNextGraphItem</code> is called for the first time)
4963 it generates <code>CV_GRAPH_NEW_TREE</code> event.<br>
4964 For undirected graphs each search tree corresponds to a connected component of the graph.<br>
4965 <li>CV_GRAPH_BACKTRACKING - stop at every already visited vertex during backtracking - returning to
4966 already visited vertexes of the traversal tree.<br></ul>
4968 The function <code>cvCreateGraphScanner</code> creates structure for
4969 depth-first graph traversal/search.
4970 The initialized structure is used in <a href="#decl_cvNextGraphItem">cvNextGraphItem</a> function
4971 - the incremental traversal procedure.</p>
4974 <hr><h3><a name="decl_cvNextGraphItem">NextGraphItem</a></h3>
4975 <p class="Blurb">Makes one or more steps of the graph traversal procedure</p>
4977 int cvNextGraphItem( CvGraphScanner* scanner );
4979 <dt>scanner<dd>Graph traversal state. It is updated by the function.
4981 The function <code>cvNextGraphItem</code> traverses through the graph until an event interesting to the user
4982 (that is, an event, specified in the <code>mask</code> in <a href="#decl_cvCreateGraphScanner">cvCreateGraphScanner</a> call)
4983 is met or the traversal is over. In the first case it returns one of the events,
4984 listed in the description of <code>mask</code> parameter above and with the next call
4985 it resumes the traversal. In the latter case it returns CV_GRAPH_OVER (-1).
4986 When the event is <code>CV_GRAPH_VERTEX</code>, or <code>CV_GRAPH_BACKTRACKING</code> or <code>CV_GRAPH_NEW_TREE</code>,
4987 the currently observed vertex is stored in <code>scanner->vtx</code>. And if the event is edge-related,
4988 the edge itself is stored at <code>scanner->edge</code>,
4989 the previously visited vertex - at <code>scanner->vtx</code> and the other ending vertex of the edge -
4990 at <code>scanner->dst</code>.</p>
4993 <hr><h3><a name="decl_cvReleaseGraphScanner">ReleaseGraphScanner</a></h3>
4994 <p class="Blurb">Finishes graph traversal procedure</p>
4996 void cvReleaseGraphScanner( CvGraphScanner** scanner );
4998 <dt>scanner<dd>Double pointer to graph traverser.
5000 The function <code>cvGraphScanner</code> finishes graph traversal procedure
5001 and releases the traverser state.</p>
5004 <hr><h2><a name="cxcore_ds_trees">Trees</a></h2>
5007 <hr><h3><a name="decl_CV_TREE_NODE_FIELDS">CV_TREE_NODE_FIELDS</a></h3>
5008 <p class="Blurb">Helper macro for a tree node type declaration</p>
5010 #define CV_TREE_NODE_FIELDS(node_type) \
5011 int flags; /* miscellaneous flags */ \
5012 int header_size; /* size of sequence header */ \
5013 struct node_type* h_prev; /* previous sequence */ \
5014 struct node_type* h_next; /* next sequence */ \
5015 struct node_type* v_prev; /* 2nd previous sequence */ \
5016 struct node_type* v_next; /* 2nd next sequence */
5018 <p>The macro <code>CV_TREE_NODE_FIELDS()</code> is used to declare structures
5019 that can be organized into hierarchical structures (trees), such as <a href="#decl_CvSeq">CvSeq</a> -
5020 the basic type for all dynamical structures.
5021 The trees made of nodes declared using this macro can be processed using
5022 the functions described below in this section.</p>
5025 <hr><h3><a name="decl_CvTreeNodeIterator">CvTreeNodeIterator</a></h3>
5026 <p class="Blurb">Opens existing or creates new file storage</p>
5028 typedef struct CvTreeNodeIterator
5036 <p>The structure <a href="#decl_CvTreeNodeIterator">CvTreeNodeIterator</a> is used to traverse trees.
5037 The tree node declaration should start with <code>CV_TREE_NODE_FIELDS(...)</code> macro.</p>
5040 <hr><h3><a name="decl_cvInitTreeNodeIterator">InitTreeNodeIterator</a></h3>
5041 <p class="Blurb">Initializes tree node iterator</p>
5043 void cvInitTreeNodeIterator( CvTreeNodeIterator* tree_iterator,
5044 const void* first, int max_level );
5046 <dt>tree_iterator<dd>Tree iterator initialized by the function.
5047 <dt>first<dd>The initial node to start traversing from.
5048 <dt>max_level<dd>The maximal level of the tree (<code>first</code> node assumed to be at the first level) to
5049 traverse up to. For example, 1 means that only nodes at the same level as <code>first</code>
5050 should be visited, 2 means that the nodes on the same level as <code>first</code> and
5051 their direct children should be visited etc.
5053 The function <code>cvInitTreeNodeIterator</code> initializes tree iterator.
5054 The tree is traversed in depth-first order.</p>
5057 <hr><h3><a name="decl_cvNextTreeNode">NextTreeNode</a></h3>
5058 <p class="Blurb">Returns the currently observed node and moves iterator toward the next node</p>
5060 void* cvNextTreeNode( CvTreeNodeIterator* tree_iterator );
5062 <dt>tree_iterator<dd>Tree iterator initialized by the function.
5064 The function <code>cvNextTreeNode</code> returns the currently observed node and then
5065 updates the iterator - moves it toward the next node. In other words, the function behavior
5066 is similar to *p++ expression on usual C pointer or C++ collection iterator.
5067 The function returns NULL if there is no more nodes.</p>
5070 <hr><h3><a name="decl_cvPrevTreeNode">PrevTreeNode</a></h3>
5071 <p class="Blurb">Returns the currently observed node and moves iterator toward the previous node</p>
5073 void* cvPrevTreeNode( CvTreeNodeIterator* tree_iterator );
5075 <dt>tree_iterator<dd>Tree iterator initialized by the function.
5077 The function <code>cvPrevTreeNode</code> returns the currently observed node and then
5078 updates the iterator - moves it toward the previous node. In other words, the function behavior
5079 is similar to *p-- expression on usual C pointer or C++ collection iterator.
5080 The function returns NULL if there is no more nodes.</p>
5083 <hr><h3><a name="decl_cvTreeToNodeSeq">TreeToNodeSeq</a></h3>
5084 <p class="Blurb">Gathers all node pointers to the single sequence</p>
5086 CvSeq* cvTreeToNodeSeq( const void* first, int header_size, CvMemStorage* storage );
5088 <dt>first<dd>The initial tree node.
5089 <dt>header_size<dd>Header size of the created sequence (sizeof(CvSeq) is the most used value).
5090 <dt>storage<dd>Container for the sequence.
5092 The function <code>cvTreeToNodeSeq</code> puts pointers of all nodes reachable from <code>first</code>
5093 to the single sequence. The pointers are written subsequently in the depth-first order.</p>
5096 <hr><h3><a name="decl_cvInsertNodeIntoTree">InsertNodeIntoTree</a></h3>
5097 <p class="Blurb">Adds new node to the tree</p>
5099 void cvInsertNodeIntoTree( void* node, void* parent, void* frame );
5101 <dt>node<dd>The inserted node.
5102 <dt>parent<dd>The parent node that is already in the tree.
5103 <dt>frame<dd>The top level node. If <code>parent</code> and <code>frame</code> are the same, <code>v_prev</code>
5104 field of <code>node</code> is set to NULL rather than <code>parent</code>.
5106 The function <code>cvInsertNodeIntoTree</code> adds another node into tree. The function does not
5107 allocate any memory, it can only modify links of the tree nodes.</p>
5110 <hr><h3><a name="decl_cvRemoveNodeFromTree">RemoveNodeFromTree</a></h3>
5111 <p class="Blurb">Removes node from tree</p>
5113 void cvRemoveNodeFromTree( void* node, void* frame );
5115 <dt>node<dd>The removed node.
5116 <dt>frame<dd>The top level node. If <code>node->v_prev = NULL</code> and
5117 <code>node->h_prev</code> is NULL (i.e. if <code>node</code> is the first child of <code>frame</code>),
5118 <code>frame->v_next</code> is set to <code>node->h_next</code> (i.e. the first child or frame is changed).
5120 The function <code>cvRemoveNodeFromTree</code> removes node from tree. The function does not
5121 deallocate any memory, it can only modify links of the tree nodes.</p>
5125 <hr><h1><a name="cxcore_drawing">Drawing Functions</a></h1>
5128 Drawing functions work with matrices/images or arbitrary depth.
5129 Antialiasing is implemented only for 8-bit images.
5130 All the functions include parameter color that means rgb value (that may be
5131 constructed with <code>CV_RGB</code> macro or <code>cvScalar</code> function)
5132 for color images and brightness for grayscale images.</p><p>
5133 If a drawn figure is partially or completely outside the image, it is clipped.
5134 For color images the order channel is: <font color=blue>B</font>lue <font color=green>G</font>reen <font color=red>R</font>ed ...
5135 If one needs a different channel order, it is possible to construct color
5136 via <code>cvScalar</code> with the particular channel order, or
5137 convert the image before and/or after drawing in it with
5138 <a href="opencvref_cv.htm#decl_cvCvtColor">cvCvtColor</a> or
5139 <a href="#decl_cvTransform">cvTransform</a>.
5143 <hr><h2><a name="cxcore_drawing_shapes">Curves and Shapes</a></h2>
5145 <hr><h3><a name="decl_CV_RGB">CV_RGB</a></h3>
5146 <p class="Blurb">Constructs a color value</p>
5148 #define CV_RGB( r, g, b ) cvScalar( (b), (g), (r) )
5152 <hr><h3><a name="decl_cvLine">Line</a></h3>
5153 <p class="Blurb">Draws a line segment connecting two points</p>
5155 void cvLine( CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color,
5156 int thickness=1, int line_type=8, int shift=0 );
5158 <dt>img<dd>The image.
5159 <dt>pt1<dd>First point of the line segment.
5160 <dt>pt2<dd>Second point of the line segment.
5161 <dt>color<dd>Line color.
5162 <dt>thickness<dd>Line thickness.
5163 <dt>line_type<dd>Type of the line:<br>
5164 <code>8</code> (or <code>0</code>) - 8-connected line.<br>
5165 <code>4</code> - 4-connected line.<br>
5166 <code>CV_AA</code> - antialiased line.
5167 <dt>shift<dd>Number of fractional bits in the point coordinates.
5169 The function <code>cvLine</code> draws the line segment between <code>pt1</code> and <code>pt2</code> points
5170 in the image. The line is clipped by the image or ROI rectangle. For non-antialiased lines
5171 with integer coordinates the 8-connected or 4-connected Bresenham algorithm is used.
5172 Thick lines are drawn with rounding endings. Antialiased lines are drawn using Gaussian filtering.
5173 To specify the line color, the user may use the macro <code>CV_RGB( r, g, b )</code>.</p>
5176 <hr><h3><a name="decl_cvRectangle">Rectangle</a></h3>
5177 <p class="Blurb">Draws simple, thick or filled rectangle</p>
5179 void cvRectangle( CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color,
5180 int thickness=1, int line_type=8, int shift=0 );
5183 <dt>pt1<dd>One of the rectangle vertices.
5184 <dt>pt2<dd>Opposite rectangle vertex.
5185 <dt>color<dd>Line color (RGB) or brightness (grayscale image).
5186 <dt>thickness<dd>Thickness of lines that make up the rectangle. Negative values, e.g. CV_FILLED,
5187 make the function to draw a filled rectangle.
5188 <dt>line_type<dd>Type of the line, see <a href="#decl_cvLine">cvLine</a> description.
5189 <dt>shift<dd>Number of fractional bits in the point coordinates.
5191 The function <code>cvRectangle</code> draws a rectangle with
5192 two opposite corners <code>pt1</code> and <code>pt2</code>.</p>
5195 <hr><h3><a name="decl_cvCircle">Circle</a></h3>
5196 <p class="Blurb">Draws a circle</p>
5198 void cvCircle( CvArr* img, CvPoint center, int radius, CvScalar color,
5199 int thickness=1, int line_type=8, int shift=0 );
5201 <dt>img<dd>Image where the circle is drawn.
5202 <dt>center<dd>Center of the circle.
5203 <dt>radius<dd>Radius of the circle.
5204 <dt>color<dd>Circle color.
5205 <dt>thickness<dd>Thickness of the circle outline if positive, otherwise indicates that
5206 a filled circle has to be drawn.
5207 <dt>line_type<dd>Type of the circle boundary, see <a href="#decl_cvLine">cvLine</a> description.
5208 <dt>shift<dd>Number of fractional bits in the center coordinates and radius value.
5210 The function <code>cvCircle</code> draws a simple or filled circle with given center and
5211 radius. The circle is clipped by ROI rectangle. To specify the circle color, the user may
5212 use the macro <code>CV_RGB ( r, g, b )</code>.</p>
5215 <hr><h3><a name="decl_cvEllipse">Ellipse</a></h3>
5216 <p class="Blurb">Draws simple or thick elliptic arc or fills ellipse sector</p>
5218 void cvEllipse( CvArr* img, CvPoint center, CvSize axes, double angle,
5219 double start_angle, double end_angle, CvScalar color,
5220 int thickness=1, int line_type=8, int shift=0 );
5223 <dt>center<dd>Center of the ellipse.
5224 <dt>axes<dd>Length of the ellipse axes.
5225 <dt>angle<dd>Rotation angle.
5226 <dt>start_angle<dd>Starting angle of the elliptic arc.
5227 <dt>end_angle<dd>Ending angle of the elliptic arc.
5228 <dt>color<dd>Ellipse color.
5229 <dt>thickness<dd>Thickness of the ellipse arc.
5230 <dt>line_type<dd>Type of the ellipse boundary, see <a href="#decl_cvLine">cvLine</a> description.
5231 <dt>shift<dd>Number of fractional bits in the center coordinates and axes' values.
5233 The function <code>cvEllipse</code> draws a simple or thick elliptic arc or fills an ellipse
5234 sector. The arc is clipped by ROI rectangle. A piecewise-linear
5235 approximation is used for antialiased arcs and thick arcs. All the angles are
5236 given in degrees. The picture below explains the meaning of the parameters.</p>
5238 <font color=blue>Parameters of Elliptic Arc</font>
5241 <img align="center" src="pics/ellipse.png" >
5245 <hr><h3><a name="decl_cvEllipseBox">EllipseBox</a></h3>
5246 <p class="Blurb">Draws simple or thick elliptic arc or fills ellipse sector</p>
5248 void cvEllipseBox( CvArr* img, CvBox2D box, CvScalar color,
5249 int thickness=1, int line_type=8, int shift=0 );
5252 <dt>box<dd>The enclosing box of the ellipse drawn
5253 <dt>thickness<dd>Thickness of the ellipse boundary.
5254 <dt>line_type<dd>Type of the ellipse boundary, see <a href="#decl_cvLine">cvLine</a> description.
5255 <dt>shift<dd>Number of fractional bits in the box vertex coordinates.
5257 The function <code>cvEllipseBox</code> draws a simple or thick ellipse outline,
5258 or fills an ellipse. The functions provides a convenient way to draw an ellipse
5259 approximating some shape; that is what <a href="opencvref_cv.htm#decl_cvCamShift">cvCamShift</a>
5260 and <a href="opencvref_cv.htm#decl_cvFitEllipse">cvFitEllipse</a> do.
5261 The ellipse drawn is clipped by ROI rectangle. A piecewise-linear
5262 approximation is used for antialiased arcs and thick arcs.</p>
5265 <hr><h3><a name="decl_cvFillPoly">FillPoly</a></h3>
5266 <p class="Blurb">Fills polygons interior</p>
5268 void cvFillPoly( CvArr* img, CvPoint** pts, int* npts, int contours,
5269 CvScalar color, int line_type=8, int shift=0 );
5272 <dt>pts<dd>Array of pointers to polygons.
5273 <dt>npts<dd>Array of polygon vertex counters.
5274 <dt>contours<dd>Number of contours that bind the filled region.
5275 <dt>color<dd>Polygon color.
5276 <dt>line_type<dd>Type of the polygon boundaries, see <a href="#decl_cvLine">cvLine</a> description.
5277 <dt>shift<dd>Number of fractional bits in the vertex coordinates.
5279 The function <code>cvFillPoly</code> fills an area bounded by several polygonal contours.
5280 The function fills complex areas, for example, areas with holes, contour self-intersection, etc.</p>
5283 <hr><h3><a name="decl_cvFillConvexPoly">FillConvexPoly</a></h3>
5284 <p class="Blurb">Fills convex polygon</p>
5286 void cvFillConvexPoly( CvArr* img, CvPoint* pts, int npts,
5287 CvScalar color, int line_type=8, int shift=0 );
5290 <dt>pts<dd>Array of pointers to a single polygon.
5291 <dt>npts<dd>Polygon vertex counter.
5292 <dt>color<dd>Polygon color.
5293 <dt>line_type<dd>Type of the polygon boundaries, see <a href="#decl_cvLine">cvLine</a> description.
5294 <dt>shift<dd>Number of fractional bits in the vertex coordinates.
5296 The function <code>cvFillConvexPoly</code> fills convex polygon interior.
5297 This function is much faster than The function <code>cvFillPoly</code> and can fill
5298 not only the convex polygons but any monotonic polygon, i.e. a polygon whose contour intersects every
5299 horizontal line (scan line) twice at the most.</p>
5302 <hr><h3><a name="decl_cvPolyLine">PolyLine</a></h3>
5303 <p class="Blurb">Draws simple or thick polygons</p>
5305 void cvPolyLine( CvArr* img, CvPoint** pts, int* npts, int contours, int is_closed,
5306 CvScalar color, int thickness=1, int line_type=8, int shift=0 );
5309 <dt>pts<dd>Array of pointers to polylines.
5310 <dt>npts<dd>Array of polyline vertex counters.
5311 <dt>contours<dd>Number of polyline contours.
5312 <dt>is_closed<dd>Indicates whether the polylines must be drawn closed. If closed, the
5313 function draws the line from the last vertex of every contour to the first
5315 <dt>color<dd>Polyline color.
5316 <dt>thickness<dd>Thickness of the polyline edges.
5317 <dt>line_type<dd>Type of the line segments, see <a href="#decl_cvLine">cvLine</a> description.
5318 <dt>shift<dd>Number of fractional bits in the vertex coordinates.
5320 The function <code>cvPolyLine</code> draws a single or multiple polygonal curves.</p>
5323 <hr><h2><a name="cxcore_drawing_text">Text</a></h2>
5325 <hr><h3><a name="decl_cvInitFont">InitFont</a></h3>
5326 <p class="Blurb">Initializes font structure</p>
5328 void cvInitFont( CvFont* font, int font_face, double hscale,
5329 double vscale, double shear=0,
5330 int thickness=1, int line_type=8 );
5332 <dt>font<dd>Pointer to the font structure initialized by the function.
5333 <dt>font_face<dd>Font name identifier. Only a subset of Hershey fonts
5334 (<a href="http://sources.isc.org/utils/misc/hershey-font.txt">http://sources.isc.org/utils/misc/hershey-font.txt</a>)
5335 are supported now:<br>
5336 <code>CV_FONT_HERSHEY_SIMPLEX</code> - normal size sans-serif font<br>
5337 <code>CV_FONT_HERSHEY_PLAIN</code> - small size sans-serif font<br>
5338 <code>CV_FONT_HERSHEY_DUPLEX</code> - normal size sans-serif font (more complex than <code>CV_FONT_HERSHEY_SIMPLEX</code>)<br>
5339 <code>CV_FONT_HERSHEY_COMPLEX</code> - normal size serif font<br>
5340 <code>CV_FONT_HERSHEY_TRIPLEX</code> - normal size serif font (more complex than <code>CV_FONT_HERSHEY_COMPLEX</code>)<br>
5341 <code>CV_FONT_HERSHEY_COMPLEX_SMALL</code> - smaller version of <code>CV_FONT_HERSHEY_COMPLEX</code><br>
5342 <code>CV_FONT_HERSHEY_SCRIPT_SIMPLEX</code> - hand-writing style font<br>
5343 <code>CV_FONT_HERSHEY_SCRIPT_COMPLEX</code> - more complex variant of <code>CV_FONT_HERSHEY_SCRIPT_SIMPLEX</code><br>
5344 The parameter can be composed from one of the values above and optional <code>CV_FONT_ITALIC</code> flag,
5345 that means italic or oblique font.
5346 <dt>hscale<dd>Horizontal scale. If equal to <code>1.0f</code>, the characters have the original
5347 width depending on the font type. If equal to <code>0.5f</code>, the characters are of half
5349 <dt>vscale<dd>Vertical scale. If equal to <code>1.0f</code>, the characters have the original
5350 height depending on the font type. If equal to <code>0.5f</code>, the characters are of half
5351 the original height.
5352 <dt>shear<dd>Approximate tangent of the character slope relative to the vertical
5353 line. Zero value means a non-italic font, <code>1.0f</code> means <code>≈45°</code> slope, etc.
5354 thickness Thickness of lines composing letters outlines. The function <code>cvLine</code> is
5355 used for drawing letters.
5356 <dt>thickness<dd>Thickness of the text strokes.
5357 <dt>line_type<dd>Type of the strokes, see <a href="#decl_cvLine">cvLine</a> description.
5359 The function <code>cvInitFont</code> initializes the font structure that can be passed to
5360 text rendering functions.</p>
5363 <hr><h3><a name="decl_cvPutText">PutText</a></h3>
5364 <p class="Blurb">Draws text string</p>
5366 void cvPutText( CvArr* img, const char* text, CvPoint org, const CvFont* font, CvScalar color );
5368 <dt>img<dd>Input image.
5369 <dt>text<dd>String to print.
5370 <dt>org<dd>Coordinates of the bottom-left corner of the first letter.
5371 <dt>font<dd>Pointer to the font structure.
5372 <dt>color<dd>Text color.
5374 The function <code>cvPutText</code> renders the text in the image with the specified font and
5375 color. The printed text is clipped by ROI rectangle. Symbols that do not belong
5376 to the specified font are replaced with the rectangle symbol.</p>
5379 <hr><h3><a name="decl_cvGetTextSize">GetTextSize</a></h3>
5380 <p class="Blurb">Retrieves width and height of text string</p>
5382 void cvGetTextSize( const char* text_string, const CvFont* font, CvSize* text_size, int* baseline );
5384 <dt>font<dd>Pointer to the font structure.
5385 <dt>text_string<dd>Input string.
5386 <dt>text_size<dd>Resultant size of the text string. Height of the text does not include
5387 the height of character parts that are below the baseline.
5388 <dt>baseline<dd>y-coordinate of the baseline relatively to the bottom-most text point.
5390 The function <code>cvGetTextSize</code> calculates the binding rectangle for the given text
5391 string when a specified font is used.</p>
5394 <hr><h2><a name="cxcore_drawing_seq">Point Sets and Contours</a></h2>
5396 <hr><h3><a name="decl_cvDrawContours">DrawContours</a></h3>
5397 <p class="Blurb">Draws contour outlines or interiors in the image</p>
5399 void cvDrawContours( CvArr *img, CvSeq* contour,
5400 CvScalar external_color, CvScalar hole_color,
5401 int max_level, int thickness=1,
5402 int line_type=8, CvPoint offset=cvPoint(0,0) );
5404 <dt>img<dd>Image where the contours are to be drawn. Like in any other drawing
5405 function, the contours are clipped with the ROI.
5406 <dt>contour<dd>Pointer to the first contour.
5407 <dt>external_color<dd>Color of the external contours.
5408 <dt>hole_color<dd>Color of internal contours (holes).
5409 <dt>max_level<dd>Maximal level for drawn contours. If 0, only <code>contour</code> is drawn. If
5410 1, the contour and all contours after it on the same level are drawn. If 2, all
5411 contours after and all contours one level below the contours are drawn, etc.
5412 If the value is negative, the function does not draw the contours following after <code>contour</code>
5413 but draws child contours of <code>contour</code> up to abs(<code>max_level</code>)-1 level.
5414 <dt>thickness<dd>Thickness of lines the contours are drawn with. If it is negative (e.g. =CV_FILLED),
5415 the contour interiors are drawn.
5416 <dt>line_type<dd>Type of the contour segments, see <a href="#decl_cvLine">cvLine</a> description.
5417 <dt>offset<dd>Shift all the point coordinates by the specified value.
5418 It is useful in case if the contours retrieved in some image ROI and
5419 then the ROI offset needs to be taken into account during the rendering.
5421 The function <code>cvDrawContours</code> draws contour outlines in the image if <code>thickness</code>>=0
5422 or fills area bounded by the contours if <code>thickness</code><0.
5424 <h4>Example. Connected component detection via contour functions</h4>
5427 #include "highgui.h"
5429 int main( int argc, char** argv )
5432 // the first command line parameter must be file name of binary (black-n-white) image
5433 if( argc == 2 && (src=cvLoadImage(argv[1], 0))!= 0)
5435 IplImage* dst = cvCreateImage( cvGetSize(src), 8, 3 );
5436 CvMemStorage* storage = cvCreateMemStorage(0);
5439 cvThreshold( src, src, 1, 255, CV_THRESH_BINARY );
5440 cvNamedWindow( "Source", 1 );
5441 cvShowImage( "Source", src );
5443 cvFindContours( src, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
5446 for( ; contour != 0; contour = contour->h_next )
5448 CvScalar color = CV_RGB( rand()&255, rand()&255, rand()&255 );
5449 /* replace CV_FILLED with 1 to see the outlines */
5450 cvDrawContours( dst, contour, color, color, -1, CV_FILLED, 8 );
5453 cvNamedWindow( "Components", 1 );
5454 cvShowImage( "Components", dst );
5459 <p>Replace CV_FILLED with 1 in the sample below to see the contour outlines
5463 <hr><h3><a name="decl_cvInitLineIterator">InitLineIterator</a></h3>
5464 <p class="Blurb">Initializes line iterator</p>
5466 int cvInitLineIterator( const CvArr* image, CvPoint pt1, CvPoint pt2,
5467 CvLineIterator* line_iterator, int connectivity=8,
5468 int left_to_right=0 );
5470 <dt>image<dd>Image to sample the line from.
5471 <dt>pt1<dd>First ending point of the line segment.
5472 <dt>pt2<dd>Second ending point of the line segment.
5473 <dt>line_iterator<dd>Pointer to the line iterator state structure.
5474 <dt>connectivity<dd>The scanned line connectivity, 4 or 8.
5475 <dt>left_to_right<dd>The flag, indicating whether the line should be always scanned from the
5476 left-most point to the right-most out of <code>pt1</code> and <code>pt2</code> (<code>left_to_right≠0</code>),
5477 or it is scanned in the specified order, from <code>pt1</code> to <code>pt2</code> (<code>left_to_right=0</code>).
5479 The function <code>cvInitLineIterator</code> initializes the line iterator and returns the
5480 number of pixels between two end points. Both points must be inside the image.
5481 After the iterator has been initialized, all the points on the raster line that
5482 connects the two ending points may be retrieved by successive calls of
5483 <code>CV_NEXT_LINE_POINT</code> point. The points on the line are calculated one by one using
5484 4-connected or 8-connected Bresenham algorithm.</p>
5485 <h4>Example. Using line iterator to calculate sum of pixel values along the color line</h4>
5487 CvScalar sum_line_pixels( IplImage* image, CvPoint pt1, CvPoint pt2 )
5489 CvLineIterator iterator;
5490 int blue_sum = 0, green_sum = 0, red_sum = 0;
5491 int count = cvInitLineIterator( image, pt1, pt2, &iterator, 8, 0 );
5493 for( int i = 0; i < count; i++ ){
5494 blue_sum += iterator.ptr[0];
5495 green_sum += iterator.ptr[1];
5496 red_sum += iterator.ptr[2];
5497 CV_NEXT_LINE_POINT(iterator);
5499 /* print the pixel coordinates: demonstrates how to calculate the coordinates */
5502 /* assume that ROI is not set, otherwise need to take it into account. */
5503 offset = iterator.ptr - (uchar*)(image->imageData);
5504 y = offset/image->widthStep;
5505 x = (offset - y*image->widthStep)/(3*sizeof(uchar) /* size of pixel */);
5506 printf("(%d,%d)\n", x, y );
5509 return cvScalar( blue_sum, green_sum, red_sum );
5514 <hr><h3><a name="decl_cvClipLine">ClipLine</a></h3>
5515 <p class="Blurb">Clips the line against the image rectangle</p>
5517 int cvClipLine( CvSize img_size, CvPoint* pt1, CvPoint* pt2 );
5519 <dt>img_size<dd>Size of the image.
5520 <dt>pt1<dd>First ending point of the line segment. It is modified by the function.
5521 <dt>pt2<dd>Second ending point of the line segment. It is modified by the function.
5523 The function <code>cvClipLine</code> calculates a part of the line segment which is entirely in the image.
5524 It returns 0 if the line segment is completely outside the image and 1 otherwise.
5528 <hr><h3><a name="decl_cvEllipse2Poly">Ellipse2Poly</a></h3>
5529 <p class="Blurb">Approximates elliptic arc with polyline</p>
5531 int cvEllipse2Poly( CvPoint center, CvSize axes,
5532 int angle, int arc_start,
5533 int arc_end, CvPoint* pts, int delta );
5535 <dt>center<dd>Center of the arc.
5536 <dt>axes<dd>Half-sizes of the arc. See <a href="#decl_cvEllipse">cvEllipse</a>.
5537 <dt>angle<dd>Rotation angle of the ellipse in degrees. See <a href="#decl_cvEllipse">cvEllipse</a>.
5538 <dt>start_angle<dd>Starting angle of the elliptic arc.
5539 <dt>end_angle<dd>Ending angle of the elliptic arc.
5540 <dt>pts<dd>The array of points, filled by the function.
5541 <dt>delta<dd>Angle between the subsequent polyline vertices, approximation accuracy.
5542 So, the total number of output points will ceil((end_angle - start_angle)/delta) + 1 at max.
5544 The function <code>cvEllipse2Poly</code> computes vertices of the polyline that approximates the specified elliptic arc.
5545 It is used by <a href="#decl_cvEllipse">cvEllipse</a>.
5549 <hr><h3><a name="decl_cvClipLine">ClipLine</a></h3>
5550 CVAPI(int) cvEllipse2Poly( CvPoint center, CvSize axes,
5551 int angle, int arc_start, int arc_end, CvPoint * pts, int delta );
5554 <hr><h1><a name="cxcore_persistence">Data Persistence and RTTI</a></h1>
5556 <hr><h2><a name="cxcore_persistence_ds">File Storage</a></h2>
5558 <hr><h3><a name="decl_CvFileStorage">CvFileStorage</a></h3>
5559 <p class="Blurb">File Storage</p>
5561 typedef struct CvFileStorage
5563 ... // hidden fields
5567 The structure <a href="#decl_CvFileStorage">CvFileStorage</a> is "black box" representation of
5568 file storage that is associated with a file on disk. Several functions that are described below
5569 take <code>CvFileStorage</code> on input and allow user to save or to load hierarchical collections
5570 that consist of scalar values, standard CXCORE objects (such as matrices, sequences, graphs) and
5571 user-defined objects.
5573 CXCORE can read and write data in XML (<a href="http://www.w3c.org/XML">http://www.w3c.org/XML</a>)
5574 or YAML (<a href="http://www.yaml.org">http://www.yaml.org</a>) formats. Below is the example of
5575 3×3 floating-point identity matrix <code>A</code>, stored in XML and YAML files using CXCORE functions:</p>
5577 <dt><b>XML:</b><dd><pre>
5578 <?xml version="1.0">
5579 <opencv_storage>
5580 <A type_id="opencv-matrix">
5581 <rows>3</rows>
5582 <cols>3</cols>
5583 <dt>f</dt>
5584 <data>1. 0. 0. 0. 1. 0. 0. 0. 1.</data>
5586 </opencv_storage>
5588 <dt><b>YAML:</b><dd><pre>
5594 data: [ 1., 0., 0., 0., 1., 0., 0., 0., 1.]
5598 As it can be seen from the examples, XML uses nested tags to represent hierarchy,
5599 while YAML uses indentation for that purpose (similarly to Python programming language).
5601 The same CXCORE functions can read and write data in both formats, the particular format is determined
5602 by the extension of the opened file, .xml for XML files and .yml or .yaml for YAML.
5605 <hr><h3><a name="decl_CvFileNode">CvFileNode</a></h3>
5606 <p class="Blurb">File Storage Node</p>
5608 /* file node type */
5609 #define CV_NODE_NONE 0
5610 #define CV_NODE_INT 1
5611 #define CV_NODE_INTEGER CV_NODE_INT
5612 #define CV_NODE_REAL 2
5613 #define CV_NODE_FLOAT CV_NODE_REAL
5614 #define CV_NODE_STR 3
5615 #define CV_NODE_STRING CV_NODE_STR
5616 #define CV_NODE_REF 4 /* not used */
5617 #define CV_NODE_SEQ 5
5618 #define CV_NODE_MAP 6
5619 #define CV_NODE_TYPE_MASK 7
5621 /* optional flags */
5622 #define CV_NODE_USER 16
5623 #define CV_NODE_EMPTY 32
5624 #define CV_NODE_NAMED 64
5626 #define CV_NODE_TYPE(tag) ((tag) & CV_NODE_TYPE_MASK)
5628 #define CV_NODE_IS_INT(tag) (CV_NODE_TYPE(tag) == CV_NODE_INT)
5629 #define CV_NODE_IS_REAL(tag) (CV_NODE_TYPE(tag) == CV_NODE_REAL)
5630 #define CV_NODE_IS_STRING(tag) (CV_NODE_TYPE(tag) == CV_NODE_STRING)
5631 #define CV_NODE_IS_SEQ(tag) (CV_NODE_TYPE(tag) == CV_NODE_SEQ)
5632 #define CV_NODE_IS_MAP(tag) (CV_NODE_TYPE(tag) == CV_NODE_MAP)
5633 #define CV_NODE_IS_COLLECTION(tag) (CV_NODE_TYPE(tag) >= CV_NODE_SEQ)
5634 #define CV_NODE_IS_FLOW(tag) (((tag) & CV_NODE_FLOW) != 0)
5635 #define CV_NODE_IS_EMPTY(tag) (((tag) & CV_NODE_EMPTY) != 0)
5636 #define CV_NODE_IS_USER(tag) (((tag) & CV_NODE_USER) != 0)
5637 #define CV_NODE_HAS_NAME(tag) (((tag) & CV_NODE_NAMED) != 0)
5639 #define CV_NODE_SEQ_SIMPLE 256
5640 #define CV_NODE_SEQ_IS_SIMPLE(seq) (((seq)->flags & CV_NODE_SEQ_SIMPLE) != 0)
5642 typedef struct CvString
5649 /* all the keys (names) of elements in the read file storage
5650 are stored in the hash to speed up the lookup operations */
5651 typedef struct CvStringHashNode
5655 struct CvStringHashNode* next;
5659 /* basic element of the file storage - scalar or collection */
5660 typedef struct CvFileNode
5663 struct CvTypeInfo* info; /* type information
5664 (only for user-defined object, for others it is 0) */
5667 double f; /* scalar floating-point number */
5668 int i; /* scalar integer number */
5669 CvString str; /* text string */
5670 CvSeq* seq; /* sequence (ordered collection of file nodes) */
5671 struct CvMap* map; /* map (collection of named file nodes) */
5677 The structure is used only for retrieving data from file storage (i.e. for loading data from file).
5678 When data is written to file, it is done sequentially, with minimal buffering. No data is stored in the
5679 file storage.</p><p>In opposite, when data is read from file, the whole file is parsed and represented in memory
5680 as a tree. Every node of the tree is represented by <a href="#decl_CvFileNode">CvFileNode</a>. Type of the file node
5681 <code>N</code> can be retrieved as <code>CV_NODE_TYPE(N->tag)</code>. Some file nodes (leaves) are scalars:
5682 text strings, integer or floating-point numbers. Other file nodes are collections of file nodes, which can
5683 be scalars or collections in their turn. There are two types of collections: sequences and maps
5684 (we use YAML notation, however, the same is true for XML streams). Sequences (do not mix them with
5685 <a href="#decl_CvSeq">CvSeq</a>) are ordered collections of unnamed file nodes,
5686 maps are unordered collections of named file nodes. Thus, elements of sequences are
5687 accessed by index (<a href="#decl_cvGetSeqElem">cvGetSeqElem</a>),
5688 while elements of maps are accessed by name
5689 (<a href="#decl_cvGetFileNodeByName">cvGetFileNodeByName</a>).
5690 The table below describes the different types of a file node:</p>
5691 <p><table border=1 cellpadding="10%">
5692 <tr><td>Type</td><td>CV_NODE_TYPE(node->tag)</td><td>Value</td></tr>
5693 <tr><td>Integer</td><td>CV_NODE_INT</td><td>node->data.i</td></tr>
5694 <tr><td>Floating-point</td><td>CV_NODE_REAL</td><td>node->data.f</td></tr>
5695 <tr><td>Text string</td><td>CV_NODE_STR</td><td>node->data.str.ptr</td></tr>
5696 <tr><td>Sequence</td><td>CV_NODE_SEQ</td><td>node->data.seq</td></tr>
5697 <tr><td>Map</td><td>CV_NODE_MAP</td><td>node->data.map*</td></tr>
5700 <dt>*<dd>There is no need to access <code>map</code> field directly (BTW, <code>CvMap</code> is a hidden structure).
5701 The elements of the map can be retrieved with <a href="#decl_cvGetFileNodeByName">cvGetFileNodeByName</a>
5702 function that takes pointer to the "map" file node.
5704 <dt>**<dd>Tag of the file node that represent a user object (see below) also includes <code>CV_NODE_USER</code> flag.
5705 That is, <code>CV_NODE_IS_USER(node->tag)</code> returns 1 on such a node.
5706 <dt>***<dd>The field contains pointer to decoded object. The original map is still available as
5707 <code>node->data.obj.map</code>. -->
5710 A user (custom) object is instance of either one of standard CXCORE types, such as <a href="#decl_CvMat">CvMat</a>, <a href="#decl_CvSeq">CvSeq</a> etc.,
5711 or any type registered with <a href="#decl_cvRegisterTypeInfo">cvRegisterTypeInfo</a>. Such an object is initially represented in file as a map
5712 (as shown in XML and YAML sample files above), after file storage has been opened and parsed.
5713 Then the object can be decoded (converted to the native representation) by request -
5714 when user calls <a href="#decl_cvRead">cvRead</a> or
5715 <a href="#decl_cvReadByName">cvReadByName</a> function.</p>
5717 <hr><h3><a name="decl_CvAttrList">CvAttrList</a></h3>
5718 <p class="Blurb">List of attributes</p>
5720 typedef struct CvAttrList
5722 const char** attr; /* NULL-terminated array of (attribute_name,attribute_value) pairs */
5723 struct CvAttrList* next; /* pointer to next chunk of the attributes list */
5727 /* initializes CvAttrList structure */
5728 inline CvAttrList cvAttrList( const char** attr=NULL, CvAttrList* next=NULL );
5730 /* returns attribute value or 0 (NULL) if there is no such attribute */
5731 const char* cvAttrValue( const CvAttrList* attr, const char* attr_name );
5734 In the current implementation attributes are used to pass extra parameters when writing user objects
5735 (see <a href="#decl_cvWrite">cvWrite</a>).
5736 XML attributes inside tags are not supported, besides the object type specification
5737 (<code>type_id</code> attribute).
5741 <hr><h3><a name="decl_cvOpenFileStorage">OpenFileStorage</a></h3>
5742 <p class="Blurb">Opens file storage for reading or writing data</p>
5744 CvFileStorage* cvOpenFileStorage( const char* filename, CvMemStorage* memstorage, int flags );
5746 <dt>filename<dd>Name of the file associated with the storage.
5747 <dt>memstorage<dd>Memory storage used for temporary data and for storing dynamic structures,
5748 such as <a href="#decl_CvSeq">CvSeq</a> or <a href="#decl_CvGraph">CvGraph</a>.
5749 If it is NULL, a temporary memory storage is created and used.
5750 <dt>flags<dd>Can be one of the following:<br>
5751 <code>CV_STORAGE_READ</code> - the storage is open for reading<br>
5752 <code>CV_STORAGE_WRITE</code> - the storage is open for writing<br>
5754 The function <code>cvOpenFileStorage</code> opens file storage for
5755 reading or writing data. In the latter case a new file is created or existing file is
5756 rewritten. Type of the read of written file is determined by the filename extension: <code>.xml</code>
5757 for <em>XML</em>, and <code>.yml</code> or <code>.yaml</code> for <em>YAML</em>.
5758 The function returns pointer to <a href="#decl_CvFileStorage">CvFileStorage</a> structure.</p>
5761 <hr><h3><a name="decl_cvReleaseFileStorage">ReleaseFileStorage</a></h3>
5762 <p class="Blurb">Releases file storage</p>
5764 void cvReleaseFileStorage( CvFileStorage** fs );
5766 <dt>fs<dd>Double pointer to the released file storage.
5768 The function <code>cvReleaseFileStorage</code> closes the file
5769 associated with the storage and releases all the temporary structures.
5770 It must be called after all I/O operations with the storage are finished.</p>
5773 <hr><h2><a name="cxcore_persistence_writing">Writing Data</a></h2>
5775 <hr><h3><a name="decl_cvStartWriteStruct">StartWriteStruct</a></h3>
5776 <p class="Blurb">Starts writing a new structure</p>
5778 void cvStartWriteStruct( CvFileStorage* fs, const char* name,
5779 int struct_flags, const char* type_name=NULL,
5780 CvAttrList attributes=cvAttrList());
5782 <dt>fs<dd>File storage.
5783 <dt>name<dd>Name of the written structure. The structure can be accessed by this name when
5784 the storage is read.
5785 <dt>struct_flags<dd>A combination one of the following values:<br>
5786 <code>CV_NODE_SEQ</code> - the written structure is a sequence (see discussion of
5787 <a href="#decl_CvFileStorage">CvFileStorage</a>), that is,
5788 its elements do not have a name. <br>
5789 <code>CV_NODE_MAP</code> - the written structure is a map (see discussion of
5790 <a href="#decl_CvFileStorage">CvFileStorage</a>), that is,
5791 all its elements have names. <br>
5792 <em>One and only one of the two above flags must be specified</em><br>
5793 <code>CV_NODE_FLOW</code> - the optional flag that has sense only for YAML streams.
5794 It means that the structure is written as a flow (not as a block), which is more
5795 compact. It is recommended to use this flag for structures or arrays whose elements are
5797 <dt>type_name<dd>Optional parameter - the object type name. In case of XML it is written as
5798 <code>type_id</code> attribute of the structure opening tag. In case of YAML
5799 it is written after a colon following the structure name (see the example in
5800 <a href="#decl_CvFileStorage">CvFileStorage</a> description).
5801 Mainly it comes with user objects.
5802 When the storage is read, the encoded type name is used to determine
5803 the object type (see <a href="#decl_CvTypeInfo">CvTypeInfo</a> and
5804 <a href="#decl_cvFindTypeInfo">cvFindTypeInfo</a>).
5805 <dt>attributes<dd>This parameter is not used in the current implementation.
5807 The function <code>cvStartWriteStruct</code> starts writing
5808 a compound structure (collection) that can be a sequence or a map. After all the structure
5809 fields, which can be scalars or structures, are written,
5810 <a href="#decl_cvEndWriteStruct">cvEndWriteStruct</a> should be called.
5811 The function can be used to group some objects or to implement <em>write</em> function for a
5812 some user object (see <a href="#decl_CvTypeInfo">CvTypeInfo</a>).
5816 <hr><h3><a name="decl_cvEndWriteStruct">EndWriteStruct</a></h3>
5817 <p class="Blurb">Ends writing a structure</p>
5819 void cvEndWriteStruct( CvFileStorage* fs );
5821 <dt>fs<dd>File storage.
5823 The function <code>cvEndWriteStruct</code> finishes the
5824 currently written structure.</p>
5827 <hr><h3><a name="decl_cvWriteInt">WriteInt</a></h3>
5828 <p class="Blurb">Writes an integer value</p>
5830 void cvWriteInt( CvFileStorage* fs, const char* name, int value );
5832 <dt>fs<dd>File storage.
5833 <dt>name<dd>Name of the written value. Should be NULL if and only if the
5834 parent structure is a sequence.
5835 <dt>value<dd>The written value.
5837 The function <code>cvWriteInt</code> writes a single integer value
5838 (with or without a name) to the file storage.</p>
5841 <hr><h3><a name="decl_cvWriteReal">WriteReal</a></h3>
5842 <p class="Blurb">Writes a floating-point value</p>
5844 void cvWriteReal( CvFileStorage* fs, const char* name, double value );
5846 <dt>fs<dd>File storage.
5847 <dt>name<dd>Name of the written value. Should be NULL if and only if the
5848 parent structure is a sequence.
5849 <dt>value<dd>The written value.
5851 The function <code>cvWriteReal</code> writes a single
5852 floating-point value (with or without a name) to the file storage. The special
5853 values are encoded: NaN (Not A Number) as .NaN, ±Infinity as +.Inf (-.Inf).</p>
5854 <p>The following example shows how to use the low-level writing functions to
5855 store custom structures, such as termination criteria, without registering
5858 void write_termcriteria( CvFileStorage* fs, const char* struct_name,
5859 CvTermCriteria* termcrit )
5861 cvStartWriteStruct( fs, struct_name, CV_NODE_MAP, NULL, cvAttrList(0,0));
5862 cvWriteComment( fs, "termination criteria", 1 ); // just a description
5863 if( termcrit->type & CV_TERMCRIT_ITER )
5864 cvWriteInt( fs, "max_iterations", termcrit->max_iter );
5865 if( termcrit->type & CV_TERMCRIT_EPS )
5866 cvWriteReal( fs, "accuracy", termcrit->epsilon );
5867 cvEndWriteStruct( fs );
5872 <hr><h3><a name="decl_cvWriteString">WriteString</a></h3>
5873 <p class="Blurb">Writes a text string</p>
5875 void cvWriteString( CvFileStorage* fs, const char* name,
5876 const char* str, int quote=0 );
5878 <dt>fs<dd>File storage.
5879 <dt>name<dd>Name of the written string. Should be NULL if and only if the
5880 parent structure is a sequence.
5881 <dt>str<dd>The written text string.
5882 <dt>quote<dd>If non-zero, the written string is put in quotes, regardless of whether
5883 they are required or not. Otherwise, if the flag is zero, quotes are used
5884 only when they are required (e.g. when the string starts with a digit or contains
5887 The function <code>cvWriteString</code> writes a text string
5888 to the file storage.</p>
5891 <hr><h3><a name="decl_cvWriteComment">WriteComment</a></h3>
5892 <p class="Blurb">Writes comment</p>
5894 void cvWriteComment( CvFileStorage* fs, const char* comment, int eol_comment );
5896 <dt>fs<dd>File storage.
5897 <dt>comment<dd>The written comment, single-line or multi-line.
5898 <dt>eol_comment<dd>If non-zero, the function tries to put the comment in the end
5899 of current line. If the flag is zero, if the comment is multi-line, or
5900 if it does not fit in the end of the current line, the comment starts
5903 The function <code>cvWriteComment</code> writes a comment into the
5904 file storage. The comments are skipped when the storage is read, so they may be
5905 used only for debugging or descriptive purposes.</p>
5908 <hr><h3><a name="decl_cvStartNextStream">StartNextStream</a></h3>
5909 <p class="Blurb">Starts the next stream</p>
5911 void cvStartNextStream( CvFileStorage* fs );
5913 <dt>fs<dd>File storage.
5915 The function <code>cvStartNextStream</code> starts the next
5916 stream in the file storage. Both YAML and XML supports multiple "streams". This
5917 is useful for concatenating files or for resuming the writing process.</p>
5920 <hr><h3><a name="decl_cvWrite">Write</a></h3>
5921 <p class="Blurb">Writes user object</p>
5923 void cvWrite( CvFileStorage* fs, const char* name,
5924 const void* ptr, CvAttrList attributes=cvAttrList() );
5926 <dt>fs<dd>File storage.
5927 <dt>name<dd>Name, of the written object. Should be NULL if and only if the parent
5928 structure is a sequence.
5929 <dt>ptr<dd>Pointer to the object.
5930 <dt>attributes<dd>The attributes of the object. They are specific for each particular
5931 type (see the discussion).
5933 The function <code>cvWrite</code> writes the object to file storage.
5934 First, the appropriate type info is found using <a href="#decl_cvTypeOf">cvTypeOf</a>.
5935 Then, <code>write</code> method of the type info is called.
5937 Attributes are used to customize the writing procedure.
5938 The standard types support the following attributes
5939 (all the <code>*dt</code> attributes have the same format as in
5940 <a href="#decl_cvWriteRawData">cvWriteRawData</a>):
5942 <dt><a href="#decl_CvSeq">CvSeq</a><dd>
5944 <li><code>header_dt</code> - description
5945 of user fields of the sequence header that follow CvSeq, or CvChain
5946 (if the sequence is Freeman chain) or CvContour (if the sequence is a contour
5948 <li><code>dt</code> - description of the sequence elements.
5949 <li><code>recursive</code> - if the attribute is present and is not equal to "0" or "false",
5950 the whole tree of sequences (contours) is stored.
5952 <dt><code>CvGraph</code><dd>
5954 <li><code>header_dt</code> - description of user fields
5955 of the graph header that follow CvGraph;
5956 <li><code>vertex_dt</code> - description of
5957 user fields of graph vertices
5958 <li><code>edge_dt</code> - description of user fields of graph edges (note, that
5959 edge weight is always written, so there is no need to specify it explicitly)
5963 Below is the code that creates the YAML file shown in <code>CvFileStorage</code>
5968 int main( int argc, char** argv )
5970 CvMat* mat = cvCreateMat( 3, 3, CV_32F );
5971 CvFileStorage* fs = cvOpenFileStorage( "example.yml", 0, CV_STORAGE_WRITE );
5973 cvSetIdentity( mat );
5974 cvWrite( fs, "A", mat, cvAttrList(0,0) );
5976 cvReleaseFileStorage( &fs );
5977 cvReleaseMat( &mat );
5983 <hr><h3><a name="decl_cvWriteRawData">WriteRawData</a></h3>
5984 <p class="Blurb">Writes multiple numbers</p>
5986 void cvWriteRawData( CvFileStorage* fs, const void* src,
5987 int len, const char* dt );
5989 <dt>fs<dd>File storage.
5990 <dt>src<dd>Pointer to the written array
5991 <dt>len<dd>Number of the array elements to write.
5992 <dt>dt<dd>Specification of each array element that has the following format:
5993 <code>([count]{'u'|'c'|'w'|'s'|'i'|'f'|'d'})...</code>, where the characters
5994 correspond to fundamental C types:
5996 <li>'u' - 8-bit unsigned number
5997 <li>'c' - 8-bit signed number
5998 <li>'w' - 16-bit unsigned number
5999 <li>'s' - 16-bit signed number
6000 <li>'i' - 32-bit signed number
6001 <li>'f' - single precision floating-point number
6002 <li>'d' - double precision floating-point number
6003 <li>'r' - pointer. 32 lower bits of it are written as a signed
6004 integer. The type can be used to store structures with
6005 links between the elements.
6007 <code>count</code> is the optional counter of values of the certain type.
6008 For example, <code>dt='2if'</code> means that each array element is a structure
6009 of 2 integers, followed by a single-precision floating-point number. The equivalent
6010 notations of the above specification are <code>'iif'</code>, <code>'2i1f'</code> etc.
6011 Other examples: <code>dt='u'</code> means that the array consists of bytes,
6012 <code>dt='2d'</code> - the array consists of pairs of double’s.
6014 The function <code>cvWriteRawData</code> writes array,
6015 which elements consist of a single of multiple numbers. The function call can
6016 be replaced with a loop containing a few <a href="#decl_cvWriteInt">cvWriteInt</a>
6017 and <a href="#decl_cvWriteReal">cvWriteReal</a> calls,
6018 but a single call is more efficient. Note, that because none of the elements
6019 have a name, they should be written to a sequence rather than a map.</p>
6022 <hr><h3><a name="decl_cvWriteFileNode">WriteFileNode</a></h3>
6023 <p class="Blurb">Writes file node to another file storage</p>
6025 void cvWriteFileNode( CvFileStorage* fs, const char* new_node_name,
6026 const CvFileNode* node, int embed );
6028 <dt>fs<dd>Destination file storage.
6029 <dt>new_file_node<dd>New name of the file node in the destination file storage.
6030 To keep the existing name,
6031 use <code><a href="#decl_cvGetFileNodeName">cvGetFileNodeName</a>(node)</code>.
6032 <dt>node<dd>The written node
6033 <dt>embed<dd>If the written node is a collection and this parameter is not zero,
6034 no extra level of hierarchy is created. Instead, all the elements of <code>node</code> are
6035 written into the currently written structure. Of course, map elements may be
6036 written only to map, and sequence elements may be written only to sequence.
6038 The function <code>cvWriteFileNode</code>
6039 writes a copy of file node to file storage. The possible application of the function are:
6040 merging several file storages into one. Conversion between XML and YAML formats etc.
6044 <hr><h2><a name="cxcore_persistence_reading">Reading Data</a></h2>
6046 <p>Data are retrieved from file storage in 2 steps:
6047 first, the file node containing the requested data is found;
6048 then, data is extracted from the node manually or using custom <code>read</code> method.</p>
6051 <hr><h3><a name="decl_cvGetRootFileNode">GetRootFileNode</a></h3>
6052 <p class="Blurb">Retrieves one of top-level nodes of the file storage</p>
6054 CvFileNode* cvGetRootFileNode( const CvFileStorage* fs, int stream_index=0 );
6056 <dt>fs<dd>File storage.
6057 <dt>stream_index<dd>Zero-based index of the stream. See <a href="#decl_cvStartNextStream">cvStartNextStream</a>.
6058 In most cases, there is only one stream in the file, however there can be several.
6060 The function <code>cvGetRootFileNode</code> returns
6061 one of top-level file nodes. The top-level nodes do not have a name, they correspond to
6062 the streams, that are stored one after another in the file storage.
6063 If the index is out of range, the function returns NULL pointer, so all the top-level
6064 nodes may be iterated by subsequent calls to the function with <code>stream_index=0,1,...</code>,
6065 until NULL pointer is returned. This function may be used as a base for recursive traversal of the
6069 <hr><h3><a name="decl_cvGetFileNodeByName">GetFileNodeByName</a></h3>
6070 <p class="Blurb">Finds node in the map or file storage</p>
6072 CvFileNode* cvGetFileNodeByName( const CvFileStorage* fs,
6073 const CvFileNode* map,
6076 <dt>fs<dd>File storage.
6077 <dt>map<dd>The parent map. If it is NULL, the function searches
6078 in all the top-level nodes (streams), starting from the first one.
6079 <dt>name<dd>The file node name.
6081 The function <code>cvGetFileNodeByName</code> finds
6082 a file node by <code>name</code>. The node is searched either in <code>map</code>
6083 or, if the pointer is NULL, among the top-level file nodes of the storage.
6084 Using this function for maps and <a href="#decl_cvGetSeqElem">cvGetSeqElem</a>
6085 (or sequence reader) for sequences, it is possible to navigate through the file storage.
6086 To speed up multiple queries for a certain key (e.g. in case of array of structures)
6087 one may use a pair of <a href="#decl_cvGetHashedKey">cvGetHashedKey</a> and
6088 <a href="#decl_cvGetFileNode">cvGetFileNode</a>.</p>
6091 <hr><h3><a name="decl_cvGetHashedKey">GetHashedKey</a></h3>
6092 <p class="Blurb">Returns a unique pointer for given name</p>
6094 CvStringHashNode* cvGetHashedKey( CvFileStorage* fs, const char* name,
6095 int len=-1, int create_missing=0 );
6097 <dt>fs<dd>File storage.
6098 <dt>name<dd>Literal node name.
6099 <dt>len<dd>Length of the name (if it is known a priori), or -1 if it needs to
6101 <dt>create_missing<dd>Flag that specifies, whether an absent key should be
6102 added into the hash table, or not.
6104 The function <code>cvGetHashedKey</code> returns
6105 the unique pointer for each particular file node name. This pointer can be
6106 then passed to <a href="#decl_cvGetFileNode">cvGetFileNode</a> function that
6107 is faster than <a href="#decl_cvGetFileNodeByName">cvGetFileNodeByName</a>
6108 because it compares text strings by comparing pointers rather than the
6109 strings' content.</p><p>Consider the following example: an array of points
6110 is encoded as a sequence of 2-entry maps, e.g.:
6119 Then, it is possible to get hashed "x" and "y" pointers to speed up
6120 decoding of the points.
6121 <h4>Example. Reading an array of structures from file storage</h4>
6125 int main( int argc, char** argv )
6127 CvFileStorage* fs = cvOpenFileStorage( "points.yml", 0, CV_STORAGE_READ );
6128 CvStringHashNode* x_key = cvGetHashedNode( fs, "x", -1, 1 );
6129 CvStringHashNode* y_key = cvGetHashedNode( fs, "y", -1, 1 );
6130 CvFileNode* points = cvGetFileNodeByName( fs, 0, "points" );
6132 if( CV_NODE_IS_SEQ(points->tag) )
6134 CvSeq* seq = points->data.seq;
6135 int i, total = seq->total;
6137 cvStartReadSeq( seq, &reader, 0 );
6138 for( i = 0; i < total; i++ )
6140 CvFileNode* pt = (CvFileNode*)reader.ptr;
6141 #if 1 /* faster variant */
6142 CvFileNode* xnode = cvGetFileNode( fs, pt, x_key, 0 );
6143 CvFileNode* ynode = cvGetFileNode( fs, pt, y_key, 0 );
6144 assert( xnode && CV_NODE_IS_INT(xnode->tag) &&
6145 ynode && CV_NODE_IS_INT(ynode->tag));
6146 int x = xnode->data.i; // or x = cvReadInt( xnode, 0 );
6147 int y = ynode->data.i; // or y = cvReadInt( ynode, 0 );
6148 #elif 1 /* slower variant; does not use x_key & y_key */
6149 CvFileNode* xnode = cvGetFileNodeByName( fs, pt, "x" );
6150 CvFileNode* ynode = cvGetFileNodeByName( fs, pt, "y" );
6151 assert( xnode && CV_NODE_IS_INT(xnode->tag) &&
6152 ynode && CV_NODE_IS_INT(ynode->tag));
6153 int x = xnode->data.i; // or x = cvReadInt( xnode, 0 );
6154 int y = ynode->data.i; // or y = cvReadInt( ynode, 0 );
6155 #else /* the slowest yet the easiest to use variant */
6156 int x = cvReadIntByName( fs, pt, "x", 0 /* default value */ );
6157 int y = cvReadIntByName( fs, pt, "y", 0 /* default value */ );
6159 CV_NEXT_SEQ_ELEM( seq->elem_size, reader );
6160 printf("%d: (%d, %d)\n", i, x, y );
6163 cvReleaseFileStorage( &fs );
6167 <p>Please note that, whatever method of accessing map you are using,
6168 it is still <em>much</em> slower than using plain sequences, for example,
6169 in the above sample, it is more efficient to encode the points as pairs of
6170 integers in the single numeric sequence.</p>
6173 <hr><h3><a name="decl_cvGetFileNode">GetFileNode</a></h3>
6174 <p class="Blurb">Finds node in the map or file storage</p>
6176 CvFileNode* cvGetFileNode( CvFileStorage* fs, CvFileNode* map,
6177 const CvStringHashNode* key, int create_missing=0 );
6179 <dt>fs<dd>File storage.
6180 <dt>map<dd>The parent map. If it is NULL, the function searches a top-level node.
6181 If both <code>map</code> and <code>key</code> are NULLs,
6182 the function returns the root file node - a map
6183 that contains top-level nodes.
6184 <dt>key<dd>Unique pointer to the node name, retrieved with
6185 <a href="#decl_cvGetHashedKey">cvGetHashedKey</a>.
6186 <dt>create_missing<dd>Flag that specifies, whether an absent node should be
6187 added to the map, or not.
6189 The function <code>cvGetFileNode</code> finds a file node. It is
6190 a faster version <a href="#decl_cvGetFileNodeByName">cvGetFileNodeByName</a>
6191 (see <a href="#decl_cvGetHashedKey">cvGetHashedKey</a> discussion). Also,
6192 the function can insert a new node, if it is not in the map yet (which is used
6193 by parsing functions).</p>
6196 <hr><h3><a name="decl_cvGetFileNodeName">GetFileNodeName</a></h3>
6197 <p class="Blurb">Returns name of file node</p>
6199 const char* cvGetFileNodeName( const CvFileNode* node );
6201 <dt>node<dd>File node
6203 The function <code>cvGetFileNodeName</code> returns name of the file node
6204 or NULL, if the file node does not have a name, or if <code>node</code> is <code>NULL</code>.</p>
6207 <hr><h3><a name="decl_cvReadInt">ReadInt</a></h3>
6208 <p class="Blurb">Retrieves integer value from file node</p>
6210 int cvReadInt( const CvFileNode* node, int default_value=0 );
6212 <dt>node<dd>File node.
6213 <dt>default_value<dd>The value that is returned if <code>node</code> is NULL.
6215 The function <code>cvReadInt</code> returns integer that is
6216 represented by the file node. If the file node is NULL, <code>default_value</code>
6217 is returned (thus, it is convenient to call the function right after
6218 <a href="#decl_cvGetFileNode">cvGetFileNode</a> without checking for NULL pointer),
6219 otherwise if the file node has type <code>CV_NODE_INT</code>,
6220 then <code>node->data.i</code> is returned, otherwise if the file node has
6221 type <code>CV_NODE_REAL</code>, then <code>node->data.f</code>
6222 is converted to integer and returned, otherwise the result is not determined.</p>
6225 <hr><h3><a name="decl_cvReadIntByName">ReadIntByName</a></h3>
6226 <p class="Blurb">Finds file node and returns its value</p>
6228 int cvReadIntByName( const CvFileStorage* fs, const CvFileNode* map,
6229 const char* name, int default_value=0 );
6231 <dt>fs<dd>File storage.
6232 <dt>map<dd>The parent map. If it is NULL, the function searches a top-level node.
6233 <dt>name<dd>The node name.
6234 <dt>default_value<dd>The value that is returned if the file node is not found.
6236 The function <code>cvReadIntByName</code> is a simple
6237 superposition of <a href="#decl_cvGetFileNodeByName">cvGetFileNodeByName</a> and
6238 <a href="#decl_cvReadInt">cvReadInt</a>.
6241 <hr><h3><a name="decl_cvReadReal">ReadReal</a></h3>
6242 <p class="Blurb">Retrieves floating-point value from file node</p>
6244 double cvReadReal( const CvFileNode* node, double default_value=0. );
6246 <dt>node<dd>File node.
6247 <dt>default_value<dd>The value that is returned if <code>node</code> is NULL.
6249 The function <code>cvReadReal</code> returns floating-point value that is
6250 represented by the file node. If the file node is NULL, <code>default_value</code>
6251 is returned (thus, it is convenient to call the function right after
6252 <a href="#decl_cvGetFileNode">cvGetFileNode</a> without checking for NULL pointer),
6253 otherwise if the file node has type <code>CV_NODE_REAL</code>,
6254 then <code>node->data.f</code> is returned, otherwise if the file node has
6255 type <code>CV_NODE_INT</code>, then <code>node->data.f</code>
6256 is converted to floating-point and returned, otherwise the result is not determined.</p>
6259 <hr><h3><a name="decl_cvReadRealByName">ReadRealByName</a></h3>
6260 <p class="Blurb">Finds file node and returns its value</p>
6262 double cvReadRealByName( const CvFileStorage* fs, const CvFileNode* map,
6263 const char* name, double default_value=0. );
6265 <dt>fs<dd>File storage.
6266 <dt>map<dd>The parent map. If it is NULL, the function searches a top-level node.
6267 <dt>name<dd>The node name.
6268 <dt>default_value<dd>The value that is returned if the file node is not found.
6270 The function <code>cvReadRealByName</code> is a simple
6271 superposition of <a href="#decl_cvGetFileNodeByName">cvGetFileNodeByName</a> and
6272 <a href="#decl_cvReadReal">cvReadReal</a>.
6275 <hr><h3><a name="decl_cvReadString">ReadString</a></h3>
6276 <p class="Blurb">Retrieves text string from file node</p>
6278 const char* cvReadString( const CvFileNode* node, const char* default_value=NULL );
6280 <dt>node<dd>File node.
6281 <dt>default_value<dd>The value that is returned if <code>node</code> is NULL.
6283 The function <code>cvReadString</code> returns text string that is
6284 represented by the file node. If the file node is NULL, <code>default_value</code>
6285 is returned (thus, it is convenient to call the function right after
6286 <a href="#decl_cvGetFileNode">cvGetFileNode</a> without checking for NULL pointer),
6287 otherwise if the file node has type <code>CV_NODE_STR</code>,
6288 then <code>node->data.str.ptr</code> is returned, otherwise the result is not determined.</p>
6291 <hr><h3><a name="decl_cvReadStringByName">ReadStringByName</a></h3>
6292 <p class="Blurb">Finds file node and returns its value</p>
6294 const char* cvReadStringByName( const CvFileStorage* fs, const CvFileNode* map,
6295 const char* name, const char* default_value=NULL );
6297 <dt>fs<dd>File storage.
6298 <dt>map<dd>The parent map. If it is NULL, the function searches a top-level node.
6299 <dt>name<dd>The node name.
6300 <dt>default_value<dd>The value that is returned if the file node is not found.
6302 The function <code>cvReadStringByName</code> is a simple
6303 superposition of <a href="#decl_cvGetFileNodeByName">cvGetFileNodeByName</a> and
6304 <a href="#decl_cvReadString">cvReadString</a>.
6307 <hr><h3><a name="decl_cvRead">Read</a></h3>
6308 <p class="Blurb">Decodes object and returns pointer to it</p>
6310 void* cvRead( CvFileStorage* fs, CvFileNode* node,
6311 CvAttrList* attributes=NULL );
6313 <dt>fs<dd>File storage.
6314 <dt>node<dd>The root object node.
6315 <dt>attributes<dd>Unused parameter.
6317 The function <code>cvRead</code> decodes user object (creates object
6318 in a native representation from the file storage subtree) and returns it.
6319 The object to be decoded must be an instance of registered type that supports <code>read</code>
6320 method (see <a href="#decl_CvTypeInfo">CvTypeInfo</a>). Type of the object is determined by
6321 the type name that is encoded in the file. If the object is dynamic structure, it is
6322 created either in memory storage, passed to <a href="#decl_cvOpenFileStorage">cvOpenFileStorage</a>
6323 or, if NULL pointer was passed, in temporary memory storage, which is release when
6324 <a href="#decl_cvReleaseFileStorage">cvReleaseFileStorage</a> is called.
6325 Otherwise, if the object is not a dynamic structure, it is created in heap and should be
6326 released with a specialized function or using generic <a href="#decl_cvRelease">cvRelease</a>.</p>
6329 <hr><h3><a name="decl_cvReadByName">ReadByName</a></h3>
6330 <p class="Blurb">Finds object and decodes it</p>
6332 void* cvReadByName( CvFileStorage* fs, const CvFileNode* map,
6333 const char* name, CvAttrList* attributes=NULL );
6335 <dt>fs<dd>File storage.
6336 <dt>map<dd>The parent map. If it is NULL, the function searches a top-level node.
6337 <dt>name<dd>The node name.
6338 <dt>attributes<dd>Unused parameter.
6340 The function <code>cvReadByName</code> is a simple
6341 superposition of <a href="#decl_cvGetFileNodeByName">cvGetFileNodeByName</a> and
6342 <a href="#decl_cvRead">cvRead</a>.
6345 <hr><h3><a name="decl_cvReadRawData">ReadRawData</a></h3>
6346 <p class="Blurb">Reads multiple numbers</p>
6348 void cvReadRawData( const CvFileStorage* fs, const CvFileNode* src,
6349 void* dst, const char* dt );
6351 <dt>fs<dd>File storage.
6352 <dt>src<dd>The file node (a sequence) to read numbers from.
6353 <dt>dst<dd>Pointer to the destination array.
6354 <dt>dt<dd>Specification of each array element. It has the same format as in
6355 <a href="#decl_cvWriteRawData">cvWriteRawData</a>.
6357 The function <code>cvReadRawData</code> reads elements
6358 from a file node that represents a sequence of scalars</p>
6361 <hr><h3><a name="decl_cvStartReadRawData">StartReadRawData</a></h3>
6362 <p class="Blurb">Initializes file node sequence reader</p>
6364 void cvStartReadRawData( const CvFileStorage* fs, const CvFileNode* src,
6365 CvSeqReader* reader );
6367 <dt>fs<dd>File storage.
6368 <dt>src<dd>The file node (a sequence) to read numbers from.
6369 <dt>reader<dd>Pointer to the sequence reader.
6371 The function <code>cvStartReadRawData</code> initializes
6372 sequence reader to read data from file node. The initialized reader can be then
6373 passed to <a href="#decl_cvReadRawDataSlice">cvReadRawDataSlice</a>.</p>
6376 <hr><h3><a name="decl_cvReadRawDataSlice">ReadRawDataSlice</a></h3>
6377 <p class="Blurb">Initializes file node sequence reader</p>
6379 void cvReadRawDataSlice( const CvFileStorage* fs, CvSeqReader* reader,
6380 int count, void* dst, const char* dt );
6382 <dt>fs<dd>File storage.
6383 <dt>reader<dd>The sequence reader. Initialize it with <a href="#decl_cvStartReadRawData">cvStartReadRawData</a>.
6384 <dt>count<dd>The number of elements to read.
6385 <dt>dst<dd>Pointer to the destination array.
6386 <dt>dt<dd>Specification of each array element. It has the same format as in
6387 <a href="#decl_cvWriteRawData">cvWriteRawData</a>.
6389 The function <code>cvReadRawDataSlice</code> reads one
6390 or more elements from the file node, representing a sequence, to user-specified
6391 array. The total number of read sequence elements is a product of <code>total</code>
6392 and the number of components in each array element.
6393 For example, if <code>dt='2if'</code>, the function will read <code>total*3</code> sequence elements.
6394 As with any sequence, some parts of the file node sequence may be skipped or read repeatedly
6395 by repositioning the reader using <a href="#decl_cvSetSeqReaderPos">cvSetSeqReaderPos</a>.</p>
6398 <hr><h2><a name="cxcore_persistence_rtti">RTTI and Generic Functions</a></h2>
6401 <hr><h3><a name="decl_CvTypeInfo">CvTypeInfo</a></h3>
6402 <p class="Blurb">Type information</p>
6404 typedef int (CV_CDECL *CvIsInstanceFunc)( const void* struct_ptr );
6405 typedef void (CV_CDECL *CvReleaseFunc)( void** struct_dblptr );
6406 typedef void* (CV_CDECL *CvReadFunc)( CvFileStorage* storage, CvFileNode* node );
6407 typedef void (CV_CDECL *CvWriteFunc)( CvFileStorage* storage,
6409 const void* struct_ptr,
6410 CvAttrList attributes );
6411 typedef void* (CV_CDECL *CvCloneFunc)( const void* struct_ptr );
6413 typedef struct CvTypeInfo
6415 int flags; /* not used */
6416 int header_size; /* sizeof(CvTypeInfo) */
6417 struct CvTypeInfo* prev; /* previous registered type in the list */
6418 struct CvTypeInfo* next; /* next registered type in the list */
6419 const char* type_name; /* type name, written to file storage */
6422 CvIsInstanceFunc is_instance; /* checks if the passed object belongs to the type */
6423 CvReleaseFunc release; /* releases object (memory etc.) */
6424 CvReadFunc read; /* reads object from file storage */
6425 CvWriteFunc write; /* writes object to file storage */
6426 CvCloneFunc clone; /* creates a copy of the object */
6430 The structure <a href="#decl_CvTypeInfo">CvTypeInfo</a> contains information about
6431 one of standard or user-defined types. Instances of the type may or may not contain
6432 pointer to the corresponding <a href="#decl_CvTypeInfo">CvTypeInfo</a> structure.
6433 In any case there is a way to find type info structure for given object - using
6434 <a href="#decl_cvTypeOf">cvTypeOf</a> function. Alternatively, type info can be
6435 found by the type name using <a href="#decl_cvFindType">cvFindType</a>, which is
6436 used when object is read from file storage. User can register a new type with
6437 <a href="#decl_cvRegisterType">cvRegisterType</a> that adds the type information structure
6438 into the beginning of the type list - thus, it is possible to create specialized types
6439 from generic standard types and override the basic methods.
6443 <hr><h3><a name="decl_cvRegisterType">RegisterType</a></h3>
6444 <p class="Blurb">Registers new type</p>
6446 void cvRegisterType( const CvTypeInfo* info );
6448 <dt>info<dd>Type info structure.
6450 The function <code>cvRegisterType</code> registers a new type,
6451 which is described by <code>info</code>. The function creates a copy of the structure,
6452 so user should delete it after calling the function.</p>
6455 <hr><h3><a name="decl_cvUnregisterType">UnregisterType</a></h3>
6456 <p class="Blurb">Unregisters the type</p>
6458 void cvUnregisterType( const char* type_name );
6460 <dt>type_name<dd>Name of the unregistered type.
6462 The function <code>cvUnregisterType</code> unregisters the type
6463 with the specified name. If the name is unknown, it is possible to locate the type info
6464 by an instance of the type using <a href="#decl_cvTypeOf">cvTypeOf</a> or by iterating
6465 the type list, starting from <a href="#decl_cvFirstType">cvFirstType</a>, and then
6466 call <code><a href="#decl_cvUnregisterType">cvUnregisterType</a>(info->type_name)</code>.</p>
6469 <hr><h3><a name="decl_cvFirstType">FirstType</a></h3>
6470 <p class="Blurb">Returns the beginning of type list</p>
6472 CvTypeInfo* cvFirstType( void );
6474 The function <code>cvFirstType</code> returns the first type of the list
6475 of registered types. Navigation through the list can be done via <code>prev</code> and <code>next</code>
6476 fields of <a href="#decl_CvTypeInfo">CvTypeInfo</a> structure.</p>
6479 <hr><h3><a name="decl_cvFindType">FindType</a></h3>
6480 <p class="Blurb">Finds type by its name</p>
6482 CvTypeInfo* cvFindType( const char* type_name );
6484 <dt>type_name<dd>Type name.
6486 The function <code>cvFindType</code> finds a registered type by its name.
6487 It returns NULL, if there is no type with the specified name.</p>
6490 <hr><h3><a name="decl_cvTypeOf">TypeOf</a></h3>
6491 <p class="Blurb">Returns type of the object</p>
6493 CvTypeInfo* cvTypeOf( const void* struct_ptr );
6495 <dt>struct_ptr<dd>The object pointer.
6497 The function <code>cvTypeOf</code> finds the type of given object. It iterates through the list
6498 of registered types and calls <code>is_instance</code> function/method of every type info structure with
6499 the object until one of them return non-zero or until the whole list has been traversed. In the latter
6500 case the function returns NULL.</p>
6503 <hr><h3><a name="decl_cvRelease">Release</a></h3>
6504 <p class="Blurb">Releases the object</p>
6506 void cvRelease( void** struct_ptr );
6508 <dt>struct_ptr<dd>Double pointer to the object.
6510 The function <code>cvRelease</code> finds the type of given object and calls
6511 <code>release</code> with the double pointer.</p>
6514 <hr><h3><a name="decl_cvClone">Clone</a></h3>
6515 <p class="Blurb">Makes a clone of the object</p>
6517 void* cvClone( const void* struct_ptr );
6519 <dt>struct_ptr<dd>The object to clone.
6521 The function <code>cvClone</code> finds the type of given object and calls
6522 <code>clone</code> with the passed object.</p>
6525 <hr><h3><a name="decl_cvSave">Save</a></h3>
6526 <p class="Blurb">Saves object to file</p>
6528 void cvSave( const char* filename, const void* struct_ptr,
6529 const char* name=NULL,
6530 const char* comment=NULL,
6531 CvAttrList attributes=cvAttrList());
6533 <dt>filename<dd>File name.
6534 <dt>struct_ptr<dd>Object to save.
6535 <dt>name<dd>Optional object name. If it is NULL, the name will be formed from <code>filename</code>.
6536 <dt>comment<dd>Optional comment to put in the beginning of the file.
6537 <dt>attributes<dd>Optional attributes passed to <a href="#decl_cvWrite">cvWrite</a>.
6539 The function <code>cvSave</code> saves object to file. It provides a simple interface
6540 to <a href="#decl_cvWrite">cvWrite</a>.</p>
6543 <hr><h3><a name="decl_cvLoad">Load</a></h3>
6544 <p class="Blurb">Loads object from file</p>
6546 void* cvLoad( const char* filename, CvMemStorage* memstorage=NULL,
6547 const char* name=NULL, const char** real_name=NULL );
6549 <dt>filename<dd>File name.
6550 <dt>memstorage<dd>Memory storage for dynamic structures, such as <a href="#decl_CvSeq">CvSeq</a>
6551 or <a href="#decl_CvGraph">CvGraph</a>. It is not used for matrices or images.
6552 <dt>name<dd>Optional object name. If it is NULL, the first top-level object in the storage will be loaded.
6553 <dt>real_name<dd>Optional output parameter that will contain name of the loaded object
6554 (useful if <code>name=NULL</code>).
6556 The function <code>cvLoad</code> loads object from file.
6557 It provides a simple interface to <a href="#decl_cvRead">cvRead</a>.
6558 After object is loaded,
6559 the file storage is closed and all the temporary buffers are deleted. Thus, to load a dynamic structure,
6560 such as sequence, contour or graph, one should pass a valid destination
6561 memory storage to the function.</p>
6564 <hr><h1><a name="cxcore_misc">Miscellaneous Functions</a></h1>
6566 <hr><h3><a name="decl_cvCheckArr">CheckArr</a></h3>
6567 <p class="Blurb">Checks every element of input array for invalid values</p>
6569 int cvCheckArr( const CvArr* arr, int flags=0,
6570 double min_val=0, double max_val=0);
6571 #define cvCheckArray cvCheckArr
6573 <dt>arr<dd>The array to check.
6574 <dt>flags<dd>The operation flags, 0 or combination of:<br>
6575 <code>CV_CHECK_RANGE</code> - if set, the function checks that every value of
6576 array is within [minVal,maxVal) range,
6577 otherwise it just checks that every element
6578 is neither NaN nor ±Infinity.<br>
6579 <code>CV_CHECK_QUIET</code> - if set, the function does not raises an
6580 error if an element is invalid or out of range
6581 <dt>min_val<dd>The inclusive lower boundary of valid values range.
6582 It is used only if <code>CV_CHECK_RANGE</code> is set.
6583 <dt>max_val<dd>The exclusive upper boundary of valid values range.
6584 It is used only if <code>CV_CHECK_RANGE</code> is set.
6586 The function <code>cvCheckArr</code> checks that every array element
6587 is neither NaN nor ±Infinity.
6588 If <code>CV_CHECK_RANGE</code> is set, it also checks that every element is
6589 greater than or equal to <code>minVal</code> and less than <code>maxVal</code>.
6590 The function returns nonzero if the check succeeded, i.e. all elements
6591 are valid and within the range, and zero otherwise.
6592 In the latter case if <code>CV_CHECK_QUIET</code> flag is not set, the function
6593 raises runtime error.
6597 <hr><h3><a name="decl_cvKMeans2">KMeans2</a></h3>
6598 <p class="Blurb">Splits set of vectors by given number of clusters</p>
6600 void cvKMeans2( const CvArr* samples, int cluster_count,
6601 CvArr* labels, CvTermCriteria termcrit );
6603 <dt>samples<dd>Floating-point matrix of input samples, one row per sample.
6604 <dt>cluster_count<dd>Number of clusters to split the set by.
6605 <dt>labels<dd>Output integer vector storing cluster indices for every sample.
6606 <dt>termcrit<dd>Specifies maximum number of iterations and/or accuracy (distance the centers move by
6607 between the subsequent iterations).
6609 The function <code>cvKMeans2</code> implements k-means algorithm that finds centers of <code>cluster_count</code> clusters
6610 and groups the input samples around the clusters. On output <code>labels(i)</code> contains a cluster index
6611 for sample stored in the i-th row of <code>samples</code> matrix.
6612 <h4>Example. Clustering random samples of k-variate Gaussian distribution</h4>
6615 #include "highgui.h"
6617 void main( int argc, char** argv )
6619 #define MAX_CLUSTERS 5
6620 CvScalar color_tab[MAX_CLUSTERS];
6621 IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
6622 CvRNG rng = cvRNG(0xffffffff);
6624 color_tab[0] = CV_RGB(255,0,0);
6625 color_tab[1] = CV_RGB(0,255,0);
6626 color_tab[2] = CV_RGB(100,100,255);
6627 color_tab[3] = CV_RGB(255,0,255);
6628 color_tab[4] = CV_RGB(255,255,0);
6630 cvNamedWindow( "clusters", 1 );
6634 int k, cluster_count = cvRandInt(&rng)%MAX_CLUSTERS + 1;
6635 int i, sample_count = cvRandInt(&rng)%1000 + 1;
6636 CvMat* points = cvCreateMat( sample_count, 1, CV_32FC2 );
6637 CvMat* clusters = cvCreateMat( sample_count, 1, CV_32SC1 );
6639 /* generate random sample from multivariate Gaussian distribution */
6640 for( k = 0; k < cluster_count; k++ )
6644 center.x = cvRandInt(&rng)%img->width;
6645 center.y = cvRandInt(&rng)%img->height;
6646 cvGetRows( points, &point_chunk, k*sample_count/cluster_count,
6647 k == cluster_count - 1 ? sample_count : (k+1)*sample_count/cluster_count );
6648 cvRandArr( &rng, &point_chunk, CV_RAND_NORMAL,
6649 cvScalar(center.x,center.y,0,0),
6650 cvScalar(img->width/6, img->height/6,0,0) );
6653 /* shuffle samples */
6654 for( i = 0; i < sample_count/2; i++ )
6656 CvPoint2D32f* pt1 = (CvPoint2D32f*)points->data.fl + cvRandInt(&rng)%sample_count;
6657 CvPoint2D32f* pt2 = (CvPoint2D32f*)points->data.fl + cvRandInt(&rng)%sample_count;
6659 CV_SWAP( *pt1, *pt2, temp );
6662 cvKMeans2( points, cluster_count, clusters,
6663 cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0 ));
6667 for( i = 0; i < sample_count; i++ )
6669 CvPoint2D32f pt = ((CvPoint2D32f*)points->data.fl)[i];
6670 int cluster_idx = clusters->data.i[i];
6671 cvCircle( img, cvPointFrom32f(pt), 2, color_tab[cluster_idx], CV_FILLED );
6674 cvReleaseMat( &points );
6675 cvReleaseMat( &clusters );
6677 cvShowImage( "clusters", img );
6679 int key = cvWaitKey(0);
6680 if( key == 27 ) // 'ESC'
6687 <hr><h3><a name="decl_cvSeqPartition">SeqPartition</a></h3>
6688 <p class="Blurb">Splits sequence into equivalence classes</p>
6690 typedef int (CV_CDECL* CvCmpFunc)(const void* a, const void* b, void* userdata);
6691 int cvSeqPartition( const CvSeq* seq, CvMemStorage* storage, CvSeq** labels,
6692 CvCmpFunc is_equal, void* userdata );
6694 <dt>seq<dd>The sequence to partition.
6695 <dt>storage<dd>The storage to store the sequence of equivalence classes. If it is NULL,
6696 the function uses <code>seq->storage</code> for output labels.
6697 <dt>labels<dd>Output parameter. Double pointer to the sequence
6698 of 0-based labels of input sequence elements.
6699 <dt>is_equal<dd>The relation function that should return non-zero if the two particular sequence elements
6700 are from the same class, and zero otherwise.
6701 The partitioning algorithm uses transitive closure of the relation function as equivalence criteria.
6702 <dt>userdata<dd>Pointer that is transparently passed to the <code>is_equal</code> function.
6704 The function <code>cvSeqPartition</code> implements quadratic algorithm for splitting
6705 a set into one or more classes of equivalence. The function returns the number of equivalence classes.
6707 <h4>Example. Partitioning 2d point set.</h4>
6710 #include "highgui.h"
6711 #include <stdio.h>
6713 CvSeq* point_seq = 0;
6714 IplImage* canvas = 0;
6715 CvScalar* colors = 0;
6718 int is_equal( const void* _a, const void* _b, void* userdata )
6720 CvPoint a = *(const CvPoint*)_a;
6721 CvPoint b = *(const CvPoint*)_b;
6722 double threshold = *(double*)userdata;
6723 return (double)(a.x - b.x)*(a.x - b.x) + (double)(a.y - b.y)*(a.y - b.y) <= threshold;
6726 void on_track( int pos )
6729 double threshold = pos*pos;
6730 int i, class_count = cvSeqPartition( point_seq, 0, &labels, is_equal, &threshold );
6731 printf("%4d classes\n", class_count );
6734 for( i = 0; i < labels->total; i++ )
6736 CvPoint pt = *(CvPoint*)cvGetSeqElem( point_seq, i, 0 );
6737 CvScalar color = colors[*(int*)cvGetSeqElem( labels, i, 0 )];
6738 cvCircle( canvas, pt, 1, color, -1 );
6741 cvShowImage( "points", canvas );
6744 int main( int argc, char** argv )
6746 CvMemStorage* storage = cvCreateMemStorage(0);
6747 point_seq = cvCreateSeq( CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), storage );
6748 CvRNG rng = cvRNG(0xffffffff);
6750 int width = 500, height = 500;
6751 int i, count = 1000;
6752 canvas = cvCreateImage( cvSize(width,height), 8, 3 );
6754 colors = (CvScalar*)cvAlloc( count*sizeof(colors[0]) );
6755 for( i = 0; i < count; i++ )
6759 pt.x = cvRandInt( &rng ) % width;
6760 pt.y = cvRandInt( &rng ) % height;
6761 cvSeqPush( point_seq, &pt );
6762 icolor = cvRandInt( &rng ) | 0x00404040;
6763 colors[i] = CV_RGB(icolor & 255, (icolor >> 8)&255, (icolor >> 16)&255);
6766 cvNamedWindow( "points", 1 );
6767 cvCreateTrackbar( "threshold", "points", &pos, 50, on_track );
6775 <hr><h1><a name="cxcore_system">Error Handling and System Functions</a></h1>
6777 <hr><h2><a name="cxcore_system_error">Error Handling</a></h2>
6779 <p>Error handling in OpenCV is similar to IPL (Image Processing Library).
6780 In case of error functions do not return the error code. Instead, they raise
6781 an error using <a href="#decl_error_macros">CV_ERROR</a> macro that calls
6782 <a href="#decl_cvError">cvError</a> that, in its turn, sets the error status
6783 with <a href="#decl_cvSetErrStatus">cvSetErrStatus</a> and
6784 calls a standard or user-defined error handler (that can display a message box,
6785 write to log etc., see <a href="#decl_cvRedirectError">cvRedirectError</a>,
6786 <a href="#decl_cvNulDevReport">cvNulDevReport, cvStdErrReport, cvGuiBoxReport</a>).
6787 There is global variable, one per each program thread, that contains current
6788 error status (an integer value). The status can be retrieved with
6789 <a href="#decl_cvGetErrStatus">cvGetErrStatus</a> function.
6792 There are three modes of error handling (see <a href="#decl_cvSetErrMode">cvSetErrMode</a>
6793 and <a href="#decl_cvGetErrMode">cvGetErrMode</a>):
6795 <dt>Leaf<dd>The program is terminated after error handler is called.
6796 <em>This is the default value</em>. It is useful for debugging, as the error
6797 is signalled immediately after it occurs. However, for production systems
6798 other two methods may be preferable as they provide more control.
6799 <dt>Parent<dd>The program is not terminated, but the error handler is called.
6800 The stack is unwinded (it is done w/o using C++ exception mechanism).
6801 User may check error code after calling Cxcore function with
6802 <a href="#decl_cvGetErrStatus">cvGetErrStatus</a> and react.
6803 <dt>Silent<dd>Similar to <em>Parent</em> mode, but no error handler is called.
6805 <p>Actually, the semantics of <em>Leaf</em> and <em>Parent</em> modes is implemented by
6806 error handlers and the above description is true for <a href="#decl_cvNulDevReport">cvNulDevReport, cvStdErrReport</a>.
6807 <a href="#decl_cvNulDevReport">cvGuiBoxReport</a> behaves slightly differently,
6808 and some custom error handler may implement quite different semantics.</p>
6810 <hr><h3><a name="decl_error_macros">ERROR Handling Macros</a></h3>
6811 <p class="Blurb">Macros for raising an error, checking for errors etc.</p>
6813 /* special macros for enclosing processing statements within a function and separating
6814 them from prologue (resource initialization) and epilogue (guaranteed resource release) */
6816 #define __END__ goto exit; exit: ; }
6817 /* proceeds to "resource release" stage */
6818 #define EXIT goto exit
6820 /* Declares locally the function name for CV_ERROR() use */
6821 #define CV_FUNCNAME( Name ) \
6822 static char cvFuncName[] = Name
6824 /* Raises an error within the current context */
6825 #define CV_ERROR( Code, Msg ) \
6827 cvError( (Code), cvFuncName, Msg, __FILE__, __LINE__ ); \
6831 /* Checks status after calling CXCORE function */
6832 #define CV_CHECK() \
6834 if( cvGetErrStatus() < 0 ) \
6835 CV_ERROR( CV_StsBackTrace, "Inner function failed." ); \
6838 /* Provides shorthand for CXCORE function call and CV_CHECK() */
6839 #define CV_CALL( Statement ) \
6845 /* Checks some condition in both debug and release configurations */
6846 #define CV_ASSERT( Condition ) \
6848 if( !(Condition) ) \
6849 CV_ERROR( CV_StsInternal, "Assertion: " #Condition " failed" ); \
6852 /* these macros are similar to their CV_... counterparts, but they
6853 do not need exit label nor cvFuncName to be defined */
6854 #define OPENCV_ERROR(status,func_name,err_msg) ...
6855 #define OPENCV_ERRCHK(func_name,err_msg) ...
6856 #define OPENCV_ASSERT(condition,func_name,err_msg) ...
6857 #define OPENCV_CALL(statement) ...
6859 Instead of a discussion, here are the documented example
6860 of typical CXCORE function and the example of the function use.
6861 <h4><a name="decl_error_handling_sample">Use of Error Handling Macros</a></h4>
6864 #include <stdio.h>
6866 void cvResizeDCT( CvMat* input_array, CvMat* output_array )
6868 CvMat* temp_array = 0; // declare pointer that should be released anyway.
6870 CV_FUNCNAME( "cvResizeDCT" ); // declare cvFuncName
6872 __BEGIN__; // start processing. There may be some declarations just after this macro,
6873 // but they couldn't be accessed from the epilogue.
6875 if( !CV_IS_MAT(input_array) || !CV_IS_MAT(output_array) )
6876 // use CV_ERROR() to raise an error
6877 CV_ERROR( CV_StsBadArg, "input_array or output_array are not valid matrices" );
6879 // some restrictions that are going to be removed later, may be checked with CV_ASSERT()
6880 CV_ASSERT( input_array->rows == 1 && output_array->rows == 1 );
6882 // use CV_CALL for safe function call
6883 CV_CALL( temp_array = cvCreateMat( input_array->rows, MAX(input_array->cols,output_array->cols),
6884 input_array->type ));
6886 if( output_array->cols > input_array->cols )
6887 CV_CALL( cvZero( temp_array ));
6889 temp_array->cols = input_array->cols;
6890 CV_CALL( cvDCT( input_array, temp_array, CV_DXT_FORWARD ));
6891 temp_array->cols = output_array->cols;
6892 CV_CALL( cvDCT( temp_array, output_array, CV_DXT_INVERSE ));
6893 CV_CALL( cvScale( output_array, output_array, 1./sqrt((double)input_array->cols*output_array->cols), 0 ));
6895 __END__; // finish processing. Epilogue follows after the macro.
6897 // release temp_array. If temp_array has not been allocated before an error occurred, cvReleaseMat
6898 // takes care of it and does nothing in this case.
6899 cvReleaseMat( &temp_array );
6903 int main( int argc, char** argv )
6905 CvMat* src = cvCreateMat( 1, 512, CV_32F );
6906 #if 1 /* no errors */
6907 CvMat* dst = cvCreateMat( 1, 256, CV_32F );
6909 CvMat* dst = 0; /* test error processing mechanism */
6911 cvSet( src, cvRealScalar(1.), 0 );
6912 #if 0 /* change 0 to 1 to suppress error handler invocation */
6913 cvSetErrMode( CV_ErrModeSilent );
6915 cvResizeDCT( src, dst ); // if some error occurs, the message box will pop up, or a message will be
6916 // written to log, or some user-defined processing will be done
6917 if( cvGetErrStatus() < 0 )
6918 printf("Some error occurred" );
6920 printf("Everything is OK" );
6926 <hr><h3><a name="decl_cvGetErrStatus">GetErrStatus</a></h3>
6927 <p class="Blurb">Returns the current error status</p>
6929 int cvGetErrStatus( void );
6931 The function <code>cvGetErrStatus</code> returns the current error status -
6932 the value set with the last <a href="#decl_cvSetErrStatus">cvSetErrStatus</a> call. Note, that
6933 in <em>Leaf</em> mode the program terminates immediately after error occurred, so to
6934 always get control after the function call, one should call <a href="#decl_cvSetErrMode">cvSetErrMode</a>
6935 and set <em>Parent</em> or <em>Silent</em> error mode.
6939 <hr><h3><a name="decl_cvSetErrStatus">SetErrStatus</a></h3>
6940 <p class="Blurb">Sets the error status</p>
6942 void cvSetErrStatus( int status );
6944 <dt>status<dd>The error status.
6946 The function <code>cvSetErrStatus</code> sets the error status to
6947 the specified value. Mostly, the function is used to reset the error status (set to it <code>CV_StsOk</code>)
6948 to recover after error. In other cases it is more natural to call <a href="#decl_cvError">cvError</a> or
6949 <a href="#decl_error_macros">CV_ERROR</a>.
6953 <hr><h3><a name="decl_cvGetErrMode">GetErrMode</a></h3>
6954 <p class="Blurb">Returns the current error mode</p>
6956 int cvGetErrMode( void );
6958 The function <code>cvGetErrMode</code> returns the current error mode -
6959 the value set with the last <a href="#decl_cvSetErrMode">cvSetErrMode</a> call.
6963 <hr><h3><a name="decl_cvSetErrMode">SetErrMode</a></h3>
6964 <p class="Blurb">Sets the error mode</p>
6966 #define CV_ErrModeLeaf 0
6967 #define CV_ErrModeParent 1
6968 #define CV_ErrModeSilent 2
6969 int cvSetErrMode( int mode );
6971 <dt>mode<dd>The error mode.
6973 The function <code>cvSetErrMode</code> sets the specified error mode.
6974 For description of different error modes see the beginning of the <a href="#cxcore_system_error">section</a>.
6978 <hr><h3><a name="decl_cvError">Error</a></h3>
6979 <p class="Blurb">Raises an error</p>
6981 int cvError( int status, const char* func_name,
6982 const char* err_msg, const char* file_name, int line );
6984 <dt>status<dd>The error status.
6985 <dt>func_name<dd>Name of the function where the error occurred.
6986 <dt>err_msg<dd>Additional information/diagnostics about the error.
6987 <dt>file_name<dd>Name of the file where the error occurred.
6988 <dt>line<dd>Line number, where the error occurred.
6990 The function <code>cvError</code> sets the error status
6991 to the specified value (via <a href="#decl_cvSetErrStatus">cvSetErrStatus</a>)
6992 and, if the error mode is not <em>Silent</em>, calls the error handler.</p>
6995 <hr><h3><a name="decl_cvErrorStr">ErrorStr</a></h3>
6996 <p class="Blurb">Returns textual description of error status code</p>
6998 const char* cvErrorStr( int status );
7000 <dt>status<dd>The error status.
7002 The function <code>cvErrorStr</code> returns the textual description
7003 for the specified error status code. In case of unknown status the function returns NULL pointer.
7007 <hr><h3><a name="decl_cvRedirectError">RedirectError</a></h3>
7008 <p class="Blurb">Sets a new error handler</p>
7010 typedef int (CV_CDECL *CvErrorCallback)( int status, const char* func_name,
7011 const char* err_msg, const char* file_name, int line );
7013 CvErrorCallback cvRedirectError( CvErrorCallback error_handler,
7014 void* userdata=NULL, void** prev_userdata=NULL );
7016 <dt>error_handler<dd>The new error_handler.
7017 <dt>userdata<dd>Arbitrary pointer that is transparently passed to the error handler.
7018 <dt>prev_userdata<dd>Pointer to the previously assigned user data pointer.
7020 The function <code>cvRedirectError</code> sets a new error handler that
7021 can be one of <a href="#decl_cvNulDevReport">standard handlers</a> or a custom handler that has
7022 the certain interface. The handler takes the same parameters as <a href="#decl_cvError">cvError</a>
7023 function. If the handler returns non-zero value, the program is terminated, otherwise, it continues.
7024 The error handler may check the current error mode with <a href="#decl_cvGetErrMode">cvGetErrMode</a>
7029 <hr><h3><a name="decl_cvNulDevReport">cvNulDevReport</a>
7030 <a name="decl_cvStdErrReport">cvStdErrReport</a>
7031 <a name="decl_cvGuiBoxReport">cvGuiBoxReport</a></h3>
7032 <p class="Blurb">Provide standard error handling</p>
7034 int cvNulDevReport( int status, const char* func_name,
7035 const char* err_msg, const char* file_name,
7036 int line, void* userdata );
7038 int cvStdErrReport( int status, const char* func_name,
7039 const char* err_msg, const char* file_name,
7040 int line, void* userdata );
7042 int cvGuiBoxReport( int status, const char* func_name,
7043 const char* err_msg, const char* file_name,
7044 int line, void* userdata );
7046 <dt>status<dd>The error status.
7047 <dt>func_name<dd>Name of the function where the error occurred.
7048 <dt>err_msg<dd>Additional information/diagnostics about the error.
7049 <dt>file_name<dd>Name of the file where the error occurred.
7050 <dt>line<dd>Line number, where the error occurred.
7051 <dt>userdata<dd>Pointer to the user data. Ignored by the standard handlers.
7053 The functions <code>cvNullDevReport, cvStdErrReport</code> and <code>cvGuiBoxReport</code>
7054 provide standard error handling. <code>cvGuiBoxReport</code> is the default
7055 error handler on Win32 systems, <code>cvStdErrReport</code> - on other systems.
7056 <code>cvGuiBoxReport</code> pops up message box with the error description and
7057 suggest a few options. Below is the sample message box that may be received with the
7058 <a name="decl_error_handling_sample">sample code</a> above, if one introduce an error as described
7060 <h4>Error Message Box</h4>
7062 <img align="center" src="pics/errmsg.png" >
7064 If the error handler is set <code>cvStdErrReport</code>, the above message will
7065 be printed to standard error output and program will be terminated or continued, depending on the
7066 current error mode.</p>
7067 <h4>Error Message printed to Standard Error Output (in <em>Leaf</em> mode)</h4>
7069 OpenCV ERROR: Bad argument (input_array or output_array are not valid matrices)
7070 in function cvResizeDCT, D:\User\VP\Projects\avl_proba\a.cpp(75)
7071 Terminating the application...
7075 <hr><h2><a name="cxcore_system_sys">System and Utility Functions</a></h2>
7077 <hr><h3><a name="decl_cvAlloc">Alloc</a></h3>
7078 <p class="Blurb">Allocates memory buffer</p>
7080 void* cvAlloc( size_t size );
7082 <dt>size<dd>Buffer size in bytes.
7084 The function <code>cvAlloc</code> allocates <code>size</code> bytes and
7085 returns pointer to the allocated buffer. In case of error
7086 the function reports an error and returns NULL pointer.
7087 By default cvAlloc calls icvAlloc which itself calls malloc,
7088 however it is possible to assign user-defined memory allocation/deallocation
7089 functions using <a href="#decl_cvSetMemoryManager">cvSetMemoryManager</a> function.
7093 <hr><h3><a name="decl_cvFree">Free</a></h3>
7094 <p class="Blurb">Deallocates memory buffer</p>
7096 void cvFree( T** ptr );
7098 <dt>buffer<dd>Double pointer to released buffer.
7100 The function <code>cvFree</code> deallocates memory buffer allocated by <a href="#decl_cvAlloc">cvAlloc</a>.
7101 It clears the pointer to buffer upon exit, that is why the double pointer is
7102 used. If *buffer is already NULL, the function does nothing
7106 <hr><h3><a name="decl_cvGetTickCount">GetTickCount</a></h3>
7107 <p class="Blurb">Returns number of tics</p>
7109 int64 cvGetTickCount( void );
7111 The function <code>cvGetTickCount</code> returns number of tics
7112 starting from some platform-dependent event (number of CPU ticks from the startup, number of milliseconds
7113 from 1970th year etc.). The function is useful for accurate measurement of
7114 a function/user-code execution time. To convert the number of tics to time units, use
7115 <a href="#decl_cvGetTickFrequency">cvGetTickFrequency</a>.</p>
7117 <hr><h3><a name="decl_cvGetTickFrequency">GetTickFrequency</a></h3>
7118 <p class="Blurb">Returns number of tics per microsecond</p>
7120 double cvGetTickFrequency( void );
7122 The function <code>cvGetTickFrequency</code> returns number of tics
7123 per microsecond. Thus, the quotient of <a href="#decl_cvGetTickCount">cvGetTickCount</a>() and
7124 <a href="#decl_cvGetTickFrequency">cvGetTickFrequency</a>() will give a number of microseconds
7125 starting from the platform-dependent event.</p>
7128 <hr><h3><a name="decl_cvRegisterModule">RegisterModule</a></h3>
7129 <p class="Blurb">Registers another module</p>
7131 typedef struct CvPluginFuncInfo
7134 void* default_func_addr;
7135 const char* func_names;
7141 typedef struct CvModuleInfo
7143 struct CvModuleInfo* next;
7145 const char* version;
7146 CvPluginFuncInfo* func_tab;
7150 int cvRegisterModule( const CvModuleInfo* module_info );
7152 <dt>module_info<dd>Information about the module.
7154 The function <code>cvRegisterModule</code> adds module to the list of registered
7155 modules. After the module is registered, information about it can be retrieved
7156 using <a href="#decl_cvGetModuleInfo">cvGetModuleInfo</a> function. Also, the registered module
7157 makes full use of optimized plugins (IPP, MKL, ...), supported by CXCORE.
7158 CXCORE itself, CV (computer vision), CVAUX (auxiliary computer vision) and HIGHGUI
7159 (visualization & image/video acquisition) are examples of modules. Registration is usually done
7160 then the shared library is loaded. See cxcore/src/cxswitcher.cpp and cv/src/cvswitcher.cpp
7161 for details, how registration is done and
7162 look at cxcore/src/cxswitcher.cpp, cxcore/src/_cxipp.h on how IPP and MKL are connected to the modules.</p>
7165 <hr><h3><a name="decl_cvGetModuleInfo">GetModuleInfo</a></h3>
7166 <p class="Blurb">Retrieves information about the registered module(s) and plugins</p>
7168 void cvGetModuleInfo( const char* module_name,
7169 const char** version,
7170 const char** loaded_addon_plugins );
7172 <dt>module_name<dd>Name of the module of interest, or NULL, which means all the modules.
7173 <dt>version<dd>The output parameter. Information about the module(s), including version.
7174 <dt>loaded_addon_plugins<dd>The list of names and versions of the optimized plugins that CXCORE was
7175 able to find and load.
7177 The function <code>cvGetModuleInfo</code> returns information about one of
7178 or all of the registered modules. The returned information is stored inside the libraries, so
7179 user should not deallocate or modify the returned text strings.</p>
7182 <hr><h3><a name="decl_cvUseOptimized">UseOptimized</a></h3>
7183 <p class="Blurb">Switches between optimized/non-optimized modes</p>
7185 int cvUseOptimized( int on_off );
7187 <dt>on_off<dd>Use optimized (<>0) or not (0).
7189 The function <code>cvUseOptimized</code> switches between the mode, where
7190 only pure C implementations from cxcore, OpenCV etc. are used, and the mode, where
7191 IPP and MKL functions are used if available. When <code>cvUseOptimized(0)</code> is called,
7192 all the optimized libraries are unloaded. The function may be useful for debugging, IPP&MKL upgrade
7193 on the fly, online speed comparisons etc. It returns the number of optimized functions loaded.
7194 Note that by default the optimized plugins are loaded, so it is not necessary to
7195 call <code>cvUseOptimized(1)</code> in the beginning of the program (actually, it will only
7196 increase the startup time)</p>
7199 <hr><h3><a name="decl_cvSetMemoryManager">SetMemoryManager</a></h3>
7200 <p class="Blurb">Assigns custom/default memory managing functions</p>
7202 typedef void* (CV_CDECL *CvAllocFunc)(size_t size, void* userdata);
7203 typedef int (CV_CDECL *CvFreeFunc)(void* pptr, void* userdata);
7205 void cvSetMemoryManager( CvAllocFunc alloc_func=NULL,
7206 CvFreeFunc free_func=NULL,
7207 void* userdata=NULL );
7209 <dt>alloc_func<dd>Allocation function; the interface is similar to <code>malloc</code>, except that <code>userdata</code>
7210 may be used to determine the context.
7211 <dt>free_func<dd>Deallocation function; the interface is similar to <code>free</code>.
7212 <dt>userdata<dd>User data that is transparently passed to the custom functions.
7214 The function <code>cvSetMemoryManager</code>
7215 sets user-defined memory management functions (replacements for malloc and free) that
7216 will be called by cvAlloc, cvFree and higher-level functions (e.g. cvCreateImage).
7217 Note, that the function should be called when there is data allocated using <a href="#decl_cvAlloc">cvAlloc<a>.
7218 Also, to avoid infinite recursive calls, it is not allowed to call <a href="#decl_cvAlloc">cvAlloc</a>
7219 and <a href="#decl_cvFree">cvFree</a> from the custom allocation/deallocation functions.</p>
7221 If <code>alloc_func</code> and <code>free_func</code> pointers are <code>NULL</code>,
7222 the default memory managing functions are restored.</p>
7225 <hr><h3><a name="decl_cvSetIPLAllocators">SetIPLAllocators</a></h3>
7226 <p class="Blurb">Switches to IPL functions for image allocation/deallocation</p>
7228 typedef IplImage* (CV_STDCALL* Cv_iplCreateImageHeader)
7229 (int,int,int,char*,char*,int,int,int,int,int,
7230 IplROI*,IplImage*,void*,IplTileInfo*);
7231 typedef void (CV_STDCALL* Cv_iplAllocateImageData)(IplImage*,int,int);
7232 typedef void (CV_STDCALL* Cv_iplDeallocate)(IplImage*,int);
7233 typedef IplROI* (CV_STDCALL* Cv_iplCreateROI)(int,int,int,int,int);
7234 typedef IplImage* (CV_STDCALL* Cv_iplCloneImage)(const IplImage*);
7236 void cvSetIPLAllocators( Cv_iplCreateImageHeader create_header,
7237 Cv_iplAllocateImageData allocate_data,
7238 Cv_iplDeallocate deallocate,
7239 Cv_iplCreateROI create_roi,
7240 Cv_iplCloneImage clone_image );
7242 #define CV_TURN_ON_IPL_COMPATIBILITY() \
7243 cvSetIPLAllocators( iplCreateImageHeader, iplAllocateImage, \
7244 iplDeallocate, iplCreateROI, iplCloneImage )
7246 <dt>create_header<dd>Pointer to iplCreateImageHeader.
7247 <dt>allocate_data<dd>Pointer to iplAllocateImage.
7248 <dt>deallocate<dd>Pointer to iplDeallocate.
7249 <dt>create_roi<dd>Pointer to iplCreateROI.
7250 <dt>clone_image<dd>Pointer to iplCloneImage.
7252 The function <code>cvSetIPLAllocators</code>
7253 makes CXCORE to use IPL functions for image allocation/deallocation operations.
7254 For convenience, there is the wrapping macro <code>CV_TURN_ON_IPL_COMPATIBILITY</code>.
7255 The function is useful for applications where IPL and CXCORE/OpenCV are used together and still
7256 there are calls to <code>iplCreateImageHeader</code> etc. The function is not necessary if
7257 IPL is called only for data processing and all the allocation/deallocation is done by CXCORE,
7258 or if all the allocation/deallocation is done by IPL and some of OpenCV functions are used to
7259 process the data.</p>
7262 <hr><h3><a name="decl_cvGetNumThreads">GetNumThreads</a></h3>
7263 <p class="Blurb">Returns the current number of threads used</p>
7265 int cvGetNumThreads(void);
7268 The function <code>cvGetNumThreads</code> return the current number of threads
7269 that are used by parallelized (via OpenMP) OpenCV functions.</p>
7272 <hr><h3><a name="decl_cvSetNumThreads">SetNumThreads</a></h3>
7273 <p class="Blurb">Sets the number of threads</p>
7275 void cvSetNumThreads( int threads=0 );
7277 <dt>threads<dd>The number of threads.
7279 The function <code>cvSetNumThreads</code> sets the number of threads
7280 that are used by parallelized OpenCV functions. When the argument
7281 is zero or negative, and at the beginning of the program,
7282 the number of threads is set to the number of processors in the system,
7283 as returned by the function <code>omp_get_num_procs()</code> from OpenMP runtime.
7287 <hr><h3><a name="decl_cvGetThreadNum">GetThreadNum</a></h3>
7288 <p class="Blurb">Returns index of the current thread</p>
7290 int cvGetThreadNum( void );
7292 The function <code>cvGetThreadNum</code> returns the index, from 0 to
7293 <a href="#decl_cvGetNumThreads">cvGetNumThreads</a>()-1, of the thread that called the function.
7294 It is a wrapper for the function <code>omp_get_thread_num()</code> from OpenMP runtime.
7295 The retrieved index may be used to access local-thread data inside the parallelized code fragments.
7299 <hr><h1><a name="cxcore_func_index">Alphabetical List of Functions</a></h1>
7302 <table width="100%">
7304 <td width="25%"><a href="#decl_cvAbsDiff">AbsDiff</a></td>
7305 <td width="25%"><a href="#decl_cvAddWeighted">AddWeighted</a></td>
7306 <td width="25%"><a href="#decl_cvAvg">Avg</a></td>
7309 <td width="25%"><a href="#decl_cvAbsDiffS">AbsDiffS</a></td>
7310 <td width="25%"><a href="#decl_cvAlloc">Alloc</a></td>
7311 <td width="25%"><a href="#decl_cvAvgSdv">AvgSdv</a></td>
7314 <td width="25%"><a href="#decl_cvAdd">Add</a></td>
7315 <td width="25%"><a href="#decl_cvAnd">And</a></td>
7316 <td width="25%%"></td>
7319 <td width="25%"><a href="#decl_cvAddS">AddS</a></td>
7320 <td width="25%"><a href="#decl_cvAndS">AndS</a></td>
7321 <td width="25%%"></td>
7325 <table width="100%">
7327 <td width="25%"><a href="#decl_cvBackProjectPCA">BackProjectPCA</a></td>
7328 <td width="25%%"></td>
7329 <td width="25%%"></td>
7333 <table width="100%">
7335 <td width="25%"><a href="#decl_cvCalcCovarMatrix">CalcCovarMatrix</a></td>
7336 <td width="25%"><a href="#decl_cvCloneGraph">CloneGraph</a></td>
7337 <td width="25%"><a href="#decl_cvCreateGraph">CreateGraph</a></td>
7340 <td width="25%"><a href="#decl_cvCalcPCA">CalcPCA</a></td>
7341 <td width="25%"><a href="#decl_cvCloneImage">CloneImage</a></td>
7342 <td width="25%"><a href="#decl_cvCreateGraphScanner">CreateGraphScanner</a></td>
7345 <td width="25%"><a href="#decl_cvCartToPolar">CartToPolar</a></td>
7346 <td width="25%"><a href="#decl_cvCloneMat">CloneMat</a></td>
7347 <td width="25%"><a href="#decl_cvCreateImage">CreateImage</a></td>
7350 <td width="25%"><a href="#decl_cvCbrt">Cbrt</a></td>
7351 <td width="25%"><a href="#decl_cvCloneMatND">CloneMatND</a></td>
7352 <td width="25%"><a href="#decl_cvCreateImageHeader">CreateImageHeader</a></td>
7355 <td width="25%"><a href="#decl_cvCheckArr">CheckArr</a></td>
7356 <td width="25%"><a href="#decl_cvCloneSeq">CloneSeq</a></td>
7357 <td width="25%"><a href="#decl_cvCreateMat">CreateMat</a></td>
7360 <td width="25%"><a href="#decl_cvCircle">Circle</a></td>
7361 <td width="25%"><a href="#decl_cvCloneSparseMat">CloneSparseMat</a></td>
7362 <td width="25%"><a href="#decl_cvCreateMatHeader">CreateMatHeader</a></td>
7365 <td width="25%"><a href="#decl_cvClearGraph">ClearGraph</a></td>
7366 <td width="25%"><a href="#decl_cvCmp">Cmp</a></td>
7367 <td width="25%"><a href="#decl_cvCreateMatND">CreateMatND</a></td>
7370 <td width="25%"><a href="#decl_cvClearMemStorage">ClearMemStorage</a></td>
7371 <td width="25%"><a href="#decl_cvCmpS">CmpS</a></td>
7372 <td width="25%"><a href="#decl_cvCreateMatNDHeader">CreateMatNDHeader</a></td>
7375 <td width="25%"><a href="#decl_cvClearND">ClearND</a></td>
7376 <td width="25%"><a href="#decl_cvConvertScale">ConvertScale</a></td>
7377 <td width="25%"><a href="#decl_cvCreateMemStorage">CreateMemStorage</a></td>
7380 <td width="25%"><a href="#decl_cvClearSeq">ClearSeq</a></td>
7381 <td width="25%"><a href="#decl_cvConvertScaleAbs">ConvertScaleAbs</a></td>
7382 <td width="25%"><a href="#decl_cvCreateSeq">CreateSeq</a></td>
7385 <td width="25%"><a href="#decl_cvClearSet">ClearSet</a></td>
7386 <td width="25%"><a href="#decl_cvCopy">Copy</a></td>
7387 <td width="25%"><a href="#decl_cvCreateSet">CreateSet</a></td>
7390 <td width="25%"><a href="#decl_cvClipLine">ClipLine</a></td>
7391 <td width="25%"><a href="#decl_cvCountNonZero">CountNonZero</a></td>
7392 <td width="25%"><a href="#decl_cvCreateSparseMat">CreateSparseMat</a></td>
7395 <td width="25%"><a href="#decl_cvClipLine">ClipLine</a></td>
7396 <td width="25%"><a href="#decl_cvCreateChildMemStorage">CreateChildMemStorage</a></td>
7397 <td width="25%"><a href="#decl_cvCrossProduct">CrossProduct</a></td>
7400 <td width="25%"><a href="#decl_cvClone">Clone</a></td>
7401 <td width="25%"><a href="#decl_cvCreateData">CreateData</a></td>
7402 <td width="25%"><a href="#decl_cvCvtSeqToArray">CvtSeqToArray</a></td>
7406 <table width="100%">
7408 <td width="25%"><a href="#decl_cvDCT">DCT</a></td>
7409 <td width="25%"><a href="#decl_cvDet">Det</a></td>
7410 <td width="25%"><a href="#decl_cvDrawContours">DrawContours</a></td>
7413 <td width="25%"><a href="#decl_cvDFT">DFT</a></td>
7414 <td width="25%"><a href="#decl_cvDiv">Div</a></td>
7415 <td width="25%%"></td>
7418 <td width="25%"><a href="#decl_cvDecRefData">DecRefData</a></td>
7419 <td width="25%"><a href="#decl_cvDotProduct">DotProduct</a></td>
7420 <td width="25%%"></td>
7424 <table width="100%">
7426 <td width="25%"><a href="#decl_cvEigenVV">EigenVV</a></td>
7427 <td width="25%"><a href="#decl_cvEllipseBox">EllipseBox</a></td>
7428 <td width="25%"><a href="#decl_cvError">Error</a></td>
7431 <td width="25%"><a href="#decl_cvEllipse">Ellipse</a></td>
7432 <td width="25%"><a href="#decl_cvEndWriteSeq">EndWriteSeq</a></td>
7433 <td width="25%"><a href="#decl_cvErrorStr">ErrorStr</a></td>
7436 <td width="25%"><a href="#decl_cvEllipse2Poly">Ellipse2Poly</a></td>
7437 <td width="25%"><a href="#decl_cvEndWriteStruct">EndWriteStruct</a></td>
7438 <td width="25%"><a href="#decl_cvExp">Exp</a></td>
7442 <table width="100%">
7444 <td width="25%"><a href="#decl_cvFastArctan">FastArctan</a></td>
7445 <td width="25%"><a href="#decl_cvFindGraphEdgeByPtr">FindGraphEdgeByPtr</a></td>
7446 <td width="25%"><a href="#decl_cvFlushSeqWriter">FlushSeqWriter</a></td>
7449 <td width="25%"><a href="#decl_cvFillConvexPoly">FillConvexPoly</a></td>
7450 <td width="25%"><a href="#decl_cvFindType">FindType</a></td>
7451 <td width="25%"><a href="#decl_cvFree">Free</a></td>
7454 <td width="25%"><a href="#decl_cvFillPoly">FillPoly</a></td>
7455 <td width="25%"><a href="#decl_cvFirstType">FirstType</a></td>
7456 <td width="25%%"></td>
7459 <td width="25%"><a href="#decl_cvFindGraphEdge">FindGraphEdge</a></td>
7460 <td width="25%"><a href="#decl_cvFlip">Flip</a></td>
7461 <td width="25%%"></td>
7465 <table width="100%">
7467 <td width="25%"><a href="#decl_cvGEMM">GEMM</a></td>
7468 <td width="25%"><a href="#decl_cvGetMat">GetMat</a></td>
7469 <td width="25%"><a href="#decl_cvGetTickCount">GetTickCount</a></td>
7472 <td width="25%"><a href="#decl_cvGet*D">Get*D</a></td>
7473 <td width="25%"><a href="#decl_cvGetModuleInfo">GetModuleInfo</a></td>
7474 <td width="25%"><a href="#decl_cvGetTickFrequency">GetTickFrequency</a></td>
7477 <td width="25%"><a href="#decl_cvGetCol">GetCol</a></td>
7478 <td width="25%"><a href="#decl_cvGetNextSparseNode">GetNextSparseNode</a></td>
7479 <td width="25%"><a href="#decl_cvGraphAddEdge">GraphAddEdge</a></td>
7482 <td width="25%"><a href="#decl_cvGetDiag">GetDiag</a></td>
7483 <td width="25%"><a href="#decl_cvGetNumThreads">GetNumThreads</a></td>
7484 <td width="25%"><a href="#decl_cvGraphAddEdgeByPtr">GraphAddEdgeByPtr</a></td>
7487 <td width="25%"><a href="#decl_cvGetDims">GetDims</a></td>
7488 <td width="25%"><a href="#decl_cvGetOptimalDFTSize">GetOptimalDFTSize</a></td>
7489 <td width="25%"><a href="#decl_cvGraphAddVtx">GraphAddVtx</a></td>
7492 <td width="25%"><a href="#decl_cvGetElemType">GetElemType</a></td>
7493 <td width="25%"><a href="#decl_cvGetRawData">GetRawData</a></td>
7494 <td width="25%"><a href="#decl_cvGraphEdgeIdx">GraphEdgeIdx</a></td>
7497 <td width="25%"><a href="#decl_cvGetErrMode">GetErrMode</a></td>
7498 <td width="25%"><a href="#decl_cvGetReal*D">GetReal*D</a></td>
7499 <td width="25%"><a href="#decl_cvGraphRemoveEdge">GraphRemoveEdge</a></td>
7502 <td width="25%"><a href="#decl_cvGetErrStatus">GetErrStatus</a></td>
7503 <td width="25%"><a href="#decl_cvGetRootFileNode">GetRootFileNode</a></td>
7504 <td width="25%"><a href="#decl_cvGraphRemoveEdgeByPtr">GraphRemoveEdgeByPtr</a></td>
7507 <td width="25%"><a href="#decl_cvGetFileNode">GetFileNode</a></td>
7508 <td width="25%"><a href="#decl_cvGetRow">GetRow</a></td>
7509 <td width="25%"><a href="#decl_cvGraphRemoveVtx">GraphRemoveVtx</a></td>
7512 <td width="25%"><a href="#decl_cvGetFileNodeByName">GetFileNodeByName</a></td>
7513 <td width="25%"><a href="#decl_cvGetSeqElem">GetSeqElem</a></td>
7514 <td width="25%"><a href="#decl_cvGraphRemoveVtxByPtr">GraphRemoveVtxByPtr</a></td>
7517 <td width="25%"><a href="#decl_cvGetFileNodeName">GetFileNodeName</a></td>
7518 <td width="25%"><a href="#decl_cvGetSeqReaderPos">GetSeqReaderPos</a></td>
7519 <td width="25%"><a href="#decl_cvGraphVtxDegree">GraphVtxDegree</a></td>
7522 <td width="25%"><a href="#decl_cvGetGraphVtx">GetGraphVtx</a></td>
7523 <td width="25%"><a href="#decl_cvGetSetElem">GetSetElem</a></td>
7524 <td width="25%"><a href="#decl_cvGraphVtxDegreeByPtr">GraphVtxDegreeByPtr</a></td>
7527 <td width="25%"><a href="#decl_cvGetHashedKey">GetHashedKey</a></td>
7528 <td width="25%"><a href="#decl_cvGetSize">GetSize</a></td>
7529 <td width="25%"><a href="#decl_cvGraphVtxIdx">GraphVtxIdx</a></td>
7532 <td width="25%"><a href="#decl_cvGetImage">GetImage</a></td>
7533 <td width="25%"><a href="#decl_cvGetSubRect">GetSubRect</a></td>
7534 <td width="25%"><a href="#decl_cvGuiBoxReport">GuiBoxReport</a></td>
7537 <td width="25%"><a href="#decl_cvGetImageCOI">GetImageCOI</a></td>
7538 <td width="25%"><a href="#decl_cvGetTextSize">GetTextSize</a></td>
7539 <td width="25%"><a href="#decl_cvmGet">Get</a></td>
7542 <td width="25%"><a href="#decl_cvGetImageROI">GetImageROI</a></td>
7543 <td width="25%"><a href="#decl_cvGetThreadNum">GetThreadNum</a></td>
7544 <td width="25%%"></td>
7548 <table width="100%">
7550 <td width="25%"><a href="#decl_cvInRange">InRange</a></td>
7551 <td width="25%"><a href="#decl_cvInitLineIterator">InitLineIterator</a></td>
7552 <td width="25%"><a href="#decl_cvInsertNodeIntoTree">InsertNodeIntoTree</a></td>
7555 <td width="25%"><a href="#decl_cvInRangeS">InRangeS</a></td>
7556 <td width="25%"><a href="#decl_cvInitMatHeader">InitMatHeader</a></td>
7557 <td width="25%"><a href="#decl_cvInvSqrt">InvSqrt</a></td>
7560 <td width="25%"><a href="#decl_cvIncRefData">IncRefData</a></td>
7561 <td width="25%"><a href="#decl_cvInitMatNDHeader">InitMatNDHeader</a></td>
7562 <td width="25%"><a href="#decl_cvInvert">Invert</a></td>
7565 <td width="25%"><a href="#decl_cvInitFont">InitFont</a></td>
7566 <td width="25%"><a href="#decl_cvInitSparseMatIterator">InitSparseMatIterator</a></td>
7567 <td width="25%"><a href="#decl_cvIsInf">IsInf</a></td>
7570 <td width="25%"><a href="#decl_cvInitImageHeader">InitImageHeader</a></td>
7571 <td width="25%"><a href="#decl_cvInitTreeNodeIterator">InitTreeNodeIterator</a></td>
7572 <td width="25%"><a href="#decl_cvIsNaN">IsNaN</a></td>
7576 <table width="100%">
7578 <td width="25%"><a href="#decl_cvKMeans2">KMeans2</a></td>
7579 <td width="25%%"></td>
7580 <td width="25%%"></td>
7584 <table width="100%">
7586 <td width="25%"><a href="#decl_cvLUT">LUT</a></td>
7587 <td width="25%"><a href="#decl_cvLoad">Load</a></td>
7588 <td width="25%%"></td>
7591 <td width="25%"><a href="#decl_cvLine">Line</a></td>
7592 <td width="25%"><a href="#decl_cvLog">Log</a></td>
7593 <td width="25%%"></td>
7597 <table width="100%">
7599 <td width="25%"><a href="#decl_cvMahalonobis">Mahalonobis</a></td>
7600 <td width="25%"><a href="#decl_cvMemStorageAlloc">MemStorageAlloc</a></td>
7601 <td width="25%"><a href="#decl_cvMinS">MinS</a></td>
7604 <td width="25%"><a href="#decl_cvMakeSeqHeaderForArray">MakeSeqHeaderForArray</a></td>
7605 <td width="25%"><a href="#decl_cvMemStorageAllocString">MemStorageAllocString</a></td>
7606 <td width="25%"><a href="#decl_cvMixChannels">MixChannels</a></td>
7609 <td width="25%"><a href="#decl_cvMat">Mat</a></td>
7610 <td width="25%"><a href="#decl_cvMerge">Merge</a></td>
7611 <td width="25%"><a href="#decl_cvMul">Mul</a></td>
7614 <td width="25%"><a href="#decl_cvMax">Max</a></td>
7615 <td width="25%"><a href="#decl_cvMin">Min</a></td>
7616 <td width="25%"><a href="#decl_cvMulSpectrums">MulSpectrums</a></td>
7619 <td width="25%"><a href="#decl_cvMaxS">MaxS</a></td>
7620 <td width="25%"><a href="#decl_cvMinMaxLoc">MinMaxLoc</a></td>
7621 <td width="25%"><a href="#decl_cvMulTransposed">MulTransposed</a></td>
7625 <table width="100%">
7627 <td width="25%"><a href="#decl_cvNextGraphItem">NextGraphItem</a></td>
7628 <td width="25%"><a href="#decl_cvNorm">Norm</a></td>
7629 <td width="25%"><a href="#decl_cvNot">Not</a></td>
7632 <td width="25%"><a href="#decl_cvNextTreeNode">NextTreeNode</a></td>
7633 <td width="25%"><a href="#decl_cvNormalize">Normalize</a></td>
7634 <td width="25%"><a href="#decl_cvNulDevReport">NulDevReport</a></td>
7638 <table width="100%">
7640 <td width="25%"><a href="#decl_cvOpenFileStorage">OpenFileStorage</a></td>
7641 <td width="25%"><a href="#decl_cvOr">Or</a></td>
7642 <td width="25%"><a href="#decl_cvOrS">OrS</a></td>
7646 <table width="100%">
7648 <td width="25%"><a href="#decl_cvPerspectiveTransform">PerspectiveTransform</a></td>
7649 <td width="25%"><a href="#decl_cvPow">Pow</a></td>
7650 <td width="25%"><a href="#decl_cvPtr*D">Ptr*D</a></td>
7653 <td width="25%"><a href="#decl_cvPolarToCart">PolarToCart</a></td>
7654 <td width="25%"><a href="#decl_cvPrevTreeNode">PrevTreeNode</a></td>
7655 <td width="25%"><a href="#decl_cvPutText">PutText</a></td>
7658 <td width="25%"><a href="#decl_cvPolyLine">PolyLine</a></td>
7659 <td width="25%"><a href="#decl_cvProjectPCA">ProjectPCA</a></td>
7660 <td width="25%%"></td>
7664 <table width="100%">
7666 <td width="25%"><a href="#decl_cvRNG">RNG</a></td>
7667 <td width="25%"><a href="#decl_cvReadRealByName">ReadRealByName</a></td>
7668 <td width="25%"><a href="#decl_cvReleaseImageHeader">ReleaseImageHeader</a></td>
7671 <td width="25%"><a href="#decl_cvRandArr">RandArr</a></td>
7672 <td width="25%"><a href="#decl_cvReadString">ReadString</a></td>
7673 <td width="25%"><a href="#decl_cvReleaseMat">ReleaseMat</a></td>
7676 <td width="25%"><a href="#decl_cvRandInt">RandInt</a></td>
7677 <td width="25%"><a href="#decl_cvReadStringByName">ReadStringByName</a></td>
7678 <td width="25%"><a href="#decl_cvReleaseMatND">ReleaseMatND</a></td>
7681 <td width="25%"><a href="#decl_cvRandReal">RandReal</a></td>
7682 <td width="25%"><a href="#decl_cvRectangle">Rectangle</a></td>
7683 <td width="25%"><a href="#decl_cvReleaseMemStorage">ReleaseMemStorage</a></td>
7686 <td width="25%"><a href="#decl_cvRandShuffle">RandShuffle</a></td>
7687 <td width="25%"><a href="#decl_cvRedirectError">RedirectError</a></td>
7688 <td width="25%"><a href="#decl_cvReleaseSparseMat">ReleaseSparseMat</a></td>
7691 <td width="25%"><a href="#decl_cvRange">Range</a></td>
7692 <td width="25%"><a href="#decl_cvReduce">Reduce</a></td>
7693 <td width="25%"><a href="#decl_cvRemoveNodeFromTree">RemoveNodeFromTree</a></td>
7696 <td width="25%"><a href="#decl_cvRead">Read</a></td>
7697 <td width="25%"><a href="#decl_cvRegisterModule">RegisterModule</a></td>
7698 <td width="25%"><a href="#decl_cvRepeat">Repeat</a></td>
7701 <td width="25%"><a href="#decl_cvReadByName">ReadByName</a></td>
7702 <td width="25%"><a href="#decl_cvRegisterType">RegisterType</a></td>
7703 <td width="25%"><a href="#decl_cvResetImageROI">ResetImageROI</a></td>
7706 <td width="25%"><a href="#decl_cvReadInt">ReadInt</a></td>
7707 <td width="25%"><a href="#decl_cvRelease">Release</a></td>
7708 <td width="25%"><a href="#decl_cvReshape">Reshape</a></td>
7711 <td width="25%"><a href="#decl_cvReadIntByName">ReadIntByName</a></td>
7712 <td width="25%"><a href="#decl_cvReleaseData">ReleaseData</a></td>
7713 <td width="25%"><a href="#decl_cvReshapeMatND">ReshapeMatND</a></td>
7716 <td width="25%"><a href="#decl_cvReadRawData">ReadRawData</a></td>
7717 <td width="25%"><a href="#decl_cvReleaseFileStorage">ReleaseFileStorage</a></td>
7718 <td width="25%"><a href="#decl_cvRestoreMemStoragePos">RestoreMemStoragePos</a></td>
7721 <td width="25%"><a href="#decl_cvReadRawDataSlice">ReadRawDataSlice</a></td>
7722 <td width="25%"><a href="#decl_cvReleaseGraphScanner">ReleaseGraphScanner</a></td>
7723 <td width="25%"><a href="#decl_cvRound">Round</a></td>
7726 <td width="25%"><a href="#decl_cvReadReal">ReadReal</a></td>
7727 <td width="25%"><a href="#decl_cvReleaseImage">ReleaseImage</a></td>
7728 <td width="25%%"></td>
7732 <table width="100%">
7734 <td width="25%"><a href="#decl_cvSVBkSb">SVBkSb</a></td>
7735 <td width="25%"><a href="#decl_cvSeqSlice">SeqSlice</a></td>
7736 <td width="25%"><a href="#decl_cvSetSeqReaderPos">SetSeqReaderPos</a></td>
7739 <td width="25%"><a href="#decl_cvSVD">SVD</a></td>
7740 <td width="25%"><a href="#decl_cvSeqSort">SeqSort</a></td>
7741 <td width="25%"><a href="#decl_cvSetZero">SetZero</a></td>
7744 <td width="25%"><a href="#decl_cvSave">Save</a></td>
7745 <td width="25%"><a href="#decl_cvSet">Set</a></td>
7746 <td width="25%"><a href="#decl_cvSolve">Solve</a></td>
7749 <td width="25%"><a href="#decl_cvSaveMemStoragePos">SaveMemStoragePos</a></td>
7750 <td width="25%"><a href="#decl_cvSet*D">Set*D</a></td>
7751 <td width="25%"><a href="#decl_cvSolveCubic">SolveCubic</a></td>
7754 <td width="25%"><a href="#decl_cvScaleAdd">ScaleAdd</a></td>
7755 <td width="25%"><a href="#decl_cvSetAdd">SetAdd</a></td>
7756 <td width="25%"><a href="#decl_cvSplit">Split</a></td>
7759 <td width="25%"><a href="#decl_cvSeqElemIdx">SeqElemIdx</a></td>
7760 <td width="25%"><a href="#decl_cvSetData">SetData</a></td>
7761 <td width="25%"><a href="#decl_cvSqrt">Sqrt</a></td>
7764 <td width="25%"><a href="#decl_cvSeqInsert">SeqInsert</a></td>
7765 <td width="25%"><a href="#decl_cvSetErrMode">SetErrMode</a></td>
7766 <td width="25%"><a href="#decl_cvStartAppendToSeq">StartAppendToSeq</a></td>
7769 <td width="25%"><a href="#decl_cvSeqInsertSlice">SeqInsertSlice</a></td>
7770 <td width="25%"><a href="#decl_cvSetErrStatus">SetErrStatus</a></td>
7771 <td width="25%"><a href="#decl_cvStartNextStream">StartNextStream</a></td>
7774 <td width="25%"><a href="#decl_cvSeqInvert">SeqInvert</a></td>
7775 <td width="25%"><a href="#decl_cvSetIPLAllocators">SetIPLAllocators</a></td>
7776 <td width="25%"><a href="#decl_cvStartReadRawData">StartReadRawData</a></td>
7779 <td width="25%"><a href="#decl_cvSeqPartition">SeqPartition</a></td>
7780 <td width="25%"><a href="#decl_cvSetIdentity">SetIdentity</a></td>
7781 <td width="25%"><a href="#decl_cvStartReadSeq">StartReadSeq</a></td>
7784 <td width="25%"><a href="#decl_cvSeqPop">SeqPop</a></td>
7785 <td width="25%"><a href="#decl_cvSetImageCOI">SetImageCOI</a></td>
7786 <td width="25%"><a href="#decl_cvStartWriteSeq">StartWriteSeq</a></td>
7789 <td width="25%"><a href="#decl_cvSeqPopFront">SeqPopFront</a></td>
7790 <td width="25%"><a href="#decl_cvSetImageROI">SetImageROI</a></td>
7791 <td width="25%"><a href="#decl_cvStartWriteStruct">StartWriteStruct</a></td>
7794 <td width="25%"><a href="#decl_cvSeqPopMulti">SeqPopMulti</a></td>
7795 <td width="25%"><a href="#decl_cvSetMemoryManager">SetMemoryManager</a></td>
7796 <td width="25%"><a href="#decl_cvStdErrReport">StdErrReport</a></td>
7799 <td width="25%"><a href="#decl_cvSeqPush">SeqPush</a></td>
7800 <td width="25%"><a href="#decl_cvSetNew">SetNew</a></td>
7801 <td width="25%"><a href="#decl_cvSub">Sub</a></td>
7804 <td width="25%"><a href="#decl_cvSeqPushFront">SeqPushFront</a></td>
7805 <td width="25%"><a href="#decl_cvSetNumThreads">SetNumThreads</a></td>
7806 <td width="25%"><a href="#decl_cvSubRS">SubRS</a></td>
7809 <td width="25%"><a href="#decl_cvSeqPushMulti">SeqPushMulti</a></td>
7810 <td width="25%"><a href="#decl_cvSetReal*D">SetReal*D</a></td>
7811 <td width="25%"><a href="#decl_cvSubS">SubS</a></td>
7814 <td width="25%"><a href="#decl_cvSeqRemove">SeqRemove</a></td>
7815 <td width="25%"><a href="#decl_cvSetRemove">SetRemove</a></td>
7816 <td width="25%"><a href="#decl_cvSum">Sum</a></td>
7819 <td width="25%"><a href="#decl_cvSeqRemoveSlice">SeqRemoveSlice</a></td>
7820 <td width="25%"><a href="#decl_cvSetRemoveByPtr">SetRemoveByPtr</a></td>
7821 <td width="25%"><a href="#decl_cvmSet">Set</a></td>
7824 <td width="25%"><a href="#decl_cvSeqSearch">SeqSearch</a></td>
7825 <td width="25%"><a href="#decl_cvSetSeqBlockSize">SetSeqBlockSize</a></td>
7826 <td width="25%%"></td>
7830 <table width="100%">
7832 <td width="25%"><a href="#decl_cvTrace">Trace</a></td>
7833 <td width="25%"><a href="#decl_cvTranspose">Transpose</a></td>
7834 <td width="25%"><a href="#decl_cvTypeOf">TypeOf</a></td>
7837 <td width="25%"><a href="#decl_cvTransform">Transform</a></td>
7838 <td width="25%"><a href="#decl_cvTreeToNodeSeq">TreeToNodeSeq</a></td>
7839 <td width="25%%"></td>
7843 <table width="100%">
7845 <td width="25%"><a href="#decl_cvUnregisterType">UnregisterType</a></td>
7846 <td width="25%"><a href="#decl_cvUseOptimized">UseOptimized</a></td>
7847 <td width="25%%"></td>
7851 <table width="100%">
7853 <td width="25%"><a href="#decl_cvWrite">Write</a></td>
7854 <td width="25%"><a href="#decl_cvWriteInt">WriteInt</a></td>
7855 <td width="25%"><a href="#decl_cvWriteString">WriteString</a></td>
7858 <td width="25%"><a href="#decl_cvWriteComment">WriteComment</a></td>
7859 <td width="25%"><a href="#decl_cvWriteRawData">WriteRawData</a></td>
7860 <td width="25%%"></td>
7863 <td width="25%"><a href="#decl_cvWriteFileNode">WriteFileNode</a></td>
7864 <td width="25%"><a href="#decl_cvWriteReal">WriteReal</a></td>
7865 <td width="25%%"></td>
7869 <table width="100%">
7871 <td width="25%"><a href="#decl_cvXor">Xor</a></td>
7872 <td width="25%"><a href="#decl_cvXorS">XorS</a></td>
7873 <td width="25%%"></td>
7877 <hr><h1><a name="cxcore_sample_index">List of Examples</a></h1>