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 #define _CV_SNAKE_BIG 2.e+38f
44 #define _CV_SNAKE_IMAGE 1
45 #define _CV_SNAKE_GRAD 2
48 /*F///////////////////////////////////////////////////////////////////////////////////////
49 // Name: icvSnake8uC1R
53 // src - source image,
54 // srcStep - its step in bytes,
56 // pt - pointer to snake points array
57 // n - size of points array,
58 // alpha - pointer to coefficient of continuity energy,
59 // beta - pointer to coefficient of curvature energy,
60 // gamma - pointer to coefficient of image energy,
61 // coeffUsage - if CV_VALUE - alpha, beta, gamma point to single value
62 // if CV_MATAY - point to arrays
63 // criteria - termination criteria.
64 // scheme - image energy scheme
65 // if _CV_SNAKE_IMAGE - image intensity is energy
66 // if _CV_SNAKE_GRAD - magnitude of gradient is energy
71 icvSnake8uC1R( unsigned char *src,
79 int coeffUsage, CvSize win, CvTermCriteria criteria, int scheme )
82 int neighbors = win.height * win.width;
84 int centerx = win.width >> 1;
85 int centery = win.height >> 1;
97 float _alpha, _beta, _gamma;
99 /*#ifdef GRAD_SNAKE */
100 float *gradient = NULL;
102 int map_width = ((roi.width - 1) >> 3) + 1;
103 int map_height = ((roi.height - 1) >> 3) + 1;
105 #define TILE_SIZE (WTILE_SIZE + 2)
106 short dx[TILE_SIZE*TILE_SIZE], dy[TILE_SIZE*TILE_SIZE];
107 CvMat _dx = cvMat( TILE_SIZE, TILE_SIZE, CV_16SC1, dx );
108 CvMat _dy = cvMat( TILE_SIZE, TILE_SIZE, CV_16SC1, dy );
109 CvMat _src = cvMat( roi.height, roi.width, CV_8UC1, src );
110 cv::Ptr<cv::FilterEngine> pX, pY;
112 /* inner buffer of convolution process */
113 //char ConvBuffer[400];
118 /* check bad arguments */
120 return CV_NULLPTR_ERR;
121 if( (roi.height <= 0) || (roi.width <= 0) )
122 return CV_BADSIZE_ERR;
123 if( srcStep < roi.width )
124 return CV_BADSIZE_ERR;
126 return CV_NULLPTR_ERR;
128 return CV_BADSIZE_ERR;
130 return CV_NULLPTR_ERR;
132 return CV_NULLPTR_ERR;
134 return CV_NULLPTR_ERR;
135 if( coeffUsage != CV_VALUE && coeffUsage != CV_ARRAY )
136 return CV_BADFLAG_ERR;
137 if( (win.height <= 0) || (!(win.height & 1)))
138 return CV_BADSIZE_ERR;
139 if( (win.width <= 0) || (!(win.width & 1)))
140 return CV_BADSIZE_ERR;
142 invn = 1 / ((float) n);
144 if( scheme == _CV_SNAKE_GRAD )
146 pX = cv::createDerivFilter( CV_8U, CV_16S, 1, 0, 3, cv::BORDER_REPLICATE );
147 pY = cv::createDerivFilter( CV_8U, CV_16S, 0, 1, 3, cv::BORDER_REPLICATE );
148 gradient = (float *) cvAlloc( roi.height * roi.width * sizeof( float ));
150 map = (uchar *) cvAlloc( map_width * map_height );
151 /* clear map - no gradient computed */
152 memset( (void *) map, 0, map_width * map_height );
154 Econt = (float *) cvAlloc( neighbors * sizeof( float ));
155 Ecurv = (float *) cvAlloc( neighbors * sizeof( float ));
156 Eimg = (float *) cvAlloc( neighbors * sizeof( float ));
157 E = (float *) cvAlloc( neighbors * sizeof( float ));
166 /* compute average distance */
167 for( i = 1; i < n; i++ )
169 int diffx = pt[i - 1].x - pt[i].x;
170 int diffy = pt[i - 1].y - pt[i].y;
172 ave_d += cvSqrt( (float) (diffx * diffx + diffy * diffy) );
174 ave_d += cvSqrt( (float) ((pt[0].x - pt[n - 1].x) *
175 (pt[0].x - pt[n - 1].x) +
176 (pt[0].y - pt[n - 1].y) * (pt[0].y - pt[n - 1].y)));
179 /* average distance computed */
180 for( i = 0; i < n; i++ )
182 /* Calculate Econt */
186 float minEcont = _CV_SNAKE_BIG;
187 float minEcurv = _CV_SNAKE_BIG;
188 float minEimg = _CV_SNAKE_BIG;
189 float Emin = _CV_SNAKE_BIG;
196 int left = MIN( pt[i].x, win.width >> 1 );
197 int right = MIN( roi.width - 1 - pt[i].x, win.width >> 1 );
198 int upper = MIN( pt[i].y, win.height >> 1 );
199 int bottom = MIN( roi.height - 1 - pt[i].y, win.height >> 1 );
202 minEcont = _CV_SNAKE_BIG;
203 for( j = -upper; j <= bottom; j++ )
205 for( k = -left; k <= right; k++ )
212 diffx = pt[n - 1].x - (pt[i].x + k);
213 diffy = pt[n - 1].y - (pt[i].y + j);
217 diffx = pt[i - 1].x - (pt[i].x + k);
218 diffy = pt[i - 1].y - (pt[i].y + j);
220 Econt[(j + centery) * win.width + k + centerx] = energy =
221 (float) fabs( ave_d -
222 cvSqrt( (float) (diffx * diffx + diffy * diffy) ));
224 maxEcont = MAX( maxEcont, energy );
225 minEcont = MIN( minEcont, energy );
228 tmp = maxEcont - minEcont;
229 tmp = (tmp == 0) ? 0 : (1 / tmp);
230 for( k = 0; k < neighbors; k++ )
232 Econt[k] = (Econt[k] - minEcont) * tmp;
235 /* Calculate Ecurv */
237 minEcurv = _CV_SNAKE_BIG;
238 for( j = -upper; j <= bottom; j++ )
240 for( k = -left; k <= right; k++ )
247 tx = pt[n - 1].x - 2 * (pt[i].x + k) + pt[i + 1].x;
248 ty = pt[n - 1].y - 2 * (pt[i].y + j) + pt[i + 1].y;
250 else if( i == n - 1 )
252 tx = pt[i - 1].x - 2 * (pt[i].x + k) + pt[0].x;
253 ty = pt[i - 1].y - 2 * (pt[i].y + j) + pt[0].y;
257 tx = pt[i - 1].x - 2 * (pt[i].x + k) + pt[i + 1].x;
258 ty = pt[i - 1].y - 2 * (pt[i].y + j) + pt[i + 1].y;
260 Ecurv[(j + centery) * win.width + k + centerx] = energy =
261 (float) (tx * tx + ty * ty);
262 maxEcurv = MAX( maxEcurv, energy );
263 minEcurv = MIN( minEcurv, energy );
266 tmp = maxEcurv - minEcurv;
267 tmp = (tmp == 0) ? 0 : (1 / tmp);
268 for( k = 0; k < neighbors; k++ )
270 Ecurv[k] = (Ecurv[k] - minEcurv) * tmp;
274 for( j = -upper; j <= bottom; j++ )
276 for( k = -left; k <= right; k++ )
280 if( scheme == _CV_SNAKE_GRAD )
282 /* look at map and check status */
283 int x = (pt[i].x + k)/WTILE_SIZE;
284 int y = (pt[i].y + j)/WTILE_SIZE;
286 if( map[y * map_width + x] == 0 )
290 /* evaluate block location */
291 int upshift = y ? 1 : 0;
292 int leftshift = x ? 1 : 0;
293 int bottomshift = MIN( 1, roi.height - (y + 1)*WTILE_SIZE );
294 int rightshift = MIN( 1, roi.width - (x + 1)*WTILE_SIZE );
295 CvRect g_roi = { x*WTILE_SIZE - leftshift, y*WTILE_SIZE - upshift,
296 leftshift + WTILE_SIZE + rightshift, upshift + WTILE_SIZE + bottomshift };
298 cvGetSubArr( &_src, &_src1, g_roi );
300 cv::Mat _src_ = cv::cvarrToMat(&_src1);
301 cv::Mat _dx_ = cv::cvarrToMat(&_dx);
302 cv::Mat _dy_ = cv::cvarrToMat(&_dy);
304 pX->apply( _src_, _dx_, cv::Rect(0,0,-1,-1), cv::Point(), true );
305 pY->apply( _src_, _dy_, cv::Rect(0,0,-1,-1), cv::Point(), true );
307 for( l = 0; l < WTILE_SIZE + bottomshift; l++ )
309 for( m = 0; m < WTILE_SIZE + rightshift; m++ )
311 gradient[(y*WTILE_SIZE + l) * roi.width + x*WTILE_SIZE + m] =
312 (float) (dx[(l + upshift) * TILE_SIZE + m + leftshift] *
313 dx[(l + upshift) * TILE_SIZE + m + leftshift] +
314 dy[(l + upshift) * TILE_SIZE + m + leftshift] *
315 dy[(l + upshift) * TILE_SIZE + m + leftshift]);
318 map[y * map_width + x] = 1;
320 Eimg[(j + centery) * win.width + k + centerx] = energy =
321 gradient[(pt[i].y + j) * roi.width + pt[i].x + k];
325 Eimg[(j + centery) * win.width + k + centerx] = energy =
326 src[(pt[i].y + j) * srcStep + pt[i].x + k];
329 maxEimg = MAX( maxEimg, energy );
330 minEimg = MIN( minEimg, energy );
334 tmp = (maxEimg - minEimg);
335 tmp = (tmp == 0) ? 0 : (1 / tmp);
337 for( k = 0; k < neighbors; k++ )
339 Eimg[k] = (minEimg - Eimg[k]) * tmp;
342 /* locate coefficients */
343 if( coeffUsage == CV_VALUE)
356 /* Find Minimize point in the neighbors */
357 for( k = 0; k < neighbors; k++ )
359 E[k] = _alpha * Econt[k] + _beta * Ecurv[k] + _gamma * Eimg[k];
361 Emin = _CV_SNAKE_BIG;
362 for( j = -upper; j <= bottom; j++ )
364 for( k = -left; k <= right; k++ )
367 if( E[(j + centery) * win.width + k + centerx] < Emin )
369 Emin = E[(j + centery) * win.width + k + centerx];
376 if( offsetx || offsety )
383 converged = (moved == 0);
384 if( (criteria.type & CV_TERMCRIT_ITER) && (iteration >= criteria.max_iter) )
386 if( (criteria.type & CV_TERMCRIT_EPS) && (moved <= criteria.epsilon) )
395 if( scheme == _CV_SNAKE_GRAD )
405 cvSnakeImage( const IplImage* src, CvPoint* points,
406 int length, float *alpha,
407 float *beta, float *gamma,
408 int coeffUsage, CvSize win,
409 CvTermCriteria criteria, int calcGradient )
412 CV_FUNCNAME( "cvSnakeImage" );
420 if( src->nChannels != 1 )
421 CV_ERROR( CV_BadNumChannels, "input image has more than one channel" );
423 if( src->depth != IPL_DEPTH_8U )
424 CV_ERROR( CV_BadDepth, cvUnsupportedFormat );
426 cvGetRawData( src, &data, &step, &size );
428 IPPI_CALL( icvSnake8uC1R( data, step, size, points, length,
429 alpha, beta, gamma, coeffUsage, win, criteria,
430 calcGradient ? _CV_SNAKE_GRAD : _CV_SNAKE_IMAGE ));