#1672
[profile/ivi/opencv.git] / doc / opencv_cheatsheet.tex
1 %
2 %    The OpenCV cheatsheet structure:
3 %
4 %    opencv data structures
5 %        point, rect
6 %        matrix
7 %
8 %    creating matrices
9 %        from scratch
10 %        from previously allocated data: plain arrays, vectors 
11 %        converting to/from old-style structures
12 %
13 %    element access, iteration through matrix elements
14 %
15 %    copying & shuffling matrix data
16 %        copying & converting the whole matrices
17 %        extracting matrix parts & copying them
18 %        split, merge & mixchannels
19 %        flip, transpose, repeat
20 %
21 %    matrix & image operations:
22 %        arithmetics & logic
23 %        matrix multiplication, inversion, determinant, trace, SVD
24 %        statistical functions
25 %
26 %    basic image processing:
27 %        image filtering with predefined & custom filters
28 %        example: finding local maxima
29 %        geometrical transformations, resize, warpaffine, perspective & remap.
30 %        color space transformations
31 %        histograms & back projections
32 %        contours
33 %        
34 %    i/o:
35 %        displaying images
36 %        saving/loading to/from file (XML/YAML & image file formats)
37 %        reading videos & camera feed, writing videos
38 %
39 %    operations on point sets:
40 %        findcontours, bounding box, convex hull, min area rect,
41 %            transformations, to/from homogeneous coordinates
42 %        matching point sets: homography, fundamental matrix, rigid transforms
43 %        
44 %    3d:
45 %        camera calibration, pose estimation.
46 %        uncalibrated case
47 %        stereo: rectification, running stereo correspondence, obtaining the depth.
48 %    
49 %    feature detection:
50 %        features2d toolbox
51 %    
52 %    object detection:
53 %        using a classifier running on a sliding window: cascadeclassifier + hog.
54 %        using salient point features: features2d -> matching
55 %        
56 %    statistical data processing:
57 %        clustering (k-means),
58 %        classification + regression (SVM, boosting, k-nearest),
59 %        compressing data (PCA)
60 %
61
62 \documentclass[10pt,landscape]{article}
63 \usepackage[usenames,dvips,pdftex]{color}
64 \usepackage{multicol}
65 \usepackage{calc}
66 \usepackage{ifthen}
67 \usepackage[pdftex]{color,graphicx}
68 \usepackage[landscape]{geometry}
69 \usepackage{hyperref}
70 \hypersetup{colorlinks=true, filecolor=black, linkcolor=black, urlcolor=blue, citecolor=black}
71 \graphicspath{{./images/}}
72
73 % This sets page margins to .5 inch if using letter paper, and to 1cm
74 % if using A4 paper. (This probably isn't strictly necessary.)
75 % If using another size paper, use default 1cm margins.
76 \ifthenelse{\lengthtest { \paperwidth = 11in}}
77         { \geometry{top=.5in,left=.5in,right=.5in,bottom=.5in} }
78         {\ifthenelse{ \lengthtest{ \paperwidth = 297mm}}
79                 {\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
80                 {\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
81         }
82
83 % Turn off header and footer
84 % \pagestyle{empty}
85
86 % Redefine section commands to use less space
87 \makeatletter
88 \renewcommand{\section}{\@startsection{section}{1}{0mm}%
89                                 {-1ex plus -.5ex minus -.2ex}%
90                                 {0.5ex plus .2ex}%x
91                                 {\normalfont\large\bfseries}}
92 \renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}%
93                                 {-1explus -.5ex minus -.2ex}%
94                                 {0.5ex plus .2ex}%
95                                 {\normalfont\normalsize\bfseries}}
96 \renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}%
97                                 {-1ex plus -.5ex minus -.2ex}%
98                                 {1ex plus .2ex}%
99                                 {\normalfont\small\bfseries}}
100 \makeatother
101
102 % Define BibTeX command
103 \def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
104     T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}
105
106 % Don't print section numbers
107 \setcounter{secnumdepth}{0}
108
109
110 %\setlength{\parindent}{0pt}
111 %\setlength{\parskip}{0pt plus 0.5ex}
112
113 \newcommand{\ccode}[1]{
114 \begin{alltt}
115 #1
116 \end{alltt}
117 }
118
119 % -----------------------------------------------------------------------
120
121 \begin{document}
122
123 \raggedright
124 \footnotesize
125 \begin{multicols}{3}
126
127
128 % multicol parameters
129 % These lengths are set only within the two main columns
130 %\setlength{\columnseprule}{0.25pt}
131 \setlength{\premulticols}{1pt}
132 \setlength{\postmulticols}{1pt}
133 \setlength{\multicolsep}{1pt}
134 \setlength{\columnsep}{2pt}
135
136 \begin{center}
137      \Large{\textbf{OpenCV 2.3 Cheat Sheet (C++)}} \\
138 \end{center}
139 \newlength{\MyLen}
140 \settowidth{\MyLen}{\texttt{letterpaper}/\texttt{a4paper} \ }
141
142 %\section{Filesystem Concepts}
143 %\begin{tabular}{@{}p{\the\MyLen}%
144  %               @{}p{\linewidth-\the\MyLen}@{}}
145 %\texttt{\href{http://www.ros.org/wiki/Packages}{package}}   & The lowest level of ROS software organization. \\
146 %\texttt{\href{http://www.ros.org/wiki/Manifest}{manifest}}  & Description of a ROS package. \\
147 %\texttt{\href{http://www.ros.org/wiki/Stack}{stack}} & Collections of ROS packages that form a higher-level library. \\
148 %\texttt{\href{http://www.ros.org/wiki/Stack Manifest}{stack manifest}}  & Description of a ROS stack.
149 %\end{tabular}
150
151 \emph{The OpenCV C++ reference manual is here: \url{http://opencv.itseez.com}. Use \textbf{Quick Search} to find descriptions of the particular functions and classes}
152
153 \section{Key OpenCV Classes}
154 \begin{tabular}{@{}p{\the\MyLen}%
155                 @{}p{\linewidth-\the\MyLen}@{}}
156 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#Point_}{Point\_}} & Template 2D point class \\
157 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#Point3_}{Point3\_}} & Template 3D point class \\
158 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#Size_}{Size\_}} & Template size (width, height) class \\
159 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#Vec}{Vec}} & Template short vector class \\
160 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#Matx}{Matx}} & Template small matrix class \\
161 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#Scalar_}{Scalar}} & 4-element vector \\
162 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#Rect_}{Rect}} & Rectangle \\
163 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#Range}{Range}} & Integer value range \\
164 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#Mat}{Mat}} & 2D or multi-dimensional dense array (can be used to store matrices, images, histograms, feature descriptors, voxel volumes etc.)\\
165 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#sparsemat}{SparseMat}} & Multi-dimensional sparse array \\
166 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#Ptr}{Ptr}} & Template smart pointer class
167 \end{tabular}
168
169 \section{Matrix Basics}
170 \begin{tabbing}
171
172 \textbf{Cr}\=\textbf{ea}\=\textbf{te}\={} \textbf{a matrix} \\
173 \> \texttt{Mat image(240, 320, CV\_8UC3);} \\
174
175 \textbf{[Re]allocate a pre-declared matrix}\\
176 \> \texttt{image.\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#mat-create}{create}(480, 640, CV\_8UC3);}\\
177
178 \textbf{Create a matrix initialized with a constant}\\
179 \> \texttt{Mat A33(3, 3, CV\_32F, Scalar(5));} \\
180 \> \texttt{Mat B33(3, 3, CV\_32F); B33 = Scalar(5);} \\
181 \> \texttt{Mat C33 = Mat::ones(3, 3, CV\_32F)*5.;} \\
182 \> \texttt{Mat D33 = Mat::zeros(3, 3, CV\_32F) + 5.;} \\
183
184 \textbf{Create a matrix initialized with specified values}\\
185 \> \texttt{double a = CV\_PI/3;} \\
186 \> \texttt{Mat A22 = (Mat\_<float>(2, 2) <<} \\
187 \> \> \texttt{cos(a), -sin(a), sin(a), cos(a));} \\
188 \> \texttt{float B22data[] = \{cos(a), -sin(a), sin(a), cos(a)\};} \\
189 \> \texttt{Mat B22 = Mat(2, 2, CV\_32F, B22data).clone();}\\
190
191 \textbf{Initialize a random matrix}\\
192 \> \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#randu}{randu}(image, Scalar(0), Scalar(256)); }\textit{// uniform dist}\\
193 \> \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#randn}{randn}(image, Scalar(128), Scalar(10)); }\textit{// Gaussian dist}\\
194
195 \textbf{Convert matrix to/from other structures}\\
196 \>\textbf{(without copying the data)}\\
197 \> \texttt{Mat image\_alias = image;}\\
198 \> \texttt{float* Idata=new float[480*640*3];}\\
199 \> \texttt{Mat I(480, 640, CV\_32FC3, Idata);}\\
200 \> \texttt{vector<Point> iptvec(10);}\\
201 \> \texttt{Mat iP(iptvec); }\textit{// iP -- 10x1 CV\_32SC2 matrix}\\
202 \> \texttt{IplImage* oldC0 = cvCreateImage(cvSize(320,240),16,1);}\\
203 \> \texttt{Mat newC = cvarrToMat(oldC0);}\\
204 \> \texttt{IplImage oldC1 = newC; CvMat oldC2 = newC;}\\
205
206 \textbf{... (with copying the data)}\\
207 \> \texttt{Mat newC2 = cvarrToMat(oldC0).clone();}\\
208 \> \texttt{vector<Point2f> ptvec = Mat\_<Point2f>(iP);}\\
209
210 \>\\
211 \textbf{Access matrix elements}\\
212 \> \texttt{A33.at<float>(i,j) = A33.at<float>(j,i)+1;}\\
213 \> \texttt{Mat dyImage(image.size(), image.type());}\\
214 \> \texttt{for(int y = 1; y < image.rows-1; y++) \{}\\
215 \> \> \texttt{Vec3b* prevRow = image.ptr<Vec3b>(y-1);}\\
216 \> \> \texttt{Vec3b* nextRow = image.ptr<Vec3b>(y+1);}\\
217 \> \> \texttt{for(int x = 0; y < image.cols; x++)}\\
218 \> \> \> \texttt{for(int c = 0; c < 3; c++)}\\
219 \> \> \> \texttt{  dyImage.at<Vec3b>(y,x)[c] =}\\
220 \> \> \> \texttt{    saturate\_cast<uchar>(}\\
221 \> \> \> \texttt{       nextRow[x][c] - prevRow[x][c]);}\\
222 \> \texttt{\} }\\
223 \> \texttt{Mat\_<Vec3b>::iterator it = image.begin<Vec3b>(),}\\
224 \> \> \texttt{itEnd = image.end<Vec3b>();}\\
225 \> \texttt{for(; it != itEnd; ++it)}\\
226 \> \> \texttt{(*it)[1] \textasciicircum{}= 255;}\\
227
228 \end{tabbing}
229
230 \section{Matrix Manipulations: Copying, Shuffling, Part Access}
231 \begin{tabular}{@{}p{\the\MyLen}%
232                 @{}p{\linewidth-\the\MyLen}@{}}
233 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#mat-copyto}{src.copyTo(dst)}} & Copy matrix to another one \\
234 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#mat-convertto}{src.convertTo(dst,type,scale,shift)}} & \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Scale and convert to another datatype \\
235 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#mat-clone}{m.clone()}} & Make deep copy of a matrix \\
236 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#mat-reshape}{m.reshape(nch,nrows)}} & Change matrix dimensions and/or number of channels without copying data \\
237
238 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#mat-row}{m.row(i)}},
239 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#mat-col}{m.col(i)}} & Take a matrix row/column \\
240
241 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#mat-rowrange}{m.rowRange(Range(i1,i2))}}
242 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#mat-colrange}{m.colRange(Range(j1,j2))}} & \ \ \ \ \ \ \ Take a matrix row/column span \\
243
244 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#mat-diag}{m.diag(i)}} & Take a matrix diagonal \\
245
246 \texttt{\href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#Mat}{m(Range(i1,i2),Range(j1,j2)), m(roi)}} & \ \ \ \ \ \ \ \ \ \ \ \ \ Take a submatrix \\
247
248 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#repeat}{m.repeat(ny,nx)}} & Make a bigger matrix from a smaller one \\
249
250 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#flip}{flip(src,dst,dir)}} & Reverse the order of matrix rows and/or columns \\
251
252 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#split}{split(...)}} & Split multi-channel matrix into separate channels \\
253
254 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#merge}{merge(...)}} & Make a multi-channel matrix out of the separate channels \\
255
256 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#mixchannels}{mixChannels(...)}} & Generalized form of split() and merge() \\
257
258 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#randshuffle}{randShuffle(...)}} & Randomly shuffle matrix elements \\
259
260 \end{tabular}
261
262 \begin{tabbing}
263 Exa\=mple 1. Smooth image ROI in-place\\
264 \>\texttt{Mat imgroi = image(Rect(10, 20, 100, 100));}\\
265 \>\texttt{GaussianBlur(imgroi, imgroi, Size(5, 5), 1.2, 1.2);}\\
266 Example 2. Somewhere in a linear algebra algorithm \\
267 \>\texttt{m.row(i) += m.row(j)*alpha;}\\
268 Example 3. Copy image ROI to another image with conversion\\
269 \>\texttt{Rect r(1, 1, 10, 20);}\\
270 \>\texttt{Mat dstroi = dst(Rect(0,10,r.width,r.height));}\\
271 \>\texttt{src(r).convertTo(dstroi, dstroi.type(), 1, 0);}\\
272 \end{tabbing}
273
274 \section{Simple Matrix Operations}
275
276 OpenCV implements most common arithmetical, logical and
277 other matrix operations, such as
278
279 \begin{itemize}
280 \item
281 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#add}{add()}},  
282 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#subtract}{subtract()}},
283 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#multiply}{multiply()}},
284 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#divide}{divide()}},
285 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#absdiff}{absdiff()}},
286 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#bitwise-and}{bitwise\_and()}},
287 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#bitwise-or}{bitwise\_or()}},
288 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#bitwise-xor}{bitwise\_xor()}},
289 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#max}{max()}},
290 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#min}{min()}},
291 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#compare}{compare()}}
292
293 -- correspondingly, addition, subtraction, element-wise multiplication ... comparison of two matrices or a matrix and a scalar.
294
295 \begin{tabbing}
296 Exa\=mple. \href{http://en.wikipedia.org/wiki/Alpha_compositing}{Alpha compositing} function:\\
297 \texttt{void alphaCompose(const Mat\& rgba1,}\\
298 \> \texttt{const Mat\& rgba2, Mat\& rgba\_dest)}\\
299 \texttt{\{ }\\
300 \> \texttt{Mat a1(rgba1.size(), rgba1.type()), ra1;}\\
301 \> \texttt{Mat a2(rgba2.size(), rgba2.type());}\\
302 \> \texttt{int mixch[]=\{3, 0, 3, 1, 3, 2, 3, 3\};}\\
303 \> \texttt{mixChannels(\&rgba1, 1, \&a1, 1, mixch, 4);}\\
304 \> \texttt{mixChannels(\&rgba2, 1, \&a2, 1, mixch, 4);}\\
305 \> \texttt{subtract(Scalar::all(255), a1, ra1);}\\
306 \> \texttt{bitwise\_or(a1, Scalar(0,0,0,255), a1);}\\
307 \> \texttt{bitwise\_or(a2, Scalar(0,0,0,255), a2);}\\
308 \> \texttt{multiply(a2, ra1, a2, 1./255);}\\
309 \> \texttt{multiply(a1, rgba1, a1, 1./255);}\\
310 \> \texttt{multiply(a2, rgba2, a2, 1./255);}\\
311 \> \texttt{add(a1, a2, rgba\_dest);}\\
312 \texttt{\}}
313 \end{tabbing}
314
315 \item
316
317 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#sum}{sum()}},
318 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#mean}{mean()}},
319 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#meanstddev}{meanStdDev()}},
320 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#norm}{norm()}},
321 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#countnonzero}{countNonZero()}},
322 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#minmaxloc}{minMaxLoc()}},
323
324 -- various statistics of matrix elements.
325
326 \item
327 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#exp}{exp()}},
328 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#log}{log()}},
329 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#pow}{pow()}},
330 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#sqrt}{sqrt()}},
331 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#carttopolar}{cartToPolar()}},
332 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#polartocart}{polarToCart()}}
333
334 -- the classical math functions.
335
336 \item
337 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#scaleadd}{scaleAdd()}},
338 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#transpose}{transpose()}},
339 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#gemm}{gemm()}},
340 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#invert}{invert()}},
341 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#solve}{solve()}},
342 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#determinant}{determinant()}},
343 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#trace}{trace()}},
344 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#eigen}{eigen()}},
345 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#SVD}{SVD}},
346
347 -- the algebraic functions + SVD class.
348
349 \item
350 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#dft}{dft()}},
351 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#idft}{idft()}},
352 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#dct}{dct()}},
353 \texttt{\href{http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html\#idct}{idct()}},
354
355 -- discrete Fourier and cosine transformations 
356
357 \end{itemize}
358
359 For some operations a more convenient \href{http://opencv.itseez.com/modules/core/doc/basic_structures.html\#matrix-expressions}{algebraic notation} can be used, for example:
360 \begin{tabbing}
361 \texttt{Mat}\={} \texttt{delta = (J.t()*J + lambda*}\\
362 \>\texttt{Mat::eye(J.cols, J.cols, J.type()))}\\
363 \>\texttt{.inv(CV\_SVD)*(J.t()*err);}
364 \end{tabbing}
365 implements the core of Levenberg-Marquardt optimization algorithm.
366
367 \section{Image Processsing}
368
369 \subsection{Filtering}
370
371 \begin{tabular}{@{}p{\the\MyLen}%
372                 @{}p{\linewidth-\the\MyLen}@{}}
373 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/filtering.html\#filter2d}{filter2D()}} & Non-separable linear filter \\
374
375 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/filtering.html\#sepfilter2d}{sepFilter2D()}} & Separable linear filter \\
376
377 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/filtering.html\#blur}{boxFilter()}},  \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/filtering.html\#gaussianblur}{GaussianBlur()}},
378 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/filtering.html\#medianblur}{medianBlur()}},
379 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/filtering.html\#bilateralfilter}{bilateralFilter()}}
380 & Smooth the image with one of the linear or non-linear filters \\
381
382 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/filtering.html\#sobel}{Sobel()}},  \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/filtering.html\#scharr}{Scharr()}}
383 & Compute the spatial image derivatives \\
384 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/filtering.html\#laplacian}{Laplacian()}} & compute Laplacian: $\Delta I = \frac{\partial ^ 2 I}{\partial x^2} + \frac{\partial ^ 2 I}{\partial y^2}$  \\
385
386 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/filtering.html\#erode}{erode()}}, \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/filtering.html\#dilate}{dilate()}} & Morphological operations \\
387
388 \end{tabular}
389
390 \begin{tabbing}
391 Exa\=mple. Filter image in-place with a 3x3 high-pass kernel\\
392 \> (preserve negative responses by shifting the result by 128):\\
393 \texttt{filter2D(image, image, image.depth(), (Mat\_<float>(3,3)<<}\\
394 \> \texttt{-1, -1, -1, -1, 9, -1, -1, -1, -1), Point(1,1), 128);}\\
395 \end{tabbing}
396
397 \subsection{Geometrical Transformations}
398
399 \begin{tabular}{@{}p{\the\MyLen}%
400                 @{}p{\linewidth-\the\MyLen}@{}}
401 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/geometric_transformations.html\#resize}{resize()}} & Resize image \\
402
403 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/geometric_transformations.html\#getrectsubpix}{getRectSubPix()}} & Extract an image patch \\
404
405 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/geometric_transformations.html\#warpaffine}{warpAffine()}} & Warp image affinely\\
406
407 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/geometric_transformations.html\#warpperspective}{warpPerspective()}} & Warp image perspectively\\
408
409 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/geometric_transformations.html\#remap}{remap()}} & Generic image warping\\
410
411 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/geometric_transformations.html\#convertmaps}{convertMaps()}} & Optimize maps for a faster remap() execution\\
412
413 \end{tabular}
414
415 \begin{tabbing}
416 Example. Decimate image by factor of $\sqrt{2}$:\\
417 \texttt{Mat dst; resize(src, dst, Size(), 1./sqrt(2), 1./sqrt(2));}
418 \end{tabbing}
419
420 \subsection{Various Image Transformations}
421
422 \begin{tabular}{@{}p{\the\MyLen}%
423                 @{}p{\linewidth-\the\MyLen}@{}}
424
425 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/miscellaneous_transformations.html\#cvtcolor}{cvtColor()}} & Convert image from one color space to another \\
426
427 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/miscellaneous_transformations.html\#threshold}{threshold()}}, \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/miscellaneous_transformations.html\#adaptivethreshold}{adaptivethreshold()}} & Convert grayscale image to binary image using a fixed or a variable threshold \\
428
429 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/miscellaneous_transformations.html\#floodfill}{floodFill()}} & Find a connected component using region growing algorithm\\
430
431 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/miscellaneous_transformations.html\#integral}{integral()}} & Compute integral image \\
432
433 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/miscellaneous_transformations.html\#distancetransform}{distanceTransform()}}
434  & build distance map or discrete Voronoi diagram for a binary image. \\
435
436 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/miscellaneous_transformations.html\#watershed}{watershed()}},
437 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/miscellaneous_transformations.html\#grabcut}{grabCut()}}
438  & marker-based image segmentation algorithms.
439  See the samples \texttt{\href{http://code.opencv.org/svn/opencv/trunk/opencv/samples/cpp/watershed.cpp}{watershed.cpp}} and \texttt{\href{http://code.opencv.org/svn/opencv/trunk/opencv/samples/cpp/grabcut.cpp}{grabcut.cpp}}.
440
441 \end{tabular}
442
443 \subsection{Histograms}
444
445 \begin{tabular}{@{}p{\the\MyLen}%
446                 @{}p{\linewidth-\the\MyLen}@{}}
447
448 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/histograms.html\#calchist}{calcHist()}} & Compute image(s) histogram \\
449
450 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/histograms.html\#calcbackproject}{calcBackProject()}} & Back-project the histogram \\
451
452 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/histograms.html\#equalizehist}{equalizeHist()}} & Normalize image brightness and contrast\\
453
454 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/histograms.html\#comparehist}{compareHist()}} & Compare two histograms\\
455
456 \end{tabular}
457
458 \begin{tabbing}
459 Example. Compute Hue-Saturation histogram of an image:\\
460 \texttt{Mat hsv, H;}\\
461 \texttt{cvtColor(image, hsv, CV\_BGR2HSV);}\\
462 \texttt{int planes[]=\{0, 1\}, hsize[] = \{32, 32\};}\\
463 \texttt{calcHist(\&hsv, 1, planes, Mat(), H, 2, hsize, 0);}\\
464 \end{tabbing}
465
466 \subsection{Contours}
467 See \texttt{\href{http://code.opencv.org/svn/opencv/trunk/opencv/samples/cpp/contours2.cpp}{contours2.cpp}} and \texttt{\href{http://code.opencv.org/svn/opencv/trunk/opencv/samples/cpp/squares.cpp}{squares.cpp}}
468 samples on what are the contours and how to use them.
469
470 \section{Data I/O}
471
472 \href{http://opencv.itseez.com/modules/core/doc/xml_yaml_persistence.html\#xml-yaml-file-storages-writing-to-a-file-storage}{XML/YAML storages} are collections (possibly nested) of scalar values, structures and heterogeneous lists.
473
474 \begin{tabbing}
475 \textbf{Wr}\=\textbf{iting data to YAML (or XML)}\\
476 \texttt{// Type of the file is determined from the extension}\\
477 \texttt{FileStorage fs("test.yml", FileStorage::WRITE);}\\
478 \texttt{fs << "i" << 5 << "r" << 3.1 << "str" << "ABCDEFGH";}\\
479 \texttt{fs << "mtx" << Mat::eye(3,3,CV\_32F);}\\
480 \texttt{fs << "mylist" << "[" << CV\_PI << "1+1" <<}\\
481 \>\texttt{"\{:" << "month" << 12 << "day" << 31 << "year"}\\
482 \>\texttt{<< 1969 << "\}" << "]";}\\
483 \texttt{fs << "mystruct" << "\{" << "x" << 1 << "y" << 2 <<}\\
484 \>\texttt{"width" << 100 << "height" << 200 << "lbp" << "[:";}\\
485 \texttt{const uchar arr[] = \{0, 1, 1, 0, 1, 1, 0, 1\};}\\
486 \texttt{fs.writeRaw("u", arr, (int)(sizeof(arr)/sizeof(arr[0])));}\\
487 \texttt{fs << "]" << "\}";}
488 \end{tabbing}
489
490 \emph{Scalars (integers, floating-point numbers, text strings), matrices, STL vectors of scalars and some other types can be written to the file storages using \texttt{<<} operator}
491
492 \begin{tabbing}
493 \textbf{Re}\=\textbf{ading the data back}\\
494 \texttt{// Type of the file is determined from the content}\\
495 \texttt{FileStorage fs("test.yml", FileStorage::READ);}\\
496 \texttt{int i1 = (int)fs["i"]; double r1 = (double)fs["r"];}\\
497 \texttt{string str1 = (string)fs["str"];}\\
498
499 \texttt{Mat M; fs["mtx"] >> M;}\\
500
501 \texttt{FileNode tl = fs["mylist"];}\\
502 \texttt{CV\_Assert(tl.type() == FileNode::SEQ \&\& tl.size() == 3);}\\
503 \texttt{double tl0 = (double)tl[0]; string tl1 = (string)tl[1];}\\
504
505 \texttt{int m = (int)tl[2]["month"], d = (int)tl[2]["day"];}\\
506 \texttt{int year = (int)tl[2]["year"];}\\
507
508 \texttt{FileNode tm = fs["mystruct"];}\\
509
510 \texttt{Rect r; r.x = (int)tm["x"], r.y = (int)tm["y"];}\\
511 \texttt{r.width = (int)tm["width"], r.height = (int)tm["height"];}\\
512  
513 \texttt{int lbp\_val = 0;}\\
514 \texttt{FileNodeIterator it = tm["lbp"].begin();}\\
515
516 \texttt{for(int k = 0; k < 8; k++, ++it)}\\
517 \>\texttt{lbp\_val |= ((int)*it) << k;}\\
518 \end{tabbing}
519
520 \emph{Scalars are read using the corresponding FileNode's cast operators. Matrices and some other types are read using \texttt{>>} operator. Lists can be read using FileNodeIterator's.}
521
522 \begin{tabbing}
523 \textbf{Wr}\=\textbf{iting and reading raster images}\\
524 \texttt{\href{http://opencv.itseez.com/modules/highgui/doc/reading_and_writing_images_and_video.html\#imwrite}{imwrite}("myimage.jpg", image);}\\
525 \texttt{Mat image\_color\_copy = \href{http://opencv.itseez.com/modules/highgui/doc/reading_and_writing_images_and_video.html\#imread}{imread}("myimage.jpg", 1);}\\
526 \texttt{Mat image\_grayscale\_copy = \href{http://opencv.itseez.com/modules/highgui/doc/reading_and_writing_images_and_video.html\#imread}{imread}("myimage.jpg", 0);}\\
527 \end{tabbing}
528
529 \emph{The functions can read/write images in the following formats: \textbf{BMP (.bmp), JPEG (.jpg, .jpeg), TIFF (.tif, .tiff), PNG (.png), PBM/PGM/PPM (.p?m), Sun Raster (.sr), JPEG 2000 (.jp2)}. Every format supports 8-bit, 1- or 3-channel images. Some formats (PNG, JPEG 2000) support 16 bits per channel.}
530
531 \begin{tabbing}
532 \textbf{Re}\=\textbf{ading video from a file or from a camera}\\
533 \texttt{VideoCapture cap;}\\
534 \texttt{if(argc > 1) cap.open(string(argv[1])); else cap.open(0)};\\
535 \texttt{Mat frame; namedWindow("video", 1);}\\
536 \texttt{for(;;) \{}\\
537 \>\texttt{cap >> frame; if(!frame.data) break;}\\
538 \>\texttt{imshow("video", frame); if(waitKey(30) >= 0) break;}\\
539 \texttt{\} }
540 \end{tabbing}
541
542 \section{Simple GUI (highgui module)}
543
544 \begin{tabular}{@{}p{\the\MyLen}%
545                 @{}p{\linewidth-\the\MyLen}@{}}
546
547 \texttt{\href{http://opencv.itseez.com/modules/highgui/doc/user_interface.html\#namedwindow}{namedWindow(winname,flags)}} & \ \ \ \ \ \ \ \ \ \ Create named highgui window \\
548
549 \texttt{\href{http://opencv.itseez.com/modules/highgui/doc/user_interface.html\#destroywindow}{destroyWindow(winname)}} & \ \ \ Destroy the specified window \\
550
551 \texttt{\href{http://opencv.itseez.com/modules/highgui/doc/user_interface.html\#imshow}{imshow(winname, mtx)}} & Show image in the window \\
552
553 \texttt{\href{http://opencv.itseez.com/modules/highgui/doc/user_interface.html\#waitkey}{waitKey(delay)}} & Wait for a key press during the specified time interval (or forever). Process events while waiting. \emph{Do not forget to call this function several times a second in your code.} \\
554
555 \texttt{\href{http://opencv.itseez.com/modules/highgui/doc/user_interface.html\#createtrackbar}{createTrackbar(...)}} & Add trackbar (slider) to the specified window \\
556
557 \texttt{\href{http://opencv.itseez.com/modules/highgui/doc/user_interface.html\#setmousecallback}{setMouseCallback(...)}} & \ \ Set the callback on mouse clicks and movements in the specified window \\
558
559 \end{tabular}
560
561 See \texttt{\href{http://code.opencv.org/svn/opencv/trunk/opencv/samples/cpp/camshiftdemo.cpp}{camshiftdemo.cpp}} and other \href{http://code.opencv.org/svn/opencv/trunk/opencv/samples/}{OpenCV samples} on how to use the GUI functions.   
562
563 \section{Camera Calibration, Pose Estimation and Depth Estimation}
564
565 \begin{tabular}{@{}p{\the\MyLen}%
566                 @{}p{\linewidth-\the\MyLen}@{}}
567
568 \texttt{\href{http://opencv.itseez.com/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html\#calibratecamera}{calibrateCamera()}} & Calibrate camera from several views of a calibration pattern. \\
569
570 \texttt{\href{http://opencv.itseez.com/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html\#findchessboardcorners}{findChessboardCorners()}} & \ \ \ \ \ \ Find feature points on the checkerboard calibration pattern. \\
571
572 \texttt{\href{http://opencv.itseez.com/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html\#solvepnp}{solvePnP()}} & Find the object pose from the known projections of its feature points. \\
573
574 \texttt{\href{http://opencv.itseez.com/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html\#stereocalibrate}{stereoCalibrate()}} & Calibrate stereo camera. \\
575
576 \texttt{\href{http://opencv.itseez.com/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html\#stereorectify}{stereoRectify()}} & Compute the rectification transforms for a calibrated stereo camera.\\
577
578 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/geometric_transformations.html\#initundistortrectifymap}{initUndistortRectifyMap()}} & \ \ \ \ \ \ Compute rectification map (for \texttt{remap()}) for each stereo camera head.\\
579
580 \texttt{\href{http://opencv.itseez.com/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html\#StereoBM}{StereoBM}}, \texttt{\href{http://opencv.itseez.com/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html\#StereoSGBM}{StereoSGBM}} & The stereo correspondence engines to be run on rectified stereo pairs.\\
581
582 \texttt{\href{http://opencv.itseez.com/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html\#reprojectimageto3d}{reprojectImageTo3D()}} & Convert disparity map to 3D point cloud.\\
583
584 \texttt{\href{http://opencv.itseez.com/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html\#findhomography}{findHomography()}} & Find best-fit perspective transformation between two 2D point sets. \\
585
586 \end{tabular}
587
588 To calibrate a camera, you can use \texttt{\href{http://code.opencv.org/svn/opencv/trunk/opencv/samples/cpp/calibration.cpp}{calibration.cpp}} or
589 \texttt{\href{http://code.opencv.org/svn/opencv/trunk/opencv/samples/cpp/stereo\_calib.cpp}{stereo\_calib.cpp}} samples.
590 To get the disparity maps and the point clouds, use
591 \texttt{\href{http://code.opencv.org/svn/opencv/trunk/opencv/samples/cpp/stereo\_match.cpp}{stereo\_match.cpp}} sample.
592
593 \section{Object Detection}
594
595 \begin{tabular}{@{}p{\the\MyLen}%
596                 @{}p{\linewidth-\the\MyLen}@{}}
597                 \texttt{\href{http://opencv.itseez.com/modules/imgproc/doc/object_detection.html\#matchtemplate}{matchTemplate}} & Compute proximity map for given template.\\
598
599 \texttt{\href{http://opencv.itseez.com/modules/objdetect/doc/cascade_classification.html\#cascadeclassifier}{CascadeClassifier}} & Viola's Cascade of Boosted classifiers using Haar or LBP features. Suits for detecting faces, facial features and some other objects without diverse textures. See \texttt{\href{http://code.opencv.org/svn/opencv/trunk/opencv/samples/c/facedetect.cpp}{facedetect.cpp}}\\
600
601 \texttt{{HOGDescriptor}} & N. Dalal's object detector using Histogram-of-Oriented-Gradients (HOG) features. Suits for detecting people, cars and other objects with well-defined silhouettes. See \texttt{\href{http://code.opencv.org/svn/opencv/trunk/opencv/samples/cpp/peopledetect.cpp}{peopledetect.cpp}}\\
602
603 \end{tabular}
604
605 %    
606 %    feature detection:
607 %        features2d toolbox
608 %    
609 %    object detection:
610 %        using a classifier running on a sliding window: cascadeclassifier + hog.
611 %        using salient point features: features2d -> matching
612 %        
613 %    statistical data processing:
614 %        clustering (k-means),
615 %        classification + regression (SVM, boosting, k-nearest),
616 %        compressing data (PCA)
617
618 \end{multicols}
619 \end{document}