Open Source Computer Vision Library
https://opencv.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
589 lines
32 KiB
589 lines
32 KiB
% |
|
% The OpenCV cheatsheet structure: |
|
% |
|
% creating matrices |
|
% from scratch |
|
% from previously allocated data: plain arrays, vectors |
|
% converting to/from old-style structures |
|
% |
|
% element access, iteration through matrix elements |
|
% |
|
% copying & shuffling data |
|
% copying & converting the whole matrices |
|
% extracting matrix parts & copying them |
|
% split, merge & mixchannels |
|
% flip, transpose, repeat |
|
% |
|
% matrix & image operations: |
|
% arithmetics & logic |
|
% matrix multiplication, inversion, determinant, trace, SVD |
|
% statistical functions |
|
% |
|
% basic image processing: |
|
% image filtering with predefined & custom filters |
|
% example: finding local maxima |
|
% geometrical transformations, resize, warpaffine, perspective & remap. |
|
% color space transformations |
|
% histograms & back projections |
|
% contours |
|
% |
|
% i/o: |
|
% displaying images |
|
% saving/loading to/from file (XML/YAML & image file formats) |
|
% reading videos & camera feed, writing videos |
|
% |
|
% operations on point sets: |
|
% findcontours, bounding box, convex hull, min area rect, |
|
% transformations, to/from homogeneous coordinates |
|
% matching point sets: homography, fundamental matrix, rigid transforms |
|
% |
|
% 3d: |
|
% camera calibration, pose estimation. |
|
% uncalibrated case |
|
% stereo: rectification, running stereo correspondence, obtaining the depth. |
|
% |
|
% feature detection: |
|
% features2d toolbox |
|
% |
|
% object detection: |
|
% using a classifier running on a sliding window: cascadeclassifier + hog. |
|
% using salient point features: features2d -> matching |
|
% |
|
% statistical data processing: |
|
% clustering (k-means), |
|
% classification + regression (SVM, boosting, k-nearest), |
|
% compressing data (PCA) |
|
% |
|
|
|
\documentclass[10pt,landscape]{article} |
|
\usepackage[usenames,dvips,pdftex]{color} |
|
\usepackage{multicol} |
|
\usepackage{calc} |
|
\usepackage{ifthen} |
|
\usepackage[pdftex]{color,graphicx} |
|
\usepackage[landscape]{geometry} |
|
\usepackage{hyperref} |
|
\hypersetup{colorlinks=true, filecolor=black, linkcolor=black, urlcolor=blue, citecolor=black} |
|
\graphicspath{{./images/}} |
|
|
|
|
|
% This sets page margins to .5 inch if using letter paper, and to 1cm |
|
% if using A4 paper. (This probably isn't strictly necessary.) |
|
% If using another size paper, use default 1cm margins. |
|
\ifthenelse{\lengthtest { \paperwidth = 11in}} |
|
{ \geometry{top=.5in,left=.5in,right=.5in,bottom=.5in} } |
|
{\ifthenelse{ \lengthtest{ \paperwidth = 297mm}} |
|
{\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} } |
|
{\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} } |
|
} |
|
|
|
% Turn off header and footer |
|
\pagestyle{empty} |
|
|
|
% Redefine section commands to use less space |
|
\makeatletter |
|
\renewcommand{\section}{\@startsection{section}{1}{0mm}% |
|
{-1ex plus -.5ex minus -.2ex}% |
|
{0.5ex plus .2ex}%x |
|
{\normalfont\large\bfseries}} |
|
\renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}% |
|
{-1explus -.5ex minus -.2ex}% |
|
{0.5ex plus .2ex}% |
|
{\normalfont\normalsize\bfseries}} |
|
\renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}% |
|
{-1ex plus -.5ex minus -.2ex}% |
|
{1ex plus .2ex}% |
|
{\normalfont\small\bfseries}} |
|
\makeatother |
|
|
|
% Define BibTeX command |
|
\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em |
|
T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}} |
|
|
|
% Don't print section numbers |
|
\setcounter{secnumdepth}{0} |
|
|
|
|
|
%\setlength{\parindent}{0pt} |
|
%\setlength{\parskip}{0pt plus 0.5ex} |
|
|
|
\newcommand{\ccode}[1]{ |
|
\begin{alltt} |
|
#1 |
|
\end{alltt} |
|
} |
|
|
|
% ----------------------------------------------------------------------- |
|
|
|
\begin{document} |
|
|
|
\raggedright |
|
\footnotesize |
|
\begin{multicols}{3} |
|
|
|
|
|
% multicol parameters |
|
% These lengths are set only within the two main columns |
|
%\setlength{\columnseprule}{0.25pt} |
|
\setlength{\premulticols}{1pt} |
|
\setlength{\postmulticols}{1pt} |
|
\setlength{\multicolsep}{1pt} |
|
\setlength{\columnsep}{2pt} |
|
|
|
\begin{center} |
|
\Large{\textbf{OpenCV 2.1+ Cheat Sheet}} \\ |
|
\end{center} |
|
\newlength{\MyLen} |
|
\settowidth{\MyLen}{\texttt{letterpaper}/\texttt{a4paper} \ } |
|
|
|
%\section{Filesystem Concepts} |
|
%\begin{tabular}{@{}p{\the\MyLen}% |
|
% @{}p{\linewidth-\the\MyLen}@{}} |
|
%\texttt{\href{http://www.ros.org/wiki/Packages}{package}} & The lowest level of ROS software organization. \\ |
|
%\texttt{\href{http://www.ros.org/wiki/Manifest}{manifest}} & Description of a ROS package. \\ |
|
%\texttt{\href{http://www.ros.org/wiki/Stack}{stack}} & Collections of ROS packages that form a higher-level library. \\ |
|
%\texttt{\href{http://www.ros.org/wiki/Stack Manifest}{stack manifest}} & Description of a ROS stack. |
|
%\end{tabular} |
|
|
|
\section{Matrix Basics} |
|
\begin{tabbing} |
|
|
|
\textbf{Cr}\=\textbf{ea}\=\textbf{te}\={} \textbf{a matrix} \\ |
|
\> \texttt{Mat image(240, 320, CV\_8UC3);} \\ |
|
|
|
\textbf{[Re]allocate a pre-declared matrix}\\ |
|
\> \texttt{image.\href{http://opencv.willowgarage.com/documentation/cpp/basic_structures.html\#Mat::create}{create}(480, 640, CV\_8UC3);}\\ |
|
|
|
\textbf{Create a matrix initialized with a constant}\\ |
|
\> \texttt{Mat A33(3, 3, CV\_32F, Scalar(5));} \\ |
|
\> \texttt{Mat B33(3, 3, CV\_32F); B33 = Scalar(5);} \\ |
|
\> \texttt{Mat C33 = Mat::ones(3, 3, CV\_32F)*5.;} \\ |
|
\> \texttt{Mat D33 = Mat::zeros(3, 3, CV\_32F) + 5.;} \\ |
|
|
|
\textbf{Create a matrix initialized with specified values}\\ |
|
\> \texttt{double a = CV\_PI/3;} \\ |
|
\> \texttt{Mat A22 = Mat(Mat\_<float>(2, 2) <<} \\ |
|
\> \> \texttt{cos(a), -sin(a), sin(a), cos(a));} \\ |
|
\> \texttt{float B22data[] = \{cos(a), -sin(a), sin(a), cos(a)\};} \\ |
|
\> \texttt{Mat B22 = Mat(2, 2, CV\_32F, B22data).clone();}\\ |
|
|
|
\textbf{Initialize a random matrix}\\ |
|
\> \texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-randu}{randu}(image, Scalar(0), Scalar(256)); }\textit{// uniform dist}\\ |
|
\> \texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-randn}{randn}(image, Scalar(128), Scalar(10)); }\textit{// Gaussian dist}\\ |
|
|
|
\textbf{Convert matrix to/from other structures}\\ |
|
\>(without copying the data)\\ |
|
\> \texttt{Mat image\_alias = image;}\\ |
|
\> \texttt{float* Idata=new float[480*640*3];}\\ |
|
\> \texttt{Mat I(480, 640, CV\_32FC3, Idata);}\\ |
|
\> \texttt{vector<Point> iptvec(10);}\\ |
|
\> \texttt{Mat iP(iptvec); }\textit{// iP -- 10x1 CV\_32SC2 matrix}\\ |
|
\> \texttt{CvMat* oldC0 = cvCreateImage(cvSize(320, 240), 16);}\\ |
|
\> \texttt{Mat newC = cvarrToMat(oldC0);}\\ |
|
\> \texttt{IplImage oldC1 = newC; CvMat oldC2 = newC;}\\ |
|
|
|
\textbf{... (with copying the data)}\\ |
|
\> \texttt{Mat image\_copy = image.clone();}\\ |
|
\> \texttt{Mat P(10, 1, CV\_32FC2, Scalar(1, 1));}\\ |
|
\> \texttt{vector<Point2f> ptvec = Mat\_<Point2f>(P);}\\ |
|
|
|
\>\\ |
|
\textbf{Access matrix elements}\\ |
|
\> \texttt{A33.at<float>(i,j) = A33.at<float>(j,i)+1;}\\ |
|
\> \texttt{Mat dyImage(image.size(), image.type());}\\ |
|
\> \texttt{for(int y = 1; y < image.rows-1; y++) \{}\\ |
|
\> \> \texttt{Vec3b* prevRow = image.ptr<Vec3b>(y-1);}\\ |
|
\> \> \texttt{Vec3b* nextRow = image.ptr<Vec3b>(y+1);}\\ |
|
\> \> \texttt{for(int x = 0; y < image.cols; x++)}\\ |
|
\> \> \> \texttt{for(int c = 0; c < 3; c++)}\\ |
|
\> \> \> \texttt{ dyImage.at<Vec3b>(y,x)[c] =}\\ |
|
\> \> \> \texttt{ saturate\_cast<uchar>(}\\ |
|
\> \> \> \texttt{ nextRow[x][c] - prevRow[x][c]);}\\ |
|
\> \texttt{\} }\\ |
|
\> \texttt{Mat\_<Vec3b>::iterator it = image.begin<Vec3b>(),}\\ |
|
\> \> \texttt{itEnd = image.end<Vec3b>();}\\ |
|
\> \texttt{for(; it != itEnd; ++it)}\\ |
|
\> \> \texttt{(*it)[1] \textasciicircum{}= 255;}\\ |
|
|
|
\end{tabbing} |
|
|
|
\section{Matrix Manipulations: Copying, Shuffling, Part Access} |
|
\begin{tabular}{@{}p{\the\MyLen}% |
|
@{}p{\linewidth-\the\MyLen}@{}} |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/basic_structures.html\#Mat::copyTo}{Mat::copyTo()}} & Copy matrix to another one \\ |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/basic_structures.html\#Mat::convertTo}{Mat::convertTo()}} & Scale and convert matrix to the specified data type \\ |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/basic_structures.html\#Mat::clone}{Mat::clone()}} & Make deep copy of a matrix \\ |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/basic_structures.html\#Mat::reshape}{Mat::reshape()}} & Change matrix dimensions and/or number of channels without copying data \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/basic_structures.html\#Mat::row}{Mat::row()}}, \texttt{\href{http://opencv.willowgarage.com/documentation/cpp/basic_structures.html\#Mat::rowRange}{Mat::rowRange()}} & Take a matrix row (row span) \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/basic_structures.html\#Mat::col}{Mat::col()}}, \texttt{\href{http://opencv.willowgarage.com/documentation/cpp/basic_structures.html\#Mat::colRange}{Mat::colRange()}} & Take a matrix column (column span) \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/basic_structures.html\#Mat::diag}{Mat::diag()}} & Take a matrix diagonal/create a diagonal matrix \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/basic_structures.html\#index-1245}{Mat::operator ()()}} & Take a submatrix \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/basic_structures.html\#Mat::repeat}{Mat::repeat()}} & Make a bigger matrix by repeating a smaller one \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-flip}{flip()}} & Reverse the order of matrix rows and/or columns \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-split}{split()}} & Split multi-channel matrix into separate channels \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-merge}{merge()}} & Make a multi-channel matrix out of the separate channels \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-mixchannels}{mixChannels()}} & Generalized form of split() and merge() \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-randshuffle}{randShuffle()}} & Randomly shuffle matrix elements \\ |
|
|
|
\end{tabular} |
|
|
|
|
|
\section{Simple Matrix Operations} |
|
|
|
OpenCV implements most common arithmetical, logical and |
|
other matrix operations, such as |
|
|
|
\begin{itemize} |
|
\item |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-add}{add()}}, \texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-subtract}{subtract()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-multiply}{multiply()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-divide}{divide()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-absdiff}{absdiff()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#bitwise-and}{bitwise\_and()}}, \texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#bitwise-or}{bitwise\_or()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#bitwise-xor}{bitwise\_xor()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-max}{max()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-min}{min()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-compare}{compare()}} |
|
|
|
-- correspondingly, addition, subtraction, element-wise multiplication ... comparison of two matrices or a matrix and a scalar. |
|
|
|
% (a, a, a, 255)*(r, g, b, a)/255 |
|
% 255 - (a, a, a, 255) = (255 - a, ..., 0) |
|
% (b, b, b, b)*(255 - a, 255 - a, 255 - a, 0)/255 = ((255 - a)*b/255, ...., (255 - a)) |
|
|
|
\begin{tabbing} |
|
Exa\=mple. \href{http://en.wikipedia.org/wiki/Alpha_compositing}{Alpha compositing} function:\\ |
|
\texttt{void alphaCompose(const Mat\& rgba1,}\\ |
|
\> \texttt{const Mat\& rgba2, Mat\& rgba\_dest)}\\ |
|
\texttt{\{ }\\ |
|
\> \texttt{Mat a1(rgba1.size(), rgba1.type), ra1;}\\ |
|
\> \texttt{Mat a2(rgba2.size(), rgba2.type);}\\ |
|
\> \texttt{int mixch[]=\{3, 0, 3, 1, 3, 2, 3, 3\};}\\ |
|
\> \texttt{mixChannels(\&rgba1, \&a1, mixch, 4);}\\ |
|
\> \texttt{mixChannels(\&rgba2, \&a2, mixch, 4);}\\ |
|
\> \texttt{subtract(Scalar::all(255), a1, ra1);}\\ |
|
\> \texttt{bitwise\_or(a1, Scalar(0,0,0,255), a1);}\\ |
|
\> \texttt{bitwise\_or(a2, Scalar(0,0,0,255), a2);}\\ |
|
\> \texttt{multiply(a2, ra1, a2, 1./255);}\\ |
|
\> \texttt{multiply(a1, rgba1, a1, 1./255);}\\ |
|
\> \texttt{multiply(a2, rgba2, a2, 1./255);}\\ |
|
\> \texttt{add(a1, a2, rgba\_dest);}\\ |
|
\texttt{\}} |
|
\end{tabbing} |
|
|
|
\item |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-sum}{sum()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-mean}{mean()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-mean-stddev}{meanStdDev()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-norm}{norm()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-countnonzero}{countNonZero()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-minmaxloc}{minMaxLoc()}}, |
|
|
|
-- various statistics of matrix elements. |
|
|
|
\item |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-exp}{exp()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-log}{log()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-pow}{pow()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-sqrt}{sqrt()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-carttopolar}{cartToPolar()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-polarToCart}{polarToCart()}} |
|
|
|
-- the classical math functions + conversion of Cartesian to polar coordinates and back. |
|
|
|
\item |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-scaleadd}{scaleAdd()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-transpose}{transpose()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-gemm}{gemm()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-invert}{invert()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-solve}{solve()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-determinant}{determinant()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-trace}{trace()}} |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-eigen}{eigen()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-SVD}{SVD}}, |
|
|
|
-- the algebraic functions + SVD class. |
|
|
|
\item |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-dft}{dft()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-idft}{idft()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-dct}{dct()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html\#cv-idct}{idct()}}, |
|
|
|
-- discrete Fourier and cosine transformations |
|
|
|
\end{itemize} |
|
|
|
For many of the basic operations the alternative algebraic notation can be used, for example: |
|
\begin{tabbing} |
|
\texttt{Mat}\={} \texttt{delta = (J.t()*J + lambda*}\\ |
|
\>\texttt{Mat::eye(J.cols, J.cols, J.type())}\\ |
|
\>\texttt{.inv(CV\_SVD)*(J.t()*err);} |
|
\end{tabbing} |
|
implements the core of Levenberg-Marquardt optimization algorithm. |
|
Please, see the \href{http://opencv.willowgarage.com/documentation/cpp/basic_structures.html#matrix-expressions}{Matrix Expressions Reference} for details. |
|
|
|
|
|
\section{Image Processsing} |
|
|
|
\subsection{Filtering} |
|
|
|
\begin{tabular}{@{}p{\the\MyLen}% |
|
@{}p{\linewidth-\the\MyLen}@{}} |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/image_filtering.html\#cv-filter2d}{filter2D()}} & Apply a non-separable linear filter to the image \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/image_filtering.html\#cv-sepfilter2d}{sepFilter2D()}} & Apply a separable linear filter to the image \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/image_filtering.html\#cv-blur}{boxFilter()}}, \texttt{\href{http://opencv.willowgarage.com/documentation/cpp/image_filtering.html\#cv-gaussianblur}{GaussianBlur()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/image_filtering.html\#cv-medianblur}{medianBlur()}} |
|
& Smooth the image with one of the linear or non-linear filters \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/image_filtering.html\#cv-sobel}{Sobel()}}, \texttt{\href{http://opencv.willowgarage.com/documentation/cpp/image_filtering.html\#cv-scharr}{Scharr()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/image_filtering.html\#cv-laplacian}{Laplacian()}} |
|
& Compute the first, second, third or mixed spatial image derivatives. \texttt{Laplacian()} computes $\Delta I = \frac{\partial ^ 2 I}{\partial x^2} + \frac{\partial ^ 2 I}{\partial y^2}$ \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/image_filtering.html\#cv-erode}{erode()}}, \texttt{\href{http://opencv.willowgarage.com/documentation/cpp/image_filtering.html\#cv-dilate}{dilate()}} & Erode or dilate the image \\ |
|
|
|
\end{tabular} |
|
|
|
\begin{tabbing} |
|
Exa\=mple. Filter image in-place with a 3x3 high-pass filter\\ |
|
\> (preserve negative responses by shifting the result by 128):\\ |
|
\texttt{filter2D(image, image, image.depth(), Mat(Mat\_<float>(3,3)}\\ |
|
\> \texttt{ << -1, -1, -1, -1, 9, -1, -1, -1, -1), Point(1,1), 128);}\\ |
|
\end{tabbing} |
|
|
|
\subsection{Geometrical Transformations} |
|
|
|
\begin{tabular}{@{}p{\the\MyLen}% |
|
@{}p{\linewidth-\the\MyLen}@{}} |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/geometric_image_transformations.html\#cv-resize}{resize()}} & Resize image \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/geometric_image_transformations.html\#cv-getrectsubpix}{getRectSubPix()}} & Extract an image patch with bilinear interpolation \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/geometric_image_transformations.html\#cv-warpaffine}{warpAffine()}} & Warp image using an affine transformation (rotation, scaling, shearing, reflection)\\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/geometric_image_transformations.html\#cv-warpperspective}{warpPerspective()}} & Warp image using a perspective transformation\\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/geometric_image_transformations.html\#cv-remap}{remap()}} & Generic image warping using the pre-computed maps\\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/geometric_image_transformations.html\#cv-convertmaps}{convertMaps()}} & Optimize maps for a faster remap() execution\\ |
|
|
|
\end{tabular} |
|
|
|
\begin{tabbing} |
|
Example. Decimate image by factor of $\sqrt{2}$:\\ |
|
\texttt{Mat dst; resize(src, dst, Size(), 1./sqrt(2), 1./sqrt(2));} |
|
\end{tabbing} |
|
|
|
\subsection{Various Image Transformations} |
|
|
|
\begin{tabular}{@{}p{\the\MyLen}% |
|
@{}p{\linewidth-\the\MyLen}@{}} |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/miscellaneous_image_transformations.html\#cvtColor}{cvtColor()}} & Convert image from one color space to another \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/miscellaneous_image_transformations.html\#threshold}{threshold()}}, \texttt{\href{http://opencv.willowgarage.com/documentation/cpp/miscellaneous_image_transformations.html\#adaptivethreshold}{adaptivethreshold()}} & Convert grayscale image to binary image using a fixed or a variable (location-dependent) threshold \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/miscellaneous_image_transformations.html\#floodfill}{floodFill()}} & Find a connected component starting from the specified seed point by region growing technique \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/miscellaneous_image_transformations.html\#floodfill}{integral()}} & Compute integral image, used further for to compute cumulative characteristics over rectangular regions in O(1) time \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/miscellaneous_image_transformations.html\#distancetransform}{distanceTransform()}}, |
|
& build a distance map or a discrete Voronoi diagram from binary image. \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/miscellaneous_image_transformations.html\#floodfill}{watershed()}}, |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/miscellaneous_image_transformations.html\#grabcut}{grabCut()}} |
|
& marker-based image segmentation algorithms. See |
|
See \texttt{\href{https://code.ros.org/svn/opencv/trunk/opencv/samples/c/watershed.cpp}{watershed.cpp}} and \texttt{\href{https://code.ros.org/svn/opencv/trunk/opencv/samples/c/grabcut.c}{grabcut.cpp}} |
|
samples. |
|
|
|
\end{tabular} |
|
|
|
\subsection{Histograms} |
|
|
|
\begin{tabular}{@{}p{\the\MyLen}% |
|
@{}p{\linewidth-\the\MyLen}@{}} |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/histograms.html\#calchist}{calcHist()}} & Compute a histogram from one or more images \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/histograms.html\#calcbackproject}{calcBackProject()}} & Compute histogram back-projection (the posterior probability map) for the images \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/histograms.html\#equalizehist}{equalizeHist()}} & Normalize image brightness and contrast by equalizing the image histogram\\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/histograms.html\#comparehist}{compareHist()}} & Compare two histograms\\ |
|
|
|
\end{tabular} |
|
|
|
\begin{tabbing} |
|
Example. Compute Hue-Saturation histogram of an image:\\ |
|
\texttt{Mat hsv, H; MatND tempH;}\\ |
|
\texttt{cvtColor(image, hsv, CV\_BGR2HSV);}\\ |
|
\texttt{int planes[]=\{0, 1\}, hsize[] = \{32, 32\};}\\ |
|
\texttt{calcHist(\&hsv, 1, planes, Mat(), tempH, 2, hsize, 0);}\\ |
|
\texttt{H = tempH;} |
|
\end{tabbing} |
|
|
|
\subsection{Contours} |
|
See \texttt{\href{https://code.ros.org/svn/opencv/trunk/opencv/samples/cpp/contours.cpp}{contours.cpp}} and \texttt{\href{https://code.ros.org/svn/opencv/trunk/opencv/samples/c/squares.c}{squares.c}} |
|
samples on what are the contours and how to use them. |
|
|
|
\section{Data I/O} |
|
|
|
XML/YAML storages are collections (possibly nested) of scalar values, structures and heterogeneous lists. |
|
|
|
\begin{tabbing} |
|
\textbf{Wr}\=\textbf{iting data to YAML (or XML)}\\ |
|
\texttt{// Type of the file is determined from the extension}\\ |
|
\texttt{FileStorage fs("test.yml", FileStorage::WRITE);}\\ |
|
\texttt{fs << "i" << 5 << "r" << 3.1 << "str" << "ABCDEFGH";}\\ |
|
\texttt{fs << "mtx" << Mat::eye(3,3,CV\_32F);}\\ |
|
\texttt{fs << "mylist" << "[" << CV\_PI << "1+1" <<}\\ |
|
\>\texttt{"\{:" << "month" << 12 << "day" << 31 << "year"}\\ |
|
\>\texttt{<< 1969 << "\}" << "]";}\\ |
|
\texttt{fs << "mystruct" << "\{" << "x" << 1 << "y" << 2 <<}\\ |
|
\>\texttt{"width" << 100 << "height" << 200 << "lbp" << "[:";}\\ |
|
\texttt{const uchar arr[] = \{0, 1, 1, 0, 1, 1, 0, 1\};}\\ |
|
\texttt{fs.writeRaw("u", arr, (int)(sizeof(arr)/sizeof(arr[0])));}\\ |
|
\texttt{fs << "]" << "\}";} |
|
\end{tabbing} |
|
|
|
\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} |
|
|
|
\begin{tabbing} |
|
\textbf{Re}\=\textbf{ading the data back}\\ |
|
\texttt{// Type of the file is determined from the content}\\ |
|
\texttt{FileStorage fs("test.yml", FileStorage::READ);}\\ |
|
\texttt{int i1 = (int)fs["i"]; double r1 = (double)fs["r"];}\\ |
|
\texttt{string str1 = (string)fs["str"];}\\ |
|
|
|
\texttt{Mat M; fs["mtx"] >> M;}\\ |
|
|
|
\texttt{FileNode tl = fs["mylist"];}\\ |
|
\texttt{CV\_Assert(tl.type() == FileNode::SEQ \&\& tl.size() == 3);}\\ |
|
\texttt{double tl0 = (double)tl[0]; string tl1 = (string)tl[1];}\\ |
|
|
|
\texttt{int m = (int)tl[2]["month"], d = (int)tl[2]["day"]};\\ |
|
\texttt{int year = (int)tl[2]["year"];}\\ |
|
|
|
\texttt{FileNode tm = fs["mystruct"];}\\ |
|
|
|
\texttt{Rect r; r.x = (int)tm["x"], r.y = (int)tm["y"];}\\ |
|
\texttt{r.width = (int)tm["width"], r.height = (int)tm["height"];}\\ |
|
|
|
\texttt{int lbp\_val = 0;}\\ |
|
\texttt{FileNodeIterator it = tm["lbp"].begin();}\\ |
|
|
|
\texttt{for(int k = 0; k < 8; k++, ++it)}\\ |
|
\>\texttt{lbp\_val |= ((int)*it) << k;}\\ |
|
\end{tabbing} |
|
|
|
\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.} |
|
|
|
\begin{tabbing} |
|
\textbf{Wr}\=\textbf{iting and reading raster images}\\ |
|
\texttt{imwrite("myimage.jpg", image);}\\ |
|
\texttt{Mat image\_color\_copy = imread("myimage.jpg", 1);}\\ |
|
\texttt{Mat image\_grayscale\_copy = imread("myimage.jpg", 0);}\\ |
|
\end{tabbing} |
|
|
|
\emph{The following formats are supported: \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.} |
|
|
|
\begin{tabbing} |
|
\textbf{Re}\=\textbf{ading video from a file or from a camera}\\ |
|
\texttt{VideoCapture cap;}\\ |
|
\texttt{if(argc > 1) cap.open(string(argv[1])); else cap.open(0)};\\ |
|
\texttt{Mat frame; namedWindow("video", 1);}\\ |
|
\texttt{for(;;) \{}\\ |
|
\>\texttt{cap >> frame; if(!frame.data) break;}\\ |
|
\>\texttt{imshow("video", frame); if(waitKey(30) >= 0) break;}\\ |
|
\texttt{\} } |
|
\end{tabbing} |
|
|
|
\section{Simple GUI (highgui module)} |
|
|
|
\begin{tabular}{@{}p{\the\MyLen}% |
|
@{}p{\linewidth-\the\MyLen}@{}} |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/user_interface.html\#cv-namedwindow}{namedWindow()}} & Create window with the specified name \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/user_interface.html\#cv-destroywindow}{destroyWindow()}} & Destroy the specified window \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/user_interface.html\#cv-imshow}{imshow()}} & Show image in the window \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/user_interface.html\#cv-waitKey}{waitKey()}} & 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.} \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/user_interface.html\#cv-createTrackbar}{createTrackbar()}} & Add trackbar (slider) to the specified window \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/user_interface.html\#cv-setmousecallback}{setMouseCallback()}} & Set the callback on mouse clicks and movements in the specified window \\ |
|
|
|
\end{tabular} |
|
|
|
See \texttt{\href{https://code.ros.org/svn/opencv/trunk/opencv/samples/c/camshiftdemo.c}{camshiftdemo.c}} and other \href{https://code.ros.org/svn/opencv/trunk/opencv/samples/}{OpenCV samples} on how to use the GUI functions. |
|
|
|
\section{Camera Calibration, Pose Estimation and Depth Estimation} |
|
|
|
\begin{tabular}{@{}p{\the\MyLen}% |
|
@{}p{\linewidth-\the\MyLen}@{}} |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/camera_calibration_and_3d_reconstruction.html\#cv-calibratecamera}{calibrateCamera()}} & Calibrate monocular camera from multiple known projections of a calibration pattern feature points collected from several views. \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/camera_calibration_and_3d_reconstruction.html\#cv-findchessboardcorners}{findChessboardCorners()}} & \ \ \ \ \ \ Find feature points on the checkerboard calibration pattern with known geometry. \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/camera_calibration_and_3d_reconstruction.html\#cv-solvepnp}{solvePnP()}} & Find the object pose from the known projections of its feature points. \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/camera_calibration_and_3d_reconstruction.html\#cv-stereocalibrate}{stereoCalibrate()}} & Calibrate stereo camera using several stereo views of a calibration pattern. \\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/camera_calibration_and_3d_reconstruction.html\#cv-stereorectify}{stereoRectify()}} & Compute the rectification transforms for a stereo camera and the visible area on the rectified images. Camera must be calibrated first using stereoCalibrate().\\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/camera_calibration_and_3d_reconstruction.html\#cv-initundistortrectifymap}{initUndistortRectifyMap()}} & \ \ \ \ \ \ Compute rectification map (for \texttt{remap()}) for each head of a stereo camera. Must be called twice, for each head, after \texttt{stereoRectify()}.\\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/camera_calibration_and_3d_reconstruction.html\#cv-StereoBM}{StereoBM}}, \texttt{\href{http://opencv.willowgarage.com/documentation/cpp/camera_calibration_and_3d_reconstruction.html\#cv-StereoSGBM}{StereoSGBM}} & The two primary stereo correspondence algorithms in OpenCV. They work on the rectified images.\\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/camera_calibration_and_3d_reconstruction.html\#cv-reprojectimageto3d}{reprojectImageTo3D()}} & Convert the disparity map, computed by \texttt{StereoBM::operator ()} or \texttt{StereoSGBM::operator ()} to the 3D point cloud.\\ |
|
|
|
\end{tabular} |
|
|
|
To calibrate a camera, you can use \texttt{\href{https://code.ros.org/svn/opencv/trunk/opencv/samples/c/calibration.cpp}{calibration.cpp}} or |
|
\texttt{\href{https://code.ros.org/svn/opencv/trunk/opencv/samples/c/stereo\_calib.cpp}{stereo\_calib.cpp}} samples. |
|
To run stereo correspondence and optionally get the point clouds, you can use |
|
\texttt{\href{https://code.ros.org/svn/opencv/trunk/opencv/samples/c/stereo\_match.cpp}{stereo\_match.cpp}} sample. |
|
|
|
\section{Object Detection} |
|
|
|
\begin{tabular}{@{}p{\the\MyLen}% |
|
@{}p{\linewidth-\the\MyLen}@{}} |
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/object_detection.html\#matchTemplate}{matchTemplate}} & Primitive Viola's Cascade of Boosted classifiers using Haar or LBP features. Detects objects by sliding a window and running the cascade on them. Suits for detecting faces, facial features and some other objects without diverse textures.\\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/object_detection.html\#CascadeClassifier}{CascadeClassifier}} & Viola's Cascade of Boosted classifiers using Haar or LBP features. Detects objects by sliding a window and running the cascade on them. Suits for detecting faces, facial features and some other objects without diverse textures. See \texttt{\href{https://code.ros.org/svn/opencv/trunk/opencv/samples/c/facedetect.cpp}{facedetect.cpp}}\\ |
|
|
|
\texttt{\href{http://opencv.willowgarage.com/documentation/cpp/object_detection.html\#HOGDescriptor}{HOGDescriptor}} & N. Dalal's object detector using Histogram-of-Oriented-Gradients (HOG) features. Detects objects by sliding a window and running SVM classifier on them. Suits for detecting people, cars and other objects with well-defined silhouettes. See \texttt{\href{https://code.ros.org/svn/opencv/trunk/opencv/samples/c/peopledetect.cpp}{peopledetect.cpp}}\\ |
|
|
|
\end{tabular} |
|
|
|
% |
|
% feature detection: |
|
% features2d toolbox |
|
% |
|
% object detection: |
|
% using a classifier running on a sliding window: cascadeclassifier + hog. |
|
% using salient point features: features2d -> matching |
|
% |
|
% statistical data processing: |
|
% clustering (k-means), |
|
% classification + regression (SVM, boosting, k-nearest), |
|
% compressing data (PCA) |
|
|
|
\end{multicols} |
|
\end{document}
|
|
|