📄 cvpyrsegmentation.cpp
字号:
size.height = roi.height >> level;
p_cur = pyram[level];
for( i = 0; i < size.height; i++, p_cur += size.width + 1 )
{
for( j = 0; j < size.width; j++ )
{
if( p_cur[j].a != 0 )
{
cmp_node.data = p_cur + j;
CV_WRITE_SEQ_ELEM( cmp_node, writer );
}
}
}
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 )
{
Cv32suf _c;
/* copy the segmented values to destination image */
_c.f = p_cur->c.blue; dst_image[j*3] = (uchar)_c.i;
_c.f = p_cur->c.green; dst_image[j*3+1] = (uchar)_c.i;
_c.f = p_cur->c.red; dst_image[j*3+2] = (uchar)_c.i;
p_cur = _CV_NEXT_BASE_C3(p_cur,1);
}
else
{
p_cur++;
}
}
if( l == 0 )
dst_image += dst_step;
}
}
M_END:
cvFree( &buffer );
cvReleaseMemStorage( &temp_storage );
if( status == CV_OK )
*dst_comp = res_seq;
return status;
}
static CvStatus icvUpdatePyrLinks_8u_C1
(int layer, void *layer_data, CvSize size, void *parent_layer,
void *_writer, float threshold, int is_last_iter, void *_stub, CvWriteNodeFunction /*func*/)
{
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++;
p_cur = (_CvPyramid*)((char*)p_cur + sizeof(_CvPyramidBase));
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 )
{
p_cur = _CV_NEXT_BASE_C1(p_cur,1);
}
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++;
p_cur = _CV_NEXT_BASE_C1(p_cur,1);
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 )
{
p_cur = _CV_NEXT_BASE_C1(p_cur,1);
}
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;
}
static CvStatus icvUpdatePyrLinks_8u_C3
(int layer, void *layer_data, CvSize size, void *parent_layer,
void *_writer, float threshold, int is_last_iter, void *_stub, CvWriteNodeFunction /*func*/)
{
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++;
p_cur = _CV_NEXT_BASE_C3(p_cur,1);
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 )
{
p_cur = _CV_NEXT_BASE_C3(p_cur,1);
}
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++;
p_cur = _CV_NEXT_BASE_C3(p_cur,1);
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 )
{
p_cur = _CV_NEXT_BASE_C3(p_cur,1);
}
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 = (int)((_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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -