cvpyrsegmentation.cpp.svn-base
来自「非结构化路识别」· SVN-BASE 代码 · 共 1,877 行 · 第 1/4 页
SVN-BASE
1,877 行
}
}
cvEndWriteSeq( &writer );
/* clusterization segmented components and construction
output connected components */
icvSegmentClusterC3( cmp_seq, res_seq, threshold2, pyram[1], roi );
/* convert (inplace) resultant segment values to int (top level) */
/* propagate segment values top down */
for( l = level - 1; l >= 0; l-- )
{
p_cur = pyram[l];
size.width <<= 1;
size.height <<= 1;
if( l == 0 )
{
size.width--;
size.height--;
}
for( i = 0; i <= size.height; i++ )
{
for( j = 0; j <= size.width; j++ )
{
_CvPyramidC3 *p = p_cur->p;
assert( p != 0 );
if( p != &stub )
{
p_cur->c = p->c;
}
if( l == 0 )
{
/* copy the segmented values to destination image */
dst_image[j * 3] = (uchar) * (int *) &(p_cur->c.blue);
dst_image[j * 3 + 1] = (uchar) * (int *) &(p_cur->c.green);
dst_image[j * 3 + 2] = (uchar) * (int *) &(p_cur->c.red);
*(char **) &p_cur += sizeof( _CvPyramidBaseC3 );
}
else
{
p_cur++;
}
}
if( l == 0 )
dst_image += dst_step;
}
}
M_END:
icvFree( &buffer );
cvReleaseMemStorage( &temp_storage );
if( status == CV_OK )
*dst_comp = res_seq;
return status;
}
IPCVAPI_IMPL( CvStatus, icvUpdatePyrLinks_8u_C1, (int layer,
void *layer_data,
CvSize size,
void *parent_layer,
void *_writer,
float threshold,
int is_last_iter,
void *_stub,
ICVWriteNodeFunction ))
{
int i, j;
_CvListNode cmp_node;
_CvPyramid *stub = (_CvPyramid *) _stub;
_CvPyramid *p_cur = (_CvPyramid *) layer_data;
_CvPyramid *p_next1 = (_CvPyramid *) parent_layer;
_CvPyramid *p_next3 = p_next1 + (size.width >> 1) + 1;
CvSeqWriter & writer = *(CvSeqWriter *) _writer;
for( i = 0; i < size.height; i++ )
{
for( j = 0; j < size.width; j += 2 )
{
float c0, c1, c2, c3, c4;
_CvPyramid *p;
/* son-father threshold linking for the current node establish */
c0 = p_cur->c;
/* find pointer for the first pixel */
c1 = (float) fabs( c0 - p_next1[0].c );
c2 = (float) fabs( c0 - p_next1[1].c );
c3 = (float) fabs( c0 - p_next3[0].c );
c4 = (float) fabs( c0 - p_next3[1].c );
p = p_next1;
if( c1 > c2 )
{
p = p_next1 + 1;
c1 = c2;
}
if( c1 > c3 )
{
p = p_next3;
c1 = c3;
}
if( c1 > c4 )
{
p = p_next3 + 1;
c1 = c4;
}
if( c1 <= threshold )
{
p_cur->p = p;
if( layer == 0 )
{
p->a++;
(*(_CvPyramidBase **) & p_cur)++;
if( is_last_iter )
icvMaxRoi1( &(p->rect), j, i );
}
else
{
int a = p_cur->a;
p->a += a;
p_cur->c = 0;
p_cur++;
if( is_last_iter && a != 0 )
icvMaxRoi( &(p->rect), &(p_cur[-1].rect) );
}
}
else
{
p_cur->p = stub;
if( is_last_iter )
{
cmp_node.data = p_cur;
CV_WRITE_SEQ_ELEM( cmp_node, writer );
}
if( layer == 0 )
{
(*(_CvPyramidBase **) & p_cur)++;
}
else
{
p_cur->c = 0;
p_cur++;
}
}
/* find pointer for the second pixel */
c0 = p_cur->c;
c1 = (float) fabs( c0 - p_next1[0].c );
c2 = (float) fabs( c0 - p_next1[1].c );
c3 = (float) fabs( c0 - p_next3[0].c );
c4 = (float) fabs( c0 - p_next3[1].c );
p = p_next1;
p_next1++;
if( c1 > c2 )
{
p = p_next1;
c1 = c2;
}
if( c1 > c3 )
{
p = p_next3;
c1 = c3;
}
p_next3++;
if( c1 > c4 )
{
p = p_next3;
c1 = c4;
}
if( c1 <= threshold )
{
p_cur->p = p;
if( layer == 0 )
{
p->a++;
(*(_CvPyramidBase **) & p_cur)++;
if( is_last_iter )
icvMaxRoi1( &(p->rect), j + 1, i );
}
else
{
int a = p_cur->a;
p->a += a;
p_cur->c = 0;
p_cur++;
if( is_last_iter && a != 0 )
icvMaxRoi( &(p->rect), &(p_cur[-1].rect) );
}
}
else
{
p_cur->p = stub;
if( is_last_iter )
{
cmp_node.data = p_cur;
CV_WRITE_SEQ_ELEM( cmp_node, writer );
}
if( layer == 0 )
{
(*(_CvPyramidBase **) & p_cur)++;
}
else
{
p_cur->c = 0;
p_cur++;
}
}
}
/* clear c's */
if( layer > 0 )
{
p_cur->c = 0;
p_cur++;
}
if( !(i & 1) )
{
p_next1 -= size.width >> 1;
p_next3 -= size.width >> 1;
}
else
{
p_next1++;
p_next3++;
}
}
return CV_OK;
}
IPCVAPI_IMPL( CvStatus, icvUpdatePyrLinks_8u_C3, (int layer,
void *layer_data,
CvSize size,
void *parent_layer,
void *_writer,
float threshold,
int is_last_iter,
void *_stub,
ICVWriteNodeFunction ))
{
int i, j;
_CvListNode cmp_node;
_CvPyramidC3 *stub = (_CvPyramidC3 *) _stub;
_CvPyramidC3 *p_cur = (_CvPyramidC3 *) layer_data;
_CvPyramidC3 *p_next1 = (_CvPyramidC3 *) parent_layer;
_CvPyramidC3 *p_next3 = p_next1 + (size.width >> 1) + 1;
CvSeqWriter & writer = *(CvSeqWriter *) _writer;
for( i = 0; i < size.height; i++ )
{
for( j = 0; j < size.width; j += 2 )
{
float c1, c2, c3, c4;
_CvPyramidC3 *p;
/* find pointer for the first pixel */
c1 = _CV_RGB_DIST( p_cur->c, p_next1[0].c );
c2 = _CV_RGB_DIST( p_cur->c, p_next1[1].c );
c3 = _CV_RGB_DIST( p_cur->c, p_next3[0].c );
c4 = _CV_RGB_DIST( p_cur->c, p_next3[1].c );
p = p_next1;
if( c1 > c2 )
{
p = p_next1 + 1;
c1 = c2;
}
if( c1 > c3 )
{
p = p_next3;
c1 = c3;
}
if( c1 > c4 )
{
p = p_next3 + 1;
c1 = c4;
}
if( c1 < threshold )
{
p_cur->p = p;
if( layer == 0 )
{
p->a++;
(*(_CvPyramidBaseC3 **) & p_cur)++;
if( is_last_iter )
icvMaxRoi1( &(p->rect), j, i );
}
else
{
int a = p_cur->a;
p->a += a;
p_cur->c.blue = p_cur->c.green = p_cur->c.red = 0;
p_cur++;
if( is_last_iter && a != 0 )
icvMaxRoi( &(p->rect), &(p_cur[-1].rect) );
}
}
else
{
p_cur->p = stub;
if( is_last_iter /* && ( == 0 || p_cur->a != 0) */ )
{
cmp_node.data = p_cur;
CV_WRITE_SEQ_ELEM( cmp_node, writer );
}
if( layer == 0 )
{
(*(_CvPyramidBaseC3 **) & p_cur)++;
}
else
{
p_cur->c.blue = p_cur->c.green = p_cur->c.red = 0;
p_cur++;
}
}
/* find pointer for the second pixel */
c1 = _CV_RGB_DIST( p_cur->c, p_next1[0].c );
c2 = _CV_RGB_DIST( p_cur->c, p_next1[1].c );
c3 = _CV_RGB_DIST( p_cur->c, p_next3[0].c );
c4 = _CV_RGB_DIST( p_cur->c, p_next3[1].c );
p = p_next1;
p_next1++;
if( c1 > c2 )
{
p = p_next1;
c1 = c2;
}
if( c1 > c3 )
{
p = p_next3;
c1 = c3;
}
p_next3++;
if( c1 > c4 )
{
p = p_next3;
c1 = c4;
}
if( c1 < threshold )
{
p_cur->p = p;
if( layer == 0 )
{
p->a++;
(*(_CvPyramidBaseC3 **) & p_cur)++;
if( is_last_iter )
icvMaxRoi1( &(p->rect), j + 1, i );
}
else
{
int a = p_cur->a;
p->a += a;
p_cur->c.blue = p_cur->c.green = p_cur->c.red = 0;
p_cur++;
if( is_last_iter && a != 0 )
icvMaxRoi( &(p->rect), &(p_cur[-1].rect) );
}
}
else
{
p_cur->p = stub;
if( is_last_iter /* && ( == 0 || p_cur->a != 0) */ )
{
cmp_node.data = p_cur;
CV_WRITE_SEQ_ELEM( cmp_node, writer );
}
if( layer == 0 )
{
(*(_CvPyramidBaseC3 **) & p_cur)++;
}
else
{
p_cur->c.blue = p_cur->c.green = p_cur->c.red = 0;
p_cur++;
}
}
}
/* clear c's */
if( layer > 0 )
{
p_cur->c.blue = p_cur->c.green = p_cur->c.red = 0;
p_cur++;
}
if( !(i & 1) )
{
p_next1 -= size.width >> 1;
p_next3 -= size.width >> 1;
}
else
{
p_next1++;
p_next3++;
}
}
return CV_OK;
}
/****************************************************************************************\
clusterization segmented components
\****************************************************************************************/
static void
icvExpandBaseLevelC1( _CvPyramid * base_p, _CvPyramid * p, _CvPyramidBase * start, int width )
{
int x = (_CvPyramidBase *) base_p - start;
int y = x / width;
x -= y * width;
p->a = 1;
p->rect.x1 = (ushort) x;
p->rect.y1 = (ushort) y;
p->rect.x2 = (ushort) (x + 1);
p->rect.y2 = (ushort) (y + 1);
p->c = base_p->c;
}
CvStatus
icvSegmentClusterC1( CvSeq * cmp_seq, CvSeq * res_seq,
double threshold, _CvPyramid * first_level_end, CvSize first_level_size )
{
const double eps = 1.;
CvSeqWriter writer;
CvSeqReader reader;
_CvPyramid temp_cmp;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?