29 #ifndef __LABELING_H__ 30 #define __LABELING_H__ 38 #define CLEAR_DST_BUFFER 1 39 #define CLEAR_ALL_DST_BUFFER 0 40 #define CALC_CENTER_OF_GRAVITY 1 42 template<
class SrcT,
class DstT>
56 const int n_y,
const SrcT n_source_value )
57 : left_x( n_left_x ), right_x( n_right_x ), y( n_y ),
58 source_value( n_source_value )
121 s << rs.
LeftX() <<
" " 131 typedef typename std::list<RasterSegment *>::iterator
RSPIterator;
148 #if CALC_CENTER_OF_GRAVITY 156 raster_segment_list.clear();
162 for ( rspi = raster_segment_list.begin();
163 rspi != raster_segment_list.end(); rspi++ ) {
167 raster_segment_list.erase( raster_segment_list.begin(),
168 raster_segment_list.end());
183 num_of_pixels = n_num_of_pixels;
216 const int n_max_x,
const int n_max_y )
218 SetMin( n_min_x, n_min_y );
219 SetMax( n_max_x, n_max_y );
220 SetCenter(( n_min_x + n_max_x ) / 2.0
f,
221 ( n_min_y + n_max_y ) / 2.0
f );
222 SetSize( n_max_x - n_min_x + 1, n_max_y - n_min_y + 1 );
235 source_value = n_source_value;
249 return num_of_pixels;
310 raster_segment_list.push_front( rs );
316 RSPIterator rspi = raster_segment_list.begin();
318 raster_segment_list.erase( rspi );
324 return raster_segment_list.size();
345 s <<
"center: " << cx <<
"," << cy << std::endl;
348 s <<
"size: " << x <<
"," << y << std::endl;
351 s <<
"min: " << x <<
"," << y << std::endl;
354 s <<
"max: " << x <<
"," << y << std::endl;
356 #if CALC_CENTER_OF_GRAVITY 358 s <<
"center_of_graivty: " << cx <<
"," << cy << std::endl;
361 s <<
"source_value: " 364 << static_cast<int>( ri.
GetResult()) << std::endl;
399 const int y,
const SrcT src_value )
403 raster_segment_list[
y ].push_back( rs );
404 num_of_raster_segments++;
410 RSPList *rspl_p = &raster_segment_list[ rs_seed->
Y() + dy ];
413 int rs_seed_lx = rs_seed->
LeftX();
414 int rs_seed_rx = rs_seed->
RightX();
417 rspi = rspl_p->begin();
420 if ( rspi == rspl_p->end()) {
424 while (( *rspi )->RightX() < rs_seed_lx ) {
426 if ( rspi == rspl_p->end()) {
431 while (( rs = *rspi )->LeftX() <= rs_seed_rx ) {
433 rspi = rspl_p->erase( rspi );
434 seed_queue.push( rs );
438 if ( rspi == rspl_p->end()) {
446 while ( rspi != rspl_p->end()) {
449 && rs_seed_lx <= rs->
RightX()
450 && rs_seed_rx >= rs->
LeftX()) {
451 rspi = rspl_p->erase( rspi );
452 seed_queue.push( rs );
462 const DstT region_num )
466 int num_of_pixels = 0;
471 min_x = rs_seed->
LeftX();
472 max_x = rs_seed->
RightX();
473 min_y = max_y = rs_seed->
Y();
476 #if CALC_CENTER_OF_GRAVITY 481 seed_queue.push( rs_seed );
483 while ( seed_queue.size() > 0 ) {
490 if ( rs->
LeftX() < min_x ) {
493 if ( rs->
RightX() > max_x ) {
496 if ( rs->
Y() < min_y ) {
498 }
else if ( rs->
Y() > max_y ) {
501 #if CALC_CENTER_OF_GRAVITY 503 sum_y += rs->
Y() * n;
509 if ( rs->
Y() < height - 1 ) {
515 ri->
SetMinMax( min_x, min_y, max_x, max_y );
518 #if CALC_CENTER_OF_GRAVITY 519 float gx =
static_cast<float>( sum_x ) / ( 2 * num_of_pixels );
520 float gy =
static_cast<float>( sum_y ) / num_of_pixels;
547 DstT *sp = dst_frame + rs->
LeftX() + rs->
Y() *
width;
548 for (
int i = 0; i < rs->
RightX() - rs->
LeftX() + 1; i++ ) {
554 for ( RSPIterator rspi = rspl.begin(); rspi != rspl.end(); rspi++ ) {
556 int lx = rs->
LeftX();
559 DstT *sp = dst_frame + lx + y *
width;
560 for (
int i = 0; i < ( rx - lx + 1 ); i++ ) {
583 return result_region_info[ num ];
588 raster_segment_list = 0;
589 region_info_list.clear();
590 result_region_info.clear();
595 for ( RIPIterator ripi = region_info_list.begin();
596 ripi != region_info_list.end(); ripi++ ) {
600 region_info_list.erase( region_info_list.begin(),
601 region_info_list.end());
602 result_region_info.clear();
605 #define CHECK_FOR_PHASE1 0 606 #define CHECK_FOR_PHASE2 0 610 int target_width,
int target_height,
611 const bool is_sort_region,
612 const int region_size_min )
617 width = target_width;
618 height = target_height;
619 total_num = width *
height;
623 for ( RIPIterator ripi = region_info_list.begin();
624 ripi != region_info_list.end(); ripi++ ) {
628 region_info_list.erase( region_info_list.begin(),
629 region_info_list.end());
630 result_region_info.clear();
632 raster_segment_list =
new RSPList[
height ];
633 num_of_raster_segments = 0;
639 #if ( CLEAR_DST_BUFFER || CLEAR_ALL_DST_BUFFER ) 642 if ( src_frame != reinterpret_cast<SrcT *>( dst_frame )) {
643 #if CLEAR_ALL_DST_BUFFER 644 for (
int i = 0; i < width *
height; i++ ) {
650 int current_src_value = 0;
652 if ( *p != current_src_value ) {
653 if ( current_src_value != 0 ) {
656 current_src_value = *
p;
659 #if ( CLEAR_DST_BUFFER && !CLEAR_ALL_DST_BUFFER ) 667 if ( current_src_value != 0 ) {
674 int current_src_value = 0;
676 if ( *p != current_src_value ) {
677 if ( current_src_value != 0 ) {
680 current_src_value = *
p;
685 if ( current_src_value != 0 ) {
693 cout <<
y <<
":" << raster_segment_list[
y ].size() << endl;
694 RSPList *rspl_p = &raster_segment_list[
y ];
696 for ( i = rspl_p->begin(); i != rspl_p->end(); i++ ) {
701 cout <<
"num_of_raster_segments: " << num_of_raster_segments << endl;
706 region_info_list.clear();
712 RSPList *rspl_p = &raster_segment_list[
y ];
713 while ( rspl_p->size() > 0 ) {
714 RSPIterator rspi = rspl_p->begin();
716 rspl_p->erase( rspi );
719 num_of_regions + 1 );
720 region_info_list.push_back( rip );
727 if ( !raster_segment_list[
y ].empty()) {
728 cout <<
"mmmm" <<
y << endl;
733 for ( RIPIterator ripi = region_info_list.begin();
734 ripi != region_info_list.end(); ripi++ ) {
743 cout <<
"num_of_pixels: " << n_p << endl;
744 cout <<
"num_of_regions: " << num_of_regions << endl;
750 result_region_info.resize( num_of_regions );
752 for ( RIPIterator ripi = region_info_list.begin();
753 ripi != region_info_list.end(); ripi++ ) {
754 result_region_info[ n ] = *ripi;
758 if ( is_sort_region ) {
761 sort( result_region_info.begin(), result_region_info.end(),
767 if ( is_sort_region && region_size_min > 0 ) {
769 while ( n < num_of_regions
770 && result_region_info[ n ]->GetNumOfPixels()
771 >= region_size_min ) {
772 result_region_info[ n ]->SetResult( n + 1 );
775 num_of_result_regions = n;
777 result_region_info[ i ]->SetResult( 0 );
781 result_region_info[ i ]->SetResult( i + 1 );
807 #endif // __LABELING_H__
int GetNumOfResultRegions(void) const
void GetSize(int &x, int &y) const
SrcT GetSourceValue(void) const
RIPVector result_region_info
RSPList raster_segment_list
void SetResult(const DstT n_result)
int Exec(SrcT *target, DstT *result, int target_width, int target_height, const bool is_sort_region, const int region_size_min)
std::list< RasterSegment * > RSPList
DstT GetResult(void) const
Labeling< unsigned char, short > LabelingBS
Labeling< unsigned char, short >::RegionInfo RegionInfoBS
void Pop(RasterSegment *&rs)
RasterSegment(const int n_left_x, const int n_right_x, const int n_y, const SrcT n_source_value)
int num_of_result_regions
int GetRightX(void) const
void GetCenter(float &x, float &y) const
void SetMinMax(const int n_min_x, const int n_min_y, const int n_max_x, const int n_max_y)
void SetSize(const int x, const int y)
RSPList & GetRasterSegmentList(void)
int GetNumOfPixels(void) const
void RegisterSegment(const int lx, const int rx, const int y, const SrcT src_value)
static bool RevCompRegionInfoPointer(const RegionInfo *const &l, const RegionInfo *const &r)
int num_of_raster_segments
RegionInfo * GetResultRegionInfo(const int num) const
std::queue< RasterSegment * > RSPQueue
void SetCenterOfGravity(const float x, const float y)
friend std::ostream & operator<<(std::ostream &s, RasterSegment &rs)
void SetNumOfPixels(const int n_num_of_pixels)
void SetCenter(const float x, const float y)
std::list< RegionInfo * >::iterator RIPIterator
void SetMax(const int x, const int y)
RegionInfo * ConnectRasterSegment(RasterSegment *rs_seed, const DstT region_num)
std::list< RegionInfo * > RIPList
Labeling< short, short >::RegionInfo RegionInfoSS
RSPList * raster_segment_list
void Push(RasterSegment *rs)
std::vector< RegionInfo * > RIPVector
void SetSourceValue(const SrcT n_source_value)
static const int DEFAULT_REGION_SIZE_MIN
void SearchNeighboringSegment(RasterSegment *rs_seed, const int dy)
std::list< RasterSegment * >::iterator RSPIterator
int GetNumOfRasterSegments(void)
void FillFrame(RegionInfo *ri, const DstT fill_value)
void GetCenterOfGravity(float &x, float &y) const
void GetMax(int &x, int &y) const
SrcT SourceValue(void) const
int GetNumOfRegions(void) const
SrcT GetSourceValue(void) const
Labeling< short, short > LabelingSS
void SetMin(const int x, const int y)
void GetMin(int &x, int &y) const