1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
43 typedef struct _PointInfo
54 icvFindDominantPointsIPAN( CvSeq * contour,
55 CvMemStorage * storage,
56 CvSeq ** corners, int dmin2, int dmax2, int dneigh2, float amax )
58 CvStatus status = CV_OK;
61 int n = contour->total;
71 float mincos = (float) cos( 3.14159265359 * amax / 180 );
73 /* check bad arguments */
75 return CV_NULLPTR_ERR;
77 return CV_NULLPTR_ERR;
79 return CV_NULLPTR_ERR;
81 return CV_BADSIZE_ERR;
83 return CV_BADSIZE_ERR;
84 if( (dneigh2 > dmax2) || (dneigh2 < 0) )
85 return CV_BADSIZE_ERR;
86 if( (amax < 0) || (amax > 180) )
87 return CV_BADSIZE_ERR;
89 sharpness = (float *) cvAlloc( n * sizeof( float ));
90 distance = (float *) cvAlloc( n * sizeof( float ));
92 ptInf = (icvPointInfo *) cvAlloc( n * sizeof( icvPointInfo ));
94 /*****************************************************************************************/
96 /*****************************************************************************************/
98 if( CV_IS_SEQ_CHAIN_CONTOUR( contour ))
100 CvChainPtReader reader;
102 cvStartReadChainPoints( (CvChain *) contour, &reader );
104 for( i = 0; i < n; i++ )
106 CV_READ_CHAIN_POINT( ptInf[i].pt, reader );
109 else if( CV_IS_SEQ_POLYGON( contour ))
113 cvStartReadSeq( contour, &reader, 0 );
115 for( i = 0; i < n; i++ )
117 CV_READ_SEQ_ELEM( ptInf[i].pt, reader );
122 return CV_BADFLAG_ERR;
125 for( i = 0; i < n; i++ )
127 /* find nearest suitable points
128 which satisfy distance constraint >dmin */
131 int left_far, right_far;
141 /* find right minimum */
142 while( dist_r < dmin2 )
152 if( dist_r < dneigh2 )
153 ptInf[i].right_neigh = i_plus;
157 ind = (i + i_plus) % n;
158 dx = (float) (ptInf[i].pt.x - ptInf[ind].pt.x);
159 dy = (float) (ptInf[i].pt.y - ptInf[ind].pt.y);
160 dist_r = dx * dx + dy * dy;
162 /* find right maximum */
163 while( dist_r <= dmax2 )
171 distance[(i + i_plus) % n] = cvSqrt( dist_r );
173 if( dist_r < dneigh2 )
174 ptInf[i].right_neigh = i_plus;
180 ind = (i + i_plus) % n;
182 dx = (float) (ptInf[i].pt.x - ptInf[ind].pt.x);
183 dy = (float) (ptInf[i].pt.y - ptInf[ind].pt.y);
184 dist_r = dx * dx + dy * dy;
189 while( dist_l < dmin2 )
199 if( dist_l < dneigh2 )
200 ptInf[i].left_neigh = i_minus;
205 ind = (ind < 0) ? (n + ind) : ind;
207 dx = (float) (ptInf[i].pt.x - ptInf[ind].pt.x);
208 dy = (float) (ptInf[i].pt.y - ptInf[ind].pt.y);
209 dist_l = dx * dx + dy * dy;
212 /* find left maximum */
213 while( dist_l <= dmax2 )
222 ind = (ind < 0) ? (n + ind) : ind;
224 distance[ind] = cvSqrt( dist_l );
226 if( dist_l < dneigh2 )
227 ptInf[i].left_neigh = i_minus;
234 ind = (ind < 0) ? (n + ind) : ind;
236 dx = (float) (ptInf[i].pt.x - ptInf[ind].pt.x);
237 dy = (float) (ptInf[i].pt.y - ptInf[ind].pt.y);
238 dist_l = dx * dx + dy * dy;
242 if( (i_plus - i_minus) > n + 2 )
246 for( j = left_far + 1; j < left_near; j++ )
252 leftind = (leftind < 0) ? (n + leftind) : leftind;
254 a = distance[leftind];
257 for( k = right_near + 1; k < right_far; k++ )
259 int ind = (i + k) % n;
261 float b = distance[ind];
264 /* compute cosinus */
265 dx = (float) (ptInf[leftind].pt.x - ptInf[ind].pt.x);
266 dy = (float) (ptInf[leftind].pt.y - ptInf[ind].pt.y);
268 c2 = dx * dx + dy * dy;
269 cosalpha = (a2 + b2 - c2) / (2 * a * b);
271 max_cos_alpha = MAX( max_cos_alpha, cosalpha );
273 if( max_cos_alpha < mincos )
276 sharpness[i] = max_cos_alpha;
280 /*****************************************************************************************/
282 /*****************************************************************************************/
284 cvStartWriteSeq( (contour->flags & ~CV_SEQ_ELTYPE_MASK) | CV_SEQ_ELTYPE_INDEX,
285 sizeof( CvSeq ), sizeof( int ), storage, &writer );
287 /* second pass - nonmaxima suppression */
288 /* neighborhood of point < dneigh2 */
289 for( i = 0; i < n; i++ )
292 if( sharpness[i] == -1 )
295 for( j = 1; (j <= ptInf[i].right_neigh) && (suppressed == 0); j++ )
297 if( sharpness[i] < sharpness[(i + j) % n] )
301 for( j = -1; (j >= ptInf[i].left_neigh) && (suppressed == 0); j-- )
305 ind = (ind < 0) ? (n + ind) : ind;
306 if( sharpness[i] < sharpness[ind] )
311 CV_WRITE_SEQ_ELEM( i, writer );
314 *corners = cvEndWriteSeq( &writer );
316 cvFree( &sharpness );
323 /* dmax is so big (more than contour diameter)
324 that algorithm could become infinite cycle */
325 cvFree( &sharpness );
329 return CV_BADRANGE_ERR;
333 /*F///////////////////////////////////////////////////////////////////////////////////////
334 // Name: icvFindDominantPoints
336 // Applies some algorithm to find dominant points ( corners ) of contour
340 // contours - pointer to input contour object.
341 // out_numbers - array of dominant points indices
342 // count - length of out_numbers array on input
343 // and numbers of founded dominant points on output
345 // method - only CV_DOMINANT_IPAN now
346 // parameters - array of parameters
347 // for IPAN algorithm
348 // [0] - minimal distance
349 // [1] - maximal distance
350 // [2] - neighborhood distance (must be not greater than dmaximal distance)
351 // [3] - maximal possible angle of curvature
353 // CV_OK or error code
355 // User must allocate out_numbers array. If it is small - function fills array
356 // with part of points and returns error
359 cvFindDominantPoints( CvSeq * contour, CvMemStorage * storage, int method,
360 double parameter1, double parameter2, double parameter3, double parameter4 )
364 CV_FUNCNAME( "cvFindDominantPoints" );
368 CV_ERROR( CV_StsNullPtr, "" );
371 storage = contour->storage;
374 CV_ERROR( CV_StsNullPtr, "" );
378 case CV_DOMINANT_IPAN:
380 int dmin = cvRound(parameter1);
381 int dmax = cvRound(parameter2);
382 int dneigh = cvRound(parameter3);
383 int amax = cvRound(parameter4);
394 IPPI_CALL( icvFindDominantPointsIPAN( contour, storage, &corners,
395 dmin*dmin, dmax*dmax, dneigh*dneigh, (float)amax ));
399 CV_ERROR_FROM_STATUS( CV_BADFLAG_ERR );