]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/doc/cv_feature_detection.tex
updated StereoBM description, added StereoSGBM description, partly fixed the table...
[opencv.git] / opencv / doc / cv_feature_detection.tex
1 \section{Feature Detection}
2
3 \ifCPy
4
5 \cvCPyFunc{Canny}
6 Implements the Canny algorithm for edge detection.
7
8 \cvdefC{
9 void cvCanny(\par const CvArr* image,
10 \par CvArr* edges,
11 \par double threshold1,
12 \par double threshold2,
13 \par int aperture\_size=3 );
14 }\cvdefPy{Canny(image,edges,threshold1,threshold2,aperture\_size=3)-> None}
15 \begin{description}
16 \cvarg{image}{Single-channel input image}
17 \cvarg{edges}{Single-channel image to store the edges found by the function}
18 \cvarg{threshold1}{The first threshold}
19 \cvarg{threshold2}{The second threshold}
20 \cvarg{aperture\_size}{Aperture parameter for the Sobel operator (see \cvCPyCross{Sobel})}
21 \end{description}
22
23 The function finds the edges on the input image \texttt{image} and marks them in the output image \texttt{edges} using the Canny algorithm. The smallest value between \texttt{threshold1} and \texttt{threshold2} is used for edge linking, the largest value is used to find the initial segments of strong edges.
24
25 \cvCPyFunc{CornerEigenValsAndVecs}
26 Calculates eigenvalues and eigenvectors of image blocks for corner detection.
27
28 \cvdefC{
29 void cvCornerEigenValsAndVecs( \par const CvArr* image,\par CvArr* eigenvv,\par int blockSize,\par int aperture\_size=3 );
30
31 }\cvdefPy{CornerEigenValsAndVecs(image,eigenvv,blockSize,aperture\_size=3)-> None}
32
33 \begin{description}
34 \cvarg{image}{Input image}
35 \cvarg{eigenvv}{Image to store the results. It must be 6 times wider than the input image}
36 \cvarg{blockSize}{Neighborhood size (see discussion)}
37 \cvarg{aperture\_size}{Aperture parameter for the Sobel operator (see \cvCPyCross{Sobel})}
38 \end{description}
39
40 For every pixel, the function \texttt{cvCornerEigenValsAndVecs} considers a $\texttt{blockSize} \times \texttt{blockSize}$ neigborhood S(p). It calcualtes the covariation matrix of derivatives over the neigborhood as:
41
42 \[
43 M = \begin{bmatrix}
44 \sum_{S(p)}(dI/dx)^2 & \sum_{S(p)}(dI/dx \cdot dI/dy)^2 \\
45 \sum_{S(p)}(dI/dx \cdot dI/dy)^2 & \sum_{S(p)}(dI/dy)^2
46 \end{bmatrix}
47 \]
48
49 After that it finds eigenvectors and eigenvalues of the matrix and stores them into destination image in form
50 $(\lambda_1, \lambda_2, x_1, y_1, x_2, y_2)$ where
51 \begin{description}
52 \item[$\lambda_1, \lambda_2$]are the eigenvalues of $M$; not sorted
53 \item[$x_1, y_1$]are the eigenvectors corresponding to $\lambda_1$
54 \item[$x_2, y_2$]are the eigenvectors corresponding to $\lambda_2$
55 \end{description}
56
57 \cvCPyFunc{CornerHarris}
58 Harris edge detector.
59
60 \cvdefC{
61 void cvCornerHarris(
62 \par const CvArr* image,
63 \par CvArr* harris\_dst,
64 \par int blockSize,
65 \par int aperture\_size=3,
66 \par double k=0.04 );
67 }
68 \cvdefPy{CornerHarris(image,harris\_dst,blockSize,aperture\_size=3,k=0.04)-> None}
69
70 \begin{description}
71 \cvarg{image}{Input image}
72 \cvarg{harris\_dst}{Image to store the Harris detector responses. Should have the same size as \texttt{image}}
73 \cvarg{blockSize}{Neighborhood size (see the discussion of \cvCPyCross{CornerEigenValsAndVecs})}
74 \cvarg{aperture\_size}{Aperture parameter for the Sobel operator (see \cvCPyCross{Sobel}).}
75 % format. In the case of floating-point input format this parameter is the number of the fixed float filter used for differencing
76 \cvarg{k}{Harris detector free parameter. See the formula below}
77 \end{description}
78
79 The function runs the Harris edge detector on the image. Similarly to \cvCPyCross{CornerMinEigenVal} and \cvCPyCross{CornerEigenValsAndVecs}, for each pixel it calculates a $2\times2$ gradient covariation matrix $M$ over a $\texttt{blockSize} \times \texttt{blockSize}$ neighborhood. Then, it stores
80
81 \[
82 det(M) - k \, trace(M)^2
83 \]
84
85 to the destination image. Corners in the image can be found as the local maxima of the destination image.
86
87 \cvCPyFunc{CornerMinEigenVal}
88 Calculates the minimal eigenvalue of gradient matrices for corner detection.
89
90 \cvdefC{
91 void cvCornerMinEigenVal(
92 \par const CvArr* image,
93 \par CvArr* eigenval,
94 \par int blockSize,
95 \par int aperture\_size=3 );
96 }\cvdefPy{CornerMinEigenVal(image,eigenval,blockSize,aperture\_size=3)-> None}
97 \begin{description}
98 \cvarg{image}{Input image}
99 \cvarg{eigenval}{Image to store the minimal eigenvalues. Should have the same size as \texttt{image}}
100 \cvarg{blockSize}{Neighborhood size (see the discussion of \cvCPyCross{CornerEigenValsAndVecs})}
101 \cvarg{aperture\_size}{Aperture parameter for the Sobel operator (see \cvCPyCross{Sobel}).}
102 %  format. In the case of floating-point input format this parameter is the number of the fixed float filter used for differencing
103 \end{description}
104
105 The function is similar to \cvCPyCross{CornerEigenValsAndVecs} but it calculates and stores only the minimal eigen value of derivative covariation matrix for every pixel, i.e. $min(\lambda_1, \lambda_2)$ in terms of the previous function.
106
107 \ifPy
108 \cvclass{CvSURFPoint}
109 A SURF keypoint, represented as a tuple \texttt{((x, y), laplacian, size, dir, hessian)}.
110
111 \begin{description}
112 \cvarg{x}{x-coordinate of the feature within the image}
113 \cvarg{y}{y-coordinate of the feature within the image}
114 \cvarg{laplacian}{-1, 0 or +1. sign of the laplacian at the point.  Can be used to speedup feature comparison since features with laplacians of different signs can not match}
115 \cvarg{size}{size of the feature}
116 \cvarg{dir}{orientation of the feature: 0..360 degrees}
117 \cvarg{hessian}{value of the hessian (can be used to approximately estimate the feature strengths; see also params.hessianThreshold)}
118 \end{description}
119 \fi
120
121 \cvCPyFunc{ExtractSURF}
122 Extracts Speeded Up Robust Features from an image.
123
124 \cvdefC{
125 void cvExtractSURF( \par const CvArr* image,\par const CvArr* mask,\par CvSeq** keypoints,\par CvSeq** descriptors,\par CvMemStorage* storage,\par CvSURFParams params );
126 }
127 \cvdefPy{ExtractSURF(image,mask,storage,params)-> (keypoints,descriptors)}
128
129 \begin{description}
130 \cvarg{image}{The input 8-bit grayscale image}
131 \cvarg{mask}{The optional input 8-bit mask. The features are only found in the areas that contain more than 50\% of non-zero mask pixels}
132 \ifC
133 \cvarg{keypoints}{The output parameter; double pointer to the sequence of keypoints. The sequence of CvSURFPoint structures is as follows:}
134 \begin{lstlisting}
135  typedef struct CvSURFPoint
136  {
137     CvPoint2D32f pt; // position of the feature within the image
138     int laplacian;   // -1, 0 or +1. sign of the laplacian at the point.
139                      // can be used to speedup feature comparison
140                      // (normally features with laplacians of different 
141              // signs can not match)
142     int size;        // size of the feature
143     float dir;       // orientation of the feature: 0..360 degrees
144     float hessian;   // value of the hessian (can be used to 
145              // approximately estimate the feature strengths;
146                      // see also params.hessianThreshold)
147  }
148  CvSURFPoint;
149 \end{lstlisting}
150 \cvarg{descriptors}{The optional output parameter; double pointer to the sequence of descriptors. Depending on the params.extended value, each element of the sequence will be either a 64-element or a 128-element floating-point (\texttt{CV\_32F}) vector. If the parameter is NULL, the descriptors are not computed}
151 \else
152 \cvarg{keypoints}{sequence of keypoints.}
153 \cvarg{descriptors}{sequence of descriptors.  Each SURF descriptor is a list of floats, of length 64 or 128.}
154 \fi
155 \cvarg{storage}{Memory storage where keypoints and descriptors will be stored}
156 \ifC
157 \cvarg{params}{Various algorithm parameters put to the structure CvSURFParams:}
158 \begin{lstlisting}
159  typedef struct CvSURFParams
160  {
161     int extended; // 0 means basic descriptors (64 elements each),
162                   // 1 means extended descriptors (128 elements each)
163     double hessianThreshold; // only features with keypoint.hessian 
164           // larger than that are extracted.
165                   // good default value is ~300-500 (can depend on the 
166           // average local contrast and sharpness of the image).
167                   // user can further filter out some features based on 
168           // their hessian values and other characteristics.
169     int nOctaves; // the number of octaves to be used for extraction.
170                   // With each next octave the feature size is doubled 
171           // (3 by default)
172     int nOctaveLayers; // The number of layers within each octave 
173           // (4 by default)
174  }
175  CvSURFParams;
176
177  CvSURFParams cvSURFParams(double hessianThreshold, int extended=0); 
178           // returns default parameters
179 \end{lstlisting}
180 \else
181 \cvarg{params}{Various algorithm parameters in a tuple \texttt{(extended, hessianThreshold, nOctaves, nOctaveLayers)}:
182 \begin{description}
183 \cvarg{extended}{0 means basic descriptors (64 elements each), 1 means extended descriptors (128 elements each)}
184 \cvarg{hessianThreshold}{only features with hessian larger than that are extracted.  good default value is ~300-500 (can depend on the average local contrast and sharpness of the image).  user can further filter out some features based on their hessian values and other characteristics.}
185 \cvarg{nOctaves}{the number of octaves to be used for extraction.  With each next octave the feature size is doubled (3 by default)}
186 \cvarg{nOctaveLayers}{The number of layers within each octave (4 by default)}
187 \end{description}}
188 \fi
189 \end{description}
190
191 The function cvExtractSURF finds robust features in the image, as
192 described in \cite{Bay06}. For each feature it returns its location, size,
193 orientation and optionally the descriptor, basic or extended. The function
194 can be used for object tracking and localization, image stitching etc.
195
196 \ifC
197 See the
198 \texttt{find\_obj.cpp} demo in OpenCV samples directory.
199 \else
200 To extract strong SURF features from an image
201
202 \begin{lstlisting}
203 >>> import cv
204 >>> im = cv.LoadImageM("building.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE)
205 >>> (keypoints, descriptors) = cv.ExtractSURF(im, None, cv.CreateMemStorage(), (0, 30000, 3, 1))
206 >>> print len(keypoints), len(descriptors)
207 6 6
208 >>> for ((x, y), laplacian, size, dir, hessian) in keypoints:
209 ...     print "x=\%d y=\%d laplacian=\%d size=\%d dir=\%f hessian=\%f" \% (x, y, laplacian, size, dir, hessian)
210 x=30 y=27 laplacian=-1 size=31 dir=69.778503 hessian=36979.789062
211 x=296 y=197 laplacian=1 size=33 dir=111.081039 hessian=31514.349609
212 x=296 y=266 laplacian=1 size=32 dir=107.092300 hessian=31477.908203
213 x=254 y=284 laplacian=1 size=31 dir=279.137360 hessian=34169.800781
214 x=498 y=525 laplacian=-1 size=33 dir=278.006592 hessian=31002.759766
215 x=777 y=281 laplacian=1 size=70 dir=167.940964 hessian=35538.363281
216 \end{lstlisting}
217
218 \fi
219
220 \cvCPyFunc{FindCornerSubPix}
221 Refines the corner locations.
222
223 \cvdefC{
224 void cvFindCornerSubPix(
225 \par const CvArr* image,
226 \par CvPoint2D32f* corners,
227 \par int count,
228 \par CvSize win,
229 \par CvSize zero\_zone,
230 \par CvTermCriteria criteria );
231 }\cvdefPy{FindCornerSubPix(image,corners,win,zero\_zone,criteria)-> corners}
232
233 \begin{description}
234 \cvarg{image}{Input image}
235 \ifC
236 \cvarg{corners}{Initial coordinates of the input corners; refined coordinates on output}
237 \cvarg{count}{Number of corners}
238 \fi
239 \ifPy
240 \cvarg{corners}{Initial coordinates of the input corners as a list of (x, y) pairs}
241 \fi
242 \cvarg{win}{Half of the side length of the search window. For example, if \texttt{win}=(5,5), then a $5*2+1 \times 5*2+1 = 11 \times 11$ search window would be used}
243 \cvarg{zero\_zone}{Half of the size of the dead region in the middle of the search zone over which the summation in the formula below is not done. It is used sometimes to avoid possible singularities of the autocorrelation matrix. The value of (-1,-1) indicates that there is no such size}
244 \cvarg{criteria}{Criteria for termination of the iterative process of corner refinement. That is, the process of corner position refinement stops either after a certain number of iterations or when a required accuracy is achieved. The \texttt{criteria} may specify either of or both the maximum number of iteration and the required accuracy}
245 \end{description}
246
247 The function iterates to find the sub-pixel accurate location of corners, or radial saddle points, as shown in on the picture below.
248 \ifPy
249 It returns the refined coordinates as a list of (x, y) pairs.
250 \fi
251
252 \includegraphics[width=1.0\textwidth]{pics/cornersubpix.png}
253
254 Sub-pixel accurate corner locator is based on the observation that every vector from the center $q$ to a point $p$ located within a neighborhood of $q$ is orthogonal to the image gradient at $p$ subject to image and measurement noise. Consider the expression:
255
256 \[
257 \epsilon_i = {DI_{p_i}}^T \cdot (q - p_i)
258 \]
259
260 where ${DI_{p_i}}$ is the image gradient at the one of the points $p_i$ in a neighborhood of $q$. The value of $q$ is to be found such that $\epsilon_i$ is minimized. A system of equations may be set up with $\epsilon_i$ set to zero:
261
262 \[
263 \sum_i(DI_{p_i} \cdot {DI_{p_i}}^T) q = \sum_i(DI_{p_i} \cdot {DI_{p_i}}^T \cdot p_i)
264 \]
265
266 where the gradients are summed within a neighborhood ("search window") of $q$. Calling the first gradient term $G$ and the second gradient term $b$ gives:
267
268 \[
269 q = G^{-1} \cdot b
270 \]
271
272 The algorithm sets the center of the neighborhood window at this new center $q$ and then iterates until the center keeps within a set threshold.
273
274 \cvCPyFunc{GetStarKeypoints}
275 Retrieves keypoints using the StarDetector algorithm.
276
277 \cvdefC{
278 CvSeq* cvGetStarKeypoints( \par const CvArr* image,\par CvMemStorage* storage,\par CvStarDetectorParams params=cvStarDetectorParams() );
279 }
280 \cvdefPy{GetStarKeypoints(image,storage,params)-> keypoints}
281
282 \begin{description}
283 \cvarg{image}{The input 8-bit grayscale image}
284 \cvarg{storage}{Memory storage where the keypoints will be stored}
285 \ifC
286 \cvarg{params}{Various algorithm parameters given to the structure CvStarDetectorParams:}
287 \begin{lstlisting}
288  typedef struct CvStarDetectorParams
289  {
290     int maxSize; // maximal size of the features detected. The following 
291                  // values of the parameter are supported:
292                  // 4, 6, 8, 11, 12, 16, 22, 23, 32, 45, 46, 64, 90, 128
293     int responseThreshold; // threshold for the approximatd laplacian,
294                            // used to eliminate weak features
295     int lineThresholdProjected; // another threshold for laplacian to 
296                 // eliminate edges
297     int lineThresholdBinarized; // another threshold for the feature 
298                 // scale to eliminate edges
299     int suppressNonmaxSize; // linear size of a pixel neighborhood 
300                 // for non-maxima suppression
301  }
302  CvStarDetectorParams;
303 \end{lstlisting}
304 \else
305 \cvarg{params}{Various algorithm parameters in a tuple \texttt{(maxSize, responseThreshold, lineThresholdProjected, lineThresholdBinarized, suppressNonmaxSize)}:
306 \begin{description}
307 \cvarg{maxSize}{maximal size of the features detected. The following values of the parameter are supported: 4, 6, 8, 11, 12, 16, 22, 23, 32, 45, 46, 64, 90, 128}
308 \cvarg{responseThreshold}{threshold for the approximatd laplacian, used to eliminate weak features}
309 \cvarg{lineThresholdProjected}{another threshold for laplacian to eliminate edges}
310 \cvarg{lineThresholdBinarized}{another threshold for the feature scale to eliminate edges}
311 \cvarg{suppressNonmaxSize}{linear size of a pixel neighborhood for non-maxima suppression}
312 \end{description}
313 }
314 \fi
315 \end{description}
316
317 The function GetStarKeypoints extracts keypoints that are local
318 scale-space extremas. The scale-space is constructed by computing
319 approximate values of laplacians with different sigma's at each
320 pixel. Instead of using pyramids, a popular approach to save computing
321 time, all of the laplacians are computed at each pixel of the original
322 high-resolution image. But each approximate laplacian value is computed
323 in O(1) time regardless of the sigma, thanks to the use of integral
324 images. The algorithm is based on the paper 
325 Agrawal08
326 , but instead
327 of a square, hexagon or octagon it uses an 8-end star shape, hence the name,
328 consisting of overlapping upright and tilted squares.
329
330 \ifC
331 Each computed feature is represented by the following structure:
332
333 \begin{lstlisting}
334 typedef struct CvStarKeypoint
335 {
336     CvPoint pt; // coordinates of the feature
337     int size; // feature size, see CvStarDetectorParams::maxSize
338     float response; // the approximated laplacian value at that point.
339 }
340 CvStarKeypoint;
341
342 inline CvStarKeypoint cvStarKeypoint(CvPoint pt, int size, float response);
343 \end{lstlisting}
344 \else
345 Each keypoint is represented by a tuple \texttt{((x, y), size, response)}:
346 \begin{description}
347 \cvarg{x, y}{Screen coordinates of the keypoint}
348 \cvarg{size}{feature size, up to \texttt{maxSize}}
349 \cvarg{response}{approximated laplacian value for the keypoint}
350 \end{description}
351 \fi
352
353 \ifC
354 Below is the small usage sample:
355
356 \begin{lstlisting}
357 #include "cv.h"
358 #include "highgui.h"
359
360 int main(int argc, char** argv)
361 {
362     const char* filename = argc > 1 ? argv[1] : "lena.jpg";
363     IplImage* img = cvLoadImage( filename, 0 ), *cimg;
364     CvMemStorage* storage = cvCreateMemStorage(0);
365     CvSeq* keypoints = 0;
366     int i;
367
368     if( !img )
369         return 0;
370     cvNamedWindow( "image", 1 );
371     cvShowImage( "image", img );
372     cvNamedWindow( "features", 1 );
373     cimg = cvCreateImage( cvGetSize(img), 8, 3 );
374     cvCvtColor( img, cimg, CV_GRAY2BGR );
375
376     keypoints = cvGetStarKeypoints( img, storage, cvStarDetectorParams(45) );
377
378     for( i = 0; i < (keypoints ? keypoints->total : 0); i++ )
379     {
380         CvStarKeypoint kpt = *(CvStarKeypoint*)cvGetSeqElem(keypoints, i);
381         int r = kpt.size/2;
382         cvCircle( cimg, kpt.pt, r, CV_RGB(0,255,0));
383         cvLine( cimg, cvPoint(kpt.pt.x + r, kpt.pt.y + r),
384             cvPoint(kpt.pt.x - r, kpt.pt.y - r), CV_RGB(0,255,0));
385         cvLine( cimg, cvPoint(kpt.pt.x - r, kpt.pt.y + r),
386             cvPoint(kpt.pt.x + r, kpt.pt.y - r), CV_RGB(0,255,0));
387     }
388     cvShowImage( "features", cimg );
389     cvWaitKey();
390 }
391 \end{lstlisting}
392 \fi
393
394 \cvCPyFunc{GoodFeaturesToTrack}
395 Determines strong corners on an image.
396
397 \cvdefC{
398 void cvGoodFeaturesToTrack(
399 \par const CvArr* image
400 \par CvArr* eigImage, CvArr* tempImage
401 \par CvPoint2D32f* corners
402 \par int* cornerCount
403 \par double qualityLevel
404 \par double minDistance
405 \par const CvArr* mask=NULL
406 \par int blockSize=3
407 \par int useHarris=0
408 \par double k=0.04 );
409 }
410 \cvdefPy{GoodFeaturesToTrack(image,eigImage,tempImage,cornerCount,qualityLevel,minDistance,mask=NULL,blockSize=3,useHarris=0,k=0.04)-> corners}
411
412 \begin{description}
413 \cvarg{image}{The source 8-bit or floating-point 32-bit, single-channel image}
414 \cvarg{eigImage}{Temporary floating-point 32-bit image, the same size as \texttt{image}}
415 \cvarg{tempImage}{Another temporary image, the same size and format as \texttt{eigImage}}
416 \ifC
417 \cvarg{corners}{Output parameter; detected corners}
418 \cvarg{cornerCount}{Output parameter; number of detected corners}
419 \else
420 \cvarg{cornerCount}{number of corners to detect}
421 \fi
422 \cvarg{qualityLevel}{Multiplier for the max/min eigenvalue; specifies the minimal accepted quality of image corners}
423 \cvarg{minDistance}{Limit, specifying the minimum possible distance between the returned corners; Euclidian distance is used}
424 \cvarg{mask}{Region of interest. The function selects points either in the specified region or in the whole image if the mask is NULL}
425 \cvarg{blockSize}{Size of the averaging block, passed to the underlying \cvCPyCross{CornerMinEigenVal} or \cvCPyCross{CornerHarris} used by the function}
426 \cvarg{useHarris}{If nonzero, Harris operator (\cvCPyCross{CornerHarris}) is used instead of default \cvCPyCross{CornerMinEigenVal}}
427 \cvarg{k}{Free parameter of Harris detector; used only if ($\texttt{useHarris} != 0$)}
428 \end{description}
429
430 The function finds the corners with big eigenvalues in the image. The function first calculates the minimal
431 eigenvalue for every source image pixel using the \cvCPyCross{CornerMinEigenVal}
432 function and stores them in \texttt{eigImage}. Then it performs
433 non-maxima suppression (only the local maxima in $3\times 3$ neighborhood
434 are retained). The next step rejects the corners with the minimal
435 eigenvalue less than $\texttt{qualityLevel} \cdot max(\texttt{eigImage}(x,y))$.
436 Finally, the function ensures that the distance between any two corners is not smaller than \texttt{minDistance}. The weaker corners (with a smaller min eigenvalue) that are too close to the stronger corners are rejected.
437
438 Note that the if the function is called with different values \texttt{A} and \texttt{B} of the parameter \texttt{qualityLevel}, and \texttt{A} > {B}, the array of returned corners with \texttt{qualityLevel=A} will be the prefix of the output corners array with \texttt{qualityLevel=B}.
439
440 \cvCPyFunc{HoughLines2}
441 Finds lines in a binary image using a Hough transform.
442
443 \cvdefC{
444 CvSeq* cvHoughLines2( \par CvArr* image,\par void* storage,\par int method,\par double rho,\par double theta,\par int threshold,\par double param1=0,\par double param2=0 );
445 }
446 \cvdefPy{HoughLines2(image,storage,method,rho,theta,threshold,param1=0,param2=0)-> lines}
447
448 \begin{description}
449 \cvarg{image}{The 8-bit, single-channel, binary source image. In the case of a probabilistic method, the image is modified by the function}
450 \cvarg{storage}{The storage for the lines that are detected. It can
451 be a memory storage (in this case a sequence of lines is created in
452 the storage and returned by the function) or single row/single column
453 matrix (CvMat*) of a particular type (see below) to which the lines'
454 parameters are written. The matrix header is modified by the function
455 so its \texttt{cols} or \texttt{rows} will contain the number of lines
456 detected. If \texttt{storage} is a matrix and the actual number
457 of lines exceeds the matrix size, the maximum possible number of lines
458 is returned (in the case of standard hough transform the lines are sorted
459 by the accumulator value)}
460 \cvarg{method}{The Hough transform variant, one of the following:
461 \begin{description}
462   \cvarg{CV\_HOUGH\_STANDARD}{classical or standard Hough transform. Every line is represented by two floating-point numbers $(\rho, \theta)$, where $\rho$ is a distance between (0,0) point and the line, and $\theta$ is the angle between x-axis and the normal to the line. Thus, the matrix must be (the created sequence will be) of \texttt{CV\_32FC2} type}
463   \cvarg{CV\_HOUGH\_PROBABILISTIC}{probabilistic Hough transform (more efficient in case if picture contains a few long linear segments). It returns line segments rather than the whole line. Each segment is represented by starting and ending points, and the matrix must be (the created sequence will be) of \texttt{CV\_32SC4} type}
464   \cvarg{CV\_HOUGH\_MULTI\_SCALE}{multi-scale variant of the classical Hough transform. The lines are encoded the same way as \texttt{CV\_HOUGH\_STANDARD}}
465 \end{description}}
466 \cvarg{rho}{Distance resolution in pixel-related units}
467 \cvarg{theta}{Angle resolution measured in radians}
468 \cvarg{threshold}{Threshold parameter. A line is returned by the function if the corresponding accumulator value is greater than \texttt{threshold}}
469 \cvarg{param1}{The first method-dependent parameter:
470 \begin{itemize}
471   \item For the classical Hough transform it is not used (0).
472   \item For the probabilistic Hough transform it is the minimum line length.
473   \item For the multi-scale Hough transform it is the divisor for the distance resolution $\rho$. (The coarse distance resolution will be $\rho$ and the accurate resolution will be $(\rho / \texttt{param1})$).
474 \end{itemize}}
475 \cvarg{param2}{The second method-dependent parameter:
476 \begin{itemize}
477   \item For the classical Hough transform it is not used (0).
478   \item For the probabilistic Hough transform it is the maximum gap between line segments lying on the same line to treat them as a single line segment (i.e. to join them).
479   \item For the multi-scale Hough transform it is the divisor for the angle resolution $\theta$. (The coarse angle resolution will be $\theta$ and the accurate resolution will be $(\theta / \texttt{param2})$).
480 \end{itemize}}
481 \end{description}
482
483 The function implements a few variants of the Hough transform for line detection.
484
485 \ifC
486 \textbf{Example. Detecting lines with Hough transform.}
487 \begin{lstlisting}
488 /* This is a standalone program. Pass an image name as a first parameter
489 of the program.  Switch between standard and probabilistic Hough transform
490 by changing "#if 1" to "#if 0" and back */
491 #include <cv.h>
492 #include <highgui.h>
493 #include <math.h>
494
495 int main(int argc, char** argv)
496 {
497     IplImage* src;
498     if( argc == 2 && (src=cvLoadImage(argv[1], 0))!= 0)
499     {
500         IplImage* dst = cvCreateImage( cvGetSize(src), 8, 1 );
501         IplImage* color_dst = cvCreateImage( cvGetSize(src), 8, 3 );
502         CvMemStorage* storage = cvCreateMemStorage(0);
503         CvSeq* lines = 0;
504         int i;
505         cvCanny( src, dst, 50, 200, 3 );
506         cvCvtColor( dst, color_dst, CV_GRAY2BGR );
507 #if 1
508         lines = cvHoughLines2( dst,
509                                storage,
510                                CV_HOUGH_STANDARD,
511                                1,
512                                CV_PI/180,
513                                100,
514                                0,
515                                0 );
516
517         for( i = 0; i < MIN(lines->total,100); i++ )
518         {
519             float* line = (float*)cvGetSeqElem(lines,i);
520             float rho = line[0];
521             float theta = line[1];
522             CvPoint pt1, pt2;
523             double a = cos(theta), b = sin(theta);
524             double x0 = a*rho, y0 = b*rho;
525             pt1.x = cvRound(x0 + 1000*(-b));
526             pt1.y = cvRound(y0 + 1000*(a));
527             pt2.x = cvRound(x0 - 1000*(-b));
528             pt2.y = cvRound(y0 - 1000*(a));
529             cvLine( color_dst, pt1, pt2, CV_RGB(255,0,0), 3, 8 );
530         }
531 #else
532         lines = cvHoughLines2( dst,
533                                storage,
534                                CV_HOUGH_PROBABILISTIC,
535                                1,
536                                CV_PI/180,
537                                80,
538                                30,
539                                10 );
540         for( i = 0; i < lines->total; i++ )
541         {
542             CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
543             cvLine( color_dst, line[0], line[1], CV_RGB(255,0,0), 3, 8 );
544         }
545 #endif
546         cvNamedWindow( "Source", 1 );
547         cvShowImage( "Source", src );
548
549         cvNamedWindow( "Hough", 1 );
550         cvShowImage( "Hough", color_dst );
551
552         cvWaitKey(0);
553     }
554 }
555 \end{lstlisting}
556
557 This is the sample picture the function parameters have been tuned for:
558
559 \includegraphics[width=0.5\textwidth]{pics/building.jpg}
560
561 And this is the output of the above program in the case of probabilistic Hough transform (\texttt{\#if 0} case):
562
563 \includegraphics[width=0.5\textwidth]{pics/houghp.png}
564 \fi
565
566 \cvCPyFunc{PreCornerDetect}
567 Calculates the feature map for corner detection.
568
569 \cvdefC{
570 void cvPreCornerDetect(
571 \par const CvArr* image,
572 \par CvArr* corners,
573 \par int apertureSize=3 );
574 }
575 \cvdefPy{PreCornerDetect(image,corners,apertureSize=3)-> None}
576 \begin{description}
577 \cvarg{image}{Input image}
578 \cvarg{corners}{Image to store the corner candidates}
579 \cvarg{apertureSize}{Aperture parameter for the Sobel operator (see \cvCPyCross{Sobel})}
580 \end{description}
581
582 The function calculates the function
583
584 \[
585 D_x^2 D_{yy} + D_y^2 D_{xx} - 2 D_x D_y D_{xy}
586 \]
587
588 where $D_?$ denotes one of the first image derivatives and $D_{??}$ denotes a second image derivative.
589
590 The corners can be found as local maximums of the function below:
591
592 \ifC
593 \begin{lstlisting}
594 // assume that the image is floating-point
595 IplImage* corners = cvCloneImage(image);
596 IplImage* dilated_corners = cvCloneImage(image);
597 IplImage* corner_mask = cvCreateImage( cvGetSize(image), 8, 1 );
598 cvPreCornerDetect( image, corners, 3 );
599 cvDilate( corners, dilated_corners, 0, 1 );
600 cvSubS( corners, dilated_corners, corners );
601 cvCmpS( corners, 0, corner_mask, CV_CMP_GE );
602 cvReleaseImage( &corners );
603 cvReleaseImage( &dilated_corners );
604 \end{lstlisting}
605
606 \else
607 \lstinputlisting{python_fragments/precornerdetect.py}
608 \fi
609
610 \ifC
611 \cvCPyFunc{SampleLine}
612 Reads the raster line to the buffer.
613
614 \cvdefC{
615 int cvSampleLine(
616 \par const CvArr* image
617 \par CvPoint pt1
618 \par CvPoint pt2
619 \par void* buffer
620 \par int connectivity=8 );
621 }
622
623 \begin{description}
624 \cvarg{image}{Image to sample the line from}
625 \cvarg{pt1}{Starting line point}
626 \cvarg{pt2}{Ending line point}
627 \cvarg{buffer}{Buffer to store the line points; must have enough size to store
628 $max( |\texttt{pt2.x} - \texttt{pt1.x}|+1, |\texttt{pt2.y} - \texttt{pt1.y}|+1 )$
629 points in the case of an 8-connected line and
630 $ (|\texttt{pt2.x}-\texttt{pt1.x}|+|\texttt{pt2.y}-\texttt{pt1.y}|+1) $
631 in the case of a 4-connected line}
632 \cvarg{connectivity}{The line connectivity, 4 or 8}
633 \end{description}
634
635 The function implements a particular application of line iterators. The function reads all of the image points lying on the line between \texttt{pt1} and \texttt{pt2}, including the end points, and stores them into the buffer.
636
637 \fi
638
639 \fi
640
641
642 \ifCpp
643
644 \cvCppFunc{Canny}
645 Finds edges in an image using Canny algorithm.
646
647 \cvdefCpp{void Canny( const Mat\& image, Mat\& edges,\par
648             double threshold1, double threshold2,\par
649             int apertureSize=3, bool L2gradient=false );}
650 \begin{description}
651 \cvarg{image}{Single-channel 8-bit input image}
652 \cvarg{edges}{The output edge map. It will have the same size and the same type as \texttt{image}}
653 \cvarg{threshold1}{The first threshold for the hysteresis procedure}
654 \cvarg{threshold2}{The second threshold for the hysteresis procedure}
655 \cvarg{apertureSize}{Aperture size for the \cvCppCross{Sobel} operator}
656 \cvarg{L2gradient}{Indicates, whether the more accurate $L_2$ norm $=\sqrt{(dI/dx)^2 + (dI/dy)^2}$ should be used to compute the image gradient magnitude (\texttt{L2gradient=true}), or a faster default $L_1$ norm $=|dI/dx|+|dI/dy|$ is enough (\texttt{L2gradient=false})}
657 \end{description}
658
659 The function finds edges in the input image \texttt{image} and marks them in the output map \texttt{edges} using the Canny algorithm. The smallest value between \texttt{threshold1} and \texttt{threshold2} is used for edge linking, the largest value is used to find the initial segments of strong edges, see
660 \url{http://en.wikipedia.org/wiki/Canny_edge_detector}
661
662 \cvCppFunc{cornerEigenValsAndVecs}
663 Calculates eigenvalues and eigenvectors of image blocks for corner detection.
664
665 \cvdefCpp{void cornerEigenValsAndVecs( const Mat\& src, Mat\& dst,\par
666                             int blockSize, int apertureSize,\par
667                             int borderType=BORDER\_DEFAULT );}
668 \begin{description}
669 \cvarg{src}{Input single-channel 8-bit or floating-point image}
670 \cvarg{dst}{Image to store the results. It will have the same size as \texttt{src} and the type \texttt{CV\_32FC(6)}}
671 \cvarg{blockSize}{Neighborhood size (see discussion)}
672 \cvarg{apertureSize}{Aperture parameter for the \cvCppCross{Sobel} operator}
673 \cvarg{boderType}{Pixel extrapolation method; see \cvCppCross{borderInterpolate}}
674 \end{description}
675
676 For every pixel $p$, the function \texttt{cornerEigenValsAndVecs} considers a \texttt{blockSize} $\times$ \texttt{blockSize} neigborhood $S(p)$. It calculates the covariation matrix of derivatives over the neighborhood as:
677
678 \[
679 M = \begin{bmatrix}
680 \sum_{S(p)}(dI/dx)^2 & \sum_{S(p)}(dI/dx dI/dy)^2 \\
681 \sum_{S(p)}(dI/dx dI/dy)^2 & \sum_{S(p)}(dI/dy)^2
682 \end{bmatrix}
683 \]
684
685 Where the derivatives are computed using \cvCppCross{Sobel} operator.
686
687 After that it finds eigenvectors and eigenvalues of $M$ and stores them into destination image in the form
688 $(\lambda_1, \lambda_2, x_1, y_1, x_2, y_2)$ where
689 \begin{description}
690 \item[$\lambda_1, \lambda_2$]are the eigenvalues of $M$; not sorted
691 \item[$x_1, y_1$]are the eigenvectors corresponding to $\lambda_1$
692 \item[$x_2, y_2$]are the eigenvectors corresponding to $\lambda_2$
693 \end{description}
694
695 The output of the function can be used for robust edge or corner detection.
696
697 See also: \cvCppCross{cornerMinEigenVal}, \cvCppCross{cornerHarris}, \cvCppCross{preCornerDetect}
698
699 \cvCppFunc{cornerHarris}
700 Harris edge detector.
701
702 \cvdefCpp{void cornerHarris( const Mat\& src, Mat\& dst, int blockSize,\par
703                   int apertureSize, double k,\par
704                   int borderType=BORDER\_DEFAULT );}
705 \begin{description}
706 \cvarg{src}{Input single-channel 8-bit or floating-point image}
707 \cvarg{dst}{Image to store the Harris detector responses; will have type \texttt{CV\_32FC1} and the same size as \texttt{src}}
708 \cvarg{blockSize}{Neighborhood size (see the discussion of \cvCppCross{cornerEigenValsAndVecs})}
709 \cvarg{apertureSize}{Aperture parameter for the \cvCppCross{Sobel} operator}
710 \cvarg{k}{Harris detector free parameter. See the formula below}
711 \cvarg{boderType}{Pixel extrapolation method; see \cvCppCross{borderInterpolate}}
712 \end{description}
713
714 The function runs the Harris edge detector on the image. Similarly to \cvCppCross{cornerMinEigenVal} and \cvCppCross{cornerEigenValsAndVecs}, for each pixel $(x, y)$ it calculates a $2\times2$ gradient covariation matrix $M^{(x,y)}$ over a $\texttt{blockSize} \times \texttt{blockSize}$ neighborhood. Then, it computes the following characteristic:
715
716 \[
717 \texttt{dst}(x,y) = \mathrm{det} M^{(x,y)} - k \cdot \left(\mathrm{tr} M^{(x,y)}\right)^2
718 \]
719
720 Corners in the image can be found as the local maxima of this response map.
721
722 \cvCppFunc{cornerMinEigenVal}
723 Calculates the minimal eigenvalue of gradient matrices for corner detection.
724
725 \cvdefCpp{void cornerMinEigenVal( const Mat\& src, Mat\& dst,\par
726                         int blockSize, int apertureSize=3,\par
727                         int borderType=BORDER\_DEFAULT );}
728 \begin{description}
729 \cvarg{src}{Input single-channel 8-bit or floating-point image}
730 \cvarg{dst}{Image to store the minimal eigenvalues; will have type \texttt{CV\_32FC1} and the same size as \texttt{src}}
731 \cvarg{blockSize}{Neighborhood size (see the discussion of \cvCppCross{cornerEigenValsAndVecs})}
732 \cvarg{apertureSize}{Aperture parameter for the \cvCppCross{Sobel} operator}
733 \cvarg{boderType}{Pixel extrapolation method; see \cvCppCross{borderInterpolate}}
734 \end{description}
735
736 The function is similar to \cvCppCross{cornerEigenValsAndVecs} but it calculates and stores only the minimal eigenvalue of the covariation matrix of derivatives, i.e. $\min(\lambda_1, \lambda_2)$ in terms of the formulae in \cvCppCross{cornerEigenValsAndVecs} description.
737
738 \cvCppFunc{cornerSubPix}
739 Refines the corner locations.
740
741 \cvdefCpp{void cornerSubPix( const Mat\& image, vector<Point2f>\& corners,\par
742                    Size winSize, Size zeroZone,\par
743                    TermCriteria criteria );}
744 \begin{description}
745 \cvarg{image}{Input image}
746 \cvarg{corners}{Initial coordinates of the input corners; refined coordinates on output}
747 \cvarg{winSize}{Half of the side length of the search window. For example, if \texttt{winSize=Size(5,5)}, then a $5*2+1 \times 5*2+1 = 11 \times 11$ search window would be used}
748 \cvarg{zeroZone}{Half of the size of the dead region in the middle of the search zone over which the summation in the formula below is not done. It is used sometimes to avoid possible singularities of the autocorrelation matrix. The value of (-1,-1) indicates that there is no such size}
749 \cvarg{criteria}{Criteria for termination of the iterative process of corner refinement. That is, the process of corner position refinement stops either after a certain number of iterations or when a required accuracy is achieved. The \texttt{criteria} may specify either of or both the maximum number of iteration and the required accuracy}
750 \end{description}
751
752 The function iterates to find the sub-pixel accurate location of corners, or radial saddle points, as shown in on the picture below.
753
754 \includegraphics[width=1.0\textwidth]{pics/cornersubpix.png}
755
756 Sub-pixel accurate corner locator is based on the observation that every vector from the center $q$ to a point $p$ located within a neighborhood of $q$ is orthogonal to the image gradient at $p$ subject to image and measurement noise. Consider the expression:
757
758 \[
759 \epsilon_i = {DI_{p_i}}^T \cdot (q - p_i)
760 \]
761
762 where ${DI_{p_i}}$ is the image gradient at the one of the points $p_i$ in a neighborhood of $q$. The value of $q$ is to be found such that $\epsilon_i$ is minimized. A system of equations may be set up with $\epsilon_i$ set to zero:
763
764 \[
765 \sum_i(DI_{p_i} \cdot {DI_{p_i}}^T) - \sum_i(DI_{p_i} \cdot {DI_{p_i}}^T \cdot p_i)
766 \]
767
768 where the gradients are summed within a neighborhood ("search window") of $q$. Calling the first gradient term $G$ and the second gradient term $b$ gives:
769
770 \[
771 q = G^{-1} \cdot b
772 \]
773
774 The algorithm sets the center of the neighborhood window at this new center $q$ and then iterates until the center keeps within a set threshold.
775
776
777 \cvCppFunc{goodFeaturesToTrack}
778 Determines strong corners on an image.
779
780 \cvdefCpp{void goodFeaturesToTrack( const Mat\& image, vector<Point2f>\& corners,\par
781                          int maxCorners, double qualityLevel, double minDistance,\par
782                          const Mat\& mask=Mat(), int blockSize=3,\par
783                          bool useHarrisDetector=false, double k=0.04 );}
784 \begin{description}
785 \cvarg{image}{The input 8-bit or floating-point 32-bit, single-channel image}
786 \cvarg{corners}{The output vector of detected corners}
787 \cvarg{maxCorners}{The maximum number of corners to return. If there are more corners than that will be found, the strongest of them will be returned}
788 \cvarg{qualityLevel}{Characterizes the minimal accepted quality of image corners; the value of the parameter is multiplied by the by the best corner quality measure (which is the min eigenvalue, see \cvCppCross{cornerMinEigenVal}, or the Harris function response, see \cvCppCross{cornerHarris}). The corners, which quality measure is less than the product, will be rejected. For example, if the best corner has the quality measure = 1500, and the \texttt{qualityLevel=0.01}, then all the corners which quality measure is less than 15 will be rejected.}
789 \cvarg{minDistance}{The minimum possible Euclidean distance between the returned corners}
790 \cvarg{mask}{The optional region of interest. If the image is not empty (then it needs to have the type \texttt{CV\_8UC1} and the same size as \texttt{image}), it will specify the region in which the corners are detected}
791 \cvarg{blockSize}{Size of the averaging block for computing derivative covariation matrix over each pixel neighborhood, see \cvCppCross{cornerEigenValsAndVecs}}
792 \cvarg{useHarrisDetector}{Indicates, whether to use \hyperref[cornerHarris]{Harris} operator or \cvCppCross{cornerMinEigenVal}}
793 \cvarg{k}{Free parameter of Harris detector}
794 \end{description}
795
796 The function finds the most prominent corners in the image or in the specified image region, as described
797 in \cite{Shi94}:
798 \begin{enumerate}
799 \item the function first calculates the corner quality measure at every source image pixel using the \cvCppCross{cornerMinEigenVal} or \cvCppCross{cornerHarris}
800 \item then it performs non-maxima suppression (the local maxima in $3\times 3$ neighborhood
801 are retained).
802 \item the next step rejects the corners with the minimal eigenvalue less than $\texttt{qualityLevel} \cdot \max_{x,y} qualityMeasureMap(x,y)$.
803 \item the remaining corners are then sorted by the quality measure in the descending order.
804 \item finally, the function throws away each corner $pt_j$ if there is a stronger corner $pt_i$ ($i < j$) such that the distance between them is less than \texttt{minDistance}
805 \end{enumerate}
806
807 The function can be used to initialize a point-based tracker of an object.
808
809 Note that the if the function is called with different values \texttt{A} and \texttt{B} of the parameter \texttt{qualityLevel}, and \texttt{A} > {B}, the vector of returned corners with \texttt{qualityLevel=A} will be the prefix of the output vector with \texttt{qualityLevel=B}.
810
811 See also: \cvCppCross{cornerMinEigenVal}, \cvCppCross{cornerHarris}, \cvCppCross{calcOpticalFlowPyrLK}, \cvCppCross{estimateRigidMotion}, \cvCppCross{PlanarObjectDetector}, \cvCppCross{OneWayDescriptor}
812
813 \cvCppFunc{HoughCircles}
814 Finds circles in a grayscale image using a Hough transform.
815
816 \cvdefCpp{void HoughCircles( Mat\& image, vector<Vec3f>\& circles,\par
817                  int method, double dp, double minDist,\par
818                  double param1=100, double param2=100,\par
819                  int minRadius=0, int maxRadius=0 );}
820 \begin{description}
821 \cvarg{image}{The 8-bit, single-channel, grayscale input image}
822 \cvarg{circles}{The output vector of found circles. Each vector is encoded as 3-element floating-point vector $(x, y, radius)$}
823 \cvarg{method}{Currently, the only implemented method is \texttt{CV\_HOUGH\_GRADIENT}, which is basically \emph{21HT}, described in \cite{Yuen90}.}
824 \cvarg{dp}{The inverse ratio of the accumulator resolution to the image resolution. For example, if \texttt{dp=1}, the accumulator will have the same resolution as the input image, if \texttt{dp=2} - accumulator will have half as big width and height, etc}
825 \cvarg{minDist}{Minimum distance between the centers of the detected circles. If the parameter is too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed}
826 \cvarg{param1}{The first method-specific parameter. in the case of \texttt{CV\_HOUGH\_GRADIENT} it is the higher threshold of the two passed to \cvCppCross{Canny} edge detector (the lower one will be twice smaller)}
827 \cvarg{param2}{The second method-specific parameter. in the case of \texttt{CV\_HOUGH\_GRADIENT} it is the accumulator threshold at the center detection stage. The smaller it is, the more false circles may be detected. Circles, corresponding to the larger accumulator values, will be returned first}
828 \cvarg{minRadius}{Minimum circle radius}
829 \cvarg{maxRadius}{Maximum circle radius}
830 \end{description}
831
832 The function finds circles in a grayscale image using some modification of Hough transform. Here is a short usage example:
833
834 \begin{lstlisting}
835 #include <cv.h>
836 #include <highgui.h>
837 #include <math.h>
838
839 using namespace cv;
840
841 int main(int argc, char** argv)
842 {
843     Mat img, gray;
844     if( argc != 2 && !(img=imread(argv[1], 1)).data)
845         return -1;
846     cvtColor(img, gray, CV_BGR2GRAY);
847     // smooth it, otherwise a lot of false circles may be detected
848     GaussianBlur( gray, gray, 9, 9, 2, 2 );
849     vector<Vec3f> circles;
850     houghCircles(gray, circles, CV_HOUGH_GRADIENT,
851                  2, gray->rows/4, 200, 100 );
852     for( size_t i = 0; i < circles.size(); i++ )
853     {
854          Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
855          int radius = cvRound(circles[i][2]);
856          // draw the circle center
857          circle( img, center, 3, Scalar(0,255,0), -1, 8, 0 );
858          // draw the circle outline
859          circle( img, center, radius, Scalar(0,0,255), 3, 8, 0 );
860     }
861     namedWindow( "circles", 1 );
862     imshow( "circles", img );
863     return 0;
864 }
865 \end{lstlisting}
866
867 Note that usually the function detects the circles' centers well, however it may fail to find the correct radii. You can assist the function by specifying the radius range (\texttt{minRadius} and \texttt{maxRadius}) if you know it, or you may ignore the returned radius, use only the center and find the correct radius using some additional procedure.
868
869 See also: \cvCppCross{fitEllipse}, \cvCppCross{minEnclosingCircle}
870
871 \cvCppFunc{HoughLines}
872 Finds lines in a binary image using standard Hough transform.
873
874 \cvdefCpp{void HoughLines( Mat\& image, vector<Vec2f>\& lines,\par
875                  double rho, double theta, int threshold,\par
876                  double srn=0, double stn=0 );}
877 \begin{description}
878 \cvarg{image}{The 8-bit, single-channel, binary source image. The image may be modified by the function}
879 \cvarg{lines}{The output vector of lines. Each line is represented by a two-element vector $(\rho, \theta)$. $\rho$ is the distance from the coordinate origin $(0,0)$ (top-left corner of the image) and $\theta$ is the line rotation angle in radians ($0 \sim \textrm{vertical line}, \pi/2 \sim \textrm{horizontal line}$)}
880 \cvarg{rho}{Distance resolution of the accumulator in pixels}
881 \cvarg{theta}{Angle resolution of the accumulator in radians}
882 \cvarg{threshold}{The accumulator threshold parameter. Only those lines are returned that get enough votes ($>\texttt{threshold}$)}
883 \cvarg{srn}{For the multi-scale Hough transform it is the divisor for the distance resolution \texttt{rho}. The coarse accumulator distance resolution will be \texttt{rho} and the accurate accumulator resolution will be \texttt{rho/srn}. If both \texttt{srn=0} and \texttt{stn=0} then the classical Hough transform is used, otherwise both these parameters should be positive.}
884 \cvarg{stn}{For the multi-scale Hough transform it is the divisor for the distance resolution \texttt{theta}}
885 \end{description}
886
887 The function implements standard or standard multi-scale Hough transform algorithm for line detection. See \cvCppCross{HoughLinesP} for the code example.
888
889
890 \cvCppFunc{HoughLinesP}
891 Finds lines segments in a binary image using probabilistic Hough transform.
892
893 \cvdefCpp{void HoughLinesP( Mat\& image, vector<Vec4i>\& lines,\par
894                   double rho, double theta, int threshold,\par
895                   double minLineLength=0, double maxLineGap=0 );}
896 \begin{description}
897 \cvarg{image}{The 8-bit, single-channel, binary source image. The image may be modified by the function}
898 \cvarg{lines}{The output vector of lines. Each line is represented by a 4-element vector $(x_1, y_1, x_2, y_2)$, where $(x_1,y_1)$ and $(x_2, y_2)$ are the ending points of each line segment detected.}
899 \cvarg{rho}{Distance resolution of the accumulator in pixels}
900 \cvarg{theta}{Angle resolution of the accumulator in radians}
901 \cvarg{threshold}{The accumulator threshold parameter. Only those lines are returned that get enough votes ($>\texttt{threshold}$)}
902 \cvarg{minLineLength}{The minimum line length. Line segments shorter than that will be rejected}
903 \cvarg{maxLineGap}{The maximum allowed gap between points on the same line to link them.}
904 \end{description}
905
906 The function implements probabilistic Hough transform algorithm for line detection, described in \cite{Matas00}. Below is line detection example:
907
908 \begin{lstlisting}
909 /* This is a standalone program. Pass an image name as a first parameter
910 of the program.  Switch between standard and probabilistic Hough transform
911 by changing "#if 1" to "#if 0" and back */
912 #include <cv.h>
913 #include <highgui.h>
914 #include <math.h>
915
916 using namespace cv;
917
918 int main(int argc, char** argv)
919 {
920     Mat src, dst, color_dst;
921     if( argc != 2 || !(src=imread(argv[1], 0)).data)
922         return -1;
923         
924     Canny( src, dst, 50, 200, 3 );
925     cvtColor( dst, color_dst, CV_GRAY2BGR );    
926         
927 #if 0
928     vector<Vec2f> lines;
929     HoughLines( dst, lines, 1, CV_PI/180, 100 );
930
931     for( size_t i = 0; i < lines.size(); i++ )
932     {
933         float rho = lines[i][0];
934         float theta = lines[i][1];
935         double a = cos(theta), b = sin(theta);
936         double x0 = a*rho, y0 = b*rho;
937         Point pt1(cvRound(x0 + 1000*(-b)),
938                   cvRound(y0 + 1000*(a)));
939         Point pt2(cvRound(x0 - 1000*(-b)),
940                   cvRound(y0 - 1000*(a)));
941         line( color_dst, pt1, pt2, Scalar(0,0,255), 3, 8 );
942     }
943 #else
944     vector<Vec4i> lines;
945     HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );
946     for( size_t i = 0; i < lines.size(); i++ )
947     {
948         line( color_dst, Point(lines[i][0], lines[i][1]),
949             Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );
950     }
951 #endif
952     namedWindow( "Source", 1 );
953     imshow( "Source", src );
954
955     namedWindow( "Detected Lines", 1 );
956     imshow( "Detected Lines", color_dst );
957
958     waitKey(0);
959     return 0;
960 }
961 \end{lstlisting}
962
963
964 This is the sample picture the function parameters have been tuned for:
965
966 \includegraphics[width=0.5\textwidth]{pics/building.jpg}
967
968 And this is the output of the above program in the case of probabilistic Hough transform
969
970 \includegraphics[width=0.5\textwidth]{pics/houghp.png}
971
972 \cvCppFunc{perCornerDetect}
973 Calculates the feature map for corner detection
974
975 \cvdefCpp{void preCornerDetect( const Mat\& src, Mat\& dst, int apertureSize,\par
976                      int borderType=BORDER\_DEFAULT );}
977 \begin{description}
978 \cvarg{src}{The source single-channel 8-bit of floating-point image}
979 \cvarg{dst}{The output image; will have type \texttt{CV\_32F} and the same size as \texttt{src}}
980 \cvarg{apertureSize}{Aperture size of \cvCppCross{Sobel}}
981 \cvarg{borderType}{The pixel extrapolation method; see \cvCppCross{borderInterpolate}}
982 \end{description}
983
984 The function calculates the complex spatial derivative-based function of the source image
985
986 \[
987 \texttt{dst} = (D_x \texttt{src})^2 \cdot D_{yy} \texttt{src} + (D_y \texttt{src})^2 \cdot D_{xx} \texttt{src} - 2 D_x \texttt{src} \cdot D_y \texttt{src} \cdot D_{xy} \texttt{src}
988 \]
989
990 where $D_x$, $D_y$ are the first image derivatives, $D_{xx}$, $D_{yy}$ are the second image derivatives and $D_{xy}$ is the mixed derivative.
991
992 The corners can be found as local maximums of the functions, as shown below:
993
994 \begin{lstlisting}
995 Mat corners, dilated_corners;
996 preCornerDetect(image, corners, 3);
997 // dilation with 3x3 rectangular structuring element
998 dilate(corners, dilated_corners, Mat(), 1);
999 Mat corner_mask = corners == dilated_corners;
1000 \end{lstlisting}
1001
1002
1003 \cvCppFunc{KeyPoint}
1004 Data structure for salient point detectors
1005
1006 \begin{lstlisting}
1007 KeyPoint
1008 {
1009 public:
1010     // default constructor
1011     KeyPoint();
1012     // two complete constructors
1013     KeyPoint(Point2f _pt, float _size, float _angle=-1,
1014             float _response=0, int _octave=0, int _class_id=-1);
1015     KeyPoint(float x, float y, float _size, float _angle=-1,
1016              float _response=0, int _octave=0, int _class_id=-1);
1017     // coordinate of the point
1018     Point2f pt;
1019     // feature size
1020     float size;
1021     // feature orintation in degrees
1022     // (has negative value if the orientation
1023     // is not defined/not computed)
1024     float angle;
1025     // feature strength
1026     // (can be used to select only
1027     // the most prominent key points)
1028     float response;
1029     // scale-space octave in which the feature has been found;
1030     // may correlate with the size
1031     int octave;
1032     // point (can be used by feature
1033     // classifiers or object detectors)
1034     int class_id;
1035 };
1036
1037 // reading/writing a vector of keypoints to a file storage
1038 void write(FileStorage& fs, const string& name, const vector<KeyPoint>& keypoints);
1039 void read(const FileNode& node, vector<KeyPoint>& keypoints);    
1040 \end{lstlisting}
1041
1042
1043 \cvCppFunc{MSER}
1044 Maximally-Stable Extremal Region Extractor
1045
1046 \begin{lstlisting}
1047 class MSER : public CvMSERParams
1048 {
1049 public:
1050     // default constructor
1051     MSER();
1052     // constructor that initializes all the algorithm parameters
1053     MSER( int _delta, int _min_area, int _max_area,
1054           float _max_variation, float _min_diversity,
1055           int _max_evolution, double _area_threshold,
1056           double _min_margin, int _edge_blur_size );
1057     // runs the extractor on the specified image; returns the MSERs,
1058     // each encoded as a contour (vector<Point>, see findContours)
1059     // the optional mask marks the area where MSERs are searched for
1060     void operator()(Mat& image, vector<vector<Point> >& msers, const Mat& mask) const;
1061 };
1062 \end{lstlisting}
1063
1064 The class encapsulates all the parameters of MSER (see \url{http://en.wikipedia.org/wiki/Maximally_stable_extremal_regions}) extraction algorithm. 
1065
1066 \cvCppFunc{SURF}
1067 Class for extracting Speeded Up Robust Features from an image.
1068
1069 \begin{lstlisting}
1070 class SURF : public CvSURFParams
1071 {
1072 public:
1073     // default constructor
1074     SURF();
1075     // constructor that initializes all the algorithm parameters
1076     SURF(double _hessianThreshold, int _nOctaves=4,
1077          int _nOctaveLayers=2, bool _extended=false);
1078     // returns the number of elements in each descriptor (64 or 128)
1079     int descriptorSize() const;
1080     // detects keypoints using fast multi-scale Hessian detector
1081     void operator()(const Mat& img, const Mat& mask,
1082                     vector<KeyPoint>& keypoints) const;
1083     // detects keypoints and computes the SURF descriptors for them
1084     void operator()(const Mat& img, const Mat& mask,
1085                     vector<KeyPoint>& keypoints,
1086                     vector<float>& descriptors,
1087                     bool useProvidedKeypoints=false) const;
1088 };
1089 \end{lstlisting}
1090
1091 The class \texttt{SURF} implements Speeded Up Robust Features descriptor \cite{Bay06}.
1092 There is fast multi-scale Hessian keypoint detector that can be used to find the keypoints
1093 (which is the default option), but the descriptors can be also computed for the user-specified keypoints.
1094 The function can be used for object tracking and localization, image stitching etc. See the
1095 \texttt{find\_obj.cpp} demo in OpenCV samples directory.
1096
1097
1098 \cvCppFunc{StarDetector}
1099 Implements Star keypoint detector
1100
1101 \begin{lstlisting}
1102 class StarDetector : CvStarDetectorParams
1103 {
1104 public:
1105     // default constructor
1106     StarDetector();
1107     // the full constructor initialized all the algorithm parameters:
1108     // maxSize - maximum size of the features. The following 
1109     //      values of the parameter are supported:
1110     //      4, 6, 8, 11, 12, 16, 22, 23, 32, 45, 46, 64, 90, 128
1111     // responseThreshold - threshold for the approximated laplacian,
1112     //      used to eliminate weak features. The larger it is,
1113     //      the less features will be retrieved
1114     // lineThresholdProjected - another threshold for the laplacian to 
1115     //      eliminate edges
1116     // lineThresholdBinarized - another threshold for the feature 
1117     //      size to eliminate edges.
1118     // The larger the 2 threshold, the more points you get.
1119     StarDetector(int maxSize, int responseThreshold,
1120                  int lineThresholdProjected,
1121                  int lineThresholdBinarized,
1122                  int suppressNonmaxSize);
1123
1124     // finds keypoints in an image
1125     void operator()(const Mat& image, vector<KeyPoint>& keypoints) const;
1126 };
1127 \end{lstlisting}
1128
1129 The class implements a modified version of CenSurE keypoint detector described in
1130 \cite{Agrawal08}
1131
1132 \fi