📄 cvpgh.cpp
字号:
return CV_OUTOFMEM_ERR;
}
cvStartReadSeq( contour, &reader1, 0 );
cvStartReadSeq( contour, &reader2, 0 );
/* calc & store squared edge lengths, calculate maximal distance between edges */
for( i = 0; i < count; i++ )
{
CvPoint pt1, pt2;
double dx, dy;
CV_READ_EDGE( pt1, pt2, reader1 );
dx = pt2.x - pt1.x;
dy = pt2.y - pt1.y;
buffer[i] = (float)(1./sqrt(dx * dx + dy * dy));
}
/*
do 2 passes.
First calculates maximal distance.
Second calculates histogram itself.
*/
for( pass = 1; pass <= 2; pass++ )
{
double dist_coeff = 0, angle_coeff = 0;
/* run external loop */
for( i = 0; i < count; i++ )
{
CvPoint pt1, pt2;
int dx, dy;
int dist = 0;
CV_READ_EDGE( pt1, pt2, reader1 );
dx = pt2.x - pt1.x;
dy = pt2.y - pt1.y;
if( (dx | dy) != 0 )
{
int j;
if( pass == 2 )
{
dist_coeff = buffer[i] * dist_scale;
angle_coeff = buffer[i] * (_CV_ACOS_TABLE_SIZE / 2);
}
/* run internal loop (for current edge) */
for( j = 0; j < count; j++ )
{
CvPoint pt3, pt4;
CV_READ_EDGE( pt3, pt4, reader2 );
if( i != j ) /* process edge pair */
{
int d1 = (pt3.y - pt1.y) * dx - (pt3.x - pt1.x) * dy;
int d2 = (pt4.y - pt1.y) * dx - (pt2.x - pt1.x) * dy;
int cross_flag;
int *hist_row = 0;
if( pass == 2 )
{
int dp = (pt4.x - pt3.x) * dx + (pt4.y - pt3.y) * dy;
dp = cvRound( dp * angle_coeff * buffer[j] ) +
(_CV_ACOS_TABLE_SIZE / 2);
dp = MAX( dp, 0 );
dp = MIN( dp, _CV_ACOS_TABLE_SIZE - 1 );
hist_row = pghi + dist_dim *
cvRound( icv_acos_table[dp] * angle_scale );
d1 = cvRound( d1 * dist_coeff );
d2 = cvRound( d2 * dist_coeff );
}
cross_flag = (d1 ^ d2) < 0;
d1 = CV_IABS( d1 );
d2 = CV_IABS( d2 );
if( pass == 2 )
{
if( d1 >= dist_dim )
d1 = dist_dim - 1;
if( d2 >= dist_dim )
d2 = dist_dim - 1;
if( !cross_flag )
{
if( d1 > d2 ) /* make d1 <= d2 */
{
d1 ^= d2;
d2 ^= d1;
d1 ^= d2;
}
for( ; d1 <= d2; d1++ )
hist_row[d1]++;
}
else
{
for( ; d1 >= 0; d1-- )
hist_row[d1]++;
for( ; d2 >= 0; d2-- )
hist_row[d2]++;
}
}
else /* 1st pass */
{
d1 = CV_IMAX( d1, d2 );
dist = CV_IMAX( dist, d1 );
}
} /* end of processing of edge pair */
} /* end of internal loop */
if( pass == 1 )
{
double scale = dist * buffer[i];
dist_scale = MAX( dist_scale, scale );
}
}
} /* end of external loop */
if( pass == 1 )
{
dist_scale = (dist_dim - 0.51) / dist_scale;
}
} /* end of pass on loops */
/* convert hist to floats */
for( i = 0; i < hist_size; i++ )
{
((float *) pghi)[i] = (float) pghi[i];
}
if( buffer != local_buffer_ptr )
cvFree( &buffer );
return CV_OK;
}
CV_IMPL void
cvCalcPGH( const CvSeq * contour, CvHistogram * hist )
{
CV_FUNCNAME( "cvCalcPGH" );
__BEGIN__;
int size[CV_MAX_DIM];
int dims;
if( !CV_IS_HIST(hist))
CV_ERROR( CV_StsBadArg, "The histogram header is invalid " );
if( CV_IS_SPARSE_HIST( hist ))
CV_ERROR( CV_StsUnsupportedFormat, "Sparse histogram are not supported" );
dims = cvGetDims( hist->bins, size );
if( dims != 2 )
CV_ERROR( CV_StsBadSize, "The histogram must be two-dimensional" );
if( !CV_IS_SEQ_POLYGON( contour ) || CV_SEQ_ELTYPE( contour ) != CV_32SC2 )
CV_ERROR( CV_StsUnsupportedFormat, "The contour is not valid or the point type is not supported" );
IPPI_CALL( icvCalcPGH( contour, ((CvMatND*)(hist->bins))->data.fl, size[0], size[1] ));
__END__;
}
/* End of file. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -