cvhough.cpp.svn-base
来自「非结构化路识别」· SVN-BASE 代码 · 共 1,035 行 · 第 1/3 页
SVN-BASE
1,035 行
/* Starting additional analysis */
count = 0;
for( ri = 0; ri < rn; ri++ )
{
for( ti = 0; ti < tn; ti++ )
{
if( caccum[ri * tn + ti > threshold] )
{
count++;
}
}
}
if( count * 100 > rn * tn )
{
icvHoughLines_8uC1R( image_src, step, size, rho, theta,
threshold, lines, linesMax );
goto func_exit;
}
buffer = (uchar *) icvAlloc( (srn * stn + 2) * sizeof( uchar ));
mcaccum = buffer + 1;
count = 0;
for( ri = 0; ri < rn; ri++ )
{
for( ti = 0; ti < tn; ti++ )
{
if( caccum[ri * tn + ti] > threshold )
{
count++;
memset( mcaccum, 0, sfn * sizeof( uchar ));
for( index = 0; index < fn; index++ )
{
int ti2;
float r0;
yc = (float) y[index] + 0.5f;
xc = (float) x[index] + 0.5f;
/* Update the accumulator */
t = (float) fabs( icvFastArctan32f( yc, xc ) * d2r );
r = (float) sqrt( (double)xc * xc + (double)yc * yc ) * isrho;
ti0 = cvFloor( (t + Pi * 0.5f) * istheta );
ti2 = (ti * stn - ti0) * 5;
r0 = (float) ri *srn;
for( ti1 = 0 /*, phi = ti*theta - Pi/2 - t */ ; ti1 < stn; ti1++, ti2 += 5
/*phi += stheta */ )
{
/*rv = r*_cos(phi) - r0; */
rv = r * sinTable[(int) (abs( ti2 ))] - r0;
i = cvFloor( rv ) * stn + ti1;
i = CV_IMAX( i, -1 );
i = CV_IMIN( i, sfn );
mcaccum[i]++;
assert( i >= -1 );
assert( i <= sfn );
}
}
/* Find peaks in maccum... */
for( index = 0; index < sfn; index++ )
{
i = 0;
pos = h_get_tail_pos__index( list );
if( h_get_prev__index( &pos )->value < mcaccum[index] )
{
vi.value = mcaccum[index];
vi.rho = index / stn * srho + ri * rho;
vi.theta = index % stn * stheta + ti * theta - halfPi;
while( h_is_pos__index( pos ))
{
if( h_get__index( pos )->value > mcaccum[index] )
{
h_insert_after__index( list, pos, &vi );
if( h_get_count__index( list ) > linesMax )
{
h_remove_tail__index( list );
}
break;
}
h_get_prev__index( &pos );
}
if( !h_is_pos__index( pos ))
{
h_add_head__index( list, &vi );
if( h_get_count__index( list ) > linesMax )
{
h_remove_tail__index( list );
}
}
}
}
}
}
}
pos = h_get_head_pos__index( list );
if( h_get_count__index( list ) == 1 )
{
if( h_get__index( pos )->rho < 0 )
{
h_clear_list__index( list );
}
}
else
{
while( h_is_pos__index( pos ))
{
CvLinePolar line;
pindex = h_get__index( pos );
if( pindex->rho < 0 )
{
/* This should be the last element... */
h_get_next__index( &pos );
assert( !h_is_pos__index( pos ));
break;
}
line.rho = pindex->rho;
line.angle = pindex->theta;
cvSeqPush( lines, &line );
if( lines->total >= linesMax )
goto func_exit;
h_get_next__index( &pos );
}
}
func_exit:
h_destroy_list__index( list );
icvFree( &sinTable );
icvFree( &x );
icvFree( &y );
icvFree( &caccum );
icvFree( &buffer );
return CV_OK;
}
/****************************************************************************************\
* Probabilistic Hough Transform *
\****************************************************************************************/
#define _PHOUGH_SIN_TABLE
static CvStatus icvHoughLinesP_8uC1R( uchar * image_src, int step, CvSize size,
float rho, float theta, int threshold,
int lineLength, int lineGap,
CvSeq *lines, int linesMax )
{
#define _POINT(row, column)\
(image_src[(row)*step+(column)])
int *map = 0;
int rn, tn; /* number of rho and theta discrete values */
#define ROUNDR(x) cvFloor(x)
#define ROUNDT(x) cvFloor(x)
#ifdef _PHOUGH_SIN_TABLE
#define SIN(a) sinTable[a]
#else
#define SIN(a) sin((a)*theta)
#endif
int *iaccum = 0;
uchar *caccum = 0;
int imaccum;
uchar cmaccum;
int *x = 0;
int *y = 0;
int index, i;
int ri, ri1, ti, ti1, ti0;
int halftn;
int row, col;
float r, t; /* Current rho and theta */
float rv; /* Some temporary rho value */
float irho;
float itheta;
int w = size.width;
int h = size.height;
int fn = 0;
float xc, yc;
const float d2r = (float)(Pi / 180);
int fpn = 0;
float *sinTable = 0;
CvRandState state;
if( linesMax <= 0 )
return CV_BADSIZE_ERR;
if( rho <= 0 || theta <= 0 )
return CV_BADARG_ERR;
irho = 1 / rho;
itheta = 1 / theta;
rn = cvFloor( sqrt( (double)w * w + (double)h * h ) * irho );
tn = cvFloor( 2 * Pi * itheta );
halftn = cvFloor( Pi * itheta );
/* Allocating memory for the accumulator ad initializing it */
if( threshold > 255 )
{
iaccum = (int *) icvAlloc( rn * tn * sizeof( int ));
memset( iaccum, 0, rn * tn * sizeof( int ));
}
else
{
caccum = (uchar *) icvAlloc( rn * tn * sizeof( uchar ));
memset( caccum, 0, rn * tn * sizeof( uchar ));
}
/* Counting all feature pixels */
for( row = 0; row < h; row++ )
{
for( col = 0; col < w; col++ )
{
fn += !!_POINT( row, col );
}
}
x = (int *) icvAlloc( fn * sizeof( int ));
y = (int *) icvAlloc( fn * sizeof( int ));
map = (int *) icvAlloc( w * h * sizeof( int ));
memset( map, -1, w * h * sizeof( int ));
#ifdef _PHOUGH_SIN_TABLE
sinTable = (float *) icvAlloc( tn * sizeof( float ));
for( ti = 0; ti < tn; ti++ )
{
sinTable[ti] = _sin( ti * theta );
}
#endif
index = 0;
for( row = 0; row < h; row++ )
{
for( col = 0; col < w; col++ )
{
if( _POINT( row, col ))
{
x[index] = col;
y[index] = row;
map[row * w + col] = index;
index++;
}
}
}
/* Starting Hough Transform */
cvRandInit( &state, 0, 1, -1, CV_RAND_UNI ); /* Initializing random counter */
while( fn != 0 )
{
int temp;
int index0;
int cl; /* Counter of length of lines of feature pixels */
int cg; /* Counter of gaps length in lines of feature pixels */
float dx = 1.0f, dy = 0.0f, ax, ay;
float msx = 0, msy = 0, mex = 0, mey = 0, mdx = 1.0f, mdy = 0.0f;
int ml;
/* The x, y and length of a line (remember the maximum length) */
float curx = 0, cury = 0;
int ox, oy; /* Rounded ax and ay */
#define _EXCHANGE(x1, x2) temp = x1;x1 = x2;x2 = temp
/* Select a pixel randomly */
index0 = cvRandNext(&state) % fn;
/* Remove the pixel from the feature points set */
if( index0 != fn - 1 )
{
/* Exchange the point with the last one */
_EXCHANGE( x[index0], x[fn - 1] );
_EXCHANGE( y[index0], y[fn - 1] );
_EXCHANGE( map[y[index0] * w + x[index0]], map[y[fn - 1] * w + x[fn - 1]] );
}
fn--;
fpn++;
yc = (float) y[fn] + 0.5f;
xc = (float) x[fn] + 0.5f;
/* Update the accumulator */
t = (float) fabs( icvFastArctan32f( yc, xc ) * d2r );
r = (float) sqrt( (double)xc * xc + (double)yc * yc );
ti0 = ROUNDT( t * itheta );
/* ti1 = 0 */
if( threshold > 255 )
{
rv = 0.0f;
ri1 = 0;
i = ti0;
iaccum[i]++;
imaccum = iaccum[i];
ri = ri1;
ti = ti0;
for( ti1 = 1; ti1 < halftn; ti1++ )
{
rv = r * SIN( ti1 );
ri1 = ROUNDR( rv * irho );
i = ri1 * tn + ti1 + ti0;
iaccum[i]++;
if( imaccum < iaccum[i] )
{
imaccum = iaccum[i];
ri = ri1;
ti = ti1 + ti0;
}
}
r = ri * rho + rho / 2;
t = ti * theta + theta / 2;
if( iaccum[ri * tn + ti] < threshold )
{
continue;
}
/* Unvote all the pixels from the detected line */
iaccum[ri * tn + ti] = 0;
}
else
{
rv = 0.0f;
ri1 = 0;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?