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