]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/doc/cxcore_array_operations.tex
c2198c42f7c51134dc25f4262452fbcd7fee00f5
[opencv.git] / opencv / doc / cxcore_array_operations.tex
1 \section{Operations on Arrays}
2
3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4 %                                                                                    %
5 %                                         C                                          %
6 %                                                                                    %
7 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8
9 \ifCPy
10
11 \cvCPyFunc{AbsDiff}
12 Calculates absolute difference between two arrays.
13
14 \cvdefC{void cvAbsDiff(const CvArr* src1, const CvArr* src2, CvArr* dst);}
15 \cvdefPy{AbsDiff(src1,src2,dst)-> None}
16
17 \begin{description}
18 \cvarg{src1}{The first source array}
19 \cvarg{src2}{The second source array}
20 \cvarg{dst}{The destination array}
21 \end{description}
22
23 The function calculates absolute difference between two arrays.
24
25 \[ \texttt{dst}(i)_c = |\texttt{src1}(I)_c - \texttt{src2}(I)_c| \]
26
27 All the arrays must have the same data type and the same size (or ROI size).
28
29 \cvCPyFunc{AbsDiffS}
30 Calculates absolute difference between an array and a scalar.
31
32 \cvdefC{void cvAbsDiffS(const CvArr* src, CvArr* dst, CvScalar value);}
33 \cvdefPy{AbsDiffS(src,value,dst)-> None}
34 \ifC
35 \begin{lstlisting}
36 #define cvAbs(src, dst) cvAbsDiffS(src, dst, cvScalarAll(0))
37 \end{lstlisting}
38 \fi
39 \begin{description}
40 \cvarg{src}{The source array}
41 \cvarg{dst}{The destination array}
42 \cvarg{value}{The scalar}
43 \end{description}
44
45 The function calculates absolute difference between an array and a scalar.
46
47 \[ \texttt{dst}(i)_c = |\texttt{src}(I)_c - \texttt{value}_c| \]
48
49 All the arrays must have the same data type and the same size (or ROI size).
50
51
52 \cvCPyFunc{Add}
53 Computes the per-element sum of two arrays.
54
55 \cvdefC{void cvAdd(const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask=NULL);}
56 \cvdefPy{Add(src1,src2,dst,mask=NULL)-> None}
57
58 \begin{description}
59 \cvarg{src1}{The first source array}
60 \cvarg{src2}{The second source array}
61 \cvarg{dst}{The destination array}
62 \cvarg{mask}{Operation mask, 8-bit single channel array; specifies elements of the destination array to be changed}
63 \end{description}
64
65 The function adds one array to another:
66
67 \begin{lstlisting}
68 dst(I)=src1(I)+src2(I) if mask(I)!=0
69 \end{lstlisting}
70
71 All the arrays must have the same type, except the mask, and the same size (or ROI size).
72 For types that have limited range this operation is saturating.
73
74 \cvCPyFunc{AddS}
75 Computes the sum of an array and a scalar.
76
77 \cvdefC{void cvAddS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL);}
78 \cvdefPy{AddS(src,value,dst,mask=NULL)-> None}
79
80 \begin{description}
81 \cvarg{src}{The source array}
82 \cvarg{value}{Added scalar}
83 \cvarg{dst}{The destination array}
84 \cvarg{mask}{Operation mask, 8-bit single channel array; specifies elements of the destination array to be changed}
85 \end{description}
86
87 The function adds a scalar \texttt{value} to every element in the source array \texttt{src1} and stores the result in \texttt{dst}.
88 For types that have limited range this operation is saturating.
89
90 \begin{lstlisting}
91 dst(I)=src(I)+value if mask(I)!=0
92 \end{lstlisting}
93
94 All the arrays must have the same type, except the mask, and the same size (or ROI size).
95
96
97 \cvCPyFunc{AddWeighted}
98 Computes the weighted sum of two arrays.
99
100 \cvdefC{void  cvAddWeighted(const CvArr* src1, double alpha,
101                      const CvArr* src2, double beta,
102                      double gamma, CvArr* dst);}
103 \cvdefPy{AddWeighted(src1,alpha,src2,beta,gamma,dst)-> None}
104
105 \begin{description}
106 \cvarg{src1}{The first source array}
107 \cvarg{alpha}{Weight for the first array elements}
108 \cvarg{src2}{The second source array}
109 \cvarg{beta}{Weight for the second array elements}
110 \cvarg{dst}{The destination array}
111 \cvarg{gamma}{Scalar, added to each sum}
112 \end{description}
113
114 The function calculates the weighted sum of two arrays as follows:
115
116 \begin{lstlisting}
117 dst(I)=src1(I)*alpha+src2(I)*beta+gamma
118 \end{lstlisting}
119
120 All the arrays must have the same type and the same size (or ROI size).
121 For types that have limited range this operation is saturating.
122
123
124 \cvCPyFunc{And}
125 Calculates per-element bit-wise conjunction of two arrays.
126
127 \cvdefC{void cvAnd(const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask=NULL);}
128 \cvdefPy{And(src1,src2,dst,mask=NULL)-> None}
129
130 \begin{description}
131 \cvarg{src1}{The first source array}
132 \cvarg{src2}{The second source array}
133 \cvarg{dst}{The destination array}
134 \cvarg{mask}{Operation mask, 8-bit single channel array; specifies elements of the destination array to be changed}
135 \end{description}
136
137 The function calculates per-element bit-wise logical conjunction of two arrays:
138
139 \begin{lstlisting}
140 dst(I)=src1(I)&src2(I) if mask(I)!=0
141 \end{lstlisting}
142
143 In the case of floating-point arrays their bit representations are used for the operation. All the arrays must have the same type, except the mask, and the same size.
144
145 \cvCPyFunc{AndS}
146 Calculates per-element bit-wise conjunction of an array and a scalar.
147
148 \cvdefC{void cvAndS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL);}
149 \cvdefPy{AndS(src,value,dst,mask=NULL)-> None}
150
151 \begin{description}
152 \cvarg{src}{The source array}
153 \cvarg{value}{Scalar to use in the operation}
154 \cvarg{dst}{The destination array}
155 \cvarg{mask}{Operation mask, 8-bit single channel array; specifies elements of the destination array to be changed}
156 \end{description}
157
158 The function calculates per-element bit-wise conjunction of an array and a scalar:
159
160 \begin{lstlisting}
161 dst(I)=src(I)&value if mask(I)!=0
162 \end{lstlisting}
163
164 Prior to the actual operation, the scalar is converted to the same type as that of the array(s). In the case of floating-point arrays their bit representations are used for the operation. All the arrays must have the same type, except the mask, and the same size.
165
166 The following sample demonstrates how to calculate the absolute value of floating-point array elements by clearing the most-significant bit:
167
168 \begin{lstlisting}
169 float a[] = { -1, 2, -3, 4, -5, 6, -7, 8, -9 };
170 CvMat A = cvMat(3, 3, CV\_32F, &a);
171 int i, absMask = 0x7fffffff;
172 cvAndS(&A, cvRealScalar(*(float*)&absMask), &A, 0);
173 for(i = 0; i < 9; i++ )
174     printf("%.1f ", a[i]);
175 \end{lstlisting}
176
177 The code should print:
178
179 \begin{lstlisting}
180 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
181 \end{lstlisting}
182
183
184 \cvCPyFunc{Avg}
185 Calculates average (mean) of array elements.
186
187 \cvdefC{CvScalar cvAvg(const CvArr* arr, const CvArr* mask=NULL);}
188 \cvdefPy{Avg(arr,mask=NULL)-> CvScalar}
189
190 \begin{description}
191 \cvarg{arr}{The array}
192 \cvarg{mask}{The optional operation mask}
193 \end{description}
194
195 The function calculates the average value \texttt{M} of array elements, independently for each channel:
196
197 \[
198 \begin{array}{l}
199 N = \sum_I (\texttt{mask}(I) \ne 0)\\
200 M_c = \frac{\sum_{I, \, \texttt{mask}(I) \ne 0} \texttt{arr}(I)_c}{N}
201 \end{array}
202 \]
203
204 If the array is \texttt{IplImage} and COI is set, the function processes the selected channel only and stores the average to the first scalar component $ S_0 $ .
205
206 \cvCPyFunc{AvgSdv}
207 Calculates average (mean) of array elements.
208
209 \cvdefC{void cvAvgSdv(const CvArr* arr, CvScalar* mean, CvScalar* stdDev, const CvArr* mask=NULL);}
210 \cvdefPy{AvgSdv(arr,mask=NULL)-> (mean, stdDev)}
211
212 \begin{description}
213 \cvarg{arr}{The array}
214 \ifC
215 \cvarg{mean}{Pointer to the output mean value, may be NULL if it is not needed}
216 \cvarg{stdDev}{Pointer to the output standard deviation}
217 \fi
218 \cvarg{mask}{The optional operation mask}
219 \ifPy
220 \cvarg{mean}{Mean value, a CvScalar}
221 \cvarg{stdDev}{Standard deviation, a CvScalar}
222 \fi
223
224 \end{description}
225
226 The function calculates the average value and standard deviation of array elements, independently for each channel:
227
228 \[
229 \begin{array}{l}
230 N = \sum_I (\texttt{mask}(I) \ne 0)\\
231 mean_c = \frac{1}{N} \, \sum_{ I, \, \texttt{mask}(I) \ne 0} \texttt{arr}(I)_c\\
232 stdDev_c = \sqrt{\frac{1}{N} \, \sum_{ I, \, \texttt{mask}(I) \ne 0} (\texttt{arr}(I)_c - mean_c)^2}
233 \end{array}
234 \]
235
236 If the array is \texttt{IplImage} and COI is set, the function processes the selected channel only and stores the average and standard deviation to the first components of the output scalars ($mean_0$ and $stdDev_0$).
237
238 \cvCPyFunc{CalcCovarMatrix}
239 Calculates covariance matrix of a set of vectors.
240
241 \cvdefC{
242 void cvCalcCovarMatrix(\par const CvArr** vects,\par int count,\par CvArr* covMat,\par CvArr* avg,\par int flags);}
243 \cvdefPy{CalcCovarMatrix(vects,covMat,avg,flags)-> None}
244
245 \begin{description}
246 \cvarg{vects}{The input vectors, all of which must have the same type and the same size. The vectors do not have to be 1D, they can be 2D (e.g., images) and so forth}
247 \ifC
248 \cvarg{count}{The number of input vectors}
249 \fi
250 \cvarg{covMat}{The output covariance matrix that should be floating-point and square}
251 \cvarg{avg}{The input or output (depending on the flags) array - the mean (average) vector of the input vectors}
252 \cvarg{flags}{The operation flags, a combination of the following values
253 \begin{description}
254 \cvarg{CV\_COVAR\_SCRAMBLED}{The output covariance matrix is calculated as:
255 \[
256  \texttt{scale} * [ \texttt{vects} [0]- \texttt{avg} ,\texttt{vects} [1]- \texttt{avg} ,...]^T \cdot [\texttt{vects} [0]-\texttt{avg} ,\texttt{vects} [1]-\texttt{avg} ,...] 
257 \],
258 that is, the covariance matrix is
259 $\texttt{count} \times \texttt{count}$.
260 Such an unusual covariance matrix is used for fast PCA
261 of a set of very large vectors (see, for example, the EigenFaces technique
262 for face recognition). Eigenvalues of this "scrambled" matrix will
263 match the eigenvalues of the true covariance matrix and the "true"
264 eigenvectors can be easily calculated from the eigenvectors of the
265 "scrambled" covariance matrix.}
266 \cvarg{CV\_COVAR\_NORMAL}{The output covariance matrix is calculated as:
267 \[
268  \texttt{scale} * [ \texttt{vects} [0]- \texttt{avg} ,\texttt{vects} [1]- \texttt{avg} ,...] \cdot [\texttt{vects} [0]-\texttt{avg} ,\texttt{vects} [1]-\texttt{avg} ,...]^T 
269 \],
270 that is, \texttt{covMat} will be a covariance matrix
271 with the same linear size as the total number of elements in each
272 input vector. One and only one of \texttt{CV\_COVAR\_SCRAMBLED} and
273 \texttt{CV\_COVAR\_NORMAL} must be specified}
274 \cvarg{CV\_COVAR\_USE\_AVG}{If the flag is specified, the function does not calculate \texttt{avg} from the input vectors, but, instead, uses the passed \texttt{avg} vector. This is useful if \texttt{avg} has been already calculated somehow, or if the covariance matrix is calculated by parts - in this case, \texttt{avg} is not a mean vector of the input sub-set of vectors, but rather the mean vector of the whole set.}
275 \cvarg{CV\_COVAR\_SCALE}{If the flag is specified, the covariance matrix is scaled. In the "normal" mode \texttt{scale} is '1./count'; in the "scrambled" mode \texttt{scale} is the reciprocal of the total number of elements in each input vector. By default (if the flag is not specified) the covariance matrix is not scaled ('scale=1').}
276
277 \cvarg{CV\_COVAR\_ROWS}{Means that all the input vectors are stored as rows of a single matrix, \texttt{vects[0]}. \texttt{count} is ignored in this case, and \texttt{avg} should be a single-row vector of an appropriate size.}
278 \cvarg{CV\_COVAR\_COLS}{Means that all the input vectors are stored as columns of a single matrix, \texttt{vects[0]}. \texttt{count} is ignored in this case, and \texttt{avg} should be a single-column vector of an appropriate size.}
279
280 \end{description}}
281 \end{description}
282
283 The function calculates the covariance matrix
284 and, optionally, the mean vector of the set of input vectors. The function
285 can be used for PCA, for comparing vectors using Mahalanobis distance and so forth.
286
287 \cvCPyFunc{CartToPolar}
288 Calculates the magnitude and/or angle of 2d vectors.
289
290 \cvdefC{void cvCartToPolar(\par const CvArr* x,\par const CvArr* y,\par CvArr* magnitude,\par CvArr* angle=NULL,\par int angleInDegrees=0);}
291 \cvdefPy{CartToPolar(x,y,magnitude,angle=NULL,angleInDegrees=0)-> None}
292
293 \begin{description}
294 \cvarg{x}{The array of x-coordinates}
295 \cvarg{y}{The array of y-coordinates}
296 \cvarg{magnitude}{The destination array of magnitudes, may be set to NULL if it is not needed}
297 \cvarg{angle}{The destination array of angles, may be set to NULL if it is not needed. The angles are measured in radians $(0$ to $2 \pi )$ or in degrees (0 to 360 degrees).}
298 \cvarg{angleInDegrees}{The flag indicating whether the angles are measured in radians, which is default mode, or in degrees}
299 \end{description}
300
301 The function calculates either the magnitude, angle, or both of every 2d vector (x(I),y(I)):
302
303 \begin{lstlisting}
304
305 magnitude(I)=sqrt(x(I)^2^+y(I)^2^ ),
306 angle(I)=atan(y(I)/x(I) )
307
308 \end{lstlisting}
309
310 The angles are calculated with 0.1 degree accuracy. For the (0,0) point, the angle is set to 0.
311
312 \cvCPyFunc{Cbrt}
313 Calculates the cubic root
314
315 \cvdefC{float cvCbrt(float value);}
316 \cvdefPy{Cbrt(value)-> float}
317
318 \begin{description}
319 \cvarg{value}{The input floating-point value}
320 \end{description}
321
322
323 The function calculates the cubic root of the argument, and normally it is faster than \texttt{pow(value,1./3)}. In addition, negative arguments are handled properly. Special values ($\pm \infty $, NaN) are not handled.
324
325 \cvCPyFunc{ClearND}
326 Clears a specific array element.
327 \cvdefC{void cvClearND(CvArr* arr, int* idx);}
328 \cvdefPy{ClearND(arr,idx)-> None}
329
330 \begin{description}
331 \cvarg{arr}{Input array}
332 \cvarg{idx}{Array of the element indices}
333 \end{description}
334
335 The function \cvCPyCross{ClearND} clears (sets to zero) a specific element of a dense array or deletes the element of a sparse array. If the sparse array element does not exists, the function does nothing.
336
337 \cvCPyFunc{CloneImage}
338 Makes a full copy of an image, including the header, data, and ROI.
339
340 \cvdefC{IplImage* cvCloneImage(const IplImage* image);}
341 \cvdefPy{CloneImage(image)-> copy}
342
343 \begin{description}
344 \cvarg{image}{The original image}
345 \end{description}
346
347 The returned \texttt{IplImage*} points to the image copy.
348
349 \cvCPyFunc{CloneMat}
350 Creates a full matrix copy.
351
352 \cvdefC{CvMat* cvCloneMat(const CvMat* mat);}
353 \cvdefPy{CloneMat(mat)-> copy}
354
355 \begin{description}
356 \cvarg{mat}{Matrix to be copied}
357 \end{description}
358
359 Creates a full copy of a matrix and returns a pointer to the copy.
360
361 \cvCPyFunc{CloneMatND}
362 Creates full copy of a multi-dimensional array and returns a pointer to the copy.
363
364 \cvdefC{CvMatND* cvCloneMatND(const CvMatND* mat);}
365 \cvdefPy{CloneMatND(mat)-> copy}
366
367 \begin{description}
368 \cvarg{mat}{Input array}
369 \end{description}
370
371
372 \ifC % {
373 \cvCPyFunc{CloneSparseMat}
374 Creates full copy of sparse array.
375
376 \cvdefC{CvSparseMat* cvCloneSparseMat(const CvSparseMat* mat);}
377 \cvdefPy{CloneSparseMat(mat) -> mat}
378
379 \begin{description}
380 \cvarg{mat}{Input array}
381 \end{description}
382
383 The function creates a copy of the input array and returns pointer to the copy.
384 \fi % }
385
386 \cvCPyFunc{Cmp}
387 Performs per-element comparison of two arrays.
388
389 \cvdefC{void cvCmp(const CvArr* src1, const CvArr* src2, CvArr* dst, int cmpOp);}
390 \cvdefPy{Cmp(src1,src2,dst,cmpOp)-> None}
391
392 \begin{description}
393 \cvarg{src1}{The first source array}
394 \cvarg{src2}{The second source array. Both source arrays must have a single channel.}
395 \cvarg{dst}{The destination array, must have 8u or 8s type}
396 \cvarg{cmpOp}{The flag specifying the relation between the elements to be checked
397 \begin{description}
398  \cvarg{CV\_CMP\_EQ}{src1(I) "equal to" value}
399  \cvarg{CV\_CMP\_GT}{src1(I) "greater than" value}
400  \cvarg{CV\_CMP\_GE}{src1(I) "greater or equal" value}
401  \cvarg{CV\_CMP\_LT}{src1(I) "less than" value}
402  \cvarg{CV\_CMP\_LE}{src1(I) "less or equal" value}
403  \cvarg{CV\_CMP\_NE}{src1(I) "not equal" value}
404 \end{description}}
405 \end{description}
406
407 The function compares the corresponding elements of two arrays and fills the destination mask array:
408
409 \begin{lstlisting}
410 dst(I)=src1(I) op src2(I),
411 \end{lstlisting}
412
413 \texttt{dst(I)} is set to 0xff (all \texttt{1}-bits) if the specific relation between the elements is true and 0 otherwise. All the arrays must have the same type, except the destination, and the same size (or ROI size)
414
415 \cvCPyFunc{CmpS}
416 Performs per-element comparison of an array and a scalar.
417
418 \cvdefC{void cvCmpS(const CvArr* src, double value, CvArr* dst, int cmpOp);}
419 \cvdefPy{CmpS(src,value,dst,cmpOp)-> None}
420
421 \begin{description}
422 \cvarg{src}{The source array, must have a single channel}
423 \cvarg{value}{The scalar value to compare each array element with}
424 \cvarg{dst}{The destination array, must have 8u or 8s type}
425 \cvarg{cmpOp}{The flag specifying the relation between the elements to be checked
426 \begin{description}
427  \cvarg{CV\_CMP\_EQ}{src1(I) "equal to" value}
428  \cvarg{CV\_CMP\_GT}{src1(I) "greater than" value}
429  \cvarg{CV\_CMP\_GE}{src1(I) "greater or equal" value}
430  \cvarg{CV\_CMP\_LT}{src1(I) "less than" value}
431  \cvarg{CV\_CMP\_LE}{src1(I) "less or equal" value}
432  \cvarg{CV\_CMP\_NE}{src1(I) "not equal" value}
433 \end{description}}
434 \end{description}
435
436 The function compares the corresponding elements of an array and a scalar and fills the destination mask array:
437
438 \begin{lstlisting}
439 dst(I)=src(I) op scalar
440 \end{lstlisting}
441
442 where \texttt{op} is $=,\; >,\; \ge,\; <,\; \le\; or\; \ne$.
443
444 \texttt{dst(I)} is set to 0xff (all \texttt{1}-bits) if the specific relation between the elements is true and 0 otherwise. All the arrays must have the same size (or ROI size).
445
446 \ifPy % {
447 \cvCPyFunc{Convert}
448 Converts one array to another.
449
450 \cvdefPy{Convert(src,dst)-> None}
451
452 \begin{description}
453 \cvarg{src}{Source array}
454 \cvarg{dst}{Destination array}
455 \end{description}
456
457
458 The type of conversion is done with rounding and saturation, that is if the
459 result of scaling + conversion can not be represented exactly by a value
460 of the destination array element type, it is set to the nearest representable
461 value on the real axis.
462
463 All the channels of multi-channel arrays are processed independently.
464
465 \fi % }
466
467 \cvCPyFunc{ConvertScale}
468 Converts one array to another with optional linear transformation.
469
470 \cvdefC{void cvConvertScale(const CvArr* src, CvArr* dst, double scale=1, double shift=0);}
471 \cvdefPy{ConvertScale(src,dst,scale=1.0,shift=0.0)-> None}
472
473 \ifC
474 \begin{lstlisting}
475 #define cvCvtScale cvConvertScale
476 #define cvScale  cvConvertScale
477 #define cvConvert(src, dst )  cvConvertScale((src), (dst), 1, 0 )
478 \end{lstlisting}
479 \fi
480
481 \begin{description}
482 \cvarg{src}{Source array}
483 \cvarg{dst}{Destination array}
484 \cvarg{scale}{Scale factor}
485 \cvarg{shift}{Value added to the scaled source array elements}
486 \end{description}
487
488
489 The function has several different purposes, and thus has several different names. It copies one array to another with optional scaling, which is performed first, and/or optional type conversion, performed after:
490
491 \[
492 \texttt{dst}(I) = \texttt{scale} \texttt{src}(I) + (\texttt{shift}_0,\texttt{shift}_1,...)
493 \]
494
495 All the channels of multi-channel arrays are processed independently.
496
497 The type of conversion is done with rounding and saturation, that is if the
498 result of scaling + conversion can not be represented exactly by a value
499 of the destination array element type, it is set to the nearest representable
500 value on the real axis.
501
502 In the case of \texttt{scale=1, shift=0} no prescaling is done. This is a specially
503 optimized case and it has the appropriate \cvCPyCross{Convert} name. If
504 source and destination array types have equal types, this is also a
505 special case that can be used to scale and shift a matrix or an image
506 and that is caled \cvCPyCross{Scale}.
507
508
509 \cvCPyFunc{ConvertScaleAbs}
510 Converts input array elements to another 8-bit unsigned integer with optional linear transformation.
511
512 \cvdefC{void cvConvertScaleAbs(const CvArr* src, CvArr* dst, double scale=1, double shift=0);}
513 \cvdefPy{ConvertScaleAbs(src,dst,scale=1.0,shift=0.0)-> None}
514
515 \begin{description}
516 \cvarg{src}{Source array}
517 \cvarg{dst}{Destination array (should have 8u depth)}
518 \cvarg{scale}{ScaleAbs factor}
519 \cvarg{shift}{Value added to the scaled source array elements}
520 \end{description}
521
522 \begin{lstlisting}
523 #define cvCvtScaleAbs cvConvertScaleAbs
524 \end{lstlisting}
525
526
527 The function is similar to \cvCPyCross{ConvertScale}, but it stores absolute values of the conversion results:
528
529 \[
530 \texttt{dst}(I) = |\texttt{scale} \texttt{src}(I) + (\texttt{shift}_0,\texttt{shift}_1,...)|
531 \]
532
533 The function supports only destination arrays of 8u (8-bit unsigned integers) type; for other types the function can be emulated by a combination of \cvCPyCross{ConvertScale} and \cvCPyCross{Abs} functions.
534
535 \cvCPyFunc{Copy}
536 Copies one array to another.
537
538 \cvdefC{void cvCopy(const CvArr* src, CvArr* dst, const CvArr* mask=NULL);}
539 \cvdefPy{Copy(src,dst,mask=NULL)-> None}
540
541 \begin{description}
542 \cvarg{src}{The source array}
543 \cvarg{dst}{The destination array}
544 \cvarg{mask}{Operation mask, 8-bit single channel array; specifies elements of the destination array to be changed}
545 \end{description}
546
547
548 The function copies selected elements from an input array to an output array:
549
550 \[
551 \texttt{dst}(I)=\texttt{src}(I) \quad \text{if} \quad \texttt{mask}(I) \ne 0.
552 \]
553
554 If any of the passed arrays is of \texttt{IplImage} type, then its ROI
555 and COI fields are used. Both arrays must have the same type, the same
556 number of dimensions, and the same size. The function can also copy sparse
557 arrays (mask is not supported in this case).
558
559 \cvCPyFunc{CountNonZero}
560 Counts non-zero array elements.
561
562 \cvdefC{int cvCountNonZero(const CvArr* arr);}
563 \cvdefPy{CountNonZero(arr)-> int}
564
565 \begin{description}
566 \cvarg{arr}{The array must be a single-channel array or a multi-channel image with COI set}
567 \end{description}
568
569
570 The function returns the number of non-zero elements in arr:
571
572 \[ \sum_I (\texttt{arr}(I) \ne 0) \]
573
574 In the case of \texttt{IplImage} both ROI and COI are supported.
575
576
577 \cvCPyFunc{CreateData}
578 Allocates array data
579
580 \cvdefC{void cvCreateData(CvArr* arr);}
581 \cvdefPy{CreateData(arr) -> None}
582
583 \begin{description}
584 \cvarg{arr}{Array header}
585 \end{description}
586
587
588 The function allocates image, matrix or
589 multi-dimensional array data. Note that in the case of matrix types OpenCV
590 allocation functions are used and in the case of IplImage they are used
591 unless \texttt{CV\_TURN\_ON\_IPL\_COMPATIBILITY} was called. In the
592 latter case IPL functions are used to allocate the data.
593
594 \cvCPyFunc{CreateImage}
595 Creates an image header and allocates the image data.
596
597 \cvdefC{IplImage* cvCreateImage(CvSize size, int depth, int channels);}
598 \cvdefPy{CreateImage(size, depth, channels)->image}
599
600 \begin{description}
601 \cvarg{size}{Image width and height}
602 \cvarg{depth}{Bit depth of image elements. See \cross{IplImage} for valid depths.}
603 \cvarg{channels}{Number of channels per pixel. See \cross{IplImage} for details. This function only creates images with interleaved channels.}
604 \end{description}
605
606 This call is a shortened form of
607 \begin{lstlisting}
608 header = cvCreateImageHeader(size, depth, channels);
609 cvCreateData(header);
610 \end{lstlisting}
611
612
613 \cvCPyFunc{CreateImageHeader}
614 Creates an image header but does not allocate the image data.
615
616 \cvdefC{IplImage* cvCreateImageHeader(CvSize size, int depth, int channels);}
617 \cvdefPy{CreateImageHeader(size, depth, channels) -> image}
618
619 \begin{description}
620 \cvarg{size}{Image width and height}
621 \cvarg{depth}{Image depth (see \cvCPyCross{CreateImage})}
622 \cvarg{channels}{Number of channels (see \cvCPyCross{CreateImage})}
623 \end{description}
624
625 This call is an analogue of
626 \begin{lstlisting}
627 hdr=iplCreateImageHeader(channels, 0, depth,
628                       channels == 1 ? "GRAY" : "RGB",
629                       channels == 1 ? "GRAY" : channels == 3 ? "BGR" :
630                       channels == 4 ? "BGRA" : "",
631                       IPL_DATA_ORDER_PIXEL, IPL_ORIGIN_TL, 4,
632                       size.width, size.height,
633                       0,0,0,0);
634 \end{lstlisting}
635 but it does not use IPL functions by default (see the \texttt{CV\_TURN\_ON\_IPL\_COMPATIBILITY} macro).
636
637 \cvCPyFunc{CreateMat}\label{cvCreateMat}
638 Creates a matrix header and allocates the matrix data. 
639
640 \cvdefC{CvMat* cvCreateMat(\par int rows,\par int cols,\par int type);}
641 \cvdefPy{CreateMat(rows, cols, type) -> mat}
642
643 \begin{description}
644 \cvarg{rows}{Number of rows in the matrix}
645 \cvarg{cols}{Number of columns in the matrix}
646 \cvarg{type}{The type of the matrix elements in the form \texttt{CV\_<bit depth><S|U|F>C<number of channels>}, where S=signed, U=unsigned, F=float. For example, CV\_8UC1 means the elements are 8-bit unsigned and the there is 1 channel, and CV\_32SC2 means the elements are 32-bit signed and there are 2 channels.}
647 \end{description}
648
649 This is the concise form for:
650
651 \begin{lstlisting}
652 CvMat* mat = cvCreateMatHeader(rows, cols, type);
653 cvCreateData(mat);
654 \end{lstlisting}
655
656 \cvCPyFunc{CreateMatHeader}
657 Creates a matrix header but does not allocate the matrix data.
658
659 \cvdefC{CvMat* cvCreateMatHeader(\par int rows,\par int cols,\par int type);}
660 \cvdefPy{CreateMatHeader(rows, cols, type) -> mat}
661
662 \begin{description}
663 \cvarg{rows}{Number of rows in the matrix}
664 \cvarg{cols}{Number of columns in the matrix}
665 \cvarg{type}{Type of the matrix elements, see \cvCPyCross{CreateMat}}
666 \end{description}
667
668 The function allocates a new matrix header and returns a pointer to it. The matrix data can then be allocated using \cvCPyCross{CreateData} or set explicitly to user-allocated data via \cvCPyCross{SetData}.
669
670 \cvCPyFunc{CreateMatND}
671 Creates the header and allocates the data for a multi-dimensional dense array.
672
673 \cvdefC{CvMatND* cvCreateMatND(\par int dims,\par const int* sizes,\par int type);}
674 \cvdefPy{CreateMatND(dims, type) -> None}
675
676 \begin{description}
677 \ifPy
678 \cvarg{dims}{List or tuple of array dimensions, up to 32 in length.}
679 \else
680 \cvarg{dims}{Number of array dimensions. This must not exceed CV\_MAX\_DIM (32 by default, but can be changed at build time).}
681 \cvarg{sizes}{Array of dimension sizes.}
682 \fi
683 \cvarg{type}{Type of array elements, see \cvCPyCross{CreateMat}.}
684 \end{description}
685
686 This is a short form for:
687
688 \begin{lstlisting}
689 CvMatND* mat = cvCreateMatNDHeader(dims, sizes, type);
690 cvCreateData(mat);
691 \end{lstlisting}
692
693 \cvCPyFunc{CreateMatNDHeader}
694 Creates a new matrix header but does not allocate the matrix data.
695
696 \cvdefC{CvMatND* cvCreateMatNDHeader(\par int dims,\par const int* sizes,\par int type);}
697 \cvdefPy{CreateMatNDHeader(dims, type) -> None}
698
699 \begin{description}
700 \ifPy
701 \cvarg{dims}{List or tuple of array dimensions, up to 32 in length.}
702 \else
703 \cvarg{dims}{Number of array dimensions}
704 \cvarg{sizes}{Array of dimension sizes}
705 \fi
706 \cvarg{type}{Type of array elements, see \cvCPyCross{CreateMat}}
707 \end{description}
708
709 The function allocates a header for a multi-dimensional dense array. The array data can further be allocated using \cvCPyCross{CreateData} or set explicitly to user-allocated data via \cvCPyCross{SetData}.
710
711 \ifC % {
712 \cvCPyFunc{CreateSparseMat}
713 Creates sparse array.
714
715 \cvdefC{CvSparseMat* cvCreateSparseMat(int dims, const int* sizes, int type);}
716 \cvdefPy{CreateSparseMat(dims, type) -> cvmat}
717
718 \begin{description}
719 \ifC
720 \cvarg{dims}{Number of array dimensions. In contrast to the dense matrix, the number of dimensions is practically unlimited (up to $2^{16}$).}
721 \cvarg{sizes}{Array of dimension sizes}
722 \else
723 \cvarg{dims}{List or tuple of array dimensions.}
724 \fi
725 \cvarg{type}{Type of array elements. The same as for CvMat}
726 \end{description}
727
728 The function allocates a multi-dimensional sparse array. Initially the array contain no elements, that is \cvCPyCross{Get} or \cvCPyCross{GetReal} returns zero for every index.
729 \fi % }
730
731 \cvCPyFunc{CrossProduct}
732 Calculates the cross product of two 3D vectors.
733
734 \cvdefC{void cvCrossProduct(const CvArr* src1, const CvArr* src2, CvArr* dst);}
735 \cvdefPy{CrossProduct(src1,src2,dst)-> None}
736
737 \begin{description}
738 \cvarg{src1}{The first source vector}
739 \cvarg{src2}{The second source vector}
740 \cvarg{dst}{The destination vector}
741 \end{description}
742
743
744 The function calculates the cross product of two 3D vectors:
745
746 \[ \texttt{dst} = \texttt{src1} \times \texttt{src2} \]
747 or:
748 \[
749 \begin{array}{l}
750 \texttt{dst}_1 = \texttt{src1}_2 \texttt{src2}_3 - \texttt{src1}_3 \texttt{src2}_2\\
751 \texttt{dst}_2 = \texttt{src1}_3 \texttt{src2}_1 - \texttt{src1}_1 \texttt{src2}_3\\
752 \texttt{dst}_3 = \texttt{src1}_1 \texttt{src2}_2 - \texttt{src1}_2 \texttt{src2}_1
753 \end{array}
754 \]
755
756 \cvCPyFunc{DCT}
757 Performs a forward or inverse Discrete Cosine transform of a 1D or 2D floating-point array.
758
759 \cvdefC{void cvDCT(const CvArr* src, CvArr* dst, int flags);}
760 \cvdefPy{DCT(src,dst,flags)-> None}
761
762 \begin{description}
763 \cvarg{src}{Source array, real 1D or 2D array}
764 \cvarg{dst}{Destination array of the same size and same type as the source}
765 \cvarg{flags}{Transformation flags, a combination of the following values
766 \begin{description}
767 \cvarg{CV\_DXT\_FORWARD}{do a forward 1D or 2D transform.}
768 \cvarg{CV\_DXT\_INVERSE}{do an inverse 1D or 2D transform.}
769 \cvarg{CV\_DXT\_ROWS}{do a forward or inverse transform of every individual row of the input matrix. This flag allows user to transform multiple vectors simultaneously and can be used to decrease the overhead (which is sometimes several times larger than the processing itself), to do 3D and higher-dimensional transforms and so forth.}
770 \end{description}}
771 \end{description}
772
773 The function performs a forward or inverse transform of a 1D or 2D floating-point array:
774
775 Forward Cosine transform of 1D vector of $N$ elements:
776 \[Y = C^{(N)} \cdot X\]
777 where
778 \[C^{(N)}_{jk}=\sqrt{\alpha_j/N}\cos\left(\frac{\pi(2k+1)j}{2N}\right)\]
779 and $\alpha_0=1$, $\alpha_j=2$ for $j > 0$.
780
781 Inverse Cosine transform of 1D vector of N elements:
782 \[X = \left(C^{(N)}\right)^{-1} \cdot Y = \left(C^{(N)}\right)^T \cdot Y\]
783 (since $C^{(N)}$ is orthogonal matrix, $C^{(N)} \cdot \left(C^{(N)}\right)^T = I$)
784
785 Forward Cosine transform of 2D $M \times N$ matrix:
786 \[Y = C^{(N)} \cdot X \cdot \left(C^{(N)}\right)^T\]
787
788 Inverse Cosine transform of 2D vector of $M \times N$ elements:
789 \[X = \left(C^{(N)}\right)^T \cdot X \cdot C^{(N)}\]
790
791
792 \cvCPyFunc{DFT}
793 Performs a forward or inverse Discrete Fourier transform of a 1D or 2D floating-point array.
794
795 \cvdefC{void cvDFT(const CvArr* src, CvArr* dst, int flags, int nonzeroRows=0);}
796 \cvdefPy{DFT(src,dst,flags,nonzeroRows=0)-> None}
797
798 \begin{description}
799 \cvarg{src}{Source array, real or complex}
800 \cvarg{dst}{Destination array of the same size and same type as the source}
801 \cvarg{flags}{Transformation flags, a combination of the following values
802 \begin{description}
803 \cvarg{CV\_DXT\_FORWARD}{do a forward 1D or 2D transform. The result is not scaled.}
804 \cvarg{CV\_DXT\_INVERSE}{do an inverse 1D or 2D transform. The result is not scaled. \texttt{CV\_DXT\_FORWARD} and \texttt{CV\_DXT\_INVERSE} are mutually exclusive, of course.}
805 \cvarg{CV\_DXT\_SCALE}{scale the result: divide it by the number of array elements. Usually, it is combined with \texttt{CV\_DXT\_INVERSE}, and one may use a shortcut \texttt{CV\_DXT\_INV\_SCALE}.}
806 \cvarg{CV\_DXT\_ROWS}{do a forward or inverse transform of every individual row of the input matrix. This flag allows the user to transform multiple vectors simultaneously and can be used to decrease the overhead (which is sometimes several times larger than the processing itself), to do 3D and higher-dimensional transforms and so forth.}
807 \cvarg{CV\_DXT\_INVERSE\_SCALE}{same as \texttt{CV\_DXT\_INVERSE + CV\_DXT\_SCALE}}
808 \end{description}}
809 \cvarg{nonzeroRows}{Number of nonzero rows in the source array
810 (in the case of a forward 2d transform), or a number of rows of interest in
811 the destination array (in the case of an inverse 2d transform). If the value
812 is negative, zero, or greater than the total number of rows, it is
813 ignored. The parameter can be used to speed up 2d convolution/correlation
814 when computing via DFT. See the example below.}
815 \end{description}
816
817 The function performs a forward or inverse transform of a 1D or 2D floating-point array:
818
819
820 Forward Fourier transform of 1D vector of N elements:
821 \[y = F^{(N)} \cdot x, where F^{(N)}_{jk}=exp(-i \cdot 2\pi \cdot j \cdot k/N)\], 
822 \[i=sqrt(-1)\]
823
824 Inverse Fourier transform of 1D vector of N elements:
825 \[x'= (F^{(N)})^{-1} \cdot y = conj(F^(N)) \cdot y
826 x = (1/N) \cdot x\]
827
828 Forward Fourier transform of 2D vector of M $\times$ N elements:
829 \[Y = F^{(M)} \cdot X \cdot F^{(N)}\]
830
831 Inverse Fourier transform of 2D vector of M $\times$ N elements:
832 \[X'= conj(F^{(M)}) \cdot Y \cdot conj(F^{(N)})
833 X = (1/(M \cdot N)) \cdot X'\]
834
835
836 In the case of real (single-channel) data, the packed format, borrowed from IPL, is used to represent the result of a forward Fourier transform or input for an inverse Fourier transform:
837
838 \[\begin{bmatrix}
839 Re Y_{0,0} & Re Y_{0,1} & Im Y_{0,1} & Re Y_{0,2} & Im Y_{0,2} & \cdots & Re Y_{0,N/2-1} & Im Y_{0,N/2-1} & Re Y_{0,N/2} \\
840 Re Y_{1,0} & Re Y_{1,1} & Im Y_{1,1} & Re Y_{1,2} & Im Y_{1,2} & \cdots & Re Y_{1,N/2-1} & Im Y_{1,N/2-1} & Re Y_{1,N/2} \\
841 Im Y_{1,0} & Re Y_{2,1} & Im Y_{2,1} & Re Y_{2,2} & Im Y_{2,2} & \cdots & Re Y_{2,N/2-1} & Im Y_{2,N/2-1} & Im Y_{1,N/2} \\
842 \hdotsfor{9} \\
843 Re Y_{M/2-1,0} &  Re Y_{M-3,1}  & Im Y_{M-3,1} & \hdotsfor{3} & Re Y_{M-3,N/2-1} & Im Y_{M-3,N/2-1}& Re Y_{M/2-1,N/2} \\
844 Im Y_{M/2-1,0} &  Re Y_{M-2,1}  & Im Y_{M-2,1} & \hdotsfor{3} & Re Y_{M-2,N/2-1} & Im Y_{M-2,N/2-1}& Im Y_{M/2-1,N/2} \\
845 Re Y_{M/2,0}  &  Re Y_{M-1,1} &  Im Y_{M-1,1} & \hdotsfor{3} & Re Y_{M-1,N/2-1} & Im Y_{M-1,N/2-1}& Re Y_{M/2,N/2}
846 \end{bmatrix}
847 \]
848
849
850 Note: the last column is present if \texttt{N} is even, the last row is present if \texttt{M} is even.
851 In the case of 1D real transform the result looks like the first row of the above matrix.
852
853 Here is the example of how to compute 2D convolution using DFT.
854
855 \begin{lstlisting}
856 CvMat* A = cvCreateMat(M1, N1, CVg32F);
857 CvMat* B = cvCreateMat(M2, N2, A->type);
858
859 // it is also possible to have only abs(M2-M1)+1 times abs(N2-N1)+1
860 // part of the full convolution result
861 CvMat* conv = cvCreateMat(A->rows + B->rows - 1, A->cols + B->cols - 1, 
862                            A->type);
863
864 // initialize A and B
865 ...
866
867 int dftgM = cvGetOptimalDFTSize(A->rows + B->rows - 1);
868 int dftgN = cvGetOptimalDFTSize(A->cols + B->cols - 1);
869
870 CvMat* dftgA = cvCreateMat(dft\_M, dft\_N, A->type);
871 CvMat* dftgB = cvCreateMat(dft\_M, dft\_N, B->type);
872 CvMat tmp;
873
874 // copy A to dftgA and pad dft\_A with zeros
875 cvGetSubRect(dftgA, &tmp, cvRect(0,0,A->cols,A->rows));
876 cvCopy(A, &tmp);
877 cvGetSubRect(dftgA, &tmp, cvRect(A->cols,0,dft\_A->cols - A->cols,A->rows));
878 cvZero(&tmp);
879 // no need to pad bottom part of dftgA with zeros because of
880 // use nonzerogrows parameter in cvDFT() call below
881
882 cvDFT(dftgA, dft\_A, CV\_DXT\_FORWARD, A->rows);
883
884 // repeat the same with the second array
885 cvGetSubRect(dftgB, &tmp, cvRect(0,0,B->cols,B->rows));
886 cvCopy(B, &tmp);
887 cvGetSubRect(dftgB, &tmp, cvRect(B->cols,0,dft\_B->cols - B->cols,B->rows));
888 cvZero(&tmp);
889 // no need to pad bottom part of dftgB with zeros because of
890 // use nonzerogrows parameter in cvDFT() call below
891
892 cvDFT(dftgB, dft\_B, CV\_DXT\_FORWARD, B->rows);
893
894 cvMulSpectrums(dftgA, dft\_B, dft\_A, 0 /* or CV\_DXT\_MUL\_CONJ to get 
895                 correlation rather than convolution */);
896
897 cvDFT(dftgA, dft\_A, CV\_DXT\_INV\_SCALE, conv->rows); // calculate only 
898                                                          // the top part
899 cvGetSubRect(dftgA, &tmp, cvRect(0,0,conv->cols,conv->rows));
900
901 cvCopy(&tmp, conv);
902 \end{lstlisting}
903
904 \ifC
905
906 \cvCPyFunc{DecRefData}
907 Decrements an array data reference counter.
908
909 \cvdefC{void cvDecRefData(CvArr* arr);}
910
911 \begin{description}
912 \cvarg{arr}{Pointer to an array header}
913 \end{description}
914
915 The function decrements the data reference counter in a \cross{CvMat} or
916 \cross{CvMatND} if the reference counter pointer
917 is not NULL. If the counter reaches zero, the data is deallocated. In the
918 current implementation the reference counter is not NULL only if the data
919 was allocated using the \cvCPyCross{CreateData} function. The counter will be NULL in other cases such as:
920 external data was assigned to the header using \cvCPyCross{SetData}, the matrix
921 header is part of a larger matrix or image, or the header was converted from an image or n-dimensional matrix header. 
922
923 \fi
924
925
926 \cvCPyFunc{Det}
927 Returns the determinant of a matrix.
928
929 \cvdefC{double cvDet(const CvArr* mat);}
930 \cvdefPy{Det(mat)-> double}
931
932 \begin{description}
933 \cvarg{mat}{The source matrix}
934 \end{description}
935
936 The function returns the determinant of the square matrix \texttt{mat}. The direct method is used for small matrices and Gaussian elimination is used for larger matrices. For symmetric positive-determined matrices, it is also possible to run
937 \cvCPyCross{SVD}
938 with $U = V = 0$ and then calculate the determinant as a product of the diagonal elements of $W$.
939
940 \cvCPyFunc{Div}
941 Performs per-element division of two arrays.
942
943 \cvdefC{void cvDiv(const CvArr* src1, const CvArr* src2, CvArr* dst, double scale=1);}
944 \cvdefPy{Div(src1,src2,dst,scale)-> None}
945
946 \begin{description}
947 \cvarg{src1}{The first source array. If the pointer is NULL, the array is assumed to be all 1's.}
948 \cvarg{src2}{The second source array}
949 \cvarg{dst}{The destination array}
950 \cvarg{scale}{Optional scale factor}
951 \end{description}
952
953 The function divides one array by another:
954
955 \[
956 \texttt{dst}(I)=\fork
957 {\texttt{scale} \cdot \texttt{src1}(I)/\texttt{src2}(I)}{if \texttt{src1} is not \texttt{NULL}}
958 {\texttt{scale}/\texttt{src2}(I)}{otherwise}
959 \]
960
961 All the arrays must have the same type and the same size (or ROI size).
962
963
964 \cvCPyFunc{DotProduct}
965 Calculates the dot product of two arrays in Euclidian metrics.
966
967 \cvdefC{double cvDotProduct(const CvArr* src1, const CvArr* src2);}
968 \cvdefPy{DotProduct(src1,src2)-> double}
969
970 \begin{description}
971 \cvarg{src1}{The first source array}
972 \cvarg{src2}{The second source array}
973 \end{description}
974
975 The function calculates and returns the Euclidean dot product of two arrays.
976
977 \[
978 src1 \bullet src2 = \sum_I (\texttt{src1}(I) \texttt{src2}(I))
979 \]
980
981 In the case of multiple channel arrays, the results for all channels are accumulated. In particular, \texttt{cvDotProduct(a,a)} where \texttt{a} is a complex vector, will return $||\texttt{a}||^2$.
982 The function can process multi-dimensional arrays, row by row, layer by layer, and so on.
983
984 \cvCPyFunc{EigenVV}
985 Computes eigenvalues and eigenvectors of a symmetric matrix.
986
987 \cvdefC{
988 void cvEigenVV(\par CvArr* mat,\par CvArr* evects,\par CvArr* evals,\par double eps=0,
989 \par int lowindex = 0, \par int highindex = 0);}
990 \cvdefPy{EigenVV(mat,evects,evals,eps,lowindex,highindex)-> None}
991
992 \begin{description}
993 \cvarg{mat}{The input symmetric square matrix, modified during the processing}
994 \cvarg{evects}{The output matrix of eigenvectors, stored as subsequent rows}
995 \cvarg{evals}{The output vector of eigenvalues, stored in the descending order (order of eigenvalues and eigenvectors is syncronized, of course)}
996 \cvarg{eps}{Accuracy of diagonalization. Typically, \texttt{DBL\_EPSILON} (about $ 10^{-15} $) works well.
997 THIS PARAMETER IS CURRENTLY IGNORED.}
998 \cvarg{lowindex}{Optional index of largest eigenvalue/-vector to calculate.
999 (See below.)}
1000 \cvarg{highindex}{Optional index of smallest eigenvalue/-vector to calculate.
1001 (See below.)}
1002 \end{description}
1003
1004
1005 The function computes the eigenvalues and eigenvectors of matrix \texttt{A}:
1006
1007 \begin{lstlisting}
1008 mat*evects(i,:)' = evals(i)*evects(i,:)' (in MATLAB notation)
1009 \end{lstlisting}
1010
1011 If either low- or highindex is supplied the other is required, too.
1012 Indexing is 1-based. Example: To calculate the largest eigenvector/-value set
1013 lowindex = highindex = 1.
1014 For legacy reasons this function always returns a square matrix the same size
1015 as the source matrix with eigenvectors and a vector the length of the source
1016 matrix with eigenvalues. The selected eigenvectors/-values are always in the
1017 first highindex - lowindex + 1 rows.
1018
1019 The contents of matrix \texttt{A} is destroyed by the function.
1020
1021 Currently the function is slower than \cvCPyCross{SVD} yet less accurate,
1022 so if \texttt{A} is known to be positively-defined (for example, it
1023 is a covariance matrix)it is recommended to use \cvCPyCross{SVD} to find
1024 eigenvalues and eigenvectors of \texttt{A}, especially if eigenvectors
1025 are not required.
1026
1027 \cvCPyFunc{Exp}
1028 Calculates the exponent of every array element.
1029
1030 \cvdefC{void cvExp(const CvArr* src, CvArr* dst);}
1031 \cvdefPy{Exp(src,dst)-> None}
1032
1033 \begin{description}
1034 \cvarg{src}{The source array}
1035 \cvarg{dst}{The destination array, it should have \texttt{double} type or the same type as the source}
1036 \end{description}
1037
1038
1039 The function calculates the exponent of every element of the input array:
1040
1041 \[
1042 \texttt{dst} [I] = e^{\texttt{src}(I)}
1043 \]
1044
1045 The maximum relative error is about $7 \times 10^{-6}$. Currently, the function converts denormalized values to zeros on output.
1046
1047 \cvCPyFunc{FastArctan}
1048 Calculates the angle of a 2D vector.
1049
1050 \cvdefC{float cvFastArctan(float y, float x);}
1051 \cvdefPy{FastArctan(y,x)-> float}
1052
1053 \begin{description}
1054 \cvarg{x}{x-coordinate of 2D vector}
1055 \cvarg{y}{y-coordinate of 2D vector}
1056 \end{description}
1057
1058
1059 The function calculates the full-range angle of an input 2D vector. The angle is 
1060 measured in degrees and varies from 0 degrees to 360 degrees. The accuracy is about 0.1 degrees.
1061
1062 \cvCPyFunc{Flip}
1063 Flip a 2D array around vertical, horizontal or both axes.
1064
1065 \cvdefC{void  cvFlip(const CvArr* src, CvArr* dst=NULL, int flipMode=0);}
1066 \cvdefPy{Flip(src,dst=NULL,flipMode=0)-> None}
1067
1068 \begin{description}
1069 \cvarg{src}{Source array}
1070 \cvarg{dst}{Destination array.
1071 If $\texttt{dst} = \texttt{NULL}$ the flipping is done in place.}
1072 \cvarg{flipMode}{Specifies how to flip the array:
1073 0 means flipping around the x-axis, positive (e.g., 1) means flipping around y-axis, and negative (e.g., -1) means flipping around both axes. See also the discussion below for the formulas:}
1074 \end{description}
1075
1076 \begin{lstlisting}
1077 #define cvMirror cvFlip
1078 \end{lstlisting}
1079
1080 The function flips the array in one of three different ways (row and column indices are 0-based):
1081
1082 \[
1083 dst(i,j) = \forkthree
1084 {\texttt{src}(rows(\texttt{src})-i-1,j)}{if $\texttt{flipMode} = 0$}
1085 {\texttt{src}(i,cols(\texttt{src})-j-1)}{if $\texttt{flipMode} > 0$}
1086 {\texttt{src}(rows(\texttt{src})-i-1,cols(\texttt{src})-j-1)}{if $\texttt{flipMode} < 0$}
1087 \]
1088
1089 The example scenarios of function use are:
1090 \begin{itemize}
1091   \item vertical flipping of the image (flipMode = 0) to switch between top-left and bottom-left image origin, which is a typical operation in video processing under Win32 systems.
1092   \item horizontal flipping of the image with subsequent horizontal shift and absolute difference calculation to check for a vertical-axis symmetry (flipMode $>$ 0)
1093   \item simultaneous horizontal and vertical flipping of the image with subsequent shift and absolute difference calculation to check for a central symmetry (flipMode $<$ 0)
1094   \item reversing the order of 1d point arrays (flipMode > 0)
1095 \end{itemize}
1096
1097 \ifPy
1098
1099 \cvfunc{fromarray}
1100
1101 Create a CvMat from an object that supports the array interface.
1102
1103 \cvdefPy{fromarray(object, allowND = False) -> CvMat}
1104
1105 \begin{description}
1106 \cvarg{object}{Any object that supports the array interface}
1107 \cvarg{allowND}{If true, will return a CvMatND}
1108 \end{description}
1109
1110 If the object supports the
1111 \href{http://docs.scipy.org/doc/numpy/reference/arrays.interface.html}{array interface},
1112 return a \cross{CvMat} (\texttt{allowND = False}) or \cross{CvMatND} (\texttt{allowND = True}).
1113
1114 If \texttt{allowND = False}, then the object's array must be either 2D or 3D.  If it is 2D, then the returned CvMat has a single channel.  If it is 3D, then the returned CvMat will have N channels, where N is the last dimension of the array. In this case, N cannot be greater than OpenCV's channel limit, \texttt{CV\_CN\_MAX}.
1115
1116 If \texttt{allowND = True}, then \texttt{fromarray} returns a single-channel \cross{CvMatND} with the same shape as the original array.
1117
1118 For example, NumPy arrays support the array interface, so can be converted to OpenCV objects:
1119
1120 \begin{lstlisting}
1121 >>> import cv, numpy
1122 >>> a = numpy.ones((480, 640))
1123 >>> mat = cv.fromarray(a)
1124 >>> print cv.GetDims(mat), cv.CV_MAT_CN(cv.GetElemType(mat))
1125 (480, 640) 1
1126 >>> a = numpy.ones((480, 640, 3))
1127 >>> mat = cv.fromarray(a)
1128 >>> print cv.GetDims(mat), cv.CV_MAT_CN(cv.GetElemType(mat))
1129 (480, 640) 3
1130 >>> a = numpy.ones((480, 640, 3))
1131 >>> mat = cv.fromarray(a, allowND = True)
1132 >>> print cv.GetDims(mat), cv.CV_MAT_CN(cv.GetElemType(mat))
1133 (480, 640, 3) 1
1134 \end{lstlisting}
1135
1136 \fi
1137
1138 \cvCPyFunc{GEMM}
1139 Performs generalized matrix multiplication.
1140
1141 \cvdefC{void cvGEMM(\par const CvArr* src1, \par const CvArr* src2, double alpha,
1142               \par const CvArr* src3, \par double beta, \par CvArr* dst, \par int tABC=0);\newline
1143 \#define cvMatMulAdd(src1, src2, src3, dst ) cvGEMM(src1, src2, 1, src3, 1, dst, 0 )\par
1144 \#define cvMatMul(src1, src2, dst ) cvMatMulAdd(src1, src2, 0, dst )}
1145               
1146 \cvdefPy{GEMM(src1,src2,alphs,src3,beta,dst,tABC=0)-> None}
1147
1148 \begin{description}
1149 \cvarg{src1}{The first source array}
1150 \cvarg{src2}{The second source array}
1151 \cvarg{src3}{The third source array (shift). Can be NULL, if there is no shift.}
1152 \cvarg{dst}{The destination array}
1153 \cvarg{tABC}{The operation flags that can be 0 or a combination of the following values
1154 \begin{description}
1155 \cvarg{CV\_GEMM\_A\_T}{transpose src1}
1156 \cvarg{CV\_GEMM\_B\_T}{transpose src2}
1157 \cvarg{CV\_GEMM\_C\_T}{transpose src3}
1158 \end{description}
1159
1160 For example, \texttt{CV\_GEMM\_A\_T+CV\_GEMM\_C\_T} corresponds to
1161 \[
1162 \texttt{alpha} \, \texttt{src1} ^T \, \texttt{src2} + \texttt{beta} \, \texttt{src3} ^T
1163 \]}
1164 \end{description}
1165
1166 The function performs generalized matrix multiplication:
1167
1168 \[
1169 \texttt{dst} = \texttt{alpha} \, op(\texttt{src1}) \, op(\texttt{src2}) + \texttt{beta} \, op(\texttt{src3}) \quad \text{where $op(X)$ is $X$ or $X^T$}
1170 \]
1171
1172 All the matrices should have the same data type and coordinated sizes. Real or complex floating-point matrices are supported.
1173
1174 \ifC  % {
1175
1176 \cvCPyFunc{Get?D}
1177 Return a specific array element.
1178
1179 \cvdefC{
1180 CvScalar cvGet1D(const CvArr* arr, int idx0);
1181 CvScalar cvGet2D(const CvArr* arr, int idx0, int idx1);
1182 CvScalar cvGet3D(const CvArr* arr, int idx0, int idx1, int idx2);
1183 CvScalar cvGetND(const CvArr* arr, int* idx);
1184 }
1185
1186 \begin{description}
1187 \cvarg{arr}{Input array}
1188 \cvarg{idx0}{The first zero-based component of the element index}
1189 \cvarg{idx1}{The second zero-based component of the element index}
1190 \cvarg{idx2}{The third zero-based component of the element index}
1191 \cvarg{idx}{Array of the element indices}
1192 \end{description}
1193
1194 The functions return a specific array element. In the case of a sparse array the functions return 0 if the requested node does not exist (no new node is created by the functions).
1195 \else % }{
1196
1197 \cvCPyFunc{Get1D}
1198 Return a specific array element.
1199
1200 \cvdefPy{Get1D(arr, idx) -> scalar}
1201
1202 \begin{description}
1203 \cvarg{arr}{Input array}
1204 \cvarg{idx}{Zero-based element index}
1205 \end{description}
1206
1207 Return a specific array element.  Array must have dimension 3.
1208
1209 \cvCPyFunc{Get2D}
1210 Return a specific array element.
1211
1212 \cvdefPy{ Get2D(arr, idx0, idx1) -> scalar }
1213
1214 \begin{description}
1215 \cvarg{arr}{Input array}
1216 \cvarg{idx0}{Zero-based element row index}
1217 \cvarg{idx1}{Zero-based element column index}
1218 \end{description}
1219
1220 Return a specific array element.  Array must have dimension 2.
1221
1222 \cvCPyFunc{Get3D}
1223 Return a specific array element.
1224
1225 \cvdefPy{ Get3D(arr, idx0, idx1, idx2) -> scalar }
1226
1227 \begin{description}
1228 \cvarg{arr}{Input array}
1229 \cvarg{idx0}{Zero-based element index}
1230 \cvarg{idx1}{Zero-based element index}
1231 \cvarg{idx2}{Zero-based element index}
1232 \end{description}
1233
1234 Return a specific array element.  Array must have dimension 3.
1235
1236 \cvCPyFunc{GetND}
1237 Return a specific array element.
1238
1239 \cvdefPy{ GetND(arr, indices) -> scalar }
1240
1241 \begin{description}
1242 \cvarg{arr}{Input array}
1243 \cvarg{indices}{List of zero-based element indices}
1244 \end{description}
1245
1246 Return a specific array element.  The length of array indices must be the same as the dimension of the array.
1247
1248 \fi % }
1249
1250 \ifC % {
1251 \cvCPyFunc{GetCol(s)}
1252 Returns array column or column span.
1253
1254 \cvdefC{CvMat* cvGetCol(const CvArr* arr, CvMat* submat, int col);}
1255 \cvdefPy{GetCol(arr,row)-> submat}
1256 \cvdefC{CvMat* cvGetCols(const CvArr* arr, CvMat* submat, int startCol, int endCol);}
1257 \cvdefPy{GetCols(arr,startCol,endCol)-> submat}
1258
1259 \begin{description}
1260 \cvarg{arr}{Input array}
1261 \cvarg{submat}{Pointer to the resulting sub-array header}
1262 \cvarg{col}{Zero-based index of the selected column}
1263 \cvarg{startCol}{Zero-based index of the starting column (inclusive) of the span}
1264 \cvarg{endCol}{Zero-based index of the ending column (exclusive) of the span}
1265 \end{description}
1266
1267 The functions \texttt{GetCol} and \texttt{GetCols} return the header, corresponding to a specified column span of the input array. \texttt{GetCol} is a shortcut for \cvCPyCross{GetCols}:
1268
1269 \begin{lstlisting}
1270 cvGetCol(arr, submat, col); // ~ cvGetCols(arr, submat, col, col + 1);
1271 \end{lstlisting}
1272
1273 \else % }{
1274
1275 \cvCPyFunc{GetCol}
1276 Returns array column.
1277
1278 \cvdefPy{GetCol(arr,col)-> submat}
1279
1280 \begin{description}
1281 \cvarg{arr}{Input array}
1282 \cvarg{col}{Zero-based index of the selected column}
1283 \cvarg{submat}{resulting single-column array}
1284 \end{description}
1285
1286 The function \texttt{GetCol} returns a single column from the input array.
1287
1288 \cvCPyFunc{GetCols}
1289 Returns array column span.
1290
1291 \cvdefPy{GetCols(arr,startCol,endCol)-> submat}
1292
1293 \begin{description}
1294 \cvarg{arr}{Input array}
1295 \cvarg{startCol}{Zero-based index of the starting column (inclusive) of the span}
1296 \cvarg{endCol}{Zero-based index of the ending column (exclusive) of the span}
1297 \cvarg{submat}{resulting multi-column array}
1298 \end{description}
1299
1300 The function \texttt{GetCols} returns a column span from the input array.
1301
1302 \fi % }
1303
1304 \cvCPyFunc{GetDiag}
1305 Returns one of array diagonals.
1306
1307 \cvdefC{CvMat* cvGetDiag(const CvArr* arr, CvMat* submat, int diag=0);}
1308 \cvdefPy{GetDiag(arr,diag=0)-> submat}
1309
1310 \begin{description}
1311 \cvarg{arr}{Input array}
1312 \cvarg{submat}{Pointer to the resulting sub-array header}
1313 \cvarg{diag}{Array diagonal. Zero corresponds to the main diagonal, -1 corresponds to the diagonal above the main , 1 corresponds to the diagonal below the main, and so forth.}
1314 \end{description}
1315
1316 The function returns the header, corresponding to a specified diagonal of the input array.
1317
1318 \ifC
1319 \subsection{cvGetDims, cvGetDimSize}\label{cvGetDims}
1320
1321 Return number of array dimensions and their sizes or the size of a particular dimension.
1322
1323 \cvdefC{int cvGetDims(const CvArr* arr, int* sizes=NULL);}
1324 \cvdefC{int cvGetDimSize(const CvArr* arr, int index);}
1325
1326 \begin{description}
1327 \cvarg{arr}{Input array}
1328 \cvarg{sizes}{Optional output vector of the array dimension sizes. For
1329 2d arrays the number of rows (height) goes first, number of columns
1330 (width) next.}
1331 \cvarg{index}{Zero-based dimension index (for matrices 0 means number
1332 of rows, 1 means number of columns; for images 0 means height, 1 means
1333 width)}
1334 \end{description}
1335
1336 The function \texttt{cvGetDims} returns the array dimensionality and the
1337 array of dimension sizes. In the case of \texttt{IplImage} or \cross{CvMat} it always
1338 returns 2 regardless of number of image/matrix rows. The function
1339 \texttt{cvGetDimSize} returns the particular dimension size (number of
1340 elements per that dimension). For example, the following code calculates
1341 total number of array elements in two ways:
1342
1343 \begin{lstlisting}
1344 // via cvGetDims()
1345 int sizes[CV_MAX_DIM];
1346 int i, total = 1;
1347 int dims = cvGetDims(arr, size);
1348 for(i = 0; i < dims; i++ )
1349     total *= sizes[i];
1350
1351 // via cvGetDims() and cvGetDimSize()
1352 int i, total = 1;
1353 int dims = cvGetDims(arr);
1354 for(i = 0; i < dims; i++ )
1355     total *= cvGetDimsSize(arr, i);
1356 \end{lstlisting}
1357 \fi
1358
1359 \ifPy
1360 \cvCPyFunc{GetDims}
1361 Returns list of array dimensions
1362
1363 \cvdefPy{GetDims(arr)-> list}
1364
1365 \begin{description}
1366 \cvarg{arr}{Input array}
1367 \end{description}
1368
1369 The function returns a list of array dimensions.
1370 In the case of \texttt{IplImage} or \cross{CvMat} it always
1371 returns a list of length 2.
1372 \fi
1373
1374
1375 \cvCPyFunc{GetElemType}
1376 Returns type of array elements.
1377
1378 \cvdefC{int cvGetElemType(const CvArr* arr);}
1379 \cvdefPy{GetElemType(arr)-> int}
1380
1381 \begin{description}
1382 \cvarg{arr}{Input array}
1383 \end{description}
1384
1385 The function returns type of the array elements
1386 as described in \cvCPyCross{CreateMat} discussion: \texttt{CV\_8UC1} ... \texttt{CV\_64FC4}.
1387
1388
1389 \cvCPyFunc{GetImage}
1390 Returns image header for arbitrary array.
1391
1392 \cvdefC{IplImage* cvGetImage(const CvArr* arr, IplImage* imageHeader);}
1393 \cvdefPy{GetImage(arr) -> iplimage}
1394
1395 \begin{description}
1396 \cvarg{arr}{Input array}
1397 \ifC
1398 \cvarg{imageHeader}{Pointer to \texttt{IplImage} structure used as a temporary buffer}
1399 \fi
1400 \end{description}
1401
1402 The function returns the image header for the input array
1403 that can be a matrix - \cross{CvMat}, or an image - \texttt{IplImage*}. In
1404 the case of an image the function simply returns the input pointer. In the
1405 case of \cross{CvMat} it initializes an \texttt{imageHeader} structure
1406 with the parameters of the input matrix. Note that if we transform
1407 \texttt{IplImage} to \cross{CvMat} and then transform CvMat back to
1408 IplImage, we can get different headers if the ROI is set, and thus some
1409 IPL functions that calculate image stride from its width and align may
1410 fail on the resultant image.
1411
1412 \cvCPyFunc{GetImageCOI}
1413 Returns the index of the channel of interest. 
1414
1415 \cvdefC{int cvGetImageCOI(const IplImage* image);}
1416 \cvdefPy{GetImageCOI(image)-> channel}
1417
1418 \begin{description}
1419 \cvarg{image}{A pointer to the image header}
1420 \end{description}
1421
1422 Returns the channel of interest of in an IplImage. Returned values correspond to the \texttt{coi} in \cvCPyCross{SetImageCOI}.
1423
1424 \cvCPyFunc{GetImageROI}
1425 Returns the image ROI.
1426
1427 \cvdefC{CvRect cvGetImageROI(const IplImage* image);}
1428 \cvdefPy{GetImageROI(image)-> CvRect}
1429
1430 \begin{description}
1431 \cvarg{image}{A pointer to the image header}
1432 \end{description}
1433
1434 If there is no ROI set, \texttt{cvRect(0,0,image->width,image->height)} is returned.
1435
1436 \cvCPyFunc{GetMat}
1437 Returns matrix header for arbitrary array.
1438
1439 \cvdefC{CvMat* cvGetMat(const CvArr* arr, CvMat* header, int* coi=NULL, int allowND=0);}
1440 \cvdefPy{GetMat(arr) -> cvmat }
1441
1442 \begin{description}
1443 \cvarg{arr}{Input array}
1444 \ifC
1445 \cvarg{header}{Pointer to \cross{CvMat} structure used as a temporary buffer}
1446 \cvarg{coi}{Optional output parameter for storing COI}
1447 \cvarg{allowND}{If non-zero, the function accepts multi-dimensional dense arrays (CvMatND*) and returns 2D (if CvMatND has two dimensions) or 1D matrix (when CvMatND has 1 dimension or more than 2 dimensions). The array must be continuous.}
1448 \fi
1449 \end{description}
1450
1451 The function returns a matrix header for the input array that can be a matrix - 
1452
1453 \cross{CvMat}, an image - \texttt{IplImage} or a multi-dimensional dense array - \cross{CvMatND} (latter case is allowed only if \texttt{allowND != 0}) . In the case of matrix the function simply returns the input pointer. In the case of \texttt{IplImage*} or \cross{CvMatND} it initializes the \texttt{header} structure with parameters of the current image ROI and returns the pointer to this temporary structure. Because COI is not supported by \cross{CvMat}, it is returned separately.
1454
1455 The function provides an easy way to handle both types of arrays - \texttt{IplImage} and \cross{CvMat} - using the same code. Reverse transform from \cross{CvMat} to \texttt{IplImage} can be done using the \cvCPyCross{GetImage} function.
1456
1457 Input array must have underlying data allocated or attached, otherwise the function fails.
1458
1459 If the input array is \texttt{IplImage} with planar data layout and COI set, the function returns the pointer to the selected plane and COI = 0. It enables per-plane processing of multi-channel images with planar data layout using OpenCV functions.
1460
1461 \ifC
1462 \cvCPyFunc{GetNextSparseNode} 
1463 Returns the next sparse matrix element
1464
1465 \cvdefC{CvSparseNode* cvGetNextSparseNode(CvSparseMatIterator* matIterator);}
1466
1467 \begin{description}
1468 \cvarg{matIterator}{Sparse array iterator}
1469 \end{description}
1470
1471
1472 The function moves iterator to the next sparse matrix element and returns pointer to it. In the current version there is no any particular order of the elements, because they are stored in the hash table. The sample below demonstrates how to iterate through the sparse matrix:
1473
1474 Using \cvCPyCross{InitSparseMatIterator} and \cvCPyCross{GetNextSparseNode} to calculate sum of floating-point sparse array.
1475
1476 \begin{lstlisting}
1477 double sum;
1478 int i, dims = cvGetDims(array);
1479 CvSparseMatIterator mat_iterator;
1480 CvSparseNode* node = cvInitSparseMatIterator(array, &mat_iterator);
1481
1482 for(; node != 0; node = cvGetNextSparseNode(&mat_iterator ))
1483 {
1484     /* get pointer to the element indices */
1485     int* idx = CV_NODE_IDX(array, node);
1486     /* get value of the element (assume that the type is CV_32FC1) */
1487     float val = *(float*)CV_NODE_VAL(array, node);
1488     printf("(");
1489     for(i = 0; i < dims; i++ )
1490         printf("%4d%s", idx[i], i < dims - 1 "," : "): ");
1491     printf("%g\n", val);
1492
1493     sum += val;
1494 }
1495
1496 printf("\nTotal sum = %g\n", sum);
1497 \end{lstlisting}
1498
1499 \fi
1500
1501 \cvCPyFunc{GetOptimalDFTSize}
1502 Returns optimal DFT size for a given vector size.
1503
1504 \cvdefC{int cvGetOptimalDFTSize(int size0);}
1505 \cvdefPy{GetOptimalDFTSize(size0)-> int}
1506
1507 \begin{description}
1508 \cvarg{size0}{Vector size}
1509 \end{description}
1510
1511 The function returns the minimum number
1512 \texttt{N} that is greater than or equal to \texttt{size0}, such that the DFT
1513 of a vector of size \texttt{N} can be computed fast. In the current
1514 implementation $N=2^p \times 3^q \times 5^r$, for some $p$, $q$, $r$.
1515
1516 The function returns a negative number if \texttt{size0} is too large
1517 (very close to \texttt{INT\_MAX})
1518
1519
1520 \ifC
1521 \cvCPyFunc{GetRawData}
1522 Retrieves low-level information about the array.
1523
1524 \cvdefC{void cvGetRawData(const CvArr* arr, uchar** data,
1525                    int* step=NULL, CvSize* roiSize=NULL);}
1526
1527 \begin{description}
1528 \cvarg{arr}{Array header}
1529 \cvarg{data}{Output pointer to the whole image origin or ROI origin if ROI is set}
1530 \cvarg{step}{Output full row length in bytes}
1531 \cvarg{roiSize}{Output ROI size}
1532 \end{description}
1533
1534 The function fills output variables with low-level information about the array data. All output parameters are optional, so some of the pointers may be set to \texttt{NULL}. If the array is \texttt{IplImage} with ROI set, the parameters of ROI are returned.
1535
1536 The following example shows how to get access to array elements. GetRawData calculates the absolute value of the elements in a single-channel, floating-point array.
1537
1538 \begin{lstlisting}
1539 float* data;
1540 int step;
1541
1542 CvSize size;
1543 int x, y;
1544
1545 cvGetRawData(array, (uchar**)&data, &step, &size);
1546 step /= sizeof(data[0]);
1547
1548 for(y = 0; y < size.height; y++, data += step )
1549     for(x = 0; x < size.width; x++ )
1550         data[x] = (float)fabs(data[x]);
1551
1552 \end{lstlisting}
1553
1554 \cvCPyFunc{GetReal?D}
1555 Return a specific element of single-channel array.
1556
1557 \cvdefC{
1558 double cvGetReal1D(const CvArr* arr, int idx0); \newline
1559 double cvGetReal2D(const CvArr* arr, int idx0, int idx1); \newline
1560 double cvGetReal3D(const CvArr* arr, int idx0, int idx1, int idx2); \newline
1561 double cvGetRealND(const CvArr* arr, int* idx);
1562 }
1563
1564 \begin{description}
1565 \cvarg{arr}{Input array. Must have a single channel.}
1566 \cvarg{idx0}{The first zero-based component of the element index}
1567 \cvarg{idx1}{The second zero-based component of the element index}
1568 \cvarg{idx2}{The third zero-based component of the element index}
1569 \cvarg{idx}{Array of the element indices}
1570 \end{description}
1571
1572
1573 The functions \texttt{cvGetReal*D} return a specific element of a single-channel array. If the array has multiple channels, a runtime error is raised. Note that \cvCPyCross{Get} function can be used safely for both single-channel and multiple-channel arrays though they are a bit slower.
1574
1575 In the case of a sparse array the functions return 0 if the requested node does not exist (no new node is created by the functions).
1576
1577 \fi
1578
1579 \ifC %{
1580 \cvCPyFunc{GetRow(s)}
1581 Returns array row or row span.
1582
1583 \cvdefC{CvMat* cvGetRow(const CvArr* arr, CvMat* submat, int row);}
1584 \cvdefPy{GetRow(arr,row)-> submat}
1585 \cvdefC{CvMat* cvGetRows(const CvArr* arr, CvMat* submat, int startRow, int endRow, int deltaRow=1);}
1586 \cvdefPy{GetRows(arr,startRow,endRow,deltaRow=1)-> submat}
1587
1588 \begin{description}
1589 \cvarg{arr}{Input array}
1590 \cvarg{submat}{Pointer to the resulting sub-array header}
1591 \cvarg{row}{Zero-based index of the selected row}
1592 \cvarg{startRow}{Zero-based index of the starting row (inclusive) of the span}
1593 \cvarg{endRow}{Zero-based index of the ending row (exclusive) of the span}
1594 \cvarg{deltaRow}{Index step in the row span. That is, the function extracts every \texttt{deltaRow}-th row from \texttt{startRow} and up to (but not including) \texttt{endRow}.}
1595 \end{description}
1596
1597 The functions return the header, corresponding to a specified row/row span of the input array. Note that \texttt{GetRow} is a shortcut for \cvCPyCross{GetRows}:
1598
1599 \begin{lstlisting}
1600 cvGetRow(arr, submat, row ) ~ cvGetRows(arr, submat, row, row + 1, 1);
1601 \end{lstlisting}
1602
1603 \else % }{
1604
1605 \cvCPyFunc{GetRow}
1606 Returns array row.
1607
1608 \cvdefPy{GetRow(arr,row)-> submat}
1609
1610 \begin{description}
1611 \cvarg{arr}{Input array}
1612 \cvarg{row}{Zero-based index of the selected row}
1613 \cvarg{submat}{resulting single-row array}
1614 \end{description}
1615
1616 The function \texttt{GetRow} returns a single row from the input array.
1617
1618 \cvCPyFunc{GetRows}
1619 Returns array row span.
1620
1621 \cvdefPy{GetRows(arr,startRow,endRow,deltaRow=1)-> submat}
1622
1623 \begin{description}
1624 \cvarg{arr}{Input array}
1625 \cvarg{startRow}{Zero-based index of the starting row (inclusive) of the span}
1626 \cvarg{endRow}{Zero-based index of the ending row (exclusive) of the span}
1627 \cvarg{deltaRow}{Index step in the row span.}
1628 \cvarg{submat}{resulting multi-row array}
1629 \end{description}
1630
1631 The function \texttt{GetRows} returns a row span from the input array.
1632
1633 \fi % }
1634
1635 \cvCPyFunc{GetSize}
1636 Returns size of matrix or image ROI.
1637
1638 \cvdefC{CvSize cvGetSize(const CvArr* arr);}
1639 \cvdefPy{GetSize(arr)-> CvSize}
1640
1641 \begin{description}
1642 \cvarg{arr}{array header}
1643 \end{description}
1644
1645 The function returns number of rows (CvSize::height) and number of columns (CvSize::width) of the input matrix or image. In the case of image the size of ROI is returned.
1646
1647
1648 \cvCPyFunc{GetSubRect}
1649 Returns matrix header corresponding to the rectangular sub-array of input image or matrix.
1650
1651 \cvdefC{CvMat* cvGetSubRect(const CvArr* arr, CvMat* submat, CvRect rect);}
1652 \cvdefPy{GetSubRect(arr, rect) -> cvmat}
1653
1654 \begin{description}
1655 \cvarg{arr}{Input array}
1656 \ifC
1657 \cvarg{submat}{Pointer to the resultant sub-array header}
1658 \fi
1659 \cvarg{rect}{Zero-based coordinates of the rectangle of interest}
1660 \end{description}
1661
1662 The function returns header, corresponding to
1663 a specified rectangle of the input array. In other words, it allows
1664 the user to treat a rectangular part of input array as a stand-alone
1665 array. ROI is taken into account by the function so the sub-array of
1666 ROI is actually extracted.
1667
1668 \cvCPyFunc{InRange}
1669 Checks that array elements lie between the elements of two other arrays.
1670
1671 \cvdefC{void cvInRange(const CvArr* src, const CvArr* lower, const CvArr* upper, CvArr* dst);}
1672 \cvdefPy{InRange(src,lower,upper,dst)-> None}
1673
1674 \begin{description}
1675 \cvarg{src}{The first source array}
1676 \cvarg{lower}{The inclusive lower boundary array}
1677 \cvarg{upper}{The exclusive upper boundary array}
1678 \cvarg{dst}{The destination array, must have 8u or 8s type}
1679 \end{description}
1680
1681
1682 The function does the range check for every element of the input array:
1683
1684 \[
1685 \texttt{dst}(I)=\texttt{lower}(I)_0 <= \texttt{src}(I)_0 < \texttt{upper}(I)_0
1686 \]
1687
1688 For single-channel arrays,
1689
1690 \[
1691 \texttt{dst}(I)=
1692 \texttt{lower}(I)_0 <= \texttt{src}(I)_0 < \texttt{upper}(I)_0 \land
1693 \texttt{lower}(I)_1 <= \texttt{src}(I)_1 < \texttt{upper}(I)_1
1694 \]
1695
1696 For two-channel arrays and so forth,
1697
1698 dst(I) is set to 0xff (all \texttt{1}-bits) if src(I) is within the range and 0 otherwise. All the arrays must have the same type, except the destination, and the same size (or ROI size).
1699
1700
1701 \cvCPyFunc{InRangeS}
1702 Checks that array elements lie between two scalars.
1703
1704 \cvdefC{void cvInRangeS(const CvArr* src, CvScalar lower, CvScalar upper, CvArr* dst);}
1705 \cvdefPy{InRangeS(src,lower,upper,dst)-> None}
1706
1707 \begin{description}
1708 \cvarg{src}{The first source array}
1709 \cvarg{lower}{The inclusive lower boundary}
1710 \cvarg{upper}{The exclusive upper boundary}
1711 \cvarg{dst}{The destination array, must have 8u or 8s type}
1712 \end{description}
1713
1714
1715 The function does the range check for every element of the input array:
1716
1717 \[
1718 \texttt{dst}(I)=\texttt{lower}_0 <= \texttt{src}(I)_0 < \texttt{upper}_0
1719 \]
1720
1721 For single-channel arrays,
1722
1723 \[
1724 \texttt{dst}(I)=
1725 \texttt{lower}_0 <= \texttt{src}(I)_0 < \texttt{upper}_0 \land
1726 \texttt{lower}_1 <= \texttt{src}(I)_1 < \texttt{upper}_1
1727 \]
1728
1729 For two-channel arrays nd so forth,
1730
1731 'dst(I)' is set to 0xff (all \texttt{1}-bits) if 'src(I)' is within the range and 0 otherwise. All the arrays must have the same size (or ROI size).
1732
1733 \ifC
1734 \cvCPyFunc{IncRefData}
1735 Increments array data reference counter.
1736
1737 \cvdefC{int cvIncRefData(CvArr* arr);}
1738
1739 \begin{description}
1740 \cvarg{arr}{Array header}
1741 \end{description}
1742
1743 The function increments \cross{CvMat} or
1744 \cross{CvMatND} data reference counter and returns the new counter value
1745 if the reference counter pointer is not NULL, otherwise it returns zero.
1746
1747 \cvCPyFunc{InitImageHeader}
1748 Initializes an image header that was previously allocated.
1749
1750 \cvdefC{IplImage* cvInitImageHeader(\par IplImage* image,\par CvSize size,\par int depth,\par int channels,\par int origin=0,\par int align=4);}
1751
1752 \begin{description}
1753 \cvarg{image}{Image header to initialize}
1754 \cvarg{size}{Image width and height}
1755 \cvarg{depth}{Image depth (see \cvCPyCross{CreateImage})}
1756 \cvarg{channels}{Number of channels (see \cvCPyCross{CreateImage})}
1757 \cvarg{origin}{Top-left \texttt{IPL\_ORIGIN\_TL} or bottom-left \texttt{IPL\_ORIGIN\_BL}}
1758 \cvarg{align}{Alignment for image rows, typically 4 or 8 bytes}
1759 \end{description}
1760
1761 The returned \texttt{IplImage*} points to the initialized header.
1762
1763 \cvCPyFunc{InitMatHeader}
1764 Initializes a pre-allocated matrix header.
1765
1766 \cvdefC{
1767 CvMat* cvInitMatHeader(\par CvMat* mat,\par int rows,\par int cols,\par int type, \par void* data=NULL,\par int step=CV\_AUTOSTEP);
1768 }
1769
1770 \begin{description}
1771 \cvarg{mat}{A pointer to the matrix header to be initialized}
1772 \cvarg{rows}{Number of rows in the matrix}
1773 \cvarg{cols}{Number of columns in the matrix}
1774 \cvarg{type}{Type of the matrix elements, see \cvCPyCross{CreateMat}.}
1775 \cvarg{data}{Optional: data pointer assigned to the matrix header}
1776 \cvarg{step}{Optional: full row width in bytes of the assigned data. By default, the minimal possible step is used which assumes there are no gaps between subsequent rows of the matrix.}
1777 \end{description}
1778
1779 This function is often used to process raw data with OpenCV matrix functions. For example, the following code computes the matrix product of two matrices, stored as ordinary arrays:
1780
1781 \begin{lstlisting}
1782 double a[] = { 1, 2, 3, 4,
1783                5, 6, 7, 8,
1784                9, 10, 11, 12 };
1785
1786 double b[] = { 1, 5, 9,
1787                2, 6, 10,
1788                3, 7, 11,
1789                4, 8, 12 };
1790
1791 double c[9];
1792 CvMat Ma, Mb, Mc ;
1793
1794 cvInitMatHeader(&Ma, 3, 4, CV_64FC1, a);
1795 cvInitMatHeader(&Mb, 4, 3, CV_64FC1, b);
1796 cvInitMatHeader(&Mc, 3, 3, CV_64FC1, c);
1797
1798 cvMatMulAdd(&Ma, &Mb, 0, &Mc);
1799 // the c array now contains the product of a (3x4) and b (4x3)
1800
1801 \end{lstlisting}
1802
1803 \cvCPyFunc{InitMatNDHeader}
1804 Initializes a pre-allocated multi-dimensional array header.
1805
1806 \cvdefC{CvMatND* cvInitMatNDHeader(\par CvMatND* mat,\par int dims,\par const int* sizes,\par int type,\par void* data=NULL);}
1807
1808 \begin{description}
1809 \cvarg{mat}{A pointer to the array header to be initialized}
1810 \cvarg{dims}{The number of array dimensions}
1811 \cvarg{sizes}{An array of dimension sizes}
1812 \cvarg{type}{Type of array elements, see \cvCPyCross{CreateMat}}
1813 \cvarg{data}{Optional data pointer assigned to the matrix header}
1814 \end{description}
1815
1816 \cvCPyFunc{InitSparseMatIterator}
1817 Initializes sparse array elements iterator.
1818
1819 \cvdefC{CvSparseNode* cvInitSparseMatIterator(const CvSparseMat* mat,
1820                                        CvSparseMatIterator* matIterator);}
1821
1822 \begin{description}
1823 \cvarg{mat}{Input array}
1824 \cvarg{matIterator}{Initialized iterator}
1825 \end{description}
1826
1827 The function initializes iterator of
1828 sparse array elements and returns pointer to the first element, or NULL
1829 if the array is empty.
1830
1831 \fi
1832
1833 \cvCPyFunc{InvSqrt}
1834 Calculates the inverse square root.
1835
1836 \cvdefC{float cvInvSqrt(float value);}
1837 \cvdefPy{InvSqrt(value)-> float}
1838
1839 \begin{description}
1840 \cvarg{value}{The input floating-point value}
1841 \end{description}
1842
1843
1844 The function calculates the inverse square root of the argument, and normally it is faster than \texttt{1./sqrt(value)}. If the argument is zero or negative, the result is not determined. Special values ($\pm \infty $ , NaN) are not handled.
1845
1846 \cvCPyFunc{Invert}
1847 Finds the inverse or pseudo-inverse of a matrix.
1848
1849 \cvdefC{double cvInvert(const CvArr* src, CvArr* dst, int method=CV\_LU);}
1850 \cvdefPy{Invert(src,dst,method=CV\_LU)-> double}
1851
1852 \begin{description}
1853 \cvarg{src}{The source matrix}
1854 \cvarg{dst}{The destination matrix}
1855 \cvarg{method}{Inversion method
1856 \begin{description}
1857  \cvarg{CV\_LU}{Gaussian elimination with optimal pivot element chosen}
1858  \cvarg{CV\_SVD}{Singular value decomposition (SVD) method}
1859  \cvarg{CV\_SVD\_SYM}{SVD method for a symmetric positively-defined matrix}
1860 \end{description}}
1861 \end{description}
1862
1863 \begin{lstlisting}
1864 #define cvInv cvInvert
1865 \end{lstlisting}
1866
1867 The function inverts matrix \texttt{src1} and stores the result in \texttt{src2}.
1868
1869 In the case of \texttt{LU} method, the function returns the \texttt{src1} determinant (src1 must be square). If it is 0, the matrix is not inverted and \texttt{src2} is filled with zeros.
1870
1871 In the case of \texttt{SVD} methods, the function returns the inversed condition of \texttt{src1} (ratio of the smallest singular value to the largest singular value) and 0 if \texttt{src1} is all zeros. The SVD methods calculate a pseudo-inverse matrix if \texttt{src1} is singular.
1872
1873
1874 \cvCPyFunc{IsInf}
1875 Determines if the argument is Infinity.
1876
1877 \cvdefC{int cvIsInf(double value);}
1878 \cvdefPy{IsInf(value)-> int}
1879
1880 \begin{description}
1881 \cvarg{value}{The input floating-point value}
1882 \end{description}
1883
1884 The function returns 1 if the argument is $\pm \infty $ (as defined by IEEE754 standard), 0 otherwise.
1885
1886 \cvCPyFunc{IsNaN}
1887 Determines if the argument is Not A Number.
1888
1889 \cvdefC{int cvIsNaN(double value);}
1890 \cvdefPy{IsNaN(value)-> int}
1891
1892 \begin{description}
1893 \cvarg{value}{The input floating-point value}
1894 \end{description}
1895
1896 The function returns 1 if the argument is Not A Number (as defined by IEEE754 standard), 0 otherwise.
1897
1898
1899 \cvCPyFunc{LUT}
1900 Performs a look-up table transform of an array.
1901
1902 \cvdefC{void cvLUT(const CvArr* src, CvArr* dst, const CvArr* lut);}
1903 \cvdefPy{LUT(src,dst,lut)-> None}
1904
1905 \begin{description}
1906 \cvarg{src}{Source array of 8-bit elements}
1907 \cvarg{dst}{Destination array of a given depth and of the same number of channels as the source array}
1908 \cvarg{lut}{Look-up table of 256 elements; should have the same depth as the destination array. In the case of multi-channel source and destination arrays, the table should either have a single-channel (in this case the same table is used for all channels) or the same number of channels as the source/destination array.}
1909 \end{description}
1910
1911 The function fills the destination array with values from the look-up table. Indices of the entries are taken from the source array. That is, the function processes each element of \texttt{src} as follows:
1912
1913 \[
1914 \texttt{dst}_i \leftarrow \texttt{lut}_{\texttt{src}_i + d}
1915 \]
1916
1917 where
1918
1919 \[
1920 d = \fork
1921 {0}{if \texttt{src} has depth \texttt{CV\_8U}}
1922 {128}{if \texttt{src} has depth \texttt{CV\_8S}}
1923 \]
1924
1925 \cvCPyFunc{Log}
1926 Calculates the natural logarithm of every array element's absolute value.
1927
1928 \cvdefC{void cvLog(const CvArr* src, CvArr* dst);}
1929 \cvdefPy{Log(src,dst)-> None}
1930
1931 \begin{description}
1932 \cvarg{src}{The source array}
1933 \cvarg{dst}{The destination array, it should have \texttt{double} type or the same type as the source}
1934 \end{description}
1935
1936 The function calculates the natural logarithm of the absolute value of every element of the input array:
1937
1938 \[
1939 \texttt{dst} [I] = \fork
1940 {\log{|\texttt{src}(I)}}{if $\texttt{src}[I] \ne 0$ }
1941 {\texttt{C}}{otherwise}
1942 \]
1943
1944 Where \texttt{C} is a large negative number (about -700 in the current implementation).
1945
1946 \cvCPyFunc{Mahalonobis}
1947 Calculates the Mahalonobis distance between two vectors.
1948
1949 \cvdefC{double cvMahalanobis(\par const CvArr* vec1,\par const CvArr* vec2,\par CvArr* mat);}
1950 \cvdefPy{Mahalonobis(vec1,vec2,mat)-> None}
1951
1952 \begin{description}
1953 \cvarg{vec1}{The first 1D source vector}
1954 \cvarg{vec2}{The second 1D source vector}
1955 \cvarg{mat}{The inverse covariance matrix}
1956 \end{description}
1957
1958
1959 The function calculates and returns the weighted distance between two vectors:
1960
1961 \[
1962 d(\texttt{vec1},\texttt{vec2})=\sqrt{\sum_{i,j}{\texttt{icovar(i,j)}\cdot(\texttt{vec1}(I)-\texttt{vec2}(I))\cdot(\texttt{vec1(j)}-\texttt{vec2(j)})}}
1963 \]
1964
1965 The covariance matrix may be calculated using the \cvCPyCross{CalcCovarMatrix} function and further inverted using the \cvCPyCross{Invert} function (CV\_SVD method is the prefered one because the matrix might be singular).
1966
1967
1968 \ifC
1969 \cvCPyFunc{Mat}
1970 Initializes matrix header (lightweight variant).
1971
1972 \cvdefC{CvMat cvMat(\par int rows,\par int cols,\par int type,\par void* data=NULL);}
1973
1974 \begin{description}
1975 \cvarg{rows}{Number of rows in the matrix}
1976 \cvarg{cols}{Number of columns in the matrix}
1977 \cvarg{type}{Type of the matrix elements - see \cvCPyCross{CreateMat}}
1978 \cvarg{data}{Optional data pointer assigned to the matrix header}
1979 \end{description}
1980
1981 Initializes a matrix header and assigns data to it. The matrix is filled \textit{row}-wise (the first \texttt{cols} elements of data form the first row of the matrix, etc.)
1982
1983 This function is a fast inline substitution for \cvCPyCross{InitMatHeader}. Namely, it is equivalent to:
1984
1985 \begin{lstlisting}
1986 CvMat mat;
1987 cvInitMatHeader(&mat, rows, cols, type, data, CV\_AUTOSTEP);
1988 \end{lstlisting}
1989 \fi
1990
1991 \cvCPyFunc{Max}
1992 Finds per-element maximum of two arrays.
1993
1994 \cvdefC{void cvMax(const CvArr* src1, const CvArr* src2, CvArr* dst);}
1995 \cvdefPy{Max(src1,src2,dst)-> None}
1996
1997 \begin{description}
1998 \cvarg{src1}{The first source array}
1999 \cvarg{src2}{The second source array}
2000 \cvarg{dst}{The destination array}
2001 \end{description}
2002
2003 The function calculates per-element maximum of two arrays:
2004
2005 \[
2006 \texttt{dst}(I)=\max(\texttt{src1}(I), \texttt{src2}(I))
2007 \]
2008
2009 All the arrays must have a single channel, the same data type and the same size (or ROI size).
2010
2011
2012 \cvCPyFunc{MaxS}
2013 Finds per-element maximum of array and scalar.
2014
2015 \cvdefC{void cvMaxS(const CvArr* src, double value, CvArr* dst);}
2016 \cvdefPy{MaxS(src,value,dst)-> None}
2017
2018 \begin{description}
2019 \cvarg{src}{The first source array}
2020 \cvarg{value}{The scalar value}
2021 \cvarg{dst}{The destination array}
2022 \end{description}
2023
2024 The function calculates per-element maximum of array and scalar:
2025
2026 \[
2027 \texttt{dst}(I)=\max(\texttt{src}(I), \texttt{value})
2028 \]
2029
2030 All the arrays must have a single channel, the same data type and the same size (or ROI size).
2031
2032
2033 \cvCPyFunc{Merge}
2034 Composes a multi-channel array from several single-channel arrays or inserts a single channel into the array.
2035
2036 \cvdefC{void cvMerge(const CvArr* src0, const CvArr* src1,
2037               const CvArr* src2, const CvArr* src3, CvArr* dst);}
2038 \ifC
2039 \begin{lstlisting}
2040 #define cvCvtPlaneToPix cvMerge
2041 \end{lstlisting}
2042 \fi
2043 \cvdefPy{Merge(src0,src1,src2,src3,dst)-> None}
2044
2045 \begin{description}
2046 \cvarg{src0}{Input channel 0}
2047 \cvarg{src1}{Input channel 1}
2048 \cvarg{src2}{Input channel 2}
2049 \cvarg{src3}{Input channel 3}
2050 \cvarg{dst}{Destination array}
2051 \end{description}
2052
2053 The function is the opposite to \cvCPyCross{Split}. If the destination array has N channels then if the first N input channels are not NULL, they all are copied to the destination array; if only a single source channel of the first N is not NULL, this particular channel is copied into the destination array; otherwise an error is raised. The rest of the source channels (beyond the first N) must always be NULL. For IplImage \cvCPyCross{Copy} with COI set can be also used to insert a single channel into the image.
2054
2055 \cvCPyFunc{Min}
2056 Finds per-element minimum of two arrays.
2057
2058 \cvdefC{void cvMin(const CvArr* src1, const CvArr* src2, CvArr* dst);}
2059 \cvdefPy{Min(src1,src2,dst)-> None}
2060
2061 \begin{description}
2062 \cvarg{src1}{The first source array}
2063 \cvarg{src2}{The second source array}
2064 \cvarg{dst}{The destination array}
2065 \end{description}
2066
2067
2068 The function calculates per-element minimum of two arrays:
2069
2070 \[
2071 \texttt{dst}(I)=\min(\texttt{src1}(I),\texttt{src2}(I))
2072 \]
2073
2074 All the arrays must have a single channel, the same data type and the same size (or ROI size).
2075
2076
2077 \cvCPyFunc{MinMaxLoc}
2078 Finds global minimum and maximum in array or subarray.
2079
2080 \cvdefC{void cvMinMaxLoc(const CvArr* arr, double* minVal, double* maxVal,
2081                   CvPoint* minLoc=NULL, CvPoint* maxLoc=NULL, const CvArr* mask=NULL);}
2082 \cvdefPy{MinMaxLoc(arr,mask=NULL)-> (minVal,maxVal,minLoc,maxLoc)}
2083
2084 \begin{description}
2085 \cvarg{arr}{The source array, single-channel or multi-channel with COI set}
2086 \cvarg{minVal}{Pointer to returned minimum value}
2087 \cvarg{maxVal}{Pointer to returned maximum value}
2088 \cvarg{minLoc}{Pointer to returned minimum location}
2089 \cvarg{maxLoc}{Pointer to returned maximum location}
2090 \cvarg{mask}{The optional mask used to select a subarray}
2091 \end{description}
2092
2093 The function finds minimum and maximum element values
2094 and their positions. The extremums are searched across the whole array,
2095 selected \texttt{ROI} (in the case of \texttt{IplImage}) or, if \texttt{mask}
2096 is not \texttt{NULL}, in the specified array region. If the array has
2097 more than one channel, it must be \texttt{IplImage} with \texttt{COI}
2098 set. In the case of multi-dimensional arrays, \texttt{minLoc->x} and \texttt{maxLoc->x}
2099 will contain raw (linear) positions of the extremums.
2100
2101 \cvCPyFunc{MinS}
2102 Finds per-element minimum of an array and a scalar.
2103
2104 \cvdefC{void cvMinS(const CvArr* src, double value, CvArr* dst);}
2105 \cvdefPy{MinS(src,value,dst)-> None}
2106
2107 \begin{description}
2108 \cvarg{src}{The first source array}
2109 \cvarg{value}{The scalar value}
2110 \cvarg{dst}{The destination array}
2111 \end{description}
2112
2113 The function calculates minimum of an array and a scalar:
2114
2115 \[
2116 \texttt{dst}(I)=\min(\texttt{src}(I), \texttt{value})
2117 \]
2118
2119 All the arrays must have a single channel, the same data type and the same size (or ROI size).
2120
2121
2122 \cvCPyFunc{MixChannels}
2123 Copies several channels from input arrays to certain channels of output arrays
2124
2125 \cvdefC{void cvMixChannels(const CvArr** src, int srcCount, \par
2126                     CvArr** dst, int dstCount, \par
2127                     const int* fromTo, int pairCount);}
2128 \cvdefPy{MixChannels(src, dst, fromTo) -> None}
2129
2130 \begin{description}
2131 \cvarg{src}{Input arrays}
2132 \cvC{\cvarg{srcCount}{The number of input arrays.}}
2133 \cvarg{dst}{Destination arrays}
2134 \cvC{\cvarg{dstCount}{The number of output arrays.}}
2135 \cvarg{fromTo}{The array of pairs of indices of the planes
2136 copied. \cvC{\texttt{fromTo[k*2]} is the 0-based index of the input channel in \texttt{src} and
2137 \texttt{fromTo[k*2+1]} is the index of the output channel in \texttt{dst}.
2138 Here the continuous channel numbering is used, that is, the first input image channels are indexed
2139 from \texttt{0} to \texttt{channels(src[0])-1}, the second input image channels are indexed from
2140 \texttt{channels(src[0])} to \texttt{channels(src[0]) + channels(src[1])-1} etc., and the same
2141 scheme is used for the output image channels.
2142 As a special case, when \texttt{fromTo[k*2]} is negative,
2143 the corresponding output channel is filled with zero.}\cvPy{Each pair \texttt{fromTo[k]=(i,j)}
2144 means that i-th plane from \texttt{src} is copied to the j-th plane in \texttt{dst}, where continuous
2145 plane numbering is used both in the input array list and the output array list.
2146 As a special case, when the \texttt{fromTo[k][0]} is negative, the corresponding output plane \texttt{j}
2147  is filled with zero.}}
2148 \end{description}
2149
2150 The function is a generalized form of \cvCPyCross{cvSplit} and \cvCPyCross{Merge}
2151 and some forms of \cross{CvtColor}. It can be used to change the order of the
2152 planes, add/remove alpha channel, extract or insert a single plane or
2153 multiple planes etc.
2154
2155 As an example, this code splits a 4-channel RGBA image into a 3-channel
2156 BGR (i.e. with R and B swapped) and separate alpha channel image:
2157
2158 \ifPy
2159 \begin{lstlisting}
2160         rgba = cv.CreateMat(100, 100, cv.CV_8UC4)
2161         bgr =  cv.CreateMat(100, 100, cv.CV_8UC3)
2162         alpha = cv.CreateMat(100, 100, cv.CV_8UC1)
2163         cv.Set(rgba, (1,2,3,4))
2164         cv.MixChannels([rgba], [bgr, alpha], [
2165            (0, 2),    # rgba[0] -> bgr[2]
2166            (1, 1),    # rgba[1] -> bgr[1]
2167            (2, 0),    # rgba[2] -> bgr[0]
2168            (3, 3)     # rgba[3] -> alpha[0]
2169         ])
2170 \end{lstlisting}
2171 \fi
2172
2173 \ifC
2174 \begin{lstlisting}
2175     CvMat* rgba = cvCreateMat(100, 100, CV_8UC4);
2176     CvMat* bgr = cvCreateMat(rgba->rows, rgba->cols, CV_8UC3);
2177     CvMat* alpha = cvCreateMat(rgba->rows, rgba->cols, CV_8UC1);
2178     cvSet(rgba, cvScalar(1,2,3,4));
2179
2180     CvArr* out[] = { bgr, alpha };
2181     int from_to[] = { 0,2,  1,1,  2,0,  3,3 };
2182     cvMixChannels(&bgra, 1, out, 2, from_to, 4);
2183 \end{lstlisting}
2184 \fi
2185
2186 \cvCPyFunc{Mul}
2187 Calculates the per-element product of two arrays.
2188
2189 \cvdefC{void cvMul(const CvArr* src1, const CvArr* src2, CvArr* dst, double scale=1);}
2190 \cvdefPy{Mul(src1,src2,dst,scale)-> None}
2191
2192 \begin{description}
2193 \cvarg{src1}{The first source array}
2194 \cvarg{src2}{The second source array}
2195 \cvarg{dst}{The destination array}
2196 \cvarg{scale}{Optional scale factor}
2197 \end{description}
2198
2199
2200 The function calculates the per-element product of two arrays:
2201
2202 \[
2203 \texttt{dst}(I)=\texttt{scale} \cdot \texttt{src1}(I) \cdot \texttt{src2}(I)
2204 \]
2205
2206 All the arrays must have the same type and the same size (or ROI size).
2207 For types that have limited range this operation is saturating.
2208
2209 \cvCPyFunc{MulSpectrums}
2210 Performs per-element multiplication of two Fourier spectrums.
2211
2212 \cvdefC{void cvMulSpectrums(\par const CvArr* src1,\par const CvArr* src2,\par CvArr* dst,\par int flags);}
2213 \cvdefPy{MulSpectrums(src1,src2,dst,flags)-> None}
2214
2215 \begin{description}
2216 \cvarg{src1}{The first source array}
2217 \cvarg{src2}{The second source array}
2218 \cvarg{dst}{The destination array of the same type and the same size as the source arrays}
2219 \cvarg{flags}{A combination of the following values;
2220 \begin{description}
2221 \cvarg{CV\_DXT\_ROWS}{treats each row of the arrays as a separate spectrum (see \cvCPyCross{DFT} parameters description).}
2222 \cvarg{CV\_DXT\_MUL\_CONJ}{conjugate the second source array before the multiplication.}
2223 \end{description}}
2224
2225 \end{description}
2226
2227 The function performs per-element multiplication of the two CCS-packed or complex matrices that are results of a real or complex Fourier transform.
2228
2229 The function, together with \cvCPyCross{DFT}, may be used to calculate convolution of two arrays rapidly.
2230
2231
2232 \cvCPyFunc{MulTransposed}
2233 Calculates the product of an array and a transposed array.
2234
2235 \cvdefC{void cvMulTransposed(const CvArr* src, CvArr* dst, int order, const CvArr* delta=NULL, double scale=1.0);}
2236 \cvdefPy{MulTransposed(src,dst,order,delta=NULL,scale)-> None}
2237
2238 \begin{description}
2239 \cvarg{src}{The source matrix}
2240 \cvarg{dst}{The destination matrix. Must be \texttt{CV\_32F} or \texttt{CV\_64F}.}
2241 \cvarg{order}{Order of multipliers}
2242 \cvarg{delta}{An optional array, subtracted from \texttt{src} before multiplication}
2243 \cvarg{scale}{An optional scaling}
2244 \end{description}
2245
2246 The function calculates the product of src and its transposition:
2247
2248 \[
2249 \texttt{dst}=\texttt{scale} (\texttt{src}-\texttt{delta}) (\texttt{src}-\texttt{delta})^T
2250 \]
2251
2252 if $\texttt{order}=0$, and
2253
2254 \[
2255 \texttt{dst}=\texttt{scale} (\texttt{src}-\texttt{delta})^T (\texttt{src}-\texttt{delta})
2256 \]
2257
2258 otherwise.
2259
2260 \cvCPyFunc{Norm}
2261 Calculates absolute array norm, absolute difference norm, or relative difference norm.
2262
2263 \cvdefC{double cvNorm(const CvArr* arr1, const CvArr* arr2=NULL, int normType=CV\_L2, const CvArr* mask=NULL);}
2264 \cvdefPy{Norm(arr1,arr2,normType=CV\_L2,mask=NULL)-> double}
2265
2266 \begin{description}
2267 \cvarg{arr1}{The first source image}
2268 \cvarg{arr2}{The second source image. If it is NULL, the absolute norm of \texttt{arr1} is calculated, otherwise the absolute or relative norm of \texttt{arr1}-\texttt{arr2} is calculated.}
2269 \cvarg{normType}{Type of norm, see the discussion}
2270 \cvarg{mask}{The optional operation mask}
2271 \end{description}
2272
2273 The function calculates the absolute norm of \texttt{arr1} if \texttt{arr2} is NULL:
2274 \[
2275 norm = \forkthree
2276 {||\texttt{arr1}||_C    = \max_I |\texttt{arr1}(I)|}{if $\texttt{normType} = \texttt{CV\_C}$}
2277 {||\texttt{arr1}||_{L1} = \sum_I |\texttt{arr1}(I)|}{if $\texttt{normType} = \texttt{CV\_L1}$}
2278 {||\texttt{arr1}||_{L2} = \sqrt{\sum_I \texttt{arr1}(I)^2}}{if $\texttt{normType} = \texttt{CV\_L2}$}
2279 \]
2280
2281 or the absolute difference norm if \texttt{arr2} is not NULL:
2282 \[
2283 norm = \forkthree
2284 {||\texttt{arr1}-\texttt{arr2}||_C    = \max_I |\texttt{arr1}(I) - \texttt{arr2}(I)|}{if $\texttt{normType} = \texttt{CV\_C}$}
2285 {||\texttt{arr1}-\texttt{arr2}||_{L1} = \sum_I |\texttt{arr1}(I) - \texttt{arr2}(I)|}{if $\texttt{normType} = \texttt{CV\_L1}$}
2286 {||\texttt{arr1}-\texttt{arr2}||_{L2} = \sqrt{\sum_I (\texttt{arr1}(I) - \texttt{arr2}(I))^2}}{if $\texttt{normType} = \texttt{CV\_L2}$}
2287 \]
2288
2289 or the relative difference norm if \texttt{arr2} is not NULL and \texttt{(normType \& CV\_RELATIVE) != 0}:
2290
2291 \[
2292 norm = \forkthree
2293 {\frac{||\texttt{arr1}-\texttt{arr2}||_C    }{||\texttt{arr2}||_C   }}{if $\texttt{normType} = \texttt{CV\_RELATIVE\_C}$}
2294 {\frac{||\texttt{arr1}-\texttt{arr2}||_{L1} }{||\texttt{arr2}||_{L1}}}{if $\texttt{normType} = \texttt{CV\_RELATIVE\_L1}$}
2295 {\frac{||\texttt{arr1}-\texttt{arr2}||_{L2} }{||\texttt{arr2}||_{L2}}}{if $\texttt{normType} = \texttt{CV\_RELATIVE\_L2}$}
2296 \]
2297
2298 The function returns the calculated norm. A multiple-channel array is treated as a single-channel, that is, the results for all channels are combined.
2299
2300 \cvCPyFunc{Not}
2301 Performs per-element bit-wise inversion of array elements.
2302
2303 \cvdefC{void cvNot(const CvArr* src, CvArr* dst);}
2304 \cvdefPy{Not(src,dst)-> None}
2305
2306 \begin{description}
2307 \cvarg{src}{The source array}
2308 \cvarg{dst}{The destination array}
2309 \end{description}
2310
2311
2312 The function Not inverses every bit of every array element:
2313
2314 \begin{lstlisting}
2315 dst(I)=~src(I)
2316 \end{lstlisting}
2317
2318
2319 \cvCPyFunc{Or}
2320 Calculates per-element bit-wise disjunction of two arrays.
2321
2322 \cvdefC{void cvOr(const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask=NULL);}
2323 \cvdefPy{Or(src1,src2,dst,mask=NULL)-> None}
2324
2325 \begin{description}
2326 \cvarg{src1}{The first source array}
2327 \cvarg{src2}{The second source array}
2328 \cvarg{dst}{The destination array}
2329 \cvarg{mask}{Operation mask, 8-bit single channel array; specifies elements of the destination array to be changed}
2330 \end{description}
2331
2332
2333 The function calculates per-element bit-wise disjunction of two arrays:
2334
2335 \begin{lstlisting}
2336 dst(I)=src1(I)|src2(I)
2337 \end{lstlisting}
2338
2339 In the case of floating-point arrays their bit representations are used for the operation. All the arrays must have the same type, except the mask, and the same size.
2340
2341 \cvCPyFunc{OrS}
2342 Calculates a per-element bit-wise disjunction of an array and a scalar.
2343
2344 \cvdefC{void cvOrS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL);}
2345 \cvdefPy{OrS(src,value,dst,mask=NULL)-> None}
2346
2347 \begin{description}
2348 \cvarg{src}{The source array}
2349 \cvarg{value}{Scalar to use in the operation}
2350 \cvarg{dst}{The destination array}
2351 \cvarg{mask}{Operation mask, 8-bit single channel array; specifies elements of the destination array to be changed}
2352 \end{description}
2353
2354
2355 The function OrS calculates per-element bit-wise disjunction of an array and a scalar:
2356
2357 \begin{lstlisting}
2358 dst(I)=src(I)|value if mask(I)!=0
2359 \end{lstlisting}
2360
2361 Prior to the actual operation, the scalar is converted to the same type as that of the array(s). In the case of floating-point arrays their bit representations are used for the operation. All the arrays must have the same type, except the mask, and the same size.
2362
2363
2364 \cvCPyFunc{PerspectiveTransform}
2365 Performs perspective matrix transformation of a vector array.
2366
2367 \cvdefC{void cvPerspectiveTransform(const CvArr* src, CvArr* dst, const CvMat* mat);}
2368 \cvdefPy{PerspectiveTransform(src,dst,mat)-> None}
2369
2370 \begin{description}
2371 \cvarg{src}{The source three-channel floating-point array}
2372 \cvarg{dst}{The destination three-channel floating-point array}
2373 \cvarg{mat}{$3\times 3$ or $4 \times 4$ transformation matrix}
2374 \end{description}
2375
2376
2377 The function transforms every element of \texttt{src} (by treating it as 2D or 3D vector) in the following way:
2378
2379 \[ (x, y, z) \rightarrow (x'/w, y'/w, z'/w) \]
2380
2381 where
2382
2383 \[
2384 (x', y', z', w') = \texttt{mat} \cdot
2385 \begin{bmatrix} x & y & z & 1 \end{bmatrix}
2386 \]
2387
2388 and
2389 \[ w = \fork{w'}{if $w' \ne 0$}{\infty}{otherwise} \]
2390
2391 \cvCPyFunc{PolarToCart}
2392 Calculates Cartesian coordinates of 2d vectors represented in polar form.
2393
2394 \cvdefC{void cvPolarToCart(\par const CvArr* magnitude,\par const CvArr* angle,\par CvArr* x,\par CvArr* y,\par int angleInDegrees=0);}
2395 \cvdefPy{PolarToCart(magnitude,angle,x,y,angleInDegrees=0)-> None}
2396
2397 \begin{description}
2398 \cvarg{magnitude}{The array of magnitudes. If it is NULL, the magnitudes are assumed to be all 1's.}
2399 \cvarg{angle}{The array of angles, whether in radians or degrees}
2400 \cvarg{x}{The destination array of x-coordinates, may be set to NULL if it is not needed}
2401 \cvarg{y}{The destination array of y-coordinates, mau be set to NULL if it is not needed}
2402 \cvarg{angleInDegrees}{The flag indicating whether the angles are measured in radians, which is default mode, or in degrees}
2403 \end{description}
2404
2405 The function calculates either the x-coodinate, y-coordinate or both of every vector \texttt{magnitude(I)*exp(angle(I)*j), j=sqrt(-1)}:
2406
2407 \begin{lstlisting}
2408 x(I)=magnitude(I)*cos(angle(I)),
2409 y(I)=magnitude(I)*sin(angle(I))
2410 \end{lstlisting}
2411
2412
2413 \cvCPyFunc{Pow}
2414 Raises every array element to a power.
2415
2416 \cvdefC{void cvPow(\par const CvArr* src,\par CvArr* dst,\par double power);}
2417 \cvdefPy{Pow(src,dst,power)-> None}
2418
2419 \begin{description}
2420 \cvarg{src}{The source array}
2421 \cvarg{dst}{The destination array, should be the same type as the source}
2422 \cvarg{power}{The exponent of power}
2423 \end{description}
2424
2425
2426 The function raises every element of the input array to \texttt{p}:
2427
2428 \[
2429 \texttt{dst} [I] = \fork
2430 {\texttt{src}(I)^p}{if \texttt{p} is integer}
2431 {|\texttt{src}(I)^p|}{otherwise}
2432 \]
2433
2434 That is, for a non-integer power exponent the absolute values of input array elements are used. However, it is possible to get true values for negative values using some extra operations, as the following example, computing the cube root of array elements, shows:
2435
2436 \begin{lstlisting}
2437 CvSize size = cvGetSize(src);
2438 CvMat* mask = cvCreateMat(size.height, size.width, CVg8UC1);
2439 cvCmpS(src, 0, mask, CVgCMPgLT); /* find negative elements */
2440 cvPow(src, dst, 1./3);
2441 cvSubRS(dst, cvScalarAll(0), dst, mask); /* negate the results of negative inputs */
2442 cvReleaseMat(&mask);
2443 \end{lstlisting}
2444
2445 For some values of \texttt{power}, such as integer values, 0.5, and -0.5, specialized faster algorithms are used.
2446
2447 \ifC
2448 \cvCPyFunc{Ptr?D}
2449 Return pointer to a particular array element.
2450
2451 \cvdefC{
2452 uchar* cvPtr1D(const CvArr* arr, int idx0, int* type=NULL); \newline
2453 uchar* cvPtr2D(const CvArr* arr, int idx0, int idx1, int* type=NULL); \newline
2454 uchar* cvPtr3D(const CvArr* arr, int idx0, int idx1, int idx2, int* type=NULL); \newline
2455 uchar* cvPtrND(const CvArr* arr, int* idx, int* type=NULL, int createNode=1, unsigned* precalcHashval=NULL);
2456 }
2457
2458 \begin{description}
2459 \cvarg{arr}{Input array}
2460 \cvarg{idx0}{The first zero-based component of the element index}
2461 \cvarg{idx1}{The second zero-based component of the element index}
2462 \cvarg{idx2}{The third zero-based component of the element index}
2463 \cvarg{idx}{Array of the element indices}
2464 \cvarg{type}{Optional output parameter: type of matrix elements}
2465 \cvarg{createNode}{Optional input parameter for sparse matrices. Non-zero value of the parameter means that the requested element is created if it does not exist already.}
2466 \cvarg{precalcHashval}{Optional input parameter for sparse matrices. If the pointer is not NULL, the function does not recalculate the node hash value, but takes it from the specified location. It is useful for speeding up pair-wise operations (TODO: provide an example)}
2467 \end{description}
2468
2469 The functions return a pointer to a specific array element. Number of array dimension should match to the number of indices passed to the function except for \texttt{cvPtr1D} function that can be used for sequential access to 1D, 2D or nD dense arrays.
2470
2471 The functions can be used for sparse arrays as well - if the requested node does not exist they create it and set it to zero.
2472
2473 All these as well as other functions accessing array elements (\cvCPyCross{Get}, \cvCPyCross{GetReal}, 
2474 \cvCPyCross{Set}, \cvCPyCross{SetReal}) raise an error in case if the element index is out of range.
2475
2476 \fi
2477
2478 \cvCPyFunc{RNG}
2479 Initializes a random number generator state.
2480
2481 \cvdefC{CvRNG cvRNG(int64 seed=-1);}
2482 \cvdefPy{RNG(seed=-1LL)-> CvRNG}
2483
2484 \begin{description}
2485 \cvarg{seed}{64-bit value used to initiate a random sequence}
2486 \end{description}
2487
2488 The function initializes a random number generator
2489 and returns the state. The pointer to the state can be then passed to the
2490 \cvCPyCross{RandInt}, \cvCPyCross{RandReal} and \cvCPyCross{RandArr} functions. In the
2491 current implementation a multiply-with-carry generator is used.
2492
2493 \cvCPyFunc{RandArr}
2494 Fills an array with random numbers and updates the RNG state.
2495
2496 \cvdefC{void cvRandArr(\par CvRNG* rng,\par CvArr* arr,\par int distType,\par CvScalar param1,\par CvScalar param2);}
2497 \cvdefPy{RandArr(rng,arr,distType,param1,param2)-> None}
2498
2499 \begin{description}
2500 \cvarg{rng}{RNG state initialized by \cvCPyCross{RNG}}
2501 \cvarg{arr}{The destination array}
2502 \cvarg{distType}{Distribution type
2503 \begin{description}
2504 \cvarg{CV\_RAND\_UNI}{uniform distribution}
2505 \cvarg{CV\_RAND\_NORMAL}{normal or Gaussian distribution}
2506 \end{description}}
2507 \cvarg{param1}{The first parameter of the distribution. In the case of a uniform distribution it is the inclusive lower boundary of the random numbers range. In the case of a normal distribution it is the mean value of the random numbers.}
2508 \cvarg{param2}{The second parameter of the distribution. In the case of a uniform distribution it is the exclusive upper boundary of the random numbers range. In the case of a normal distribution it is the standard deviation of the random numbers.}
2509 \end{description}
2510
2511 The function fills the destination array with uniformly
2512 or normally distributed random numbers.
2513
2514 \ifC
2515 In the example below, the function
2516 is used to add a few normally distributed floating-point numbers to
2517 random locations within a 2d array.
2518
2519 \begin{lstlisting}
2520 /* let noisy_screen be the floating-point 2d array that is to be "crapped" */
2521 CvRNG rng_state = cvRNG(0xffffffff);
2522 int i, pointCount = 1000;
2523 /* allocate the array of coordinates of points */
2524 CvMat* locations = cvCreateMat(pointCount, 1, CV_32SC2);
2525 /* arr of random point values */
2526 CvMat* values = cvCreateMat(pointCount, 1, CV_32FC1);
2527 CvSize size = cvGetSize(noisy_screen);
2528
2529 /* initialize the locations */
2530 cvRandArr(&rng_state, locations, CV_RAND_UNI, cvScalar(0,0,0,0), 
2531            cvScalar(size.width,size.height,0,0));
2532
2533 /* generate values */
2534 cvRandArr(&rng_state, values, CV_RAND_NORMAL,
2535            cvRealScalar(100), // average intensity
2536            cvRealScalar(30) // deviation of the intensity
2537           );
2538
2539 /* set the points */
2540 for(i = 0; i < pointCount; i++ )
2541 {
2542     CvPoint pt = *(CvPoint*)cvPtr1D(locations, i, 0);
2543     float value = *(float*)cvPtr1D(values, i, 0);
2544     *((float*)cvPtr2D(noisy_screen, pt.y, pt.x, 0 )) += value;
2545 }
2546
2547 /* not to forget to release the temporary arrays */
2548 cvReleaseMat(&locations);
2549 cvReleaseMat(&values);
2550
2551 /* RNG state does not need to be deallocated */
2552 \end{lstlisting}
2553 \fi
2554
2555 \cvCPyFunc{RandInt}
2556 Returns a 32-bit unsigned integer and updates RNG.
2557
2558 \cvdefC{unsigned cvRandInt(CvRNG* rng);}
2559 \cvdefPy{RandInt(rng)-> unsigned}
2560
2561 \begin{description}
2562 \cvarg{rng}{RNG state initialized by \texttt{RandInit} and, optionally, customized by \texttt{RandSetRange} (though, the latter function does not affect the discussed function outcome)}
2563 \end{description}
2564
2565 The function returns a uniformly-distributed random
2566 32-bit unsigned integer and updates the RNG state. It is similar to the rand()
2567 function from the C runtime library, but it always generates a 32-bit number
2568 whereas rand() returns a number in between 0 and \texttt{RAND\_MAX}
2569 which is $2^{16}$ or $2^{32}$, depending on the platform.
2570
2571 The function is useful for generating scalar random numbers, such as
2572 points, patch sizes, table indices, etc., where integer numbers of a certain
2573 range can be generated using a modulo operation and floating-point numbers
2574 can be generated by scaling from 0 to 1 or any other specific range.
2575
2576 \ifC
2577 Here is the example from the previous function discussion rewritten using
2578 \cvCPyCross{RandInt}:
2579
2580 \begin{lstlisting}
2581 /* the input and the task is the same as in the previous sample. */
2582 CvRNG rnggstate = cvRNG(0xffffffff);
2583 int i, pointCount = 1000;
2584 /* ... - no arrays are allocated here */
2585 CvSize size = cvGetSize(noisygscreen);
2586 /* make a buffer for normally distributed numbers to reduce call overhead */
2587 #define bufferSize 16
2588 float normalValueBuffer[bufferSize];
2589 CvMat normalValueMat = cvMat(bufferSize, 1, CVg32F, normalValueBuffer);
2590 int valuesLeft = 0;
2591
2592 for(i = 0; i < pointCount; i++ )
2593 {
2594     CvPoint pt;
2595     /* generate random point */
2596     pt.x = cvRandInt(&rnggstate ) % size.width;
2597     pt.y = cvRandInt(&rnggstate ) % size.height;
2598
2599     if(valuesLeft <= 0 )
2600     {
2601         /* fulfill the buffer with normally distributed numbers 
2602            if the buffer is empty */
2603         cvRandArr(&rnggstate, &normalValueMat, CV\_RAND\_NORMAL, 
2604                    cvRealScalar(100), cvRealScalar(30));
2605         valuesLeft = bufferSize;
2606     }
2607     *((float*)cvPtr2D(noisygscreen, pt.y, pt.x, 0 ) = 
2608                                 normalValueBuffer[--valuesLeft];
2609 }
2610
2611 /* there is no need to deallocate normalValueMat because we have
2612 both the matrix header and the data on stack. It is a common and efficient
2613 practice of working with small, fixed-size matrices */
2614 \end{lstlisting}
2615 \fi
2616
2617 \cvCPyFunc{RandReal}
2618 Returns a floating-point random number and updates RNG.
2619
2620 \cvdefC{double cvRandReal(CvRNG* rng);}
2621 \cvdefPy{RandReal(rng)-> double}
2622
2623 \begin{description}
2624 \cvarg{rng}{RNG state initialized by \cvCPyCross{RNG}}
2625 \end{description}
2626
2627
2628 The function returns a uniformly-distributed random floating-point number between 0 and 1 (1 is not included).
2629
2630 \cvCPyFunc{Reduce}
2631 Reduces a matrix to a vector.
2632
2633 \cvdefC{void cvReduce(const CvArr* src, CvArr* dst, int dim = -1, int op=CV\_REDUCE\_SUM);}
2634 \cvdefPy{Reduce(src,dst,dim=-1,op=CV\_REDUCE\_SUM)-> None}
2635
2636 \begin{description}
2637 \cvarg{src}{The input matrix.}
2638 \cvarg{dst}{The output single-row/single-column vector that accumulates somehow all the matrix rows/columns.}
2639 \cvarg{dim}{The dimension index along which the matrix is reduced. 0 means that the matrix is reduced to a single row, 1 means that the matrix is reduced to a single column and -1 means that the dimension is chosen automatically by analysing the dst size.}
2640 \cvarg{op}{The reduction operation. It can take of the following values:
2641 \begin{description}
2642 \cvarg{CV\_REDUCE\_SUM}{The output is the sum of all of the matrix's rows/columns.}
2643 \cvarg{CV\_REDUCE\_AVG}{The output is the mean vector of all of the matrix's rows/columns.}
2644 \cvarg{CV\_REDUCE\_MAX}{The output is the maximum (column/row-wise) of all of the matrix's rows/columns.}
2645 \cvarg{CV\_REDUCE\_MIN}{The output is the minimum (column/row-wise) of all of the matrix's rows/columns.}
2646 \end{description}}
2647 \end{description}
2648
2649 The function reduces matrix to a vector by treating the matrix rows/columns as a set of 1D vectors and performing the specified operation on the vectors until a single row/column is obtained. For example, the function can be used to compute horizontal and vertical projections of an raster image. In the case of \texttt{CV\_REDUCE\_SUM} and \texttt{CV\_REDUCE\_AVG} the output may have a larger element bit-depth to preserve accuracy. And multi-channel arrays are also supported in these two reduction modes. 
2650
2651 \ifC
2652 \cvCPyFunc{ReleaseData}
2653 Releases array data.
2654
2655 \cvdefC{void cvReleaseData(CvArr* arr);}
2656
2657 \begin{description}
2658 \cvarg{arr}{Array header}
2659 \end{description}
2660
2661 The function releases the array data. In the case of \cross{CvMat} or \cross{CvMatND} it simply calls cvDecRefData(), that is the function can not deallocate external data. See also the note to \cvCPyCross{CreateData}.
2662
2663 \cvCPyFunc{ReleaseImage}
2664 Deallocates the image header and the image data.
2665
2666 \cvdefC{void cvReleaseImage(IplImage** image);}
2667
2668 \begin{description}
2669 \cvarg{image}{Double pointer to the image header}
2670 \end{description}
2671
2672 This call is a shortened form of
2673
2674 \begin{lstlisting}
2675 if(*image )
2676 {
2677     cvReleaseData(*image);
2678     cvReleaseImageHeader(image);
2679 }
2680 \end{lstlisting}
2681
2682
2683 \cvCPyFunc{ReleaseImageHeader}
2684 Deallocates an image header.
2685
2686 \cvdefC{void cvReleaseImageHeader(IplImage** image);}
2687
2688 \begin{description}
2689 \cvarg{image}{Double pointer to the image header}
2690 \end{description}
2691
2692 This call is an analogue of
2693 \begin{lstlisting}
2694 if(image )
2695 {
2696     iplDeallocate(*image, IPL_IMAGE_HEADER | IPL_IMAGE_ROI);
2697     *image = 0;
2698 }
2699 \end{lstlisting}
2700 but it does not use IPL functions by default (see the \texttt{CV\_TURN\_ON\_IPL\_COMPATIBILITY} macro).
2701
2702
2703 \cvCPyFunc{ReleaseMat}
2704 Deallocates a matrix.
2705
2706 \cvdefC{void cvReleaseMat(CvMat** mat);}
2707
2708 \begin{description}
2709 \cvarg{mat}{Double pointer to the matrix}
2710 \end{description}
2711
2712
2713 The function decrements the matrix data reference counter and deallocates matrix header. If the data reference counter is 0, it also deallocates the data.
2714
2715 \begin{lstlisting}
2716 if(*mat )
2717     cvDecRefData(*mat);
2718 cvFree((void**)mat);
2719 \end{lstlisting}
2720
2721
2722 \cvCPyFunc{ReleaseMatND}
2723 Deallocates a multi-dimensional array.
2724
2725 \cvdefC{void cvReleaseMatND(CvMatND** mat);}
2726
2727 \begin{description}
2728 \cvarg{mat}{Double pointer to the array}
2729 \end{description}
2730
2731 The function decrements the array data reference counter and releases the array header. If the reference counter reaches 0, it also deallocates the data.
2732
2733 \begin{lstlisting}
2734 if(*mat )
2735     cvDecRefData(*mat);
2736 cvFree((void**)mat);
2737 \end{lstlisting}
2738
2739 \cvCPyFunc{ReleaseSparseMat}
2740 Deallocates sparse array.
2741
2742 \cvdefC{void cvReleaseSparseMat(CvSparseMat** mat);}
2743
2744 \begin{description}
2745 \cvarg{mat}{Double pointer to the array}
2746 \end{description}
2747
2748 The function releases the sparse array and clears the array pointer upon exit.
2749
2750 \fi
2751
2752 \cvCPyFunc{Repeat}
2753 Fill the destination array with repeated copies of the source array.
2754
2755 \cvdefC{void cvRepeat(const CvArr* src, CvArr* dst);}
2756 \cvdefPy{Repeat(src,dst)-> None}
2757
2758 \begin{description}
2759 \cvarg{src}{Source array, image or matrix}
2760 \cvarg{dst}{Destination array, image or matrix}
2761 \end{description}
2762
2763 The function fills the destination array with repeated copies of the source array:
2764
2765 \begin{lstlisting}
2766 dst(i,j)=src(i mod rows(src), j mod cols(src))
2767 \end{lstlisting}
2768
2769 So the destination array may be as larger as well as smaller than the source array.
2770
2771 \cvCPyFunc{ResetImageROI}
2772 Resets the image ROI to include the entire image and releases the ROI structure.
2773
2774 \cvdefC{void cvResetImageROI(IplImage* image);}
2775 \cvdefPy{ResetImageROI(image)-> None}
2776
2777 \begin{description}
2778 \cvarg{image}{A pointer to the image header}
2779 \end{description}
2780
2781 This produces a similar result to the following, but in addition it releases the ROI structure.
2782
2783 \begin{lstlisting}
2784 cvSetImageROI(image, cvRect(0, 0, image->width, image->height ));
2785 cvSetImageCOI(image, 0);
2786 \end{lstlisting}
2787
2788
2789 \cvCPyFunc{Reshape}
2790 Changes shape of matrix/image without copying data.
2791
2792 \cvdefC{CvMat* cvReshape(const CvArr* arr, CvMat* header, int newCn, int newRows=0);}
2793 \cvdefPy{Reshape(arr, newCn, newRows=0) -> cvmat}
2794
2795 \begin{description}
2796 \cvarg{arr}{Input array}
2797 \ifC
2798 \cvarg{header}{Output header to be filled}
2799 \fi
2800 \cvarg{newCn}{New number of channels. 'newCn = 0' means that the number of channels remains unchanged.}
2801 \cvarg{newRows}{New number of rows. 'newRows = 0' means that the number of rows remains unchanged unless it needs to be changed according to \texttt{newCn} value.}
2802 \end{description}
2803
2804 The function initializes the CvMat header so that it points to the same data as the original array but has a different shape - different number of channels, different number of rows, or both.
2805
2806 \ifC
2807 The following example code creates one image buffer and two image headers, the first is for a 320x240x3 image and the second is for a 960x240x1 image:
2808
2809 \begin{lstlisting}
2810 IplImage* color_img = cvCreateImage(cvSize(320,240), IPL_DEPTH_8U, 3);
2811 CvMat gray_mat_hdr;
2812 IplImage gray_img_hdr, *gray_img;
2813 cvReshape(color_img, &gray_mat_hdr, 1);
2814 gray_img = cvGetImage(&gray_mat_hdr, &gray_img_hdr);
2815 \end{lstlisting}
2816
2817 And the next example converts a 3x3 matrix to a single 1x9 vector:
2818
2819 \begin{lstlisting}
2820 CvMat* mat = cvCreateMat(3, 3, CV_32F);
2821 CvMat row_header, *row;
2822 row = cvReshape(mat, &row_header, 0, 1);
2823 \end{lstlisting}
2824 \fi
2825
2826 \cvCPyFunc{ReshapeMatND}
2827 Changes the shape of a multi-dimensional array without copying the data.
2828
2829 \cvdefC{CvArr* cvReshapeMatND(const CvArr* arr,
2830                        int sizeofHeader, CvArr* header,
2831                        int newCn, int newDims, int* newSizes);}
2832 \cvdefPy{ReshapeMatND(arr, newCn, newDims) -> cvmat}
2833
2834 \ifC
2835 \begin{lstlisting}
2836 #define cvReshapeND(arr, header, newCn, newDims, newSizes )   \
2837       cvReshapeMatND((arr), sizeof(*(header)), (header),         \
2838                       (newCn), (newDims), (newSizes))
2839 \end{lstlisting}
2840 \fi
2841
2842 \begin{description}
2843 \cvarg{arr}{Input array}
2844 \ifC
2845 \cvarg{sizeofHeader}{Size of output header to distinguish between IplImage, CvMat and CvMatND output headers}
2846 \cvarg{header}{Output header to be filled}
2847 \cvarg{newCn}{New number of channels. $\texttt{newCn} = 0$ means that the number of channels remains unchanged.}
2848 \cvarg{newDims}{New number of dimensions. $\texttt{newDims} = 0$ means that the number of dimensions remains the same.}
2849 \cvarg{newSizes}{Array of new dimension sizes. Only $\texttt{newDims}-1$ values are used, because the total number of elements must remain the same.
2850 Thus, if $\texttt{newDims} = 1$, \texttt{newSizes} array is not used.}
2851 \else
2852 \cvarg{newDims}{List of new dimensions.}
2853 \fi
2854 \end{description}
2855
2856 The function is an advanced version of \cvCPyCross{Reshape} that can work with multi-dimensional arrays as well (though it can work with ordinary images and matrices) and change the number of dimensions.
2857
2858 \ifC
2859 Below are the two samples from the \cvCPyCross{Reshape} description rewritten using \cvCPyCross{ReshapeMatND}:
2860
2861 \begin{lstlisting}
2862
2863 IplImage* color_img = cvCreateImage(cvSize(320,240), IPL_DEPTH_8U, 3);
2864 IplImage gray_img_hdr, *gray_img;
2865 gray_img = (IplImage*)cvReshapeND(color_img, &gray_img_hdr, 1, 0, 0);
2866
2867 ...
2868
2869 /* second example is modified to convert 2x2x2 array to 8x1 vector */
2870 int size[] = { 2, 2, 2 };
2871 CvMatND* mat = cvCreateMatND(3, size, CV_32F);
2872 CvMat row_header, *row;
2873 row = (CvMat*)cvReshapeND(mat, &row_header, 0, 1, 0);
2874
2875 \end{lstlisting}
2876 \fi
2877
2878 \ifC
2879 \cvfunc{cvRound, cvFloor, cvCeil}\label{cvRound}
2880
2881 Converts a floating-point number to an integer.
2882
2883 \cvdefC{
2884 int cvRound(double value);
2885 int cvFloor(double value);
2886 int cvCeil(double value);
2887
2888 }\cvdefPy{Round, Floor, Ceil(value)-> int}
2889
2890 \begin{description}
2891 \cvarg{value}{The input floating-point value}
2892 \end{description}
2893
2894
2895 The functions convert the input floating-point number to an integer using one of the rounding
2896 modes. \texttt{Round} returns the nearest integer value to the
2897 argument. \texttt{Floor} returns the maximum integer value that is not
2898 larger than the argument. \texttt{Ceil} returns the minimum integer
2899 value that is not smaller than the argument. On some architectures the
2900 functions work much faster than the standard cast
2901 operations in C. If the absolute value of the argument is greater than
2902 $2^{31}$, the result is not determined. Special values ($\pm \infty$ , NaN)
2903 are not handled.
2904
2905 \else
2906
2907 \cvfunc{Round}
2908
2909 Converts a floating-point number to the nearest integer value.
2910
2911 \cvdefPy{Round(value) -> int}
2912
2913 \begin{description}
2914 \cvarg{value}{The input floating-point value}
2915 \end{description}
2916
2917 On some architectures this function is much faster than the standard cast
2918 operations. If the absolute value of the argument is greater than
2919 $2^{31}$, the result is not determined. Special values ($\pm \infty$ , NaN)
2920 are not handled.
2921
2922 \cvfunc{Floor}
2923
2924 Converts a floating-point number to the nearest integer value that is not larger than the argument.
2925
2926 \cvdefPy{Floor(value) -> int}
2927
2928 \begin{description}
2929 \cvarg{value}{The input floating-point value}
2930 \end{description}
2931
2932 On some architectures this function is much faster than the standard cast
2933 operations. If the absolute value of the argument is greater than
2934 $2^{31}$, the result is not determined. Special values ($\pm \infty$ , NaN)
2935 are not handled.
2936
2937 \cvfunc{Ceil}
2938
2939 Converts a floating-point number to the nearest integer value that is not smaller than the argument.
2940
2941 \cvdefPy{Ceil(value) -> int}
2942
2943 \begin{description}
2944 \cvarg{value}{The input floating-point value}
2945 \end{description}
2946
2947 On some architectures this function is much faster than the standard cast
2948 operations. If the absolute value of the argument is greater than
2949 $2^{31}$, the result is not determined. Special values ($\pm \infty$ , NaN)
2950 are not handled.
2951
2952 \fi
2953
2954
2955 \cvCPyFunc{ScaleAdd}
2956 Calculates the sum of a scaled array and another array.
2957
2958 \cvdefC{void cvScaleAdd(const CvArr* src1, CvScalar scale, const CvArr* src2, CvArr* dst);}
2959 \cvdefPy{ScaleAdd(src1,scale,src2,dst)-> None}
2960
2961 \begin{description}
2962 \cvarg{src1}{The first source array}
2963 \cvarg{scale}{Scale factor for the first array}
2964 \cvarg{src2}{The second source array}
2965 \cvarg{dst}{The destination array}
2966 \end{description}
2967
2968 \begin{lstlisting}
2969 #define cvMulAddS cvScaleAdd
2970 \end{lstlisting}
2971
2972 The function calculates the sum of a scaled array and another array:
2973
2974 \[
2975 \texttt{dst}(I)=\texttt{scale} \, \texttt{src1}(I) + \texttt{src2}(I)
2976 \]
2977
2978 All array parameters should have the same type and the same size.
2979
2980 \cvCPyFunc{Set}
2981 Sets every element of an array to a given value.
2982
2983 \cvdefC{void cvSet(CvArr* arr, CvScalar value, const CvArr* mask=NULL);}
2984 \cvdefPy{Set(arr,value,mask=NULL)-> None}
2985
2986 \begin{description}
2987 \cvarg{arr}{The destination array}
2988 \cvarg{value}{Fill value}
2989 \cvarg{mask}{Operation mask, 8-bit single channel array; specifies elements of the destination array to be changed}
2990 \end{description}
2991
2992
2993 The function copies the scalar \texttt{value} to every selected element of the destination array:
2994
2995 \[
2996 \texttt{arr}(I)=\texttt{value} \quad \text{if} \quad \texttt{mask}(I) \ne 0
2997 \]
2998
2999 If array \texttt{arr} is of \texttt{IplImage} type, then is ROI used, but COI must not be set.
3000
3001 \ifC % {
3002 \cvCPyFunc{Set?D}
3003 Change the particular array element.
3004
3005 \cvdefC{
3006 void cvSet1D(CvArr* arr, int idx0, CvScalar value); \newline
3007 void cvSet2D(CvArr* arr, int idx0, int idx1, CvScalar value); \newline
3008 void cvSet3D(CvArr* arr, int idx0, int idx1, int idx2, CvScalar value); \newline
3009 void cvSetND(CvArr* arr, int* idx, CvScalar value);
3010 }
3011
3012 \begin{description}
3013 \cvarg{arr}{Input array}
3014 \cvarg{idx0}{The first zero-based component of the element index}
3015 \cvarg{idx1}{The second zero-based component of the element index}
3016 \cvarg{idx2}{The third zero-based component of the element index}
3017 \cvarg{idx}{Array of the element indices}
3018 \cvarg{value}{The assigned value}
3019 \end{description}
3020
3021 The functions assign the new value to a particular array element. In the case of a sparse array the functions create the node if it does not exist yet.
3022
3023 \else % }{
3024
3025 \cvCPyFunc{Set1D}
3026 Set a specific array element.
3027
3028 \cvdefPy{ Set1D(arr, idx, value) -> None }
3029
3030 \begin{description}
3031 \cvarg{arr}{Input array}
3032 \cvarg{idx}{Zero-based element index}
3033 \cvarg{value}{The value to assign to the element}
3034 \end{description}
3035
3036 Sets a specific array element.  Array must have dimension 1.
3037
3038 \cvCPyFunc{Set2D}
3039 Set a specific array element.
3040
3041 \cvdefPy{ Set2D(arr, idx0, idx1, value) -> None }
3042
3043 \begin{description}
3044 \cvarg{arr}{Input array}
3045 \cvarg{idx0}{Zero-based element row index}
3046 \cvarg{idx1}{Zero-based element column index}
3047 \cvarg{value}{The value to assign to the element}
3048 \end{description}
3049
3050 Sets a specific array element.  Array must have dimension 2.
3051
3052 \cvCPyFunc{Set3D}
3053 Set a specific array element.
3054
3055 \cvdefPy{ Set3D(arr, idx0, idx1, idx2, value) -> None }
3056
3057 \begin{description}
3058 \cvarg{arr}{Input array}
3059 \cvarg{idx0}{Zero-based element index}
3060 \cvarg{idx1}{Zero-based element index}
3061 \cvarg{idx2}{Zero-based element index}
3062 \cvarg{value}{The value to assign to the element}
3063 \end{description}
3064
3065 Sets a specific array element.  Array must have dimension 3.
3066
3067 \cvCPyFunc{SetND}
3068 Set a specific array element.
3069
3070 \cvdefPy{ SetND(arr, indices, value) -> None }
3071
3072 \begin{description}
3073 \cvarg{arr}{Input array}
3074 \cvarg{indices}{List of zero-based element indices}
3075 \cvarg{value}{The value to assign to the element}
3076 \end{description}
3077
3078 Sets a specific array element.  The length of array indices must be the same as the dimension of the array.
3079 \fi % }
3080
3081 \cvCPyFunc{SetData}
3082 Assigns user data to the array header.
3083
3084 \cvdefC{void cvSetData(CvArr* arr, void* data, int step);}
3085 \cvdefPy{SetData(arr, data, step)-> None}
3086
3087 \begin{description}
3088 \cvarg{arr}{Array header}
3089 \cvarg{data}{User data}
3090 \cvarg{step}{Full row length in bytes}
3091 \end{description}
3092
3093 The function assigns user data to the array header. Header should be initialized before using \texttt{cvCreate*Header}, \texttt{cvInit*Header} or \cvCPyCross{Mat} (in the case of matrix) function.
3094
3095 \cvCPyFunc{SetIdentity}
3096 Initializes a scaled identity matrix.
3097
3098 \cvdefC{void cvSetIdentity(CvArr* mat, CvScalar value=cvRealScalar(1));}
3099 \cvdefPy{SetIdentity(mat,value=1)-> None}
3100
3101 \begin{description}
3102 \cvarg{mat}{The matrix to initialize (not necesserily square)}
3103 \cvarg{value}{The value to assign to the diagonal elements}
3104 \end{description}
3105
3106 The function initializes a scaled identity matrix:
3107
3108 \[
3109 \texttt{arr}(i,j)=\fork{\texttt{value}}{ if $i=j$}{0}{otherwise}
3110 \]
3111
3112 \cvCPyFunc{SetImageCOI}
3113 Sets the channel of interest in an IplImage.
3114
3115 \cvdefC{void cvSetImageCOI(\par IplImage* image,\par int coi);}
3116 \cvdefPy{SetImageCOI(image, coi)-> None}
3117
3118 \begin{description}
3119 \cvarg{image}{A pointer to the image header}
3120 \cvarg{coi}{The channel of interest. 0 - all channels are selected, 1 - first channel is selected, etc. Note that the channel indices become 1-based.}
3121 \end{description}
3122
3123 If the ROI is set to \texttt{NULL} and the coi is \textit{not} 0,
3124 the ROI is allocated. Most OpenCV functions do \textit{not} support
3125 the COI setting, so to process an individual image/matrix channel one
3126 may copy (via \cvCPyCross{Copy} or \cvCPyCross{Split}) the channel to a separate
3127 image/matrix, process it and then copy the result back (via \cvCPyCross{Copy}
3128 or \cvCPyCross{Merge}) if needed.
3129
3130 \cvCPyFunc{SetImageROI}
3131 Sets an image Region Of Interest (ROI) for a given rectangle.
3132
3133 \cvdefC{void cvSetImageROI(\par IplImage* image,\par CvRect rect);}
3134 \cvdefPy{SetImageROI(image, rect)-> None}
3135
3136 \begin{description}
3137 \cvarg{image}{A pointer to the image header}
3138 \cvarg{rect}{The ROI rectangle}
3139 \end{description}
3140
3141 If the original image ROI was \texttt{NULL} and the \texttt{rect} is not the whole image, the ROI structure is allocated.
3142
3143 Most OpenCV functions support the use of ROI and treat the image rectangle as a separate image. For example, all of the pixel coordinates are counted from the top-left (or bottom-left) corner of the ROI, not the original image.
3144
3145 \ifC % {
3146 \cvCPyFunc{SetReal?D}
3147 Change a specific array element.
3148
3149 \cvdefC{
3150 void cvSetReal1D(CvArr* arr, int idx0, double value); \newline
3151 void cvSetReal2D(CvArr* arr, int idx0, int idx1, double value); \newline
3152 void cvSetReal3D(CvArr* arr, int idx0, int idx1, int idx2, double value); \newline
3153 void cvSetRealND(CvArr* arr, int* idx, double value);
3154 }
3155
3156 \begin{description}
3157 \cvarg{arr}{Input array}
3158 \cvarg{idx0}{The first zero-based component of the element index}
3159 \cvarg{idx1}{The second zero-based component of the element index}
3160 \cvarg{idx2}{The third zero-based component of the element index}
3161 \cvarg{idx}{Array of the element indices}
3162 \cvarg{value}{The assigned value}
3163 \end{description}
3164
3165 The functions assign a new value to a specific
3166 element of a single-channel array. If the array has multiple channels,
3167 a runtime error is raised. Note that the \cvCPyCross{Set*D} function can be used
3168 safely for both single-channel and multiple-channel arrays, though they
3169 are a bit slower.
3170
3171 In the case of a sparse array the functions create the node if it does not yet exist.
3172
3173 \else % }{
3174
3175 \cvCPyFunc{SetReal1D}
3176 Set a specific array element.
3177
3178 \cvdefPy{ SetReal1D(arr, idx, value) -> None }
3179
3180 \begin{description}
3181 \cvarg{arr}{Input array}
3182 \cvarg{idx}{Zero-based element index}
3183 \cvarg{value}{The value to assign to the element}
3184 \end{description}
3185
3186 Sets a specific array element.  Array must have dimension 1.
3187
3188 \cvCPyFunc{SetReal2D}
3189 Set a specific array element.
3190
3191 \cvdefPy{ SetReal2D(arr, idx0, idx1, value) -> None }
3192
3193 \begin{description}
3194 \cvarg{arr}{Input array}
3195 \cvarg{idx0}{Zero-based element row index}
3196 \cvarg{idx1}{Zero-based element column index}
3197 \cvarg{value}{The value to assign to the element}
3198 \end{description}
3199
3200 Sets a specific array element.  Array must have dimension 2.
3201
3202 \cvCPyFunc{SetReal3D}
3203 Set a specific array element.
3204
3205 \cvdefPy{ SetReal3D(arr, idx0, idx1, idx2, value) -> None }
3206
3207 \begin{description}
3208 \cvarg{arr}{Input array}
3209 \cvarg{idx0}{Zero-based element index}
3210 \cvarg{idx1}{Zero-based element index}
3211 \cvarg{idx2}{Zero-based element index}
3212 \cvarg{value}{The value to assign to the element}
3213 \end{description}
3214
3215 Sets a specific array element.  Array must have dimension 3.
3216
3217 \cvCPyFunc{SetRealND}
3218 Set a specific array element.
3219
3220 \cvdefPy{ SetRealND(arr, indices, value) -> None }
3221
3222 \begin{description}
3223 \cvarg{arr}{Input array}
3224 \cvarg{indices}{List of zero-based element indices}
3225 \cvarg{value}{The value to assign to the element}
3226 \end{description}
3227
3228 Sets a specific array element.  The length of array indices must be the same as the dimension of the array.
3229 \fi % }
3230
3231 \cvCPyFunc{SetZero}
3232 Clears the array.
3233
3234 \cvdefC{void cvSetZero(CvArr* arr);}
3235 \cvdefPy{SetZero(arr)-> None}
3236
3237 \ifC
3238 \begin{lstlisting}
3239 #define cvZero cvSetZero
3240 \end{lstlisting}
3241 \fi
3242
3243 \begin{description}
3244 \cvarg{arr}{Array to be cleared}
3245 \end{description}
3246
3247 The function clears the array. In the case of dense arrays (CvMat, CvMatND or IplImage), cvZero(array) is equivalent to cvSet(array,cvScalarAll(0),0).
3248 In the case of sparse arrays all the elements are removed.
3249
3250 \cvCPyFunc{Solve}
3251 Solves a linear system or least-squares problem.
3252
3253 \cvdefC{int cvSolve(const CvArr* src1, const CvArr* src2, CvArr* dst, int method=CV\_LU);}
3254 \cvdefPy{Solve(A,B,X,method=CV\_LU)-> None}
3255
3256 \begin{description}
3257 \cvarg{A}{The source matrix}
3258 \cvarg{B}{The right-hand part of the linear system}
3259 \cvarg{X}{The output solution}
3260 \cvarg{method}{The solution (matrix inversion) method
3261 \begin{description}
3262  \cvarg{CV\_LU}{Gaussian elimination with optimal pivot element chosen}
3263  \cvarg{CV\_SVD}{Singular value decomposition (SVD) method}
3264  \cvarg{CV\_SVD\_SYM}{SVD method for a symmetric positively-defined matrix.}
3265 \end{description}}
3266 \end{description}
3267
3268 The function solves a linear system or least-squares problem (the latter is possible with SVD methods):
3269
3270 \[
3271 \texttt{dst} = argmin_X||\texttt{src1} \, \texttt{X} - \texttt{src2}||
3272 \]
3273
3274 If \texttt{CV\_LU} method is used, the function returns 1 if \texttt{src1} is non-singular and 0 otherwise; in the latter case \texttt{dst} is not valid.
3275
3276 \cvCPyFunc{SolveCubic}
3277 Finds the real roots of a cubic equation.
3278
3279 \cvdefC{void cvSolveCubic(const CvArr* coeffs, CvArr* roots);}
3280 \cvdefPy{SolveCubic(coeffs,roots)-> None}
3281
3282 \begin{description}
3283 \cvarg{coeffs}{The equation coefficients, an array of 3 or 4 elements}
3284 \cvarg{roots}{The output array of real roots which should have 3 elements}
3285 \end{description}
3286
3287 The function finds the real roots of a cubic equation:
3288
3289 If coeffs is a 4-element vector:
3290
3291 \[
3292 \texttt{coeffs}[0] x^3 + \texttt{coeffs}[1] x^2 + \texttt{coeffs}[2] x + \texttt{coeffs}[3] = 0
3293 \]
3294
3295 or if coeffs is 3-element vector:
3296
3297 \[
3298 x^3 + \texttt{coeffs}[0] x^2 + \texttt{coeffs}[1] x + \texttt{coeffs}[2] = 0
3299 \]
3300
3301 The function returns the number of real roots found. The roots are
3302 stored to \texttt{root} array, which is padded with zeros if there is
3303 only one root.
3304
3305 \cvCPyFunc{Split}
3306 Divides multi-channel array into several single-channel arrays or extracts a single channel from the array.
3307
3308 \cvdefC{void cvSplit(const CvArr* src, CvArr* dst0, CvArr* dst1,
3309               CvArr* dst2, CvArr* dst3);}
3310 \cvdefPy{Split(src,dst0,dst1,dst2,dst3)-> None}
3311
3312 \begin{description}
3313 \cvarg{src}{Source array}
3314 \cvarg{dst0}{Destination channel 0}
3315 \cvarg{dst1}{Destination channel 1}
3316 \cvarg{dst2}{Destination channel 2}
3317 \cvarg{dst3}{Destination channel 3}
3318 \end{description}
3319
3320 \begin{lstlisting}
3321 #define cvCvtPixToPlane cvSplit
3322 \end{lstlisting}
3323
3324 The function divides a multi-channel array into separate
3325 single-channel arrays. Two modes are available for the operation. If the
3326 source array has N channels then if the first N destination channels
3327 are not NULL, they all are extracted from the source array;
3328 if only a single destination channel of the first N is not NULL, this
3329 particular channel is extracted; otherwise an error is raised. The rest
3330 of the destination channels (beyond the first N) must always be NULL. For
3331 IplImage \cvCPyCross{Copy} with COI set can be also used to extract a single
3332 channel from the image.
3333
3334
3335 \cvCPyFunc{Sqrt}
3336 Calculates the square root.
3337
3338 \cvdefC{float cvSqrt(float value);}
3339 \cvdefPy{Sqrt(value)-> float}
3340
3341 \begin{description}
3342 \cvarg{value}{The input floating-point value}
3343 \end{description}
3344
3345
3346 The function calculates the square root of the argument. If the argument is negative, the result is not determined.
3347
3348 \cvCPyFunc{Sub}
3349 Computes the per-element difference between two arrays.
3350
3351 \cvdefC{void cvSub(const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask=NULL);}
3352 \cvdefPy{Sub(src1,src2,dst,mask=NULL)-> None}
3353
3354 \begin{description}
3355 \cvarg{src1}{The first source array}
3356 \cvarg{src2}{The second source array}
3357 \cvarg{dst}{The destination array}
3358 \cvarg{mask}{Operation mask, 8-bit single channel array; specifies elements of the destination array to be changed}
3359 \end{description}
3360
3361
3362 The function subtracts one array from another one:
3363
3364 \begin{lstlisting}
3365 dst(I)=src1(I)-src2(I) if mask(I)!=0
3366 \end{lstlisting}
3367
3368 All the arrays must have the same type, except the mask, and the same size (or ROI size).
3369 For types that have limited range this operation is saturating.
3370
3371 \cvCPyFunc{SubRS}
3372 Computes the difference between a scalar and an array.
3373
3374 \cvdefC{void cvSubRS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL);}
3375 \cvdefPy{SubRS(src,value,dst,mask=NULL)-> None}
3376
3377 \begin{description}
3378 \cvarg{src}{The first source array}
3379 \cvarg{value}{Scalar to subtract from}
3380 \cvarg{dst}{The destination array}
3381 \cvarg{mask}{Operation mask, 8-bit single channel array; specifies elements of the destination array to be changed}
3382 \end{description}
3383
3384 The function subtracts every element of source array from a scalar:
3385
3386 \begin{lstlisting}
3387 dst(I)=value-src(I) if mask(I)!=0
3388 \end{lstlisting}
3389
3390 All the arrays must have the same type, except the mask, and the same size (or ROI size).
3391 For types that have limited range this operation is saturating.
3392
3393 \cvCPyFunc{SubS}
3394 Computes the difference between an array and a scalar.
3395
3396 \cvdefC{void cvSubS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL);}
3397 \cvdefPy{SubS(src,value,dst,mask=NULL)-> None}
3398
3399 \begin{description}
3400 \cvarg{src}{The source array}
3401 \cvarg{value}{Subtracted scalar}
3402 \cvarg{dst}{The destination array}
3403 \cvarg{mask}{Operation mask, 8-bit single channel array; specifies elements of the destination array to be changed}
3404 \end{description}
3405
3406 The function subtracts a scalar from every element of the source array:
3407
3408 \begin{lstlisting}
3409 dst(I)=src(I)-value if mask(I)!=0
3410 \end{lstlisting}
3411
3412 All the arrays must have the same type, except the mask, and the same size (or ROI size).
3413 For types that have limited range this operation is saturating.
3414
3415
3416 \cvCPyFunc{Sum}
3417 Adds up array elements.
3418
3419 \cvdefC{CvScalar cvSum(const CvArr* arr);}
3420 \cvdefPy{Sum(arr)-> CvScalar}
3421
3422 \begin{description}
3423 \cvarg{arr}{The array}
3424 \end{description}
3425
3426
3427 The function calculates the sum \texttt{S} of array elements, independently for each channel:
3428
3429 \[ \sum_I \texttt{arr}(I)_c \]
3430
3431 If the array is \texttt{IplImage} and COI is set, the function processes the selected channel only and stores the sum to the first scalar component.
3432
3433
3434 \cvCPyFunc{SVBkSb}
3435 Performs singular value back substitution.
3436
3437 \cvdefC{
3438 void  cvSVBkSb(\par const CvArr* W,\par const CvArr* U,\par const CvArr* V,\par const CvArr* B,\par CvArr* X,\par int flags);}
3439 \cvdefPy{SVBkSb(W,U,V,B,X,flags)-> None}
3440
3441 \begin{description}
3442 \cvarg{W}{Matrix or vector of singular values}
3443 \cvarg{U}{Left orthogonal matrix (tranposed, perhaps)}
3444 \cvarg{V}{Right orthogonal matrix (tranposed, perhaps)}
3445 \cvarg{B}{The matrix to multiply the pseudo-inverse of the original matrix \texttt{A} by. This is an optional parameter. If it is omitted then it is assumed to be an identity matrix of an appropriate size (so that \texttt{X} will be the reconstructed pseudo-inverse of \texttt{A}).}
3446 \cvarg{X}{The destination matrix: result of back substitution}
3447 \cvarg{flags}{Operation flags, should match exactly to the \texttt{flags} passed to \cvCPyCross{SVD}}
3448 \end{description}
3449
3450 The function calculates back substitution for decomposed matrix \texttt{A} (see \cvCPyCross{SVD} description) and matrix \texttt{B}:
3451
3452 \[
3453 \texttt{X} = \texttt{V} \texttt{W}^{-1} \texttt{U}^T \texttt{B}
3454 \]
3455
3456 where
3457
3458 \[
3459 W^{-1}_{(i,i)}=
3460 \fork
3461 {1/W_{(i,i)}}{if $W_{(i,i)} > \epsilon \sum_i{W_{(i,i)}}$ }
3462 {0}{otherwise}
3463 \]
3464
3465 and $\epsilon$ is a small number that depends on the matrix data type.
3466
3467 This function together with \cvCPyCross{SVD} is used inside \cvCPyCross{Invert}
3468 and \cvCPyCross{Solve}, and the possible reason to use these (svd and bksb)
3469 "low-level" function, is to avoid allocation of temporary matrices inside
3470 the high-level counterparts (inv and solve).
3471
3472 \cvCPyFunc{SVD}
3473 Performs singular value decomposition of a real floating-point matrix.
3474
3475 \cvdefC{void cvSVD(\par CvArr* A, \par CvArr* W, \par CvArr* U=NULL, \par CvArr* V=NULL, \par int flags=0);}
3476 \cvdefPy{SVD(A,W, U = None, V = None, flags=0)-> None}
3477
3478 \begin{description}
3479 \cvarg{A}{Source $\texttt{M} \times \texttt{N}$ matrix}
3480 \cvarg{W}{Resulting singular value diagonal matrix ($\texttt{M} \times \texttt{N}$ or $\min(\texttt{M}, \texttt{N})  \times \min(\texttt{M}, \texttt{N})$) or $\min(\texttt{M},\texttt{N}) \times 1$ vector of the singular values}
3481 \cvarg{U}{Optional left orthogonal matrix, $\texttt{M} \times \min(\texttt{M}, \texttt{N})$ (when \texttt{CV\_SVD\_U\_T} is not set), or $\min(\texttt{M},\texttt{N}) \times \texttt{M}$ (when \texttt{CV\_SVD\_U\_T} is set), or $\texttt{M} \times \texttt{M}$ (regardless of \texttt{CV\_SVD\_U\_T} flag).}
3482 \cvarg{V}{Optional right orthogonal matrix, $\texttt{N} \times \min(\texttt{M}, \texttt{N})$ (when \texttt{CV\_SVD\_V\_T} is not set), or $\min(\texttt{M},\texttt{N}) \times \texttt{N}$ (when \texttt{CV\_SVD\_V\_T} is set), or $\texttt{N} \times \texttt{N}$ (regardless of \texttt{CV\_SVD\_V\_T} flag).}
3483 \cvarg{flags}{Operation flags; can be 0 or a combination of the following values:
3484 \begin{description}
3485   \cvarg{CV\_SVD\_MODIFY\_A}{enables modification of matrix \texttt{A} during the operation. It speeds up the processing.}
3486   \cvarg{CV\_SVD\_U\_T}{means that the transposed matrix \texttt{U} is returned. Specifying the flag speeds up the processing.}
3487   \cvarg{CV\_SVD\_V\_T}{means that the transposed matrix \texttt{V} is returned. Specifying the flag speeds up the processing.}
3488 \end{description}}
3489 \end{description}
3490
3491 The function decomposes matrix \texttt{A} into the product of a diagonal matrix and two 
3492
3493 orthogonal matrices:
3494
3495 \[
3496 A=U \, W \, V^T
3497 \]
3498
3499 where $W$ is a diagonal matrix of singular values that can be coded as a
3500 1D vector of singular values and $U$ and $V$. All the singular values
3501 are non-negative and sorted (together with $U$ and $V$ columns)
3502 in descending order.
3503
3504 An SVD algorithm is numerically robust and its typical applications include:
3505
3506 \begin{itemize}
3507   \item accurate eigenvalue problem solution when matrix \texttt{A}
3508   is a square, symmetric, and positively defined matrix, for example, when
3509   it is a covariance matrix. $W$ in this case will be a vector/matrix
3510   of the eigenvalues, and $U = V$ will be a matrix of the eigenvectors.
3511   \item accurate solution of a poor-conditioned linear system.
3512   \item least-squares solution of an overdetermined linear system. This and the preceeding is done by using the \cvCPyCross{Solve} function with the \texttt{CV\_SVD} method.
3513   \item accurate calculation of different matrix characteristics such as the matrix rank (the number of non-zero singular values), condition number (ratio of the largest singular value to the smallest one), and determinant (absolute value of the determinant is equal to the product of singular values). 
3514 \end{itemize}
3515
3516 \cvCPyFunc{Trace}
3517 Returns the trace of a matrix.
3518
3519 \cvdefC{CvScalar cvTrace(const CvArr* mat);}
3520 \cvdefPy{Trace(mat)-> CvScalar}
3521
3522 \begin{description}
3523 \cvarg{mat}{The source matrix}
3524 \end{description}
3525
3526
3527 The function returns the sum of the diagonal elements of the matrix \texttt{src1}.
3528
3529 \[ tr(\texttt{mat}) = \sum_i \texttt{mat}(i,i) \]
3530
3531 \cvCPyFunc{Transform}
3532
3533 Performs matrix transformation of every array element.
3534
3535 \cvdefC{void cvTransform(const CvArr* src, CvArr* dst, const CvMat* transmat, const CvMat* shiftvec=NULL);}
3536 \cvdefPy{Transform(src,dst,transmat,shiftvec=NULL)-> None}
3537
3538 \begin{description}
3539 \cvarg{src}{The first source array}
3540 \cvarg{dst}{The destination array}
3541 \cvarg{transmat}{Transformation matrix}
3542 \cvarg{shiftvec}{Optional shift vector}
3543 \end{description}
3544
3545 The function performs matrix transformation of every element of array \texttt{src} and stores the results in \texttt{dst}:
3546
3547 \[
3548 dst(I) = transmat \cdot src(I) + shiftvec %  or   dst(I),,k,,=sum,,j,,(transmat(k,j)*src(I),,j,,) + shiftvec(k)
3549 \]
3550
3551 That is, every element of an \texttt{N}-channel array \texttt{src} is
3552 considered as an \texttt{N}-element vector which is transformed using
3553 a $\texttt{M} \times \texttt{N}$ matrix \texttt{transmat} and shift
3554 vector \texttt{shiftvec} into an element of \texttt{M}-channel array
3555 \texttt{dst}. There is an option to embedd \texttt{shiftvec} into
3556 \texttt{transmat}. In this case \texttt{transmat} should be a $\texttt{M}
3557 \times (N+1)$ matrix and the rightmost column is treated as the shift
3558 vector.
3559
3560 Both source and destination arrays should have the same depth and the
3561 same size or selected ROI size. \texttt{transmat} and \texttt{shiftvec}
3562 should be real floating-point matrices.
3563
3564 The function may be used for geometrical transformation of n dimensional
3565 point set, arbitrary linear color space transformation, shuffling the
3566 channels and so forth.
3567
3568 \cvCPyFunc{Transpose}
3569 Transposes a matrix.
3570
3571 \cvdefC{void cvTranspose(const CvArr* src, CvArr* dst);}
3572 \cvdefPy{Transpose(src,dst)-> None}
3573
3574 \begin{description}
3575 \cvarg{src}{The source matrix}
3576 \cvarg{dst}{The destination matrix}
3577 \end{description}
3578
3579 \begin{lstlisting}
3580 #define cvT cvTranspose
3581 \end{lstlisting}
3582
3583 The function transposes matrix \texttt{src1}:
3584
3585 \[ \texttt{dst}(i,j) = \texttt{src}(j,i) \]
3586
3587 Note that no complex conjugation is done in the case of a complex
3588 matrix. Conjugation should be done separately: look at the sample code
3589 in \cvCPyCross{XorS} for an example.
3590
3591 \cvCPyFunc{Xor}
3592 Performs per-element bit-wise "exclusive or" operation on two arrays.
3593
3594 \cvdefC{void cvXor(const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask=NULL);}
3595 \cvdefPy{Xor(src1,src2,dst,mask=NULL)-> None}
3596
3597 \begin{description}
3598 \cvarg{src1}{The first source array}
3599 \cvarg{src2}{The second source array}
3600 \cvarg{dst}{The destination array}
3601 \cvarg{mask}{Operation mask, 8-bit single channel array; specifies elements of the destination array to be changed}
3602 \end{description}
3603
3604 The function calculates per-element bit-wise logical conjunction of two arrays:
3605
3606 \begin{lstlisting}
3607 dst(I)=src1(I)^src2(I) if mask(I)!=0
3608 \end{lstlisting}
3609
3610 In the case of floating-point arrays their bit representations are used for the operation. All the arrays must have the same type, except the mask, and the same size.
3611
3612 \cvCPyFunc{XorS}
3613 Performs per-element bit-wise "exclusive or" operation on an array and a scalar.
3614
3615 \cvdefC{void cvXorS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL);}
3616 \cvdefPy{XorS(src,value,dst,mask=NULL)-> None}
3617
3618 \begin{description}
3619 \cvarg{src}{The source array}
3620 \cvarg{value}{Scalar to use in the operation}
3621 \cvarg{dst}{The destination array}
3622 \cvarg{mask}{Operation mask, 8-bit single channel array; specifies elements of the destination array to be changed}
3623 \end{description}
3624
3625
3626 The function XorS calculates per-element bit-wise conjunction of an array and a scalar:
3627
3628 \begin{lstlisting}
3629 dst(I)=src(I)^value if mask(I)!=0
3630 \end{lstlisting}
3631
3632 Prior to the actual operation, the scalar is converted to the same type as that of the array(s). In the case of floating-point arrays their bit representations are used for the operation. All the arrays must have the same type, except the mask, and the same size
3633
3634 The following sample demonstrates how to conjugate complex vector by switching the most-significant bit of imaging part:
3635
3636 \begin{lstlisting}
3637
3638 float a[] = { 1, 0, 0, 1, -1, 0, 0, -1 }; /* 1, j, -1, -j */
3639 CvMat A = cvMat(4, 1, CV\_32FC2, &a);
3640 int i, negMask = 0x80000000;
3641 cvXorS(&A, cvScalar(0, *(float*)&negMask, 0, 0 ), &A, 0);
3642 for(i = 0; i < 4; i++ )
3643     printf("(%.1f, %.1f) ", a[i*2], a[i*2+1]);
3644
3645 \end{lstlisting}
3646
3647 The code should print:
3648
3649 \begin{lstlisting}
3650 (1.0,0.0) (0.0,-1.0) (-1.0,0.0) (0.0,1.0)
3651 \end{lstlisting}
3652
3653 \cvCPyFunc{mGet}
3654 Returns the particular element of single-channel floating-point matrix.
3655
3656 \cvdefC{double cvmGet(const CvMat* mat, int row, int col);}
3657 \cvdefPy{mGet(mat,row,col)-> double}
3658
3659 \begin{description}
3660 \cvarg{mat}{Input matrix}
3661 \cvarg{row}{The zero-based index of row}
3662 \cvarg{col}{The zero-based index of column}
3663 \end{description}
3664
3665 The function is a fast replacement for \cvCPyCross{GetReal2D}
3666 in the case of single-channel floating-point matrices. It is faster because
3667 it is inline, it does fewer checks for array type and array element type,
3668 and it checks for the row and column ranges only in debug mode.
3669
3670 \cvCPyFunc{mSet}
3671 Returns a specific element of a single-channel floating-point matrix.
3672
3673 \cvdefC{void cvmSet(CvMat* mat, int row, int col, double value);}
3674 \cvdefPy{mSet(mat,row,col,value)-> None}
3675
3676 \begin{description}
3677 \cvarg{mat}{The matrix}
3678 \cvarg{row}{The zero-based index of row}
3679 \cvarg{col}{The zero-based index of column}
3680 \cvarg{value}{The new value of the matrix element}
3681 \end{description}
3682
3683
3684 The function is a fast replacement for \cvCPyCross{SetReal2D}
3685 in the case of single-channel floating-point matrices. It is faster because
3686 it is inline, it does fewer checks for array type and array element type, 
3687 and it checks for the row and column ranges only in debug mode.
3688
3689 \fi
3690
3691 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3692 %                                                                              %
3693 %                                  C++ API                                     % 
3694 %                                                                              %
3695 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3696
3697 \ifCpp
3698
3699 \cvCppFunc{abs}
3700 Computes absolute value of each matrix element
3701
3702 \cvdefCpp{MatExpr<...> abs(const Mat\& src);\newline
3703 MatExpr<...> abs(const MatExpr<...>\& src);}
3704
3705 \begin{description}
3706 \cvarg{src}{matrix or matrix expression}
3707 \end{description}
3708
3709 \texttt{abs} is a meta-function that is expanded to one of \cvCppCross{absdiff} forms:
3710
3711 \begin{itemize}
3712     \item \texttt{C = abs(A-B)} is equivalent to \texttt{absdiff(A, B, C)} and
3713     \item \texttt{C = abs(A)} is equivalent to \texttt{absdiff(A, Scalar::all(0), C)}.
3714     \item \texttt{C = Mat\_<Vec<uchar,\emph{n}> >(abs(A*$\alpha$ + $\beta$))} is equivalent to \texttt{convertScaleAbs(A, C, alpha, beta)}
3715 \end{itemize}
3716
3717 The output matrix will have the same size and the same type as the input one
3718 (except for the last case, where \texttt{C} will be \texttt{depth=CV\_8U}).
3719
3720 See also: \cross{Matrix Expressions}, \cvCppCross{absdiff}, \hyperref[cppfunc.saturatecast]{saturate\_cast}
3721
3722 \cvCppFunc{absdiff}
3723 Computes per-element absolute difference between 2 arrays or between array and a scalar.
3724
3725 \cvdefCpp{void absdiff(const Mat\& src1, const Mat\& src2, Mat\& dst);\newline
3726 void absdiff(const Mat\& src1, const Scalar\& sc, Mat\& dst);\newline
3727 void absdiff(const MatND\& src1, const MatND\& src2, MatND\& dst);\newline
3728 void absdiff(const MatND\& src1, const Scalar\& sc, MatND\& dst);}
3729
3730 \begin{description}
3731 \cvarg{src1}{The first input array}
3732 \cvarg{src2}{The second input array; Must be the same size and same type as \texttt{src1}}
3733 \cvarg{sc}{Scalar; the second input parameter}
3734 \cvarg{dst}{The destination array; it will have the same size and same type as \texttt{src1}; see \texttt{Mat::create}}
3735 \end{description}
3736
3737 The functions \texttt{absdiff} compute:
3738 \begin{itemize}
3739     \item absolute difference between two arrays
3740     \[\texttt{dst}(I) = \texttt{saturate}(|\texttt{src1}(I) - \texttt{src2}(I)|)\]
3741     \item or absolute difference between array and a scalar:
3742     \[\texttt{dst}(I) = \texttt{saturate}(|\texttt{src1}(I) - \texttt{sc}|)\]
3743 \end{itemize}
3744 where \texttt{I} is multi-dimensional index of array elements.
3745 in the case of multi-channel arrays each channel is processed independently.
3746
3747 See also: \cvCppCross{abs}, \hyperref[cppfunc.saturatecast]{saturate\_cast}
3748
3749 \cvCppFunc{add}
3750 Computes the per-element sum of two arrays or an array and a scalar.
3751
3752 \cvdefCpp{void add(const Mat\& src1, const Mat\& src2, Mat\& dst);\newline
3753 void add(const Mat\& src1, const Mat\& src2, \par Mat\& dst, const Mat\& mask);\newline
3754 void add(const Mat\& src1, const Scalar\& sc, \par Mat\& dst, const Mat\& mask=Mat());\newline
3755 void add(const MatND\& src1, const MatND\& src2, MatND\& dst);\newline
3756 void add(const MatND\& src1, const MatND\& src2, \par MatND\& dst, const MatND\& mask);\newline
3757 void add(const MatND\& src1, const Scalar\& sc, \par MatND\& dst, const MatND\& mask=MatND());}
3758
3759 \begin{description}
3760 \cvarg{src1}{The first source array}
3761 \cvarg{src2}{The second source array. It must have the same size and same type as \texttt{src1}}
3762 \cvarg{sc}{Scalar; the second input parameter}
3763 \cvarg{dst}{The destination array; it will have the same size and same type as \texttt{src1}; see \texttt{Mat::create}}
3764 \cvarg{mask}{The optional operation mask, 8-bit single channel array;
3765              specifies elements of the destination array to be changed}
3766 \end{description}
3767
3768 The functions \texttt{add} compute:
3769 \begin{itemize}
3770     \item the sum of two arrays:
3771     \[\texttt{dst}(I) = \texttt{saturate}(\texttt{src1}(I) + \texttt{src2}(I))\quad\texttt{if mask}(I)\ne0\]
3772     \item or the sum of array and a scalar:
3773     \[\texttt{dst}(I) = \texttt{saturate}(\texttt{src1}(I) + \texttt{sc})\quad\texttt{if mask}(I)\ne0\]
3774 \end{itemize}
3775 where \texttt{I} is multi-dimensional index of array elements.
3776
3777 The first function in the above list can be replaced with matrix expressions:
3778 \begin{lstlisting}
3779 dst = src1 + src2;
3780 dst += src1; // equivalent to add(dst, src1, dst);
3781 \end{lstlisting}
3782
3783 in the case of multi-channel arrays each channel is processed independently.
3784
3785 See also: \cvCppCross{subtract}, \cvCppCross{addWeighted}, \cvCppCross{scaleAdd}, \cvCppCross{convertScale},
3786 \cross{Matrix Expressions}, \hyperref[cppfunc.saturatecast]{saturate\_cast}.
3787
3788 \cvCppFunc{addWeighted}
3789 Computes the weighted sum of two arrays.
3790
3791 \cvdefCpp{void addWeighted(const Mat\& src1, double alpha, const Mat\& src2,\par
3792                  double beta, double gamma, Mat\& dst);\newline
3793 void addWeighted(const MatND\& src1, double alpha, const MatND\& src2,\par
3794                  double beta, double gamma, MatND\& dst);
3795 }
3796
3797 \begin{description}
3798 \cvarg{src1}{The first source array}
3799 \cvarg{alpha}{Weight for the first array elements}
3800 \cvarg{src2}{The second source array; must have the same size and same type as \texttt{src1}}
3801 \cvarg{beta}{Weight for the second array elements}
3802 \cvarg{dst}{The destination array; it will have the same size and same type as \texttt{src1}}
3803 \cvarg{gamma}{Scalar, added to each sum}
3804 \end{description}
3805
3806 The functions \texttt{addWeighted} calculate the weighted sum of two arrays as follows:
3807 \[\texttt{dst}(I)=\texttt{saturate}(\texttt{src1}(I)*\texttt{alpha} + \texttt{src2}(I)*\texttt{beta} + \texttt{gamma})\]
3808 where \texttt{I} is multi-dimensional index of array elements.
3809
3810 The first function can be replaced with a matrix expression:
3811 \begin{lstlisting}
3812 dst = src1*alpha + src2*beta + gamma;
3813 \end{lstlisting}
3814
3815 In the case of multi-channel arrays each channel is processed independently.
3816
3817 See also: \cvCppCross{add}, \cvCppCross{subtract}, \cvCppCross{scaleAdd}, \cvCppCross{convertScale},
3818 \cross{Matrix Expressions}, \hyperref[cppfunc.saturatecast]{saturate\_cast}.
3819
3820 \cvfunc{cv::bitwise\_and}\label{cppfunc.bitwise.and}
3821 Calculates per-element bit-wise conjunction of two arrays and an array and a scalar.
3822
3823 \cvdefCpp{void bitwise\_and(const Mat\& src1, const Mat\& src2,\par Mat\& dst, const Mat\& mask=Mat());\newline
3824 void bitwise\_and(const Mat\& src1, const Scalar\& sc,\par  Mat\& dst, const Mat\& mask=Mat());\newline
3825 void bitwise\_and(const MatND\& src1, const MatND\& src2,\par  MatND\& dst, const MatND\& mask=MatND());\newline
3826 void bitwise\_and(const MatND\& src1, const Scalar\& sc,\par  MatND\& dst, const MatND\& mask=MatND());}
3827
3828 \begin{description}
3829 \cvarg{src1}{The first source array}
3830 \cvarg{src2}{The second source array. It must have the same size and same type as \texttt{src1}}
3831 \cvarg{sc}{Scalar; the second input parameter}
3832 \cvarg{dst}{The destination array; it will have the same size and same type as \texttt{src1}; see \texttt{Mat::create}}
3833 \cvarg{mask}{The optional operation mask, 8-bit single channel array;
3834              specifies elements of the destination array to be changed}
3835 \end{description}
3836
3837 The functions \texttt{bitwise\_and} compute per-element bit-wise logical conjunction:
3838 \begin{itemize}
3839     \item of two arrays
3840     \[\texttt{dst}(I) = \texttt{src1}(I) \wedge \texttt{src2}(I)\quad\texttt{if mask}(I)\ne0\]
3841     \item or array and a scalar:
3842     \[\texttt{dst}(I) = \texttt{src1}(I) \wedge \texttt{sc}\quad\texttt{if mask}(I)\ne0\]
3843 \end{itemize}
3844
3845 In the case of floating-point arrays their machine-specific bit representations (usually IEEE754-compliant) are used for the operation, and in the case of multi-channel arrays each channel is processed independently.
3846
3847 See also: \hyperref[cppfunc.bitwise.and]{bitwise\_and}, \hyperref[cppfunc.bitwise.not]{bitwise\_not}, \hyperref[cppfunc.bitwise.xor]{bitwise\_xor}
3848
3849 \cvfunc{cv::bitwise\_not}\label{cppfunc.bitwise.not}
3850 Inverts every bit of array
3851
3852 \cvdefCpp{void bitwise\_not(const Mat\& src, Mat\& dst);\newline
3853 void bitwise\_not(const MatND\& src, MatND\& dst);}
3854 \begin{description}
3855 \cvarg{src1}{The source array}
3856 \cvarg{dst}{The destination array; it is reallocated to be of the same size and
3857             the same type as \texttt{src}; see \texttt{Mat::create}}
3858 \cvarg{mask}{The optional operation mask, 8-bit single channel array;
3859              specifies elements of the destination array to be changed}
3860 \end{description}
3861
3862 The functions \texttt{bitwise\_not} compute per-element bit-wise inversion of the source array:
3863 \[\texttt{dst}(I) = \neg\texttt{src}(I)\]
3864
3865 In the case of floating-point source array its machine-specific bit representation (usually IEEE754-compliant) is used for the operation. in the case of multi-channel arrays each channel is processed independently.
3866
3867 See also: \hyperref[cppfunc.bitwise.and]{bitwise\_and}, \hyperref[cppfunc.bitwise.or]{bitwise\_or}, \hyperref[cppfunc.bitwise.xor]{bitwise\_xor}
3868
3869
3870 \cvfunc{cv::bitwise\_or}\label{cppfunc.bitwise.or}
3871 Calculates per-element bit-wise disjunction of two arrays and an array and a scalar.
3872
3873 \cvdefCpp{void bitwise\_or(const Mat\& src1, const Mat\& src2,\par Mat\& dst, const Mat\& mask=Mat());\newline
3874 void bitwise\_or(const Mat\& src1, const Scalar\& sc,\par  Mat\& dst, const Mat\& mask=Mat());\newline
3875 void bitwise\_or(const MatND\& src1, const MatND\& src2,\par  MatND\& dst, const MatND\& mask=MatND());\newline
3876 void bitwise\_or(const MatND\& src1, const Scalar\& sc,\par  MatND\& dst, const MatND\& mask=MatND());}
3877 \begin{description}
3878 \cvarg{src1}{The first source array}
3879 \cvarg{src2}{The second source array. It must have the same size and same type as \texttt{src1}}
3880 \cvarg{sc}{Scalar; the second input parameter}
3881 \cvarg{dst}{The destination array; it is reallocated to be of the same size and
3882             the same type as \texttt{src1}; see \texttt{Mat::create}}
3883 \cvarg{mask}{The optional operation mask, 8-bit single channel array;
3884              specifies elements of the destination array to be changed}
3885 \end{description}
3886
3887 The functions \texttt{bitwise\_or} compute per-element bit-wise logical disjunction
3888 \begin{itemize}
3889     \item of two arrays
3890     \[\texttt{dst}(I) = \texttt{src1}(I) \vee \texttt{src2}(I)\quad\texttt{if mask}(I)\ne0\]
3891     \item or array and a scalar:
3892     \[\texttt{dst}(I) = \texttt{src1}(I) \vee \texttt{sc}\quad\texttt{if mask}(I)\ne0\]
3893 \end{itemize}
3894
3895 In the case of floating-point arrays their machine-specific bit representations (usually IEEE754-compliant) are used for the operation. in the case of multi-channel arrays each channel is processed independently.
3896
3897 See also: \hyperref[cppfunc.bitwise.and]{bitwise\_and}, \hyperref[cppfunc.bitwise.not]{bitwise\_not}, \hyperref[cppfunc.bitwise.or]{bitwise\_or}
3898
3899 \cvfunc{cv::bitwise\_xor}\label{cppfunc.bitwise.xor}
3900 Calculates per-element bit-wise "exclusive or" operation on two arrays and an array and a scalar.
3901
3902 \cvdefCpp{void bitwise\_xor(const Mat\& src1, const Mat\& src2,\par Mat\& dst, const Mat\& mask=Mat());\newline
3903 void bitwise\_xor(const Mat\& src1, const Scalar\& sc,\par  Mat\& dst, const Mat\& mask=Mat());\newline
3904 void bitwise\_xor(const MatND\& src1, const MatND\& src2,\par  MatND\& dst, const MatND\& mask=MatND());\newline
3905 void bitwise\_xor(const MatND\& src1, const Scalar\& sc,\par  MatND\& dst, const MatND\& mask=MatND());}
3906 \begin{description}
3907 \cvarg{src1}{The first source array}
3908 \cvarg{src2}{The second source array. It must have the same size and same type as \texttt{src1}}
3909 \cvarg{sc}{Scalar; the second input parameter}
3910 \cvarg{dst}{The destination array; it is reallocated to be of the same size and
3911             the same type as \texttt{src1}; see \texttt{Mat::create}}
3912 \cvarg{mask}{The optional operation mask, 8-bit single channel array;
3913              specifies elements of the destination array to be changed}
3914 \end{description}
3915
3916 The functions \texttt{bitwise\_xor} compute per-element bit-wise logical "exclusive or" operation
3917
3918 \begin{itemize}
3919     \item on two arrays
3920     \[\texttt{dst}(I) = \texttt{src1}(I) \oplus \texttt{src2}(I)\quad\texttt{if mask}(I)\ne0\]
3921     \item or array and a scalar:
3922     \[\texttt{dst}(I) = \texttt{src1}(I) \oplus \texttt{sc}\quad\texttt{if mask}(I)\ne0\]
3923 \end{itemize}
3924
3925 In the case of floating-point arrays their machine-specific bit representations (usually IEEE754-compliant) are used for the operation. in the case of multi-channel arrays each channel is processed independently.
3926
3927 See also: \hyperref[cppfunc.bitwise.and]{bitwise\_and}, \hyperref[cppfunc.bitwise.not]{bitwise\_not}, \hyperref[cppfunc.bitwise.or]{bitwise\_or}
3928
3929 \cvCppFunc{calcCovarMatrix}
3930 Calculates covariation matrix of a set of vectors
3931
3932 \cvdefCpp{void calcCovarMatrix( const Mat* samples, int nsamples,\par
3933                       Mat\& covar, Mat\& mean,\par
3934                       int flags, int ctype=CV\_64F);\newline
3935 void calcCovarMatrix( const Mat\& samples, Mat\& covar, Mat\& mean,\par
3936                       int flags, int ctype=CV\_64F);}
3937 \begin{description}
3938 \cvarg{samples}{The samples, stored as separate matrices, or as rows or columns of a single matrix}
3939 \cvarg{nsamples}{The number of samples when they are stored separately}
3940 \cvarg{covar}{The output covariance matrix; it will have type=\texttt{ctype} and square size}
3941 \cvarg{mean}{The input or output (depending on the flags) array - the mean (average) vector of the input vectors}
3942 \cvarg{flags}{The operation flags, a combination of the following values
3943 \begin{description}
3944 \cvarg{CV\_COVAR\_SCRAMBLED}{The output covariance matrix is calculated as:
3945 \[
3946  \texttt{scale} \cdot [ \texttt{vects} [0]- \texttt{mean} ,\texttt{vects} [1]- \texttt{mean} ,...]^T \cdot [\texttt{vects} [0]-\texttt{mean} ,\texttt{vects} [1]-\texttt{mean} ,...] 
3947 \],
3948 that is, the covariance matrix will be $\texttt{nsamples} \times \texttt{nsamples}$.
3949 Such an unusual covariance matrix is used for fast PCA
3950 of a set of very large vectors (see, for example, the EigenFaces technique
3951 for face recognition). Eigenvalues of this "scrambled" matrix will
3952 match the eigenvalues of the true covariance matrix and the "true"
3953 eigenvectors can be easily calculated from the eigenvectors of the
3954 "scrambled" covariance matrix.}
3955 \cvarg{CV\_COVAR\_NORMAL}{The output covariance matrix is calculated as:
3956 \[
3957  \texttt{scale} \cdot [ \texttt{vects} [0]- \texttt{mean} ,\texttt{vects} [1]- \texttt{mean} ,...] \cdot [\texttt{vects} [0]-\texttt{mean} ,\texttt{vects} [1]-\texttt{mean} ,...]^T 
3958 \],
3959 that is, \texttt{covar} will be a square matrix
3960 of the same size as the total number of elements in each
3961 input vector. One and only one of \texttt{CV\_COVAR\_SCRAMBLED} and
3962 \texttt{CV\_COVAR\_NORMAL} must be specified}
3963 \cvarg{CV\_COVAR\_USE\_AVG}{If the flag is specified, the function does not calculate \texttt{mean} from the input vectors, but, instead, uses the passed \texttt{mean} vector. This is useful if \texttt{mean} has been pre-computed or known a-priori, or if the covariance matrix is calculated by parts - in this case, \texttt{mean} is not a mean vector of the input sub-set of vectors, but rather the mean vector of the whole set.}
3964 \cvarg{CV\_COVAR\_SCALE}{If the flag is specified, the covariance matrix is scaled. In the "normal" mode \texttt{scale} is \texttt{1./nsamples}; in the "scrambled" mode \texttt{scale} is the reciprocal of the total number of elements in each input vector. By default (if the flag is not specified) the covariance matrix is not scaled (i.e. \texttt{scale=1}).}
3965
3966 \cvarg{CV\_COVAR\_ROWS}{[Only useful in the second variant of the function] The flag means that all the input vectors are stored as rows of the \texttt{samples} matrix. \texttt{mean} should be a single-row vector in this case.}
3967 \cvarg{CV\_COVAR\_COLS}{[Only useful in the second variant of the function] The flag means that all the input vectors are stored as columns of the \texttt{samples} matrix. \texttt{mean} should be a single-column vector in this case.}
3968
3969 \end{description}}
3970 \end{description}
3971
3972 The functions \texttt{calcCovarMatrix} calculate the covariance matrix
3973 and, optionally, the mean vector of the set of input vectors.
3974
3975 See also: \cvCppCross{PCA}, \cvCppCross{mulTransposed}, \cvCppCross{Mahalanobis}
3976
3977 \cvCppFunc{cartToPolar}
3978 Calculates the magnitude and angle of 2d vectors.
3979
3980 \cvdefCpp{void cartToPolar(const Mat\& x, const Mat\& y,\par
3981                  Mat\& magnitude, Mat\& angle,\par
3982                  bool angleInDegrees=false);}
3983 \begin{description}
3984 \cvarg{x}{The array of x-coordinates; must be single-precision or double-precision floating-point array}
3985 \cvarg{y}{The array of y-coordinates; it must have the same size and same type as \texttt{x}}
3986 \cvarg{magnitude}{The destination array of magnitudes of the same size and same type as \texttt{x}}
3987 \cvarg{angle}{The destination array of angles of the same size and same type as \texttt{x}.
3988 The angles are measured in radians $(0$ to $2 \pi )$ or in degrees (0 to 360 degrees).}
3989 \cvarg{angleInDegrees}{The flag indicating whether the angles are measured in radians, which is default mode, or in degrees}
3990 \end{description}
3991
3992 The function \texttt{cartToPolar} calculates either the magnitude, angle, or both of every 2d vector (x(I),y(I)):
3993
3994 \[
3995 \begin{array}{l}
3996 \texttt{magnitude}(I)=\sqrt{\texttt{x}(I)^2+\texttt{y}(I)^2},\\
3997 \texttt{angle}(I)=\texttt{atan2}(\texttt{y}(I), \texttt{x}(I))[\cdot180/\pi]
3998 \end{array}
3999 \]
4000
4001 The angles are calculated with $\sim\,0.3^\circ$ accuracy. For the (0,0) point, the angle is set to 0.
4002
4003 \cvCppFunc{checkRange}
4004 Checks every element of an input array for invalid values.
4005
4006 \cvdefCpp{bool checkRange(const Mat\& src, bool quiet=true, Point* pos=0,\par
4007                 double minVal=-DBL\_MAX, double maxVal=DBL\_MAX);\newline
4008 bool checkRange(const MatND\& src, bool quiet=true, int* pos=0,\par
4009                 double minVal=-DBL\_MAX, double maxVal=DBL\_MAX);}
4010 \begin{description}
4011 \cvarg{src}{The array to check}
4012 \cvarg{quiet}{The flag indicating whether the functions quietly return false when the array elements are out of range, or they throw an exception.}
4013 \cvarg{pos}{The optional output parameter, where the position of the first outlier is stored. In the second function \texttt{pos}, when not NULL, must be a pointer to array of \texttt{src.dims} elements}
4014 \cvarg{minVal}{The inclusive lower boundary of valid values range}
4015 \cvarg{maxVal}{The exclusive upper boundary of valid values range}
4016 \end{description}
4017
4018 The functions \texttt{checkRange} check that every array element is
4019 neither NaN nor $\pm \infty $. When \texttt{minVal < -DBL\_MAX} and \texttt{maxVal < DBL\_MAX}, then the functions also check that
4020 each value is between \texttt{minVal} and \texttt{maxVal}. in the case of multi-channel arrays each channel is processed independently.
4021 If some values are out of range, position of the first outlier is stored in \texttt{pos} (when $\texttt{pos}\ne0$), and then the functions either return false (when \texttt{quiet=true}) or throw an exception.
4022
4023
4024 \cvCppFunc{compare}
4025 Performs per-element comparison of two arrays or an array and scalar value.
4026
4027 \cvdefCpp{void compare(const Mat\& src1, const Mat\& src2, Mat\& dst, int cmpop);\newline
4028 void compare(const Mat\& src1, double value, \par Mat\& dst, int cmpop);\newline
4029 void compare(const MatND\& src1, const MatND\& src2, \par MatND\& dst, int cmpop);\newline
4030 void compare(const MatND\& src1, double value, \par MatND\& dst, int cmpop);}
4031 \begin{description}
4032 \cvarg{src1}{The first source array}
4033 \cvarg{src2}{The second source array; must have the same size and same type as \texttt{src1}}
4034 \cvarg{value}{The scalar value to compare each array element with}
4035 \cvarg{dst}{The destination array; will have the same size as \texttt{src1} and type=\texttt{CV\_8UC1}}
4036 \cvarg{cmpop}{The flag specifying the relation between the elements to be checked
4037 \begin{description}
4038  \cvarg{CMP\_EQ}{$\texttt{src1}(I) = \texttt{src2}(I)$ or $\texttt{src1}(I) = \texttt{value}$}
4039  \cvarg{CMP\_GT}{$\texttt{src1}(I) > \texttt{src2}(I)$ or $\texttt{src1}(I) > \texttt{value}$}
4040  \cvarg{CMP\_GE}{$\texttt{src1}(I) \geq \texttt{src2}(I)$ or $\texttt{src1}(I) \geq \texttt{value}$}
4041  \cvarg{CMP\_LT}{$\texttt{src1}(I) < \texttt{src2}(I)$ or $\texttt{src1}(I) < \texttt{value}$}
4042  \cvarg{CMP\_LE}{$\texttt{src1}(I) \leq \texttt{src2}(I)$ or $\texttt{src1}(I) \leq \texttt{value}$}
4043  \cvarg{CMP\_NE}{$\texttt{src1}(I) \ne \texttt{src2}(I)$ or $\texttt{src1}(I) \ne \texttt{value}$}
4044 \end{description}}
4045 \end{description}
4046
4047 The functions \texttt{compare} compare each element of \texttt{src1} with the corresponding element of \texttt{src2}
4048 or with real scalar \texttt{value}. When the comparison result is true, the corresponding element of destination array is set to 255, otherwise it is set to 0:
4049 \begin{itemize}
4050     \item \texttt{dst(I) = src1(I) cmpop src2(I) ? 255 : 0}
4051     \item \texttt{dst(I) = src1(I) cmpop value ? 255 : 0}
4052 \end{itemize}
4053
4054 The comparison operations can be replaced with the equivalent matrix expressions:
4055
4056 \begin{lstlisting}
4057 Mat dst1 = src1 >= src2;
4058 Mat dst2 = src1 < 8;
4059 ...
4060 \end{lstlisting}
4061
4062 See also: \cvCppCross{checkRange}, \cvCppCross{min}, \cvCppCross{max}, \cvCppCross{threshold}, \cross{Matrix Expressions}
4063
4064 \cvCppFunc{completeSymm}
4065 Copies the lower or the upper half of a square matrix to another half.
4066
4067 \cvdefCpp{void completeSymm(Mat\& mtx, bool lowerToUpper=false);}
4068 \begin{description}
4069 \cvarg{mtx}{Input-output floating-point square matrix}
4070 \cvarg{lowerToUpper}{If true, the lower half is copied to the upper half, otherwise the upper half is copied to the lower half}
4071 \end{description}
4072
4073 The function \texttt{completeSymm} copies the lower half of a square matrix to its another half; the matrix diagonal remains unchanged:
4074
4075 \begin{itemize}
4076     \item $\texttt{mtx}_{ij}=\texttt{mtx}_{ji}$ for $i > j$ if \texttt{lowerToUpper=false}
4077     \item $\texttt{mtx}_{ij}=\texttt{mtx}_{ji}$ for $i < j$ if \texttt{lowerToUpper=true}
4078 \end{itemize}
4079
4080 See also: \cvCppCross{flip}, \cvCppCross{transpose}
4081
4082 \cvCppFunc{convertScaleAbs}
4083 Scales, computes absolute values and converts the result to 8-bit.
4084
4085 \cvdefCpp{void convertScaleAbs(const Mat\& src, Mat\& dst, double alpha=1, double beta=0);}
4086 \begin{description}
4087 \cvarg{src}{The source array}
4088 \cvarg{dst}{The destination array}
4089 \cvarg{alpha}{The optional scale factor}
4090 \cvarg{beta}{The optional delta added to the scaled values}
4091 \end{description}
4092
4093 On each element of the input array the function \texttt{convertScaleAbs} performs 3 operations sequentially: scaling, taking absolute value, conversion to unsigned 8-bit type:
4094 \[\texttt{dst}(I)=\texttt{saturate\_cast<uchar>}(|\texttt{src}(I)*\texttt{alpha} + \texttt{beta}|)\]
4095
4096 in the case of multi-channel arrays the function processes each channel independently. When the output is not 8-bit, the operation can be emulated by calling \texttt{Mat::convertTo} method (or by using matrix expressions) and then by computing absolute value of the result, for example:
4097
4098 \begin{lstlisting}
4099 Mat_<float> A(30,30);
4100 randu(A, Scalar(-100), Scalar(100));
4101 Mat_<float> B = A*5 + 3;
4102 B = abs(B);
4103 // Mat_<float> B = abs(A*5+3) will also do the job,
4104 // but it will allocate a temporary matrix
4105 \end{lstlisting}
4106
4107 See also: \cvCppCross{Mat::convertTo}, \cvCppCross{abs}
4108
4109 \cvCppFunc{countNonZero}
4110 Counts non-zero array elements.
4111
4112 \cvdefCpp{int countNonZero( const Mat\& mtx );\newline
4113 int countNonZero( const MatND\& mtx );}
4114 \begin{description}
4115 \cvarg{mtx}{Single-channel array}
4116 \end{description}
4117
4118 The function \texttt{cvCountNonZero} returns the number of non-zero elements in mtx:
4119
4120 \[ \sum_{I:\;\texttt{mtx}(I)\ne0} 1 \]
4121
4122 See also: \cvCppCross{mean}, \cvCppCross{meanStdDev}, \cvCppCross{norm}, \cvCppCross{minMaxLoc}, \cvCppCross{calcCovarMatrix}
4123
4124 \cvCppFunc{cubeRoot}
4125 Computes cube root of the argument
4126
4127 \cvdefCpp{float cubeRoot(float val);}
4128 \begin{description}
4129 \cvarg{val}{The function argument}
4130 \end{description}
4131
4132 The function \texttt{cubeRoot} computes $\sqrt[3]{\texttt{val}}$.
4133 Negative arguments are handled correctly, \emph{NaN} and $\pm\infty$ are not handled.
4134 The accuracy approaches the maximum possible accuracy for single-precision data.
4135
4136 \cvCppFunc{cvarrToMat}
4137 Converts CvMat, IplImage or CvMatND to cv::Mat.
4138
4139 \cvdefCpp{Mat cvarrToMat(const CvArr* src, bool copyData=false, bool allowND=true, int coiMode=0);}
4140 \begin{description}
4141 \cvarg{src}{The source \texttt{CvMat}, \texttt{IplImage} or \texttt{CvMatND}}
4142 \cvarg{copyData}{When it is false (default value), no data is copied, only the new header is created.
4143  In this case the original array should not be deallocated while the new matrix header is used. The the parameter is true, all the data is copied, then user may deallocate the original array right after the conversion}
4144 \cvarg{allowND}{When it is true (default value), then \texttt{CvMatND} is converted to \texttt{Mat} if it's possible
4145 (e.g. then the data is contiguous). If it's not possible, or when the parameter is false, the function will report an error}
4146 \cvarg{coiMode}{The parameter specifies how the IplImage COI (when set) is handled.
4147 \begin{itemize}
4148     \item If \texttt{coiMode=0}, the function will report an error if COI is set.
4149     \item If \texttt{coiMode=1}, the function will never report an error; instead it returns the header to the whole original image and user will have to check and process COI manually, see \cvCppCross{extractImageCOI}.
4150 %    \item If \texttt{coiMode=2}, the function will extract the COI into the separate matrix. \emph{This is also done when the COI is set and }\texttt{copyData=true}}
4151 \end{itemize}}
4152 \end{description}
4153
4154 The function \texttt{cvarrToMat} converts \cross{CvMat}, \cross{IplImage} or \cross{CvMatND} header to \cvCppCross{Mat} header, and optionally duplicates the underlying data. The constructed header is returned by the function.
4155
4156 When \texttt{copyData=false}, the conversion is done really fast (in O(1) time) and the newly created matrix header will have \texttt{refcount=0}, which means that no reference counting is done for the matrix data, and user has to preserve the data until the new header is destructed. Otherwise, when \texttt{copyData=true}, the new buffer will be allocated and managed as if you created a new matrix from scratch and copy the data there. That is,
4157 \texttt{cvarrToMat(src, true) $\sim$ cvarrToMat(src, false).clone()} (assuming that COI is not set). The function provides uniform way of supporting \cross{CvArr} paradigm in the code that is migrated to use new-style data structures internally. The reverse transformation, from \cvCppCross{Mat} to \cross{CvMat} or \cross{IplImage} can be done by simple assignment:
4158
4159 \begin{lstlisting}
4160 CvMat* A = cvCreateMat(10, 10, CV_32F);
4161 cvSetIdentity(A);
4162 IplImage A1; cvGetImage(A, &A1);
4163 Mat B = cvarrToMat(A);
4164 Mat B1 = cvarrToMat(&A1);
4165 IplImage C = B;
4166 CvMat C1 = B1;
4167 // now A, A1, B, B1, C and C1 are different headers
4168 // for the same 10x10 floating-point array.
4169 // note, that you will need to use "&"
4170 // to pass C & C1 to OpenCV functions, e.g:
4171 printf("%g", cvDet(&C1));
4172 \end{lstlisting}
4173
4174 Normally, the function is used to convert an old-style 2D array (\cross{CvMat} or \cross{IplImage}) to \texttt{Mat}, however, the function can also take \cross{CvMatND} on input and create \cvCppCross{Mat} for it, if it's possible. And for \texttt{CvMatND A} it is possible if and only if \texttt{A.dim[i].size*A.dim.step[i] == A.dim.step[i-1]} for all or for all but one \texttt{i, 0 < i < A.dims}. That is, the matrix data should be continuous or it should be representable as a sequence of continuous matrices. By using this function in this way, you can process \cross{CvMatND} using arbitrary element-wise function. But for more complex operations, such as filtering functions, it will not work, and you need to convert \cross{CvMatND} to \cvCppCross{MatND} using the corresponding constructor of the latter.
4175
4176 The last parameter, \texttt{coiMode}, specifies how to react on an image with COI set: by default it's 0, and then the function reports an error when an image with COI comes in. And \texttt{coiMode=1} means that no error is signaled - user has to check COI presence and handle it manually. The modern structures, such as \cvCppCross{Mat} and \cvCppCross{MatND} do not support COI natively. To process individual channel of an new-style array, you will need either to organize loop over the array (e.g. using matrix iterators) where the channel of interest will be processed, or extract the COI using \cvCppCross{mixChannels} (for new-style arrays) or \cvCppCross{extractImageCOI} (for old-style arrays), process this individual channel and insert it back to the destination array if need (using \cvCppCross{mixChannel} or \cvCppCross{insertImageCOI}, respectively).
4177
4178 See also: \cvCppCross{cvGetImage}, \cvCppCross{cvGetMat}, \cvCppCross{cvGetMatND}, \cvCppCross{extractImageCOI}, \cvCppCross{insertImageCOI}, \cvCppCross{mixChannels}
4179
4180
4181 \cvCppFunc{dct}
4182 Performs a forward or inverse discrete cosine transform of 1D or 2D array
4183
4184 \cvdefCpp{void dct(const Mat\& src, Mat\& dst, int flags=0);}
4185 \begin{description}
4186 \cvarg{src}{The source floating-point array}
4187 \cvarg{dst}{The destination array; will have the same size and same type as \texttt{src}}
4188 \cvarg{flags}{Transformation flags, a combination of the following values
4189 \begin{description}
4190 \cvarg{DCT\_INVERSE}{do an inverse 1D or 2D transform instead of the default forward transform.}
4191 \cvarg{DCT\_ROWS}{do a forward or inverse transform of every individual row of the input matrix. This flag allows user to transform multiple vectors simultaneously and can be used to decrease the overhead (which is sometimes several times larger than the processing itself), to do 3D and higher-dimensional transforms and so forth.}
4192 \end{description}}
4193 \end{description}
4194
4195 The function \texttt{dct} performs a forward or inverse discrete cosine transform (DCT) of a 1D or 2D floating-point array:
4196
4197 Forward Cosine transform of 1D vector of $N$ elements:
4198 \[Y = C^{(N)} \cdot X\]
4199 where
4200 \[C^{(N)}_{jk}=\sqrt{\alpha_j/N}\cos\left(\frac{\pi(2k+1)j}{2N}\right)\]
4201 and $\alpha_0=1$, $\alpha_j=2$ for $j > 0$.
4202
4203 Inverse Cosine transform of 1D vector of N elements:
4204 \[X = \left(C^{(N)}\right)^{-1} \cdot Y = \left(C^{(N)}\right)^T \cdot Y\]
4205 (since $C^{(N)}$ is orthogonal matrix, $C^{(N)} \cdot \left(C^{(N)}\right)^T = I$)
4206
4207 Forward Cosine transform of 2D $M \times N$ matrix:
4208 \[Y = C^{(N)} \cdot X \cdot \left(C^{(N)}\right)^T\]
4209
4210 Inverse Cosine transform of 2D vector of $M \times N$ elements:
4211 \[X = \left(C^{(N)}\right)^T \cdot X \cdot C^{(N)}\]
4212
4213 The function chooses the mode of operation by looking at the flags and size of the input array:
4214 \begin{itemize}
4215     \item if \texttt{(flags \& DCT\_INVERSE) == 0}, the function does forward 1D or 2D transform, otherwise it is inverse 1D or 2D transform.
4216     \item if \texttt{(flags \& DCT\_ROWS) $\ne$ 0}, the function performs 1D transform of each row.
4217     \item otherwise, if the array is a single column or a single row, the function performs 1D transform
4218     \item otherwise it performs 2D transform.
4219 \end{itemize}
4220
4221 \textbf{Important note}: currently cv::dct supports even-size arrays (2, 4, 6 ...). For data analysis and approximation you can pad the array when necessary.
4222
4223 Also, the function's performance depends very much, and not monotonically, on the array size, see \cvCppCross{getOptimalDFTSize}. In the current implementation DCT of a vector of size \texttt{N} is computed via DFT of a vector of size \texttt{N/2}, thus the optimal DCT size $\texttt{N}^*\geq\texttt{N}$ can be computed as:
4224
4225 \begin{lstlisting}
4226 size_t getOptimalDCTSize(size_t N) { return 2*getOptimalDFTSize((N+1)/2); }
4227 \end{lstlisting}
4228
4229 See also: \cvCppCross{dft}, \cvCppCross{getOptimalDFTSize}, \cvCppCross{idct}
4230
4231
4232 \cvCppFunc{dft}
4233 Performs a forward or inverse Discrete Fourier transform of 1D or 2D floating-point array.
4234
4235 \cvdefCpp{void dft(const Mat\& src, Mat\& dst, int flags=0, int nonzeroRows=0);}
4236 \begin{description}
4237 \cvarg{src}{The source array, real or complex}
4238 \cvarg{dst}{The destination array, which size and type depends on the \texttt{flags}}
4239 \cvarg{flags}{Transformation flags, a combination of the following values
4240 \begin{description}
4241 \cvarg{DFT\_INVERSE}{do an inverse 1D or 2D transform instead of the default forward transform.}
4242 \cvarg{DFT\_SCALE}{scale the result: divide it by the number of array elements. Normally, it is combined with \texttt{DFT\_INVERSE}}.
4243 \cvarg{DFT\_ROWS}{do a forward or inverse transform of every individual row of the input matrix. This flag allows the user to transform multiple vectors simultaneously and can be used to decrease the overhead (which is sometimes several times larger than the processing itself), to do 3D and higher-dimensional transforms and so forth.}
4244 \cvarg{DFT\_COMPLEX\_OUTPUT}{then the function performs forward transformation of 1D or 2D real array, the result, though being a complex array, has complex-conjugate symmetry (\emph{CCS}), see the description below. Such an array can be packed into real array of the same size as input, which is the fastest option and which is what the function does by default. However, you may wish to get the full complex array (for simpler spectrum analysis etc.). Pass the flag to tell the function to produce full-size complex output array.}
4245 \cvarg{DFT\_REAL\_OUTPUT}{then the function performs inverse transformation of 1D or 2D complex array, the result is normally a complex array of the same size. However, if the source array has conjugate-complex symmetry (for example, it is a result of forward transformation with \texttt{DFT\_COMPLEX\_OUTPUT} flag), then the output is real array. While the function itself does not check whether the input is symmetrical or not, you can pass the flag and then the function will assume the symmetry and produce the real output array. Note that when the input is packed real array and inverse transformation is executed, the function treats the input as packed complex-conjugate symmetrical array, so the output will also be real array}
4246 \end{description}}
4247 \cvarg{nonzeroRows}{When the parameter $\ne 0$, the function assumes that only the first \texttt{nonzeroRows} rows of the input array (\texttt{DFT\_INVERSE} is not set) or only the first \texttt{nonzeroRows} of the output array (\texttt{DFT\_INVERSE} is set) contain non-zeros, thus the function can handle the rest of the rows more efficiently and thus save some time. This technique is very useful for computing array cross-correlation or convolution using DFT}
4248 \end{description}
4249
4250 Forward Fourier transform of 1D vector of N elements:
4251 \[Y = F^{(N)} \cdot X,\]
4252 where $F^{(N)}_{jk}=\exp(-2\pi i j k/N)$ and $i=\sqrt{-1}$
4253
4254 Inverse Fourier transform of 1D vector of N elements:
4255 \[
4256 \begin{array}{l}
4257 X'= \left(F^{(N)}\right)^{-1} \cdot Y = \left(F^{(N)}\right)^* \cdot y \\
4258 X = (1/N) \cdot X,
4259 \end{array}
4260 \]
4261 where $F^*=\left(\textrm{Re}(F^{(N)})-\textrm{Im}(F^{(N)})\right)^T$
4262
4263 Forward Fourier transform of 2D vector of $M \times N$ elements:
4264 \[Y = F^{(M)} \cdot X \cdot F^{(N)}\]
4265
4266 Inverse Fourier transform of 2D vector of $M \times N$ elements:
4267 \[
4268 \begin{array}{l}
4269 X'= \left(F^{(M)}\right)^* \cdot Y \cdot \left(F^{(N)}\right)^*\\
4270 X = \frac{1}{M \cdot N} \cdot X'
4271 \end{array}
4272 \]
4273
4274 In the case of real (single-channel) data, the packed format called \emph{CCS} (complex-conjugate-symmetrical) that was borrowed from IPL and used to represent the result of a forward Fourier transform or input for an inverse Fourier transform:
4275
4276 \[\begin{bmatrix}
4277 Re Y_{0,0} & Re Y_{0,1} & Im Y_{0,1} & Re Y_{0,2} & Im Y_{0,2} & \cdots & Re Y_{0,N/2-1} & Im Y_{0,N/2-1} & Re Y_{0,N/2} \\
4278 Re Y_{1,0} & Re Y_{1,1} & Im Y_{1,1} & Re Y_{1,2} & Im Y_{1,2} & \cdots & Re Y_{1,N/2-1} & Im Y_{1,N/2-1} & Re Y_{1,N/2} \\
4279 Im Y_{1,0} & Re Y_{2,1} & Im Y_{2,1} & Re Y_{2,2} & Im Y_{2,2} & \cdots & Re Y_{2,N/2-1} & Im Y_{2,N/2-1} & Im Y_{1,N/2} \\
4280 \hdotsfor{9} \\
4281 Re Y_{M/2-1,0} &  Re Y_{M-3,1}  & Im Y_{M-3,1} & \hdotsfor{3} & Re Y_{M-3,N/2-1} & Im Y_{M-3,N/2-1}& Re Y_{M/2-1,N/2} \\
4282 Im Y_{M/2-1,0} &  Re Y_{M-2,1}  & Im Y_{M-2,1} & \hdotsfor{3} & Re Y_{M-2,N/2-1} & Im Y_{M-2,N/2-1}& Im Y_{M/2-1,N/2} \\
4283 Re Y_{M/2,0}  &  Re Y_{M-1,1} &  Im Y_{M-1,1} & \hdotsfor{3} & Re Y_{M-1,N/2-1} & Im Y_{M-1,N/2-1}& Re Y_{M/2,N/2}
4284 \end{bmatrix}
4285 \]
4286
4287 in the case of 1D transform of real vector, the output will look as the first row of the above matrix. 
4288
4289 So, the function chooses the operation mode depending on the flags and size of the input array:
4290 \begin{itemize}
4291     \item if \texttt{DFT\_ROWS} is set or the input array has single row or single column then the function performs 1D forward or inverse transform (of each row of a matrix when \texttt{DFT\_ROWS} is set, otherwise it will be 2D transform.
4292     \item if input array is real and \texttt{DFT\_INVERSE} is not set, the function does forward 1D or 2D transform:
4293     \begin{itemize}
4294         \item when \texttt{DFT\_COMPLEX\_OUTPUT} is set then the output will be complex matrix of the same size as input.
4295         \item otherwise the output will be a real matrix of the same size as input. in the case of 2D transform it will use the packed format as shown above; in the case of single 1D transform it will look as the first row of the above matrix; in the case of multiple 1D transforms (when using \texttt{DCT\_ROWS} flag) each row of the output matrix will look like the first row of the above matrix.
4296     \end{itemize}
4297     \item otherwise, if the input array is complex and either \texttt{DFT\_INVERSE} or \texttt{DFT\_REAL\_OUTPUT} are not set then the output will be a complex array of the same size as input and the function will perform the forward or inverse 1D or 2D transform of the whole input array or each row of the input array independently, depending on the flags \texttt{DFT\_INVERSE} and \texttt{DFT\_ROWS}.
4298     \item otherwise, i.e. when \texttt{DFT\_INVERSE} is set, the input array is real, or it is complex but \texttt{DFT\_REAL\_OUTPUT} is set, the output will be a real array of the same size as input, and the function will perform 1D or 2D inverse transformation of the whole input array or each individual row, depending on the flags \texttt{DFT\_INVERSE} and \texttt{DFT\_ROWS}.
4299 \end{itemize}
4300
4301 The scaling is done after the transformation if \texttt{DFT\_SCALE} is set.
4302
4303 Unlike \cvCppCross{dct}, the function supports arrays of arbitrary size, but only those arrays are processed efficiently, which sizes can be factorized in a product of small prime numbers (2, 3 and 5 in the current implementation). Such an efficient DFT size can be computed using \cvCppCross{getOptimalDFTSize} method.
4304
4305 Here is the sample on how to compute DFT-based convolution of two 2D real arrays:
4306 \begin{lstlisting}
4307 void convolveDFT(const Mat& A, const Mat& B, Mat& C)
4308 {
4309     // reallocate the output array if needed
4310     C.create(abs(A.rows - B.rows)+1, abs(A.cols - B.cols)+1, A.type());
4311     Size dftSize;
4312     // compute the size of DFT transform
4313     dftSize.width = getOptimalDFTSize(A.cols + B.cols - 1);
4314     dftSize.height = getOptimalDFTSize(A.rows + B.rows - 1);
4315     
4316     // allocate temporary buffers and initialize them with 0's
4317     Mat tempA(dftSize, A.type(), Scalar::all(0));
4318     Mat tempB(dftSize, B.type(), Scalar::all(0));
4319     
4320     // copy A and B to the top-left corners of tempA and tempB, respectively
4321     Mat roiA(tempA, Rect(0,0,A.cols,A.rows));
4322     A.copyTo(roiA);
4323     Mat roiB(tempB, Rect(0,0,B.cols,B.rows));
4324     B.copyTo(roiB);
4325     
4326     // now transform the padded A & B in-place;
4327     // use "nonzeroRows" hint for faster processing
4328     dft(tempA, tempA, 0, A.rows);
4329     dft(tempB, tempB, 0, B.rows);
4330     
4331     // multiply the spectrums;
4332     // the function handles packed spectrum representations well
4333     mulSpectrums(tempA, tempB, tempA);
4334     
4335     // transform the product back from the frequency domain.
4336     // Even though all the result rows will be non-zero,
4337     // we need only the first C.rows of them, and thus we
4338     // pass nonzeroRows == C.rows
4339     dft(tempA, tempA, DFT_INVERSE + DFT_SCALE, C.rows);
4340     
4341     // now copy the result back to C.
4342     tempA(Rect(0, 0, C.cols, C.rows)).copyTo(C);
4343     
4344     // all the temporary buffers will be deallocated automatically
4345 }
4346 \end{lstlisting}
4347
4348 What can be optimized in the above sample?
4349 \begin{itemize}
4350     \item since we passed $\texttt{nonzeroRows} \ne 0$ to the forward transform calls and
4351     since we copied \texttt{A}/\texttt{B} to the top-left corners of \texttt{tempA}/\texttt{tempB}, respectively,
4352     it's not necessary to clear the whole \texttt{tempA} and \texttt{tempB};
4353     it is only necessary to clear the \texttt{tempA.cols - A.cols} (\texttt{tempB.cols - B.cols})
4354     rightmost columns of the matrices.
4355     \item this DFT-based convolution does not have to be applied to the whole big arrays,
4356     especially if \texttt{B} is significantly smaller than \texttt{A} or vice versa.
4357     Instead, we can compute convolution by parts. For that we need to split the destination array
4358     \texttt{C} into multiple tiles and for each tile estimate, which parts of \texttt{A} and \texttt{B}
4359     are required to compute convolution in this tile. If the tiles in \texttt{C} are too small,
4360     the speed will decrease a lot, because of repeated work - in the ultimate case, when each tile in \texttt{C} is a single pixel,
4361     the algorithm becomes equivalent to the naive convolution algorithm.
4362     If the tiles are too big, the temporary arrays \texttt{tempA} and \texttt{tempB} become too big
4363     and there is also slowdown because of bad cache locality. So there is optimal tile size somewhere in the middle.
4364     \item if the convolution is done by parts, since different tiles in \texttt{C} can be computed in parallel, the loop can be threaded.
4365 \end{itemize}
4366
4367 All of the above improvements have been implemented in \cvCppCross{matchTemplate} and \cvCppCross{filter2D}, therefore, by using them, you can get even better performance than with the above theoretically optimal implementation (though, those two functions actually compute cross-correlation, not convolution, so you will need to "flip" the kernel or the image around the center using \cvCppCross{flip}).
4368
4369 See also: \cvCppCross{dct}, \cvCppCross{getOptimalDFTSize}, \cvCppCross{mulSpectrums}, \cvCppCross{filter2D}, \cvCppCross{matchTemplate}, \cvCppCross{flip}, \cvCppCross{cartToPolar}, \cvCppCross{magnitude}, \cvCppCross{phase}
4370
4371 \cvCppFunc{divide}
4372
4373 Performs per-element division of two arrays or a scalar by an array.
4374
4375 \cvdefCpp{void divide(const Mat\& src1, const Mat\& src2, \par Mat\& dst, double scale=1);\newline
4376 void divide(double scale, const Mat\& src2, Mat\& dst);\newline
4377 void divide(const MatND\& src1, const MatND\& src2, \par MatND\& dst, double scale=1);\newline
4378 void divide(double scale, const MatND\& src2, MatND\& dst);}
4379 \begin{description}
4380 \cvarg{src1}{The first source array}
4381 \cvarg{src2}{The second source array; should have the same size and same type as \texttt{src1}}
4382 \cvarg{scale}{Scale factor}
4383 \cvarg{dst}{The destination array; will have the same size and same type as \texttt{src2}}
4384 \end{description}
4385
4386 The functions \texttt{divide} divide one array by another:
4387 \[\texttt{dst(I) = saturate(src1(I)*scale/src2(I))} \]
4388
4389 or a scalar by array, when there is no \texttt{src1}:
4390 \[\texttt{dst(I) = saturate(scale/src2(I))} \]
4391
4392 The result will have the same type as \texttt{src1}. When \texttt{src2(I)=0}, \texttt{dst(I)=0} too.
4393
4394 See also: \cvCppCross{multiply}, \cvCppCross{add}, \cvCppCross{subtract}, \cross{Matrix Expressions}
4395
4396 \cvCppFunc{determinant}
4397
4398 Returns determinant of a square floating-point matrix.
4399
4400 \cvdefCpp{double determinant(const Mat\& mtx);}
4401 \begin{description}
4402 \cvarg{mtx}{The input matrix; must have \texttt{CV\_32FC1} or \texttt{CV\_64FC1} type and square size}
4403 \end{description}
4404
4405 The function \texttt{determinant} computes and returns determinant of the specified matrix. For small matrices (\texttt{mtx.cols=mtx.rows<=3})
4406 the direct method is used; for larger matrices the function uses LU factorization.
4407
4408 For symmetric positive-determined matrices, it is also possible to compute \cvCppCross{SVD}: $\texttt{mtx}=U \cdot W \cdot V^T$ and then calculate the determinant as a product of the diagonal elements of $W$.
4409
4410 See also: \cvCppCross{SVD}, \cvCppCross{trace}, \cvCppCross{invert}, \cvCppCross{solve}, \cross{Matrix Expressions}
4411
4412 \cvCppFunc{eigen}
4413 Computes eigenvalues and eigenvectors of a symmetric matrix.
4414
4415 \cvdefCpp{bool eigen(const Mat\& src, Mat\& eigenvalues, \par int lowindex=-1, int highindex=-1);\newline
4416 bool eigen(const Mat\& src, Mat\& eigenvalues, \par Mat\& eigenvectors, int lowindex=-1,\par
4417 int highindex=-1);}
4418 \begin{description}
4419 \cvarg{src}{The input matrix; must have \texttt{CV\_32FC1} or \texttt{CV\_64FC1} type, square size and be symmetric: $\texttt{src}^T=\texttt{src}$}
4420 \cvarg{eigenvalues}{The output vector of eigenvalues of the same type as \texttt{src}; The eigenvalues are stored in the descending order.}
4421 \cvarg{eigenvectors}{The output matrix of eigenvectors; It will have the same size and the same type as \texttt{src}; The eigenvectors are stored as subsequent matrix rows, in the same order as the corresponding eigenvalues}
4422 \cvarg{lowindex}{Optional index of largest eigenvalue/-vector to calculate.
4423 (See below.)}
4424 \cvarg{highindex}{Optional index of smallest eigenvalue/-vector to calculate.
4425 (See below.)}
4426 \end{description}
4427
4428 The functions \texttt{eigen} compute just eigenvalues, or eigenvalues and eigenvectors of symmetric matrix \texttt{src}:
4429
4430 \begin{lstlisting}
4431 src*eigenvectors(i,:)' = eigenvalues(i)*eigenvectors(i,:)' (in MATLAB notation)
4432 \end{lstlisting}
4433
4434 If either low- or highindex is supplied the other is required, too.
4435 Indexing is 0-based. Example: To calculate the largest eigenvector/-value set
4436 lowindex = highindex = 0.
4437 For legacy reasons this function always returns a square matrix the same size
4438 as the source matrix with eigenvectors and a vector the length of the source
4439 matrix with eigenvalues. The selected eigenvectors/-values are always in the
4440 first highindex - lowindex + 1 rows.
4441
4442 See also: \cvCppCross{SVD}, \cvCppCross{completeSymm}, \cvCppCross{PCA}
4443
4444 \cvCppFunc{exp}
4445 Calculates the exponent of every array element.
4446
4447 \cvdefCpp{void exp(const Mat\& src, Mat\& dst);\newline
4448 void exp(const MatND\& src, MatND\& dst);}
4449 \begin{description}
4450 \cvarg{src}{The source array}
4451 \cvarg{dst}{The destination array; will have the same size and same type as \texttt{src}}
4452 \end{description}
4453
4454 The function \texttt{exp} calculates the exponent of every element of the input array:
4455
4456 \[
4457 \texttt{dst} [I] = e^{\texttt{src}}(I)
4458 \]
4459
4460 The maximum relative error is about $7 \times 10^{-6}$ for single-precision and less than $10^{-10}$ for double-precision. Currently, the function converts denormalized values to zeros on output. Special values (NaN, $\pm \infty$) are not handled.
4461
4462 See also: \cvCppCross{log}, \cvCppCross{cartToPolar}, \cvCppCross{polarToCart}, \cvCppCross{phase}, \cvCppCross{pow}, \cvCppCross{sqrt}, \cvCppCross{magnitude}
4463
4464 \cvCppFunc{extractImageCOI}
4465
4466 Extract the selected image channel
4467
4468 \cvdefCpp{void extractImageCOI(const CvArr* src, Mat\& dst, int coi=-1);}
4469 \begin{description}
4470 \cvarg{src}{The source array. It should be a pointer to \cross{CvMat} or \cross{IplImage}}
4471 \cvarg{dst}{The destination array; will have single-channel, and the same size and the same depth as \texttt{src}}
4472 \cvarg{coi}{If the parameter is \texttt{>=0}, it specifies the channel to extract;
4473 If it is \texttt{<0}, \texttt{src} must be a pointer to \texttt{IplImage} with valid COI set - then the selected COI is extracted.}
4474 \end{description}
4475
4476 The function \texttt{extractImageCOI} is used to extract image COI from an old-style array and put the result to the new-style C++ matrix. As usual, the destination matrix is reallocated using \texttt{Mat::create} if needed.
4477
4478 To extract a channel from a new-style matrix, use \cvCppCross{mixChannels} or \cvCppCross{split}
4479
4480 See also: \cvCppCross{mixChannels}, \cvCppCross{split}, \cvCppCross{merge}, \cvCppCross{cvarrToMat}, \cvCppCross{cvSetImageCOI}, \cvCppCross{cvGetImageCOI}
4481
4482
4483 \cvCppFunc{fastAtan2}
4484 Calculates the angle of a 2D vector in degrees
4485
4486 \cvdefCpp{float fastAtan2(float y, float x);}
4487 \begin{description}
4488 \cvarg{x}{x-coordinate of the vector}
4489 \cvarg{y}{y-coordinate of the vector}
4490 \end{description}
4491
4492 The function \texttt{fastAtan2} calculates the full-range angle of an input 2D vector. The angle is 
4493 measured in degrees and varies from $0^\circ$ to $360^\circ$. The accuracy is about $0.3^\circ$.
4494
4495 \cvCppFunc{flip}
4496 Flips a 2D array around vertical, horizontal or both axes.
4497
4498 \cvdefCpp{void flip(const Mat\& src, Mat\& dst, int flipCode);}
4499 \begin{description}
4500 \cvarg{src}{The source array}
4501 \cvarg{dst}{The destination array; will have the same size and same type as \texttt{src}}
4502 \cvarg{flipCode}{Specifies how to flip the array:
4503 0 means flipping around the x-axis, positive (e.g., 1) means flipping around y-axis, and negative (e.g., -1) means flipping around both axes. See also the discussion below for the formulas.}
4504 \end{description}
4505
4506 The function \texttt{flip} flips the array in one of three different ways (row and column indices are 0-based):
4507
4508 \[
4509 \texttt{dst}_{ij} = \forkthree
4510 {\texttt{src}_{\texttt{src.rows}-i-1,j}}{if \texttt{flipCode} = 0}
4511 {\texttt{src}_{i,\texttt{src.cols}-j-1}}{if \texttt{flipCode} > 0}
4512 {\texttt{src}_{\texttt{src.rows}-i-1,\texttt{src.cols}-j-1}}{if \texttt{flipCode} < 0}
4513 \]
4514
4515 The example scenarios of function use are:
4516 \begin{itemize}
4517   \item vertical flipping of the image ($\texttt{flipCode} = 0$) to switch between top-left and bottom-left image origin, which is a typical operation in video processing in Windows.
4518   \item horizontal flipping of the image with subsequent horizontal shift and absolute difference calculation to check for a vertical-axis symmetry ($\texttt{flipCode} > 0$)
4519   \item simultaneous horizontal and vertical flipping of the image with subsequent shift and absolute difference calculation to check for a central symmetry ($\texttt{flipCode} < 0$)
4520   \item reversing the order of 1d point arrays ($\texttt{flipCode} > 0$ or $\texttt{flipCode} = 0$)
4521 \end{itemize}
4522
4523 See also: \cvCppCross{transpose}, \cvCppCross{repeat}, \cvCppCross{completeSymm}
4524
4525 \cvCppFunc{gemm}
4526 Performs generalized matrix multiplication.
4527
4528 \cvdefCpp{void gemm(const Mat\& src1, const Mat\& src2, double alpha,\par
4529           const Mat\& src3, double beta, Mat\& dst, int flags=0);}
4530 \begin{description}
4531 \cvarg{src1}{The first multiplied input matrix; should have \texttt{CV\_32FC1}, \texttt{CV\_64FC1}, \texttt{CV\_32FC2} or \texttt{CV\_64FC2} type}
4532 \cvarg{src2}{The second multiplied input matrix; should have the same type as \texttt{src1}}
4533 \cvarg{alpha}{The weight of the matrix product}
4534 \cvarg{src3}{The third optional delta matrix added to the matrix product; should have the same type as \texttt{src1} and \texttt{src2}}
4535 \cvarg{beta}{The weight of \texttt{src3}}
4536 \cvarg{dst}{The destination matrix; It will have the proper size and the same type as input matrices}
4537 \cvarg{flags}{Operation flags:
4538 \begin{description}
4539     \cvarg{GEMM\_1\_T}{transpose \texttt{src1}}
4540     \cvarg{GEMM\_2\_T}{transpose \texttt{src2}}
4541     \cvarg{GEMM\_3\_T}{transpose \texttt{src3}}
4542 \end{description}}
4543 \end{description}
4544
4545 The function performs generalized matrix multiplication and similar to the corresponding functions \texttt{*gemm} in BLAS level 3.
4546 For example, \texttt{gemm(src1, src2, alpha, src3, beta, dst, GEMM\_1\_T + GEMM\_3\_T)} corresponds to
4547 \[
4548 \texttt{dst} = \texttt{alpha} \cdot \texttt{src1} ^T \cdot \texttt{src2} + \texttt{beta} \cdot \texttt{src3} ^T
4549 \]
4550
4551 The function can be replaced with a matrix expression, e.g. the above call can be replaced with:
4552 \begin{lstlisting}
4553 dst = alpha*src1.t()*src2 + beta*src3.t();
4554 \end{lstlisting}
4555
4556 See also: \cvCppCross{mulTransposed}, \cvCppCross{transform}, \cross{Matrix Expressions}
4557
4558
4559 \cvCppFunc{getConvertElem}
4560 Returns conversion function for a single pixel
4561
4562 \cvdefCpp{ConvertData getConvertElem(int fromType, int toType);\newline
4563 ConvertScaleData getConvertScaleElem(int fromType, int toType);\newline
4564 typedef void (*ConvertData)(const void* from, void* to, int cn);\newline
4565 typedef void (*ConvertScaleData)(const void* from, void* to,\par
4566                                  int cn, double alpha, double beta);}
4567 \begin{description}
4568 \cvarg{fromType}{The source pixel type}
4569 \cvarg{toType}{The destination pixel type}
4570 \cvarg{from}{Callback parameter: pointer to the input pixel}
4571 \cvarg{to}{Callback parameter: pointer to the output pixel}
4572 \cvarg{cn}{Callback parameter: the number of channels; can be arbitrary, 1, 100, 100000, ...}
4573 \cvarg{alpha}{ConvertScaleData callback optional parameter: the scale factor}
4574 \cvarg{beta}{ConvertScaleData callback optional parameter: the delta or offset}
4575 \end{description}
4576
4577 The functions \texttt{getConvertElem} and \texttt{getConvertScaleElem} return pointers to the functions for converting individual pixels from one type to another. While the main function purpose is to convert single pixels (actually, for converting sparse matrices from one type to another), you can use them to convert the whole row of a dense matrix or the whole matrix at once, by setting \texttt{cn = matrix.cols*matrix.rows*matrix.channels()} if the matrix data is continuous.
4578
4579 See also: \cvCppCross{Mat::convertTo}, \cvCppCross{MatND::convertTo}, \cvCppCross{SparseMat::convertTo}
4580
4581
4582 \cvCppFunc{getOptimalDFTSize}
4583 Returns optimal DFT size for a given vector size.
4584
4585 \cvdefCpp{int getOptimalDFTSize(int vecsize);}
4586 \begin{description}
4587 \cvarg{vecsize}{Vector size}
4588 \end{description}
4589
4590 DFT performance is not a monotonic function of a vector size, therefore, when you compute convolution of two arrays or do a spectral analysis of array, it usually makes sense to pad the input data with zeros to get a bit larger array that can be transformed much faster than the original one.
4591 Arrays, which size is a power-of-two (2, 4, 8, 16, 32, ...) are the fastest to process, though, the arrays, which size is a product of 2's, 3's and 5's (e.g. 300 = 5*5*3*2*2), are also processed quite efficiently.
4592
4593 The function \texttt{getOptimalDFTSize} returns the minimum number \texttt{N} that is greater than or equal to \texttt{vecsize}, such that the DFT
4594 of a vector of size \texttt{N} can be computed efficiently. In the current implementation $N=2^p \times 3^q \times 5^r$, for some $p$, $q$, $r$.
4595
4596 The function returns a negative number if \texttt{vecsize} is too large (very close to \texttt{INT\_MAX}).
4597
4598 While the function cannot be used directly to estimate the optimal vector size for DCT transform (since the current DCT implementation supports only even-size vectors), it can be easily computed as \texttt{getOptimalDFTSize((vecsize+1)/2)*2}.
4599
4600 See also: \cvCppCross{dft}, \cvCppCross{dct}, \cvCppCross{idft}, \cvCppCross{idct}, \cvCppCross{mulSpectrums}
4601
4602 \cvCppFunc{idct}
4603 Computes inverse Discrete Cosine Transform of a 1D or 2D array
4604
4605 \cvdefCpp{void idct(const Mat\& src, Mat\& dst, int flags=0);}
4606 \begin{description}
4607 \cvarg{src}{The source floating-point single-channel array}
4608 \cvarg{dst}{The destination array. Will have the same size and same type as \texttt{src}}
4609 \cvarg{flags}{The operation flags.}
4610 \end{description}
4611
4612 \texttt{idct(src, dst, flags)} is equivalent to \texttt{dct(src, dst, flags | DCT\_INVERSE)}.
4613 See \cvCppCross{dct} for details.
4614
4615 See also: \cvCppCross{dct}, \cvCppCross{dft}, \cvCppCross{idft}, \cvCppCross{getOptimalDFTSize}
4616
4617
4618 \cvCppFunc{idft}
4619 Computes inverse Discrete Fourier Transform of a 1D or 2D array
4620
4621 \cvdefCpp{void idft(const Mat\& src, Mat\& dst, int flags=0, int outputRows=0);}
4622 \begin{description}
4623 \cvarg{src}{The source floating-point real or complex array}
4624 \cvarg{dst}{The destination array, which size and type depends on the \texttt{flags}}
4625 \cvarg{flags}{The operation flags. See \cvCppCross{dft}}
4626 \cvarg{nonzeroRows}{The number of \texttt{dst} rows to compute.
4627 The rest of the rows will have undefined content.
4628 See the convolution sample in \cvCppCross{dft} description}
4629 \end{description}
4630
4631 \texttt{idft(src, dst, flags)} is equivalent to \texttt{dct(src, dst, flags | DFT\_INVERSE)}.
4632 See \cvCppCross{dft} for details.
4633 Note, that none of \texttt{dft} and \texttt{idft} scale the result by default.
4634 Thus, you should pass \texttt{DFT\_SCALE} to one of \texttt{dft} or \texttt{idft}
4635 explicitly to make these transforms mutually inverse.
4636
4637 See also: \cvCppCross{dft}, \cvCppCross{dct}, \cvCppCross{idct}, \cvCppCross{mulSpectrums}, \cvCppCross{getOptimalDFTSize}
4638
4639
4640 \cvCppFunc{inRange}
4641 Checks if array elements lie between the elements of two other arrays.
4642
4643 \cvdefCpp{void inRange(const Mat\& src, const Mat\& lowerb,\par
4644              const Mat\& upperb, Mat\& dst);\newline
4645 void inRange(const Mat\& src, const Scalar\& lowerb,\par
4646              const Scalar\& upperb, Mat\& dst);\newline
4647 void inRange(const MatND\& src, const MatND\& lowerb,\par
4648              const MatND\& upperb, MatND\& dst);\newline
4649 void inRange(const MatND\& src, const Scalar\& lowerb,\par
4650              const Scalar\& upperb, MatND\& dst);}
4651 \begin{description}
4652 \cvarg{src}{The first source array}
4653 \cvarg{lowerb}{The inclusive lower boundary array of the same size and type as \texttt{src}}
4654 \cvarg{upperb}{The exclusive upper boundary array of the same size and type as \texttt{src}}
4655 \cvarg{dst}{The destination array, will have the same size as \texttt{src} and \texttt{CV\_8U} type}
4656 \end{description}
4657
4658 The functions \texttt{inRange} do the range check for every element of the input array:
4659
4660 \[
4661 \texttt{dst}(I)=\texttt{lowerb}(I)_0 \leq \texttt{src}(I)_0 < \texttt{upperb}(I)_0
4662 \]
4663
4664 for single-channel arrays,
4665
4666 \[
4667 \texttt{dst}(I)=
4668 \texttt{lowerb}(I)_0 \leq \texttt{src}(I)_0 < \texttt{upperb}(I)_0 \land
4669 \texttt{lowerb}(I)_1 \leq \texttt{src}(I)_1 < \texttt{upperb}(I)_1
4670 \]
4671
4672 for two-channel arrays and so forth.
4673 \texttt{dst}(I) is set to 255 (all \texttt{1}-bits) if \texttt{src}(I) is within the specified range and 0 otherwise.
4674
4675
4676 \cvCppFunc{invert}
4677 Finds the inverse or pseudo-inverse of a matrix
4678
4679 \cvdefCpp{double invert(const Mat\& src, Mat\& dst, int method=DECOMP\_LU);}
4680 \begin{description}
4681 \cvarg{src}{The source floating-point $M \times N$ matrix}
4682 \cvarg{dst}{The destination matrix; will have $N \times M$ size and the same type as \texttt{src}}
4683 \cvarg{flags}{The inversion method :
4684 \begin{description}
4685  \cvarg{DECOMP\_LU}{Gaussian elimination with optimal pivot element chosen}
4686  \cvarg{DECOMP\_SVD}{Singular value decomposition (SVD) method}
4687  \cvarg{DECOMP\_CHOLESKY}{Cholesky decomposion. The matrix must be symmetrical and positively defined}
4688 \end{description}}
4689 \end{description}
4690
4691 The function \texttt{invert} inverts matrix \texttt{src} and stores the result in \texttt{dst}.
4692 When the matrix \texttt{src} is singular or non-square, the function computes the pseudo-inverse matrix, i.e. the matrix \texttt{dst}, such that $\|\texttt{src} \cdot \texttt{dst} - I\|$ is minimal.
4693
4694 In the case of \texttt{DECOMP\_LU} method, the function returns the \texttt{src} determinant (\texttt{src} must be square). If it is 0, the matrix is not inverted and \texttt{dst} is filled with zeros.
4695
4696 In the case of \texttt{DECOMP\_SVD} method, the function returns the inversed condition number of \texttt{src} (the ratio of the smallest singular value to the largest singular value) and 0 if \texttt{src} is singular. The SVD method calculates a pseudo-inverse matrix if \texttt{src} is singular.
4697
4698 Similarly to \texttt{DECOMP\_LU}, the method \texttt{DECOMP\_CHOLESKY} works only with non-singular square matrices. In this case the function stores the inverted matrix in \texttt{dst} and returns non-zero, otherwise it returns 0.
4699
4700 See also: \cvCppCross{solve}, \cvCppCross{SVD}
4701
4702
4703 \cvCppFunc{log}
4704 Calculates the natural logarithm of every array element.
4705
4706 \cvdefCpp{void log(const Mat\& src, Mat\& dst);\newline
4707 void log(const MatND\& src, MatND\& dst);}
4708 \begin{description}
4709 \cvarg{src}{The source array}
4710 \cvarg{dst}{The destination array; will have the same size and same type as \texttt{src}}
4711 \end{description}
4712
4713 The function \texttt{log} calculates the natural logarithm of the absolute value of every element of the input array:
4714
4715 \[
4716 \texttt{dst}(I) = \fork
4717 {\log |\texttt{src}(I)|}{if $\texttt{src}(I) \ne 0$ }
4718 {\texttt{C}}{otherwise}
4719 \]
4720
4721 Where \texttt{C} is a large negative number (about -700 in the current implementation).
4722 The maximum relative error is about $7 \times 10^{-6}$ for single-precision input and less than $10^{-10}$ for double-precision input. Special values (NaN, $\pm \infty$) are not handled.
4723
4724 See also: \cvCppCross{exp}, \cvCppCross{cartToPolar}, \cvCppCross{polarToCart}, \cvCppCross{phase}, \cvCppCross{pow}, \cvCppCross{sqrt}, \cvCppCross{magnitude}
4725
4726
4727 \cvCppFunc{LUT}
4728 Performs a look-up table transform of an array.
4729
4730 \cvdefCpp{void LUT(const Mat\& src, const Mat\& lut, Mat\& dst);}
4731 \begin{description}
4732 \cvarg{src}{Source array of 8-bit elements}
4733 \cvarg{lut}{Look-up table of 256 elements. In the case of multi-channel source array, the table should either have a single channel (in this case the same table is used for all channels) or the same number of channels as in the source array}
4734 \cvarg{dst}{Destination array; will have the same size and the same number of channels as \texttt{src}, and the same depth as \texttt{lut}}
4735 \end{description}
4736
4737 The function \texttt{LUT} fills the destination array with values from the look-up table. Indices of the entries are taken from the source array. That is, the function processes each element of \texttt{src} as follows:
4738
4739 \[
4740 \texttt{dst}(I) \leftarrow \texttt{lut(src(I) + d)}
4741 \]
4742
4743 where
4744
4745 \[
4746 d = \fork
4747 {0}{if \texttt{src} has depth \texttt{CV\_8U}}
4748 {128}{if \texttt{src} has depth \texttt{CV\_8S}}
4749 \]
4750
4751 See also: \cvCppCross{convertScaleAbs}, \texttt{Mat::convertTo}
4752
4753 \cvCppFunc{magnitude}
4754 Calculates magnitude of 2D vectors.
4755
4756 \cvdefCpp{void magnitude(const Mat\& x, const Mat\& y, Mat\& magnitude);}
4757 \begin{description}
4758 \cvarg{x}{The floating-point array of x-coordinates of the vectors}
4759 \cvarg{y}{The floating-point array of y-coordinates of the vectors; must have the same size as \texttt{x}}
4760 \cvarg{dst}{The destination array; will have the same size and same type as \texttt{x}}
4761 \end{description}
4762
4763 The function \texttt{magnitude} calculates magnitude of 2D vectors formed from the corresponding elements of \texttt{x} and \texttt{y} arrays:
4764
4765 \[
4766 \texttt{dst}(I) = \sqrt{\texttt{x}(I)^2 + \texttt{y}(I)^2}
4767 \]
4768
4769 See also: \cvCppCross{cartToPolar}, \cvCppCross{polarToCart}, \cvCppCross{phase}, \cvCppCross{sqrt}
4770
4771
4772 \cvCppFunc{Mahalanobis}
4773 Calculates the Mahalanobis distance between two vectors.
4774
4775 \cvdefCpp{double Mahalanobis(const Mat\& vec1, const Mat\& vec2, \par const Mat\& icovar);}
4776 \begin{description}
4777 \cvarg{vec1}{The first 1D source vector}
4778 \cvarg{vec2}{The second 1D source vector}
4779 \cvarg{icovar}{The inverse covariance matrix}
4780 \end{description}
4781
4782 The function \texttt{cvMahalonobis} calculates and returns the weighted distance between two vectors:
4783
4784 \[
4785 d(\texttt{vec1},\texttt{vec2})=\sqrt{\sum_{i,j}{\texttt{icovar(i,j)}\cdot(\texttt{vec1}(I)-\texttt{vec2}(I))\cdot(\texttt{vec1(j)}-\texttt{vec2(j)})}}
4786 \]
4787
4788 The covariance matrix may be calculated using the \cvCppCross{calcCovarMatrix} function and then inverted using the \cvCppCross{invert} function (preferably using DECOMP\_SVD method, as the most accurate).
4789
4790
4791 \cvCppFunc{max}
4792 Calculates per-element maximum of two arrays or array and a scalar
4793
4794 \cvdefCpp{Mat\_Expr<...> max(const Mat\& src1, const Mat\& src2);\newline
4795 Mat\_Expr<...> max(const Mat\& src1, double value);\newline
4796 Mat\_Expr<...> max(double value, const Mat\& src1);\newline
4797 void max(const Mat\& src1, const Mat\& src2, Mat\& dst);\newline
4798 void max(const Mat\& src1, double value, Mat\& dst);\newline
4799 void max(const MatND\& src1, const MatND\& src2, MatND\& dst);\newline
4800 void max(const MatND\& src1, double value, MatND\& dst);}
4801 \begin{description}
4802 \cvarg{src1}{The first source array}
4803 \cvarg{src2}{The second source array of the same size and type as \texttt{src1}}
4804 \cvarg{value}{The real scalar value}
4805 \cvarg{dst}{The destination array; will have the same size and type as \texttt{src1}}
4806 \end{description}
4807
4808 The functions \texttt{max} compute per-element maximum of two arrays:
4809 \[\texttt{dst}(I)=\max(\texttt{src1}(I), \texttt{src2}(I))\]
4810 or array and a scalar:
4811 \[\texttt{dst}(I)=\max(\texttt{src1}(I), \texttt{value})\]
4812
4813 In the second variant, when the source array is multi-channel, each channel is compared with \texttt{value} independently.
4814
4815 The first 3 variants of the function listed above are actually a part of \cross{Matrix Expressions}, they return the expression object that can be further transformed, or assigned to a matrix, or passed to a function etc.
4816
4817 See also: \cvCppCross{min}, \cvCppCross{compare}, \cvCppCross{inRange}, \cvCppCross{minMaxLoc}, \cross{Matrix Expressions}
4818
4819 \cvCppFunc{mean}
4820 Calculates average (mean) of array elements
4821
4822 \cvdefCpp{Scalar mean(const Mat\& mtx);\newline
4823 Scalar mean(const Mat\& mtx, const Mat\& mask);\newline
4824 Scalar mean(const MatND\& mtx);\newline
4825 Scalar mean(const MatND\& mtx, const MatND\& mask);}
4826 \begin{description}
4827 \cvarg{mtx}{The source array; it should have 1 to 4 channels (so that the result can be stored in \cvCppCross{Scalar})}
4828 \cvarg{mask}{The optional operation mask}
4829 \end{description}
4830
4831 The functions \texttt{mean} compute mean value \texttt{M} of array elements, independently for each channel, and return it:
4832
4833 \[
4834 \begin{array}{l}
4835 N = \sum_{I:\;\texttt{mask}(I)\ne 0} 1\\
4836 M_c = \left(\sum_{I:\;\texttt{mask}(I)\ne 0}{\texttt{mtx}(I)_c}\right)/N
4837 \end{array}
4838 \]
4839
4840 When all the mask elements are 0's, the functions return \texttt{Scalar::all(0)}.
4841
4842 See also: \cvCppCross{countNonZero}, \cvCppCross{meanStdDev}, \cvCppCross{norm}, \cvCppCross{minMaxLoc}
4843
4844 \cvCppFunc{meanStdDev}
4845 Calculates mean and standard deviation of array elements
4846
4847 \cvdefCpp{void meanStdDev(const Mat\& mtx, Scalar\& mean, \par Scalar\& stddev, const Mat\& mask=Mat());\newline
4848 void meanStdDev(const MatND\& mtx, Scalar\& mean, \par Scalar\& stddev, const MatND\& mask=MatND());}
4849 \begin{description}
4850 \cvarg{mtx}{The source array; it should have 1 to 4 channels (so that the results can be stored in \cvCppCross{Scalar}'s)}
4851 \cvarg{mean}{The output parameter: computed mean value}
4852 \cvarg{stddev}{The output parameter: computed standard deviation}
4853 \cvarg{mask}{The optional operation mask}
4854 \end{description}
4855
4856 The functions \texttt{meanStdDev} compute the mean and the standard deviation \texttt{M} of array elements, independently for each channel, and return it via the output parameters:
4857
4858 \[
4859 \begin{array}{l}
4860 N = \sum_{I, \texttt{mask}(I) \ne 0} 1\\
4861 \texttt{mean}_c = \frac{\sum_{ I: \; \texttt{mask}(I) \ne 0} \texttt{src}(I)_c}{N}\\
4862 \texttt{stddev}_c = \sqrt{\sum_{ I: \; \texttt{mask}(I) \ne 0} \left(\texttt{src}(I)_c - \texttt{mean}_c\right)^2}
4863 \end{array}
4864 \]
4865
4866 When all the mask elements are 0's, the functions return \texttt{mean=stddev=Scalar::all(0)}.
4867 Note that the computed standard deviation is only the diagonal of the complete normalized covariance matrix. If the full matrix is needed, you can reshape the multi-channel array $M \times N$ to the single-channel array $M*N \times \texttt{mtx.channels}()$ (only possible when the matrix is continuous) and then pass the matrix to \cvCppCross{calcCovarMatrix}.
4868
4869 See also: \cvCppCross{countNonZero}, \cvCppCross{mean}, \cvCppCross{norm}, \cvCppCross{minMaxLoc}, \cvCppCross{calcCovarMatrix}
4870
4871
4872 \cvCppFunc{merge}
4873 Composes a multi-channel array from several single-channel arrays.
4874
4875 \cvdefCpp{void merge(const Mat* mv, size\_t count, Mat\& dst);\newline
4876 void merge(const vector<Mat>\& mv, Mat\& dst);\newline
4877 void merge(const MatND* mv, size\_t count, MatND\& dst);\newline
4878 void merge(const vector<MatND>\& mv, MatND\& dst);}
4879 \begin{description}
4880 \cvarg{mv}{The source array or vector of the single-channel matrices to be merged. All the matrices in \texttt{mv} must have the same size and the same type}
4881 \cvarg{count}{The number of source matrices when \texttt{mv} is a plain C array; must be greater than zero}
4882 \cvarg{dst}{The destination array; will have the same size and the same depth as \texttt{mv[0]}, the number of channels will match the number of source matrices}
4883 \end{description}
4884     
4885 The functions \texttt{merge} merge several single-channel arrays (or rather interleave their elements) to make a single multi-channel array.
4886
4887 \[\texttt{dst}(I)_c = \texttt{mv}[c](I)\]
4888
4889 The function \cvCppCross{split} does the reverse operation and if you need to merge several multi-channel images or shuffle channels in some other advanced way, use \cvCppCross{mixChannels}
4890
4891 See also: \cvCppCross{mixChannels}, \cvCppCross{split}, \cvCppCross{reshape}
4892
4893 \cvCppFunc{min}
4894 Calculates per-element minimum of two arrays or array and a scalar
4895
4896 \cvdefCpp{Mat\_Expr<...> min(const Mat\& src1, const Mat\& src2);\newline
4897 Mat\_Expr<...> min(const Mat\& src1, double value);\newline
4898 Mat\_Expr<...> min(double value, const Mat\& src1);\newline
4899 void min(const Mat\& src1, const Mat\& src2, Mat\& dst);\newline
4900 void min(const Mat\& src1, double value, Mat\& dst);\newline
4901 void min(const MatND\& src1, const MatND\& src2, MatND\& dst);\newline
4902 void min(const MatND\& src1, double value, MatND\& dst);}
4903 \begin{description}
4904 \cvarg{src1}{The first source array}
4905 \cvarg{src2}{The second source array of the same size and type as \texttt{src1}}
4906 \cvarg{value}{The real scalar value}
4907 \cvarg{dst}{The destination array; will have the same size and type as \texttt{src1}}
4908 \end{description}
4909
4910 The functions \texttt{min} compute per-element minimum of two arrays:
4911 \[\texttt{dst}(I)=\min(\texttt{src1}(I), \texttt{src2}(I))\]
4912 or array and a scalar:
4913 \[\texttt{dst}(I)=\min(\texttt{src1}(I), \texttt{value})\]
4914
4915 In the second variant, when the source array is multi-channel, each channel is compared with \texttt{value} independently.
4916
4917 The first 3 variants of the function listed above are actually a part of \cross{Matrix Expressions}, they return the expression object that can be further transformed, or assigned to a matrix, or passed to a function etc.
4918
4919 See also: \cvCppCross{max}, \cvCppCross{compare}, \cvCppCross{inRange}, \cvCppCross{minMaxLoc}, \cross{Matrix Expressions}
4920
4921 \cvCppFunc{minMaxLoc}
4922 Finds global minimum and maximum in a whole array or sub-array
4923
4924 \cvdefCpp{void minMaxLoc(const Mat\& src, double* minVal,\par
4925                double* maxVal=0, Point* minLoc=0,\par
4926                Point* maxLoc=0, const Mat\& mask=Mat());\newline
4927 void minMaxLoc(const MatND\& src, double* minVal,\par
4928                double* maxVal, int* minIdx=0, int* maxIdx=0,\par
4929                const MatND\& mask=MatND());\newline
4930 void minMaxLoc(const SparseMat\& src, double* minVal,\par
4931                double* maxVal, int* minIdx=0, int* maxIdx=0);}
4932 \begin{description}
4933 \cvarg{src}{The source single-channel array}
4934 \cvarg{minVal}{Pointer to returned minimum value; \texttt{NULL} if not required}
4935 \cvarg{maxVal}{Pointer to returned maximum value; \texttt{NULL} if not required}
4936 \cvarg{minLoc}{Pointer to returned minimum location (in 2D case); \texttt{NULL} if not required}
4937 \cvarg{maxLoc}{Pointer to returned maximum location (in 2D case); \texttt{NULL} if not required}
4938 \cvarg{minIdx}{Pointer to returned minimum location (in nD case);
4939  \texttt{NULL} if not required, otherwise must point to an array of \texttt{src.dims} elements and the coordinates of minimum element in each dimensions will be stored sequentially there.}
4940 \cvarg{maxIdx}{Pointer to returned maximum location (in nD case); \texttt{NULL} if not required}
4941 \cvarg{mask}{The optional mask used to select a sub-array}
4942 \end{description}
4943
4944 The functions \texttt{ninMaxLoc} find minimum and maximum element values
4945 and their positions. The extremums are searched across the whole array, or,
4946 if \texttt{mask} is not an empty array, in the specified array region.
4947
4948 The functions do not work with multi-channel arrays. If you need to find minimum or maximum elements across all the channels, use \cvCppCross{reshape} first to reinterpret the array as single-channel. Or you may extract the particular channel using \cvCppCross{extractImageCOI} or \cvCppCross{mixChannels} or \cvCppCross{split}.
4949
4950 in the case of a sparse matrix the minimum is found among non-zero elements only.
4951
4952 See also: \cvCppCross{max}, \cvCppCross{min}, \cvCppCross{compare}, \cvCppCross{inRange}, \cvCppCross{extractImageCOI}, \cvCppCross{mixChannels}, \cvCppCross{split}, \cvCppCross{reshape}.
4953
4954 \cvCppFunc{mixChannels}
4955 Copies specified channels from input arrays to the specified channels of output arrays
4956
4957 \cvdefCpp{void mixChannels(const Mat* srcv, int nsrc, Mat* dstv, int ndst,\par
4958                  const int* fromTo, size\_t npairs);\newline
4959 void mixChannels(const MatND* srcv, int nsrc, MatND* dstv, int ndst,\par
4960                  const int* fromTo, size\_t npairs);\newline
4961 void mixChannels(const vector<Mat>\& srcv, vector<Mat>\& dstv,\par
4962                  const int* fromTo, int npairs);\newline
4963 void mixChannels(const vector<MatND>\& srcv, vector<MatND>\& dstv,\par
4964                  const int* fromTo, int npairs);}
4965 \begin{description}
4966 \cvarg{srcv}{The input array or vector of matrices.
4967 All the matrices must have the same size and the same depth}
4968 \cvarg{nsrc}{The number of elements in \texttt{srcv}}
4969 \cvarg{dstv}{The output array or vector of matrices.
4970 All the matrices \emph{must be allocated}, their size and depth must be the same as in \texttt{srcv[0]}}
4971 \cvarg{ndst}{The number of elements in \texttt{dstv}}
4972 \cvarg{fromTo}{The array of index pairs, specifying which channels are copied and where.
4973 \texttt{fromTo[k*2]} is the 0-based index of the input channel in \texttt{srcv} and
4974 \texttt{fromTo[k*2+1]} is the index of the output channel in \texttt{dstv}. Here the continuous channel numbering is used, that is,
4975 the first input image channels are indexed from \texttt{0} to \texttt{srcv[0].channels()-1},
4976 the second input image channels are indexed from \texttt{srcv[0].channels()} to
4977 \texttt{srcv[0].channels() + srcv[1].channels()-1} etc., and the same scheme is used for the output image channels.
4978 As a special case, when \texttt{fromTo[k*2]} is negative, the corresponding output channel is filled with zero.
4979 }
4980 \texttt{npairs}{The number of pairs. In the latter case the parameter is not passed explicitly, but computed as \texttt{srcv.size()} (=\texttt{dstv.size()})}
4981 \end{description}
4982
4983 The functions \texttt{mixChannels} provide an advanced mechanism for shuffling image channels. \cvCppCross{split} and \cvCppCross{merge} and some forms of \cvCppCross{cvtColor} are partial cases of \texttt{mixChannels}.
4984
4985 As an example, this code splits a 4-channel RGBA image into a 3-channel
4986 BGR (i.e. with R and B channels swapped) and separate alpha channel image:
4987
4988 \begin{lstlisting}
4989 Mat rgba( 100, 100, CV_8UC4, Scalar(1,2,3,4) );
4990 Mat bgr( rgba.rows, rgba.cols, CV_8UC3 );
4991 Mat alpha( rgba.rows, rgba.cols, CV_8UC1 );
4992
4993 // forming array of matrices is quite efficient operations,
4994 // because the matrix data is not copied, only the headers
4995 Mat out[] = { bgr, alpha };
4996 // rgba[0] -> bgr[2], rgba[1] -> bgr[1],
4997 // rgba[2] -> bgr[0], rgba[3] -> alpha[0]
4998 int from_to[] = { 0,2,  1,1,  2,0,  3,3 };
4999 mixChannels( &rgba, 1, out, 2, from_to, 4 );
5000 \end{lstlisting}
5001
5002 Note that, unlike many other new-style C++ functions in OpenCV (see the introduction section and \cvCppCross{Mat::create}),
5003 \texttt{mixChannels} requires the destination arrays be pre-allocated before calling the function.
5004
5005 See also: \cvCppCross{split}, \cvCppCross{merge}, \cvCppCross{cvtColor} 
5006
5007
5008 \cvCppFunc{mulSpectrums}
5009 Performs per-element multiplication of two Fourier spectrums.
5010
5011 \cvdefCpp{void mulSpectrums(const Mat\& src1, const Mat\& src2, Mat\& dst,\par
5012                   int flags, bool conj=false);}
5013 \begin{description}
5014 \cvarg{src1}{The first source array}
5015 \cvarg{src2}{The second source array; must have the same size and the same type as \texttt{src1}}
5016 \cvarg{dst}{The destination array; will have the same size and the same type as \texttt{src1}}
5017 \cvarg{flags}{The same flags as passed to \cvCppCross{dft}; only the flag \texttt{DFT\_ROWS} is checked for}
5018 \cvarg{conj}{The optional flag that conjugate the second source array before the multiplication (true) or not (false)}
5019 \end{description}
5020
5021 The function \texttt{mulSpectrums} performs per-element multiplication of the two CCS-packed or complex matrices that are results of a real or complex Fourier transform.
5022
5023 The function, together with \cvCppCross{dft} and \cvCppCross{idft}, may be used to calculate convolution (pass \texttt{conj=false}) or correlation (pass \texttt{conj=false}) of two arrays rapidly. When the arrays are complex, they are simply multiplied (per-element) with optional conjugation of the second array elements. When the arrays are real, they assumed to be CCS-packed (see \cvCppCross{dft} for details).
5024
5025 \cvCppFunc{multiply}
5026 Calculates the per-element scaled product of two arrays
5027
5028 \cvdefCpp{void multiply(const Mat\& src1, const Mat\& src2, \par Mat\& dst, double scale=1);\newline
5029 void multiply(const MatND\& src1, const MatND\& src2, \par MatND\& dst, double scale=1);}
5030 \begin{description}
5031 \cvarg{src1}{The first source array}
5032 \cvarg{src2}{The second source array of the same size and the same type as \texttt{src1}}
5033 \cvarg{dst}{The destination array; will have the same size and the same type as \texttt{src1}}
5034 \cvarg{scale}{The optional scale factor}
5035 \end{description}
5036
5037 The function \texttt{multiply} calculates the per-element product of two arrays:
5038
5039 \[
5040 \texttt{dst}(I)=\texttt{saturate}(\texttt{scale} \cdot \texttt{src1}(I) \cdot \texttt{src2}(I))
5041 \]
5042
5043 There is also \cross{Matrix Expressions}-friendly variant of the first function, see \cvCppCross{Mat::mul}.
5044
5045 If you are looking for a matrix product, not per-element product, see \cvCppCross{gemm}.
5046
5047 See also: \cvCppCross{add}, \cvCppCross{substract}, \cvCppCross{divide}, \cross{Matrix Expressions}, \cvCppCross{scaleAdd}, \cvCppCross{addWeighted}, \cvCppCross{accumulate}, \cvCppCross{accumulateProduct}, \cvCppCross{accumulateSquare}, \cvCppCross{Mat::convertTo}
5048
5049 \cvCppFunc{mulTransposed}
5050 Calculates the product of a matrix and its transposition.
5051
5052 \cvdefCpp{void mulTransposed( const Mat\& src, Mat\& dst, bool aTa,\par
5053                     const Mat\& delta=Mat(),\par
5054                     double scale=1, int rtype=-1 );}
5055 \begin{description}
5056 \cvarg{src}{The source matrix}
5057 \cvarg{dst}{The destination square matrix}
5058 \cvarg{aTa}{Specifies the multiplication ordering; see the description below}
5059 \cvarg{delta}{The optional delta matrix, subtracted from \texttt{src} before the multiplication. When the matrix is empty (\texttt{delta=Mat()}), it's assumed to be zero, i.e. nothing is subtracted, otherwise if it has the same size as \texttt{src}, then it's simply subtracted, otherwise it is "repeated" (see \cvCppCross{repeat}) to cover the full \texttt{src} and then subtracted. Type of the delta matrix, when it's not empty, must be the same as the type of created destination matrix, see the \texttt{rtype} description}
5060 \cvarg{scale}{The optional scale factor for the matrix product}
5061 \cvarg{rtype}{When it's negative, the destination matrix will have the same type as \texttt{src}. Otherwise, it will have \texttt{type=CV\_MAT\_DEPTH(rtype)}, which should be either \texttt{CV\_32F} or \texttt{CV\_64F}}
5062 \end{description}
5063
5064 The function \texttt{mulTransposed} calculates the product of \texttt{src} and its transposition:
5065 \[
5066 \texttt{dst}=\texttt{scale} (\texttt{src}-\texttt{delta})^T (\texttt{src}-\texttt{delta})
5067 \]
5068 if \texttt{aTa=true}, and
5069
5070 \[
5071 \texttt{dst}=\texttt{scale} (\texttt{src}-\texttt{delta}) (\texttt{src}-\texttt{delta})^T
5072 \]
5073
5074 otherwise. The function is used to compute covariance matrix and with zero delta can be used as a faster substitute for general matrix product $A*B$ when $B=A^T$.
5075
5076 See also: \cvCppCross{calcCovarMatrix}, \cvCppCross{gemm}, \cvCppCross{repeat}, \cvCppCross{reduce}
5077
5078
5079 \cvCppFunc{norm}
5080 Calculates absolute array norm, absolute difference norm, or relative difference norm.
5081
5082 \cvdefCpp{double norm(const Mat\& src1, int normType=NORM\_L2);\newline
5083 double norm(const Mat\& src1, const Mat\& src2, int normType=NORM\_L2);\newline
5084 double norm(const Mat\& src1, int normType, const Mat\& mask);\newline
5085 double norm(const Mat\& src1, const Mat\& src2, \par int normType, const Mat\& mask);\newline
5086 double norm(const MatND\& src1, int normType=NORM\_L2, \par const MatND\& mask=MatND());\newline
5087 double norm(const MatND\& src1, const MatND\& src2,\par
5088             int normType=NORM\_L2, const MatND\& mask=MatND());\newline
5089 double norm( const SparseMat\& src, int normType );}
5090 \begin{description}
5091 \cvarg{src1}{The first source array}
5092 \cvarg{src2}{The second source array of the same size and the same type as \texttt{src1}}
5093 \cvarg{normType}{Type of the norm; see the discussion below}
5094 \cvarg{mask}{The optional operation mask}
5095 \end{description}
5096
5097 The functions \texttt{norm} calculate the absolute norm of \texttt{src1} (when there is no \texttt{src2}):
5098 \[
5099 norm = \forkthree
5100 {\|\texttt{src1}\|_{L_{\infty}}    = \max_I |\texttt{src1}(I)|}{if $\texttt{normType} = \texttt{NORM\_INF}$}
5101 {\|\texttt{src1}\|_{L_1} = \sum_I |\texttt{src1}(I)|}{if $\texttt{normType} = \texttt{NORM\_L1}$}
5102 {\|\texttt{src1}\|_{L_2} = \sqrt{\sum_I \texttt{src1}(I)^2}}{if $\texttt{normType} = \texttt{NORM\_L2}$}
5103 \]
5104
5105 or an absolute or relative difference norm if \texttt{src2} is there:
5106 \[
5107 norm = \forkthree
5108 {\|\texttt{src1}-\texttt{src2}\|_{L_{\infty}}    = \max_I |\texttt{src1}(I) - \texttt{src2}(I)|}{if $\texttt{normType} = \texttt{NORM\_INF}$}
5109 {\|\texttt{src1}-\texttt{src2}\|_{L_1} = \sum_I |\texttt{src1}(I) - \texttt{src2}(I)|}{if $\texttt{normType} = \texttt{NORM\_L1}$}
5110 {\|\texttt{src1}-\texttt{src2}\|_{L_2} = \sqrt{\sum_I (\texttt{src1}(I) - \texttt{src2}(I))^2}}{if $\texttt{normType} = \texttt{NORM\_L2}$}
5111 \]
5112
5113 or
5114
5115 \[
5116 norm = \forkthree
5117 {\frac{\|\texttt{src1}-\texttt{src2}\|_{L_{\infty}}    }{\|\texttt{src2}\|_{L_{\infty}}   }}{if $\texttt{normType} = \texttt{NORM\_RELATIVE\_INF}$}
5118 {\frac{\|\texttt{src1}-\texttt{src2}\|_{L_1} }{\|\texttt{src2}\|_{L_1}}}{if $\texttt{normType} = \texttt{NORM\_RELATIVE\_L1}$}
5119 {\frac{\|\texttt{src1}-\texttt{src2}\|_{L_2} }{\|\texttt{src2}\|_{L_2}}}{if $\texttt{normType} = \texttt{NORM\_RELATIVE\_L2}$}
5120 \]
5121
5122 The functions \texttt{norm} return the calculated norm.
5123
5124 When there is \texttt{mask} parameter, and it is not empty (then it should have type \texttt{CV\_8U} and the same size as \texttt{src1}), the norm is computed only over the specified by the mask region.
5125
5126 A multiple-channel source arrays are treated as a single-channel, that is, the results for all channels are combined.
5127
5128
5129 \cvCppFunc{normalize}
5130 Normalizes array's norm or the range
5131
5132 \cvdefCpp{void normalize( const Mat\& src, Mat\& dst, \par double alpha=1, double beta=0,\par
5133                 int normType=NORM\_L2, int rtype=-1, \par const Mat\& mask=Mat());\newline
5134 void normalize( const MatND\& src, MatND\& dst, \par double alpha=1, double beta=0,\par
5135                 int normType=NORM\_L2, int rtype=-1, \par const MatND\& mask=MatND());\newline
5136 void normalize( const SparseMat\& src, SparseMat\& dst, \par double alpha, int normType );}
5137 \begin{description}
5138 \cvarg{src}{The source array}
5139 \cvarg{dst}{The destination array; will have the same size as \texttt{src}}
5140 \cvarg{alpha}{The norm value to normalize to or the lower range boundary in the case of range normalization}
5141 \cvarg{beta}{The upper range boundary in the case of range normalization; not used for norm normalization}
5142 \cvarg{normType}{The normalization type, see the discussion}
5143 \cvarg{rtype}{When the parameter is negative, the destination array will have the same type as \texttt{src}, otherwise it will have the same number of channels as \texttt{src} and the depth\texttt{=CV\_MAT\_DEPTH(rtype)}}
5144 \cvarg{mask}{The optional operation mask}
5145 \end{description}
5146
5147 The functions \texttt{normalize} scale and shift the source array elements, so that
5148 \[\|\texttt{dst}\|_{L_p}=\texttt{alpha}\]
5149 (where $p=\infty$, 1 or 2) when \texttt{normType=NORM\_INF}, \texttt{NORM\_L1} or \texttt{NORM\_L2},
5150 or so that
5151 \[\min_I \texttt{dst}(I)=\texttt{alpha},\,\,\max_I \texttt{dst}(I)=\texttt{beta}\]
5152 when \texttt{normType=NORM\_MINMAX} (for dense arrays only).
5153
5154 The optional mask specifies the sub-array to be normalize, that is, the norm or min-n-max are computed over the sub-array and then this sub-array is modified to be normalized. If you want to only use the mask to compute the norm or min-max, but modify the whole array, you can use \cvCppCross{norm} and \cvCppCross{Mat::convertScale}/\cvCppCross{MatND::convertScale}/cross{SparseMat::convertScale} separately.
5155
5156 in the case of sparse matrices, only the non-zero values are analyzed and transformed. Because of this, the range transformation for sparse matrices is not allowed, since it can shift the zero level. 
5157
5158 See also: \cvCppCross{norm}, \cvCppCross{Mat::convertScale}, \cvCppCross{MatND::convertScale}, \cvCppCross{SparseMat::convertScale}
5159
5160
5161 \cvclass{PCA}
5162 Class for Principal Component Analysis
5163
5164 \begin{lstlisting}
5165 class PCA
5166 {
5167 public:
5168     // default constructor
5169     PCA();newline
5170     // computes PCA for a set of vectors stored as data rows or columns.
5171     PCA(const Mat& data, const Mat& mean, int flags, int maxComponents=0);newline
5172     // computes PCA for a set of vectors stored as data rows or columns
5173     PCA& operator()(const Mat& data, const Mat& mean, int flags, int maxComponents=0);newline
5174     // projects vector into the principal components space
5175     Mat project(const Mat& vec) const;newline
5176     void project(const Mat& vec, Mat& result) const;newline
5177     // reconstructs the vector from its PC projection
5178     Mat backProject(const Mat& vec) const;newline
5179     void backProject(const Mat& vec, Mat& result) const;newline
5180
5181     // eigenvectors of the PC space, stored as the matrix rows
5182     Mat eigenvectors;newline
5183     // the corresponding eigenvalues; not used for PCA compression/decompression
5184     Mat eigenvalues;newline
5185     // mean vector, subtracted from the projected vector
5186     // or added to the reconstructed vector
5187     Mat mean;
5188 };
5189 \end{lstlisting}
5190
5191 The class \texttt{PCA} is used to compute the special basis for a set of vectors. The basis will consist of eigenvectors of the covariance matrix computed from the input set of vectors. And also the class \texttt{PCA} can transform vectors to/from the new coordinate space, defined by the basis. Usually, in this new coordinate system each vector from the original set (and any linear combination of such vectors) can be quite accurately approximated by taking just the first few its components, corresponding to the eigenvectors of the largest eigenvalues of the covariance matrix. Geometrically it means that we compute projection of the vector to a subspace formed by a few eigenvectors corresponding to the dominant eigenvalues of the covariation matrix. And usually such a projection is very close to the original vector. That is, we can represent the original vector from a high-dimensional space with a much shorter vector consisting of the projected vector's coordinates in the subspace. Such a transformation is also known as Karhunen-Loeve Transform, or KLT. See \url{http://en.wikipedia.org/wiki/Principal\_component\_analysis}
5192
5193 The following sample is the function that takes two matrices. The first one stores the set of vectors (a row per vector) that is used to compute PCA, the second one stores another "test" set of vectors (a row per vector) that are first compressed with PCA, then reconstructed back and then the reconstruction error norm is computed and printed for each vector.
5194 \begin{lstlisting}
5195 PCA compressPCA(const Mat& pcaset, int maxComponents,
5196                 const Mat& testset, Mat& compressed)
5197 {
5198     PCA pca(pcaset, // pass the data
5199             Mat(), // we do not have a pre-computed mean vector,
5200                    // so let the PCA engine to compute it
5201             CV_PCA_DATA_AS_ROW, // indicate that the vectors
5202                                 // are stored as matrix rows
5203                                 // (use CV_PCA_DATA_AS_COL if the vectors are
5204                                 // the matrix columns)
5205             maxComponents // specify, how many principal components to retain
5206             );
5207     // if there is no test data, just return the computed basis, ready-to-use
5208     if( !testset.data )
5209         return pca;
5210     CV_Assert( testset.cols == pcaset.cols );
5211
5212     compressed.create(testset.rows, maxComponents, testset.type());
5213
5214     Mat reconstructed;
5215     for( int i = 0; i < testset.rows; i++ )
5216     {
5217         Mat vec = testset.row(i), coeffs = compressed.row(i);
5218         // compress the vector, the result will be stored
5219         // in the i-th row of the output matrix
5220         pca.project(vec, coeffs);
5221         // and then reconstruct it
5222         pca.backProject(coeffs, reconstructed);
5223         // and measure the error
5224         printf("%d. diff = %g\n", i, norm(vec, reconstructed, NORM_L2));
5225     }
5226     return pca;
5227 }
5228 \end{lstlisting}
5229
5230 See also: \cvCppCross{calcCovarMatrix}, \cvCppCross{mulTransposed}, \cvCppCross{SVD}, \cvCppCross{dft}, \cvCppCross{dct}
5231
5232 \cvCppFunc{perspectiveTransform}
5233 Performs perspective matrix transformation of vectors.
5234
5235 \cvdefCpp{void perspectiveTransform(const Mat\& src, \par Mat\& dst, const Mat\& mtx );}
5236 \begin{description}
5237 \cvarg{src}{The source two-channel or three-channel floating-point array;
5238             each element is 2D/3D vector to be transformed}
5239 \cvarg{dst}{The destination array; it will have the same size and same type as \texttt{src}}
5240 \cvarg{mtx}{$3\times 3$ or $4 \times 4$ transformation matrix}
5241 \end{description}
5242
5243 The function \texttt{perspectiveTransform} transforms every element of \texttt{src},
5244 by treating it as 2D or 3D vector, in the following way (here 3D vector transformation is shown; in the case of 2D vector transformation the $z$ component is omitted):
5245
5246 \[ (x, y, z) \rightarrow (x'/w, y'/w, z'/w) \]
5247
5248 where
5249
5250 \[
5251 (x', y', z', w') = \texttt{mat} \cdot
5252 \begin{bmatrix} x & y & z & 1 \end{bmatrix}
5253 \]
5254
5255 and
5256 \[ w = \fork{w'}{if $w' \ne 0$}{\infty}{otherwise} \]
5257
5258 Note that the function transforms a sparse set of 2D or 3D vectors. If you want to transform an image using perspective transformation, use \cvCppCross{warpPerspective}. If you have an inverse task, i.e. want to compute the most probable perspective transformation out of several pairs of corresponding points, you can use \cvCppCross{getPerspectiveTransform} or \cvCppCross{findHomography}.
5259
5260 See also: \cvCppCross{transform}, \cvCppCross{warpPerspective}, \cvCppCross{getPerspectiveTransform}, \cvCppCross{findHomography}
5261
5262 \cvCppFunc{phase}
5263 Calculates the rotation angle of 2d vectors
5264
5265 \cvdefCpp{void phase(const Mat\& x, const Mat\& y, Mat\& angle,\par
5266            bool angleInDegrees=false);}
5267 \begin{description}
5268 \cvarg{x}{The source floating-point array of x-coordinates of 2D vectors}
5269 \cvarg{y}{The source array of y-coordinates of 2D vectors; must have the same size and the same type as \texttt{x}}
5270 \cvarg{angle}{The destination array of vector angles; it will have the same size and same type as \texttt{x}}
5271 \cvarg{angleInDegrees}{When it is true, the function will compute angle in degrees, otherwise they will be measured in radians}
5272 \end{description}
5273
5274 The function \texttt{phase} computes the rotation angle of each 2D vector that is formed from the corresponding elements of \texttt{x} and \texttt{y}:
5275
5276 \[\texttt{angle}(I) = \texttt{atan2}(\texttt{y}(I), \texttt{x}(I))\]
5277
5278 The angle estimation accuracy is $\sim\,0.3^\circ$, when \texttt{x(I)=y(I)=0}, the corresponding \texttt{angle}(I) is set to $0$.
5279
5280 See also:
5281
5282 \cvCppFunc{polarToCart}
5283 Computes x and y coordinates of 2D vectors from their magnitude and angle.
5284
5285 \cvdefCpp{void polarToCart(const Mat\& magnitude, const Mat\& angle,\par
5286                  Mat\& x, Mat\& y, bool angleInDegrees=false);}
5287 \begin{description}
5288 \cvarg{magnitude}{The source floating-point array of magnitudes of 2D vectors. It can be an empty matrix (\texttt{=Mat()}) - in this case the function assumes that all the magnitudes are =1. If it's not empty, it must have the same size and same type as \texttt{angle}}
5289 \cvarg{angle}{The source floating-point array of angles of the 2D vectors}
5290 \cvarg{x}{The destination array of x-coordinates of 2D vectors; will have the same size and the same type as \texttt{angle}}
5291 \cvarg{y}{The destination array of y-coordinates of 2D vectors; will have the same size and the same type as \texttt{angle}}
5292 \cvarg{angleInDegrees}{When it is true, the input angles are measured in degrees, otherwise they are measured in radians}
5293 \end{description}
5294
5295 The function \texttt{polarToCart} computes the cartesian coordinates of each 2D vector represented by the corresponding elements of \texttt{magnitude} and \texttt{angle}:
5296
5297 \[
5298 \begin{array}{l}
5299 \texttt{x}(I) = \texttt{magnitude}(I)\cos(\texttt{angle}(I))\\
5300 \texttt{y}(I) = \texttt{magnitude}(I)\sin(\texttt{angle}(I))\\
5301 \end{array}
5302 \]
5303
5304 The relative accuracy of the estimated coordinates is $\sim\,10^{-6}$.
5305
5306 See also: \cvCppCross{cartToPolar}, \cvCppCross{magnitude}, \cvCppCross{phase}, \cvCppCross{exp}, \cvCppCross{log}, \cvCppCross{pow}, \cvCppCross{sqrt}
5307
5308 \cvCppFunc{pow}
5309 Raises every array element to a power.
5310
5311 \cvdefCpp{void pow(const Mat\& src, double p, Mat\& dst);\newline
5312 void pow(const MatND\& src, double p, MatND\& dst);}
5313 \begin{description}
5314 \cvarg{src}{The source array}
5315 \cvarg{p}{The exponent of power}
5316 \cvarg{dst}{The destination array; will have the same size and the same type as \texttt{src}}
5317 \end{description}
5318
5319 The function \texttt{pow} raises every element of the input array to \texttt{p}:
5320
5321 \[
5322 \texttt{dst}(I) = \fork
5323 {\texttt{src}(I)^p}{if \texttt{p} is integer}
5324 {|\texttt{src}(I)|^p}{otherwise}
5325 \]
5326
5327 That is, for a non-integer power exponent the absolute values of input array elements are used. However, it is possible to get true values for negative values using some extra operations, as the following example, computing the 5th root of array \texttt{src}, shows:
5328
5329 \begin{lstlisting}
5330 Mat mask = src < 0;
5331 pow(src, 1./5, dst);
5332 subtract(Scalar::all(0), dst, dst, mask);
5333 \end{lstlisting}
5334
5335 For some values of \texttt{p}, such as integer values, 0.5, and -0.5, specialized faster algorithms are used.
5336
5337 See also: \cvCppCross{sqrt}, \cvCppCross{exp}, \cvCppCross{log}, \cvCppCross{cartToPolar}, \cvCppCross{polarToCart}
5338
5339 \cvCppFunc{randu}
5340 Generates a single uniformly-distributed random number or array of random numbers
5341
5342 \cvdefCpp{template<typename \_Tp> \_Tp randu();\newline
5343 void randu(Mat\& mtx, const Scalar\& low, const Scalar\& high);}
5344 \begin{description}
5345 \cvarg{mtx}{The output array of random numbers. The array must be pre-allocated and have 1 to 4 channels}
5346 \cvarg{low}{The inclusive lower boundary of the generated random numbers}
5347 \cvarg{high}{The exclusive upper boundary of the generated random numbers}
5348 \end{description}
5349
5350 The template functions \texttt{randu} generate and return the next uniformly-distributed random value of the specified type. \texttt{randu<int>()} is equivalent to \texttt{(int)theRNG();} etc. See \cvCppCross{RNG} description.
5351
5352 The second non-template variant of the function fills the matrix \texttt{mtx} with uniformly-distributed random numbers from the specified range:
5353
5354 \[\texttt{low}_c \leq \texttt{mtx}(I)_c < \texttt{high}_c\]
5355
5356 See also: \cvCppCross{RNG}, \cvCppCross{randn}, \cvCppCross{theRNG}.
5357
5358 \cvCppFunc{randn}
5359 Fills array with normally distributed random numbers
5360
5361 \cvdefCpp{void randn(Mat\& mtx, const Scalar\& mean, const Scalar\& stddev);}
5362 \begin{description}
5363 \cvarg{mtx}{The output array of random numbers. The array must be pre-allocated and have 1 to 4 channels}
5364 \cvarg{mean}{The mean value (expectation) of the generated random numbers}
5365 \cvarg{stddev}{The standard deviation of the generated random numbers}
5366 \end{description}
5367
5368 The function \texttt{randn} fills the matrix \texttt{mtx} with normally distributed random numbers with the specified mean and standard deviation. \hyperref[cppfunc.saturatecast]{saturate\_cast} is applied to the generated numbers (i.e. the values are clipped)
5369
5370 See also: \cvCppCross{RNG}, \cvCppCross{randu}
5371
5372 \cvCppFunc{randShuffle}
5373 Shuffles the array elements randomly
5374
5375 \cvdefCpp{void randShuffle(Mat\& mtx, double iterFactor=1., RNG* rng=0);}
5376 \begin{description}
5377 \cvarg{mtx}{The input/output numerical 1D array}
5378 \cvarg{iterFactor}{The scale factor that determines the number of random swap operations. See the discussion}
5379 \cvarg{rng}{The optional random number generator used for shuffling. If it is zero, \cvCppCross{theRNG}() is used instead}
5380 \end{description}
5381
5382 The function \texttt{randShuffle} shuffles the specified 1D array by randomly choosing pairs of elements and swapping them. The number of such swap operations will be \texttt{mtx.rows*mtx.cols*iterFactor}
5383
5384 See also: \cvCppCross{RNG}, \cvCppCross{sort}
5385
5386 \cvCppFunc{reduce}
5387 Reduces a matrix to a vector
5388
5389 \cvdefCpp{void reduce(const Mat\& mtx, Mat\& vec, \par int dim, int reduceOp, int dtype=-1);}
5390 \begin{description}
5391 \cvarg{mtx}{The source 2D matrix}
5392 \cvarg{vec}{The destination vector. Its size and type is defined by \texttt{dim} and \texttt{dtype} parameters}
5393 \cvarg{dim}{The dimension index along which the matrix is reduced. 0 means that the matrix is reduced to a single row and 1 means that the matrix is reduced to a single column}
5394 \cvarg{reduceOp}{The reduction operation, one of:
5395 \begin{description}
5396 \cvarg{CV\_REDUCE\_SUM}{The output is the sum of all of the matrix's rows/columns.}
5397 \cvarg{CV\_REDUCE\_AVG}{The output is the mean vector of all of the matrix's rows/columns.}
5398 \cvarg{CV\_REDUCE\_MAX}{The output is the maximum (column/row-wise) of all of the matrix's rows/columns.}
5399 \cvarg{CV\_REDUCE\_MIN}{The output is the minimum (column/row-wise) of all of the matrix's rows/columns.}
5400 \end{description}}
5401 \cvarg{dtype}{When it is negative, the destination vector will have the same type as the source matrix, otherwise, its type will be \texttt{CV\_MAKE\_TYPE(CV\_MAT\_DEPTH(dtype), mtx.channels())}}
5402 \end{description}
5403
5404 The function \texttt{reduce} reduces matrix to a vector by treating the matrix rows/columns as a set of 1D vectors and performing the specified operation on the vectors until a single row/column is obtained. For example, the function can be used to compute horizontal and vertical projections of an raster image. In the case of \texttt{CV\_REDUCE\_SUM} and \texttt{CV\_REDUCE\_AVG} the output may have a larger element bit-depth to preserve accuracy. And multi-channel arrays are also supported in these two reduction modes. 
5405
5406 See also: \cvCppCross{repeat}
5407
5408 \cvCppFunc{repeat}
5409 Fill the destination array with repeated copies of the source array.
5410
5411 \cvdefCpp{void repeat(const Mat\& src, int ny, int nx, Mat\& dst);\newline
5412 Mat repeat(const Mat\& src, int ny, int nx);}
5413 \begin{description}
5414 \cvarg{src}{The source array to replicate}
5415 \cvarg{dst}{The destination array; will have the same type as \texttt{src}}
5416 \cvarg{ny}{How many times the \texttt{src} is repeated along the vertical axis}
5417 \cvarg{nx}{How many times the \texttt{src} is repeated along the horizontal axis}
5418 \end{description}
5419
5420 The functions \cvCppCross{repeat} duplicate the source array one or more times along each of the two axes:
5421
5422 \[\texttt{dst}_{ij}=\texttt{src}_{i\mod\texttt{src.rows},\;j\mod\texttt{src.cols}}\]
5423
5424 The second variant of the function is more convenient to use with \cross{Matrix Expressions}
5425
5426 See also: \cvCppCross{reduce}, \cross{Matrix Expressions}
5427
5428 \ifplastex
5429 \cvfunc{saturate\_cast}\label{cppfunc.saturatecast}
5430 \else
5431 \subsection{cv::saturate\_cast}\label{cppfunc.saturatecast}
5432 \fi
5433 Template function for accurate conversion from one primitive type to another
5434
5435 \cvdefCpp{template<typename \_Tp> inline \_Tp saturate\_cast(unsigned char v);\newline
5436 template<typename \_Tp> inline \_Tp saturate\_cast(signed char v);\newline
5437 template<typename \_Tp> inline \_Tp saturate\_cast(unsigned short v);\newline
5438 template<typename \_Tp> inline \_Tp saturate\_cast(signed short v);\newline
5439 template<typename \_Tp> inline \_Tp saturate\_cast(int v);\newline
5440 template<typename \_Tp> inline \_Tp saturate\_cast(unsigned int v);\newline
5441 template<typename \_Tp> inline \_Tp saturate\_cast(float v);\newline
5442 template<typename \_Tp> inline \_Tp saturate\_cast(double v);}
5443
5444 \begin{description}
5445 \cvarg{v}{The function parameter}
5446 \end{description}
5447
5448 The functions \texttt{saturate\_cast} resembles the standard C++ cast operations, such as \texttt{static\_cast<T>()} etc. They perform an efficient and accurate conversion from one primitive type to another, see the introduction. "saturate" in the name means that when the input value \texttt{v} is out of range of the target type, the result will not be formed just by taking low bits of the input, but instead the value will be clipped. For example:
5449
5450 \begin{lstlisting}
5451 uchar a = saturate_cast<uchar>(-100); // a = 0 (UCHAR_MIN)
5452 short b = saturate_cast<short>(33333.33333); // b = 32767 (SHRT_MAX)
5453 \end{lstlisting}
5454
5455 Such clipping is done when the target type is \texttt{unsigned char, signed char, unsigned short or signed short} - for 32-bit integers no clipping is done.
5456
5457 When the parameter is floating-point value and the target type is an integer (8-, 16- or 32-bit), the floating-point value is first rounded to the nearest integer and then clipped if needed (when the target type is 8- or 16-bit).
5458
5459 This operation is used in most simple or complex image processing functions in OpenCV.
5460
5461 See also: \cvCppCross{add}, \cvCppCross{subtract}, \cvCppCross{multiply}, \cvCppCross{divide}, \cvCppCross{Mat::convertTo}
5462
5463 \cvCppFunc{scaleAdd}
5464 Calculates the sum of a scaled array and another array.
5465
5466 \cvdefCpp{void scaleAdd(const Mat\& src1, double scale, \par const Mat\& src2, Mat\& dst);\newline
5467 void scaleAdd(const MatND\& src1, double scale, \par const MatND\& src2, MatND\& dst);}
5468 \begin{description}
5469 \cvarg{src1}{The first source array}
5470 \cvarg{scale}{Scale factor for the first array}
5471 \cvarg{src2}{The second source array; must have the same size and the same type as \texttt{src1}}
5472 \cvarg{dst}{The destination array; will have the same size and the same type as \texttt{src1}}
5473 \end{description}
5474
5475 The function \texttt{cvScaleAdd} is one of the classical primitive linear algebra operations, known as \texttt{DAXPY} or \texttt{SAXPY} in \href{http://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms}{BLAS}. It calculates the sum of a scaled array and another array:
5476
5477 \[
5478 \texttt{dst}(I)=\texttt{scale} \cdot \texttt{src1}(I) + \texttt{src2}(I)
5479 \]
5480
5481 The function can also be emulated with a matrix expression, for example:
5482
5483 \begin{lstlisting}
5484 Mat A(3, 3, CV_64F);
5485 ...
5486 A.row(0) = A.row(1)*2 + A.row(2);
5487 \end{lstlisting}
5488
5489 See also: \cvCppCross{add}, \cvCppCross{addWeighted}, \cvCppCross{subtract}, \cvCppCross{Mat::dot}, \cvCppCross{Mat::convertTo}, \cross{Matrix Expressions}
5490
5491 \cvCppFunc{setIdentity}
5492 Initializes a scaled identity matrix
5493
5494 \cvdefCpp{void setIdentity(Mat\& dst, const Scalar\& value=Scalar(1));}
5495 \begin{description}
5496 \cvarg{dst}{The matrix to initialize (not necessarily square)}
5497 \cvarg{value}{The value to assign to the diagonal elements}
5498 \end{description}
5499
5500 The function \cvCppCross{setIdentity} initializes a scaled identity matrix:
5501
5502 \[
5503 \texttt{dst}(i,j)=\fork{\texttt{value}}{ if $i=j$}{0}{otherwise}
5504 \]
5505
5506 The function can also be emulated using the matrix initializers and the matrix expressions:
5507 \begin{lstlisting}
5508 Mat A = Mat::eye(4, 3, CV_32F)*5;
5509 // A will be set to [[5, 0, 0], [0, 5, 0], [0, 0, 5], [0, 0, 0]]
5510 \end{lstlisting}
5511
5512 See also: \cvCppCross{Mat::zeros}, \cvCppCross{Mat::ones}, \cross{Matrix Expressions},
5513 \cvCppCross{Mat::setTo}, \cvCppCross{Mat::operator=},
5514
5515 \cvCppFunc{solve}
5516 Solves one or more linear systems or least-squares problems.
5517
5518 \cvdefCpp{bool solve(const Mat\& src1, const Mat\& src2, \par Mat\& dst, int flags=DECOMP\_LU);}
5519 \begin{description}
5520 \cvarg{src1}{The input matrix on the left-hand side of the system}
5521 \cvarg{src2}{The input matrix on the right-hand side of the system}
5522 \cvarg{dst}{The output solution}
5523 \cvarg{flags}{The solution (matrix inversion) method
5524 \begin{description}
5525  \cvarg{DECOMP\_LU}{Gaussian elimination with optimal pivot element chosen}
5526  \cvarg{DECOMP\_CHOLESKY}{Cholesky $LL^T$ factorization; the matrix \texttt{src1} must be symmetrical and positively defined}
5527  \cvarg{DECOMP\_EIG}{Eigenvalue decomposition; the matrix \texttt{src1} must be symmetrical}
5528  \cvarg{DECOMP\_SVD}{Singular value decomposition (SVD) method; the system can be over-defined and/or the matrix \texttt{src1} can be singular}
5529  \cvarg{DECOMP\_QR}{QR factorization; the system can be over-defined and/or the matrix \texttt{src1} can be singular}
5530  \cvarg{DECOMP\_NORMAL}{While all the previous flags are mutually exclusive, this flag can be used together with any of the previous. It means that the normal equations $\texttt{src1}^T\cdot\texttt{src1}\cdot\texttt{dst}=\texttt{src1}^T\texttt{src2}$ are solved instead of the original system $\texttt{src1}\cdot\texttt{dst}=\texttt{src2}$}
5531 \end{description}}
5532 \end{description}
5533
5534 The function \texttt{solve} solves a linear system or least-squares problem (the latter is possible with SVD or QR methods, or by specifying the flag \texttt{DECOMP\_NORMAL}):
5535
5536 \[
5537 \texttt{dst} = \arg \min_X\|\texttt{src1}\cdot\texttt{X} - \texttt{src2}\|
5538 \]
5539
5540 If \texttt{DECOMP\_LU} or \texttt{DECOMP\_CHOLESKY} method is used, the function returns 1 if \texttt{src1} (or $\texttt{src1}^T\texttt{src1}$) is non-singular and 0 otherwise; in the latter case \texttt{dst} is not valid. Other methods find some pseudo-solution in the case of singular left-hand side part.
5541
5542 Note that if you want to find unity-norm solution of an under-defined singular system $\texttt{src1}\cdot\texttt{dst}=0$, the function \texttt{solve} will not do the work. Use \cvCppCross{SVD::solveZ} instead.
5543
5544 See also: \cvCppCross{invert}, \cvCppCross{SVD}, \cvCppCross{eigen}
5545
5546 \cvCppFunc{solveCubic}
5547 Finds the real roots of a cubic equation.
5548
5549 \cvdefCpp{void solveCubic(const Mat\& coeffs, Mat\& roots);}
5550 \begin{description}
5551 \cvarg{coeffs}{The equation coefficients, an array of 3 or 4 elements}
5552 \cvarg{roots}{The destination array of real roots which will have 1 or 3 elements}
5553 \end{description}
5554
5555 The function \texttt{solveCubic} finds the real roots of a cubic equation:
5556
5557 (if coeffs is a 4-element vector)
5558
5559 \[
5560 \texttt{coeffs}[0] x^3 + \texttt{coeffs}[1] x^2 + \texttt{coeffs}[2] x + \texttt{coeffs}[3] = 0
5561 \]
5562
5563 or (if coeffs is 3-element vector):
5564
5565 \[
5566 x^3 + \texttt{coeffs}[0] x^2 + \texttt{coeffs}[1] x + \texttt{coeffs}[2] = 0
5567 \]
5568
5569 The roots are stored to \texttt{roots} array.
5570
5571 \cvCppFunc{solvePoly}
5572 Finds the real or complex roots of a polynomial equation
5573
5574 \cvdefCpp{void solvePoly(const Mat\& coeffs, Mat\& roots, \par int maxIters=20, int fig=100);}
5575 \begin{description}
5576 \cvarg{coeffs}{The array of polynomial coefficients}
5577 \cvarg{roots}{The destination (complex) array of roots}
5578 \cvarg{maxIters}{The maximum number of iterations the algorithm does}
5579 \cvarg{fig}{}
5580 \end{description}
5581
5582 The function \texttt{solvePoly} finds real and complex roots of a polynomial equation:
5583 \[
5584 \texttt{coeffs}[0] x^{n} + \texttt{coeffs}[1] x^{n-1} + ... + \texttt{coeffs}[n-1] x + \texttt{coeffs}[n] = 0
5585 \]
5586
5587 \cvCppFunc{sort}
5588 Sorts each row or each column of a matrix
5589
5590 \cvdefCpp{void sort(const Mat\& src, Mat\& dst, int flags);}
5591 \begin{description}
5592 \cvarg{src}{The source single-channel array}
5593 \cvarg{dst}{The destination array of the same size and the same type as \texttt{src}}
5594 \cvarg{flags}{The operation flags, a combination of the following values:
5595 \begin{description}
5596     \cvarg{CV\_SORT\_EVERY\_ROW}{Each matrix row is sorted independently}
5597     \cvarg{CV\_SORT\_EVERY\_COLUMN}{Each matrix column is sorted independently. This flag and the previous one are mutually exclusive}
5598     \cvarg{CV\_SORT\_ASCENDING}{Each matrix row is sorted in the ascending order}
5599     \cvarg{CV\_SORT\_DESCENDING}{Each matrix row is sorted in the descending order. This flag and the previous one are also mutually exclusive}
5600 \end{description}}
5601 \end{description}
5602
5603 The function \texttt{sort} sorts each matrix row or each matrix column in ascending or descending order. If you want to sort matrix rows or columns lexicographically, you can use STL \texttt{std::sort} generic function with the proper comparison predicate.
5604
5605 See also: \cvCppCross{sortIdx}, \cvCppCross{randShuffle}
5606
5607 \cvCppFunc{sortIdx}
5608 Sorts each row or each column of a matrix
5609
5610 \cvdefCpp{void sortIdx(const Mat\& src, Mat\& dst, int flags);}
5611 \begin{description}
5612 \cvarg{src}{The source single-channel array}
5613 \cvarg{dst}{The destination integer array of the same size as \texttt{src}}
5614 \cvarg{flags}{The operation flags, a combination of the following values:
5615 \begin{description}
5616     \cvarg{CV\_SORT\_EVERY\_ROW}{Each matrix row is sorted independently}
5617     \cvarg{CV\_SORT\_EVERY\_COLUMN}{Each matrix column is sorted independently. This flag and the previous one are mutually exclusive}
5618     \cvarg{CV\_SORT\_ASCENDING}{Each matrix row is sorted in the ascending order}
5619     \cvarg{CV\_SORT\_DESCENDING}{Each matrix row is sorted in the descending order. This flag and the previous one are also mutually exclusive}
5620 \end{description}}
5621 \end{description}
5622
5623 The function \texttt{sortIdx} sorts each matrix row or each matrix column in ascending or descending order. Instead of reordering the elements themselves, it stores the indices of sorted elements in the destination array. For example:
5624
5625 \begin{lstlisting}
5626 Mat A = Mat::eye(3,3,CV_32F), B;
5627 sortIdx(A, B, CV_SORT_EVERY_ROW + CV_SORT_ASCENDING);
5628 // B will probably contain
5629 // (because of equal elements in A some permutations are possible):
5630 // [[1, 2, 0], [0, 2, 1], [0, 1, 2]]
5631 \end{lstlisting}
5632
5633 See also: \cvCppCross{sort}, \cvCppCross{randShuffle}
5634
5635 \cvCppFunc{split}
5636 Divides multi-channel array into several single-channel arrays
5637
5638 \cvdefCpp{void split(const Mat\& mtx, Mat* mv);\newline
5639 void split(const Mat\& mtx, vector<Mat>\& mv);\newline
5640 void split(const MatND\& mtx, MatND* mv);\newline
5641 void split(const MatND\& mtx, vector<MatND>\& mv);}
5642 \begin{description}
5643 \cvarg{mtx}{The source multi-channel array}
5644 \cvarg{mv}{The destination array or vector of arrays; The number of arrays must match \texttt{mtx.channels()}. The arrays themselves will be reallocated if needed}
5645 \end{description}
5646
5647 The functions \texttt{split} split multi-channel array into separate single-channel arrays:
5648
5649 \[ \texttt{mv}[c](I) = \texttt{mtx}(I)_c \]
5650
5651 If you need to extract a single-channel or do some other sophisticated channel permutation, use \cvCppCross{mixChannels}
5652
5653 See also: \cvCppCross{merge}, \cvCppCross{mixChannels}, \cvCppCross{cvtColor}
5654
5655 \cvCppFunc{sqrt}
5656 Calculates square root of array elements
5657
5658 \cvdefCpp{void sqrt(const Mat\& src, Mat\& dst);\newline
5659 void sqrt(const MatND\& src, MatND\& dst);}
5660 \begin{description}
5661 \cvarg{src}{The source floating-point array}
5662 \cvarg{dst}{The destination array; will have the same size and the same type as \texttt{src}}
5663 \end{description}
5664
5665 The functions \texttt{sqrt} calculate square root of each source array element. in the case of multi-channel arrays each channel is processed independently. The function accuracy is approximately the same as of the built-in \texttt{std::sqrt}.
5666
5667 See also: \cvCppCross{pow}, \cvCppCross{magnitude}
5668
5669 \cvCppFunc{subtract}
5670 Calculates per-element difference between two arrays or array and a scalar
5671
5672 \cvdefCpp{void subtract(const Mat\& src1, const Mat\& src2, Mat\& dst);\newline
5673 void subtract(const Mat\& src1, const Mat\& src2, \par Mat\& dst, const Mat\& mask);\newline
5674 void subtract(const Mat\& src1, const Scalar\& sc, \par Mat\& dst, const Mat\& mask=Mat());\newline
5675 void subtract(const Scalar\& sc, const Mat\& src2, \par Mat\& dst, const Mat\& mask=Mat());\newline
5676 void subtract(const MatND\& src1, const MatND\& src2, MatND\& dst);\newline
5677 void subtract(const MatND\& src1, const MatND\& src2, \par MatND\& dst, const MatND\& mask);\newline
5678 void subtract(const MatND\& src1, const Scalar\& sc, \par MatND\& dst, const MatND\& mask=MatND());\newline
5679 void subtract(const Scalar\& sc, const MatND\& src2, \par MatND\& dst, const MatND\& mask=MatND());}
5680 \begin{description}
5681 \cvarg{src1}{The first source array}
5682 \cvarg{src2}{The second source array. It must have the same size and same type as \texttt{src1}}
5683 \cvarg{sc}{Scalar; the first or the second input parameter}
5684 \cvarg{dst}{The destination array; it will have the same size and same type as \texttt{src1}; see \texttt{Mat::create}}
5685 \cvarg{mask}{The optional operation mask, 8-bit single channel array;
5686              specifies elements of the destination array to be changed}
5687 \end{description}
5688
5689 The functions \texttt{subtract} compute
5690
5691 \begin{itemize}
5692     \item the difference between two arrays
5693     \[\texttt{dst}(I) = \texttt{saturate}(\texttt{src1}(I) - \texttt{src2}(I))\quad\texttt{if mask}(I)\ne0\]
5694     \item the difference between array and a scalar:
5695     \[\texttt{dst}(I) = \texttt{saturate}(\texttt{src1}(I) - \texttt{sc})\quad\texttt{if mask}(I)\ne0\]
5696     \item the difference between scalar and an array:
5697     \[\texttt{dst}(I) = \texttt{saturate}(\texttt{sc} - \texttt{src2}(I))\quad\texttt{if mask}(I)\ne0\]
5698 \end{itemize}
5699
5700 where \texttt{I} is multi-dimensional index of array elements.
5701
5702 The first function in the above list can be replaced with matrix expressions:
5703 \begin{lstlisting}
5704 dst = src1 - src2;
5705 dst -= src2; // equivalent to subtract(dst, src2, dst);
5706 \end{lstlisting}
5707
5708 See also: \cvCppCross{add}, \cvCppCross{addWeighted}, \cvCppCross{scaleAdd}, \cvCppCross{convertScale},
5709 \cross{Matrix Expressions}, \hyperref[cppfunc.saturatecast]{saturate\_cast}.
5710
5711 \cvclass{SVD}
5712 Class for computing Singular Value Decomposition
5713
5714 \begin{lstlisting}
5715 class SVD
5716 {
5717 public:
5718     enum { MODIFY_A=1, NO_UV=2, FULL_UV=4 };newline
5719     // default empty constructor
5720     SVD();newline
5721     // decomposes m into u, w and vt: m = u*w*vt;newline
5722     // u and vt are orthogonal, w is diagonal
5723     SVD( const Mat& m, int flags=0 );newline
5724     // decomposes m into u, w and vt.
5725     SVD& operator ()( const Mat& m, int flags=0 );newline
5726
5727     // finds such vector x, norm(x)=1, so that m*x = 0,
5728     // where m is singular matrix
5729     static void solveZ( const Mat& m, Mat& dst );newline
5730     // does back-subsitution:
5731     // dst = vt.t()*inv(w)*u.t()*rhs ~ inv(m)*rhs
5732     void backSubst( const Mat& rhs, Mat& dst ) const;newline
5733
5734     Mat u, w, vt;
5735 };
5736 \end{lstlisting}
5737
5738 The class \texttt{SVD} is used to compute Singular Value Decomposition of a floating-point matrix and then use it to solve least-square problems, under-determined linear systems, invert matrices, compute condition numbers etc.
5739 For a bit faster operation you can pass \texttt{flags=SVD::MODIFY\_A|...} to modify the decomposed matrix when it is not necessarily to preserve it. If you want to compute condition number of a matrix or absolute value of its determinant - you do not need \texttt{u} and \texttt{vt}, so you can pass \texttt{flags=SVD::NO\_UV|...}. Another flag \texttt{FULL\_UV} indicates that full-size \texttt{u} and \texttt{vt} must be computed, which is not necessary most of the time.
5740
5741 See also: \cvCppCross{invert}, \cvCppCross{solve}, \cvCppCross{eigen}, \cvCppCross{determinant}
5742
5743 \cvCppFunc{sum}
5744 Calculates sum of array elements
5745
5746 \cvdefCpp{Scalar sum(const Mat\& mtx);\newline
5747 Scalar sum(const MatND\& mtx);}
5748 \begin{description}
5749 \cvarg{mtx}{The source array; must have 1 to 4 channels}
5750 \end{description}
5751
5752 The functions \texttt{sum} calculate and return the sum of array elements, independently for each channel.
5753
5754 See also: \cvCppCross{countNonZero}, \cvCppCross{mean}, \cvCppCross{meanStdDev}, \cvCppCross{norm}, \cvCppCross{minMaxLoc}, \cvCppCross{reduce}
5755
5756 \cvCppFunc{theRNG}
5757 Returns the default random number generator
5758
5759 \cvdefCpp{RNG\& theRNG();}
5760
5761 The function \texttt{theRNG} returns the default random number generator. For each thread there is separate random number generator, so you can use the function safely in multi-thread environments. If you just need to get a single random number using this generator or initialize an array, you can use \cvCppCross{randu} or \cvCppCross{randn} instead. But if you are going to generate many random numbers inside a loop, it will be much faster to use this function to retrieve the generator and then use \texttt{RNG::operator \_Tp()}.
5762
5763 See also: \cvCppCross{RNG}, \cvCppCross{randu}, \cvCppCross{randn}
5764
5765 \cvCppFunc{trace}
5766 Returns the trace of a matrix
5767
5768 \cvdefCpp{Scalar trace(const Mat\& mtx);}
5769 \begin{description}
5770 \cvarg{mtx}{The source matrix}
5771 \end{description}
5772
5773 The function \texttt{trace} returns the sum of the diagonal elements of the matrix \texttt{mtx}.
5774
5775 \[ \mathrm{tr}(\texttt{mtx}) = \sum_i \texttt{mtx}(i,i) \]
5776
5777
5778 \cvCppFunc{transform}
5779 Performs matrix transformation of every array element.
5780
5781 \cvdefCpp{void transform(const Mat\& src, \par Mat\& dst, const Mat\& mtx );}
5782 \begin{description}
5783 \cvarg{src}{The source array; must have as many channels (1 to 4) as \texttt{mtx.cols} or \texttt{mtx.cols-1}}
5784 \cvarg{dst}{The destination array; will have the same size and depth as \texttt{src} and as many channels as \texttt{mtx.rows}}
5785 \cvarg{mtx}{The transformation matrix}
5786 \end{description}
5787
5788 The function \texttt{transform} performs matrix transformation of every element of array \texttt{src} and stores the results in \texttt{dst}:
5789
5790 \[
5791 \texttt{dst}(I) = \texttt{mtx} \cdot \texttt{src}(I)
5792 \]
5793 (when \texttt{mtx.cols=src.channels()}), or
5794
5795 \[
5796 \texttt{dst}(I) = \texttt{mtx} \cdot [\texttt{src}(I); 1]
5797 \]
5798 (when \texttt{mtx.cols=src.channels()+1})
5799
5800 That is, every element of an \texttt{N}-channel array \texttt{src} is
5801 considered as \texttt{N}-element vector, which is transformed using
5802 a $\texttt{M} \times \texttt{N}$ or $\texttt{M} \times \texttt{N+1}$ matrix \texttt{mtx} into
5803 an element of \texttt{M}-channel array \texttt{dst}.
5804
5805 The function may be used for geometrical transformation of $N$-dimensional
5806 points, arbitrary linear color space transformation (such as various kinds of RGB$\rightarrow$YUV transforms), shuffling the image channels and so forth.
5807
5808 See also: \cvCppCross{perspectiveTransform}, \cvCppCross{getAffineTransform}, \cvCppCross{estimateRigidTransform}, \cvCppCross{warpAffine}, \cvCppCross{warpPerspective}
5809
5810 \cvCppFunc{transpose}
5811 Transposes a matrix
5812
5813 \cvdefCpp{void transpose(const Mat\& src, Mat\& dst);}
5814 \begin{description}
5815 \cvarg{src}{The source array}
5816 \cvarg{dst}{The destination array of the same type as \texttt{src}}
5817 \end{description}
5818
5819 The function \cvCppCross{transpose} transposes the matrix \texttt{src}:
5820
5821 \[ \texttt{dst}(i,j) = \texttt{src}(j,i) \]
5822
5823 Note that no complex conjugation is done in the case of a complex
5824 matrix, it should be done separately if needed.
5825
5826 \fi