Skip to content

Commit 2087d46

Browse files
committed
using Rect for storing the bounding box instead of an integer array
1 parent ca6b30a commit 2087d46

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

modules/objdetect/include/opencv2/objdetect/erfilter.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ struct CV_EXPORTS ERStat
7676
int area;
7777
int perimeter;
7878
int euler; //!< euler number
79-
int bbox[4];
79+
Rect rect;
8080
double raw_moments[2]; //!< order 1 raw moments to derive the centroid
8181
double central_moments[3]; //!< order 2 central moments to construct the covariance matrix
8282
std::deque<int> *crossings;//!< horizontal crossings

modules/objdetect/src/erfilter.cpp

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ ERStat::ERStat(int init_level, int init_pixel, int init_x, int init_y) : pixel(i
5353
parent(0), child(0), next(0), prev(0), local_maxima(0),
5454
max_probability_ancestor(0), min_probability_ancestor(0)
5555
{
56-
bbox[0] = init_x;
57-
bbox[1] = init_y;
58-
bbox[2] = init_x;
59-
bbox[3] = init_y;
56+
rect = Rect(init_x,init_y,1,1);
6057
raw_moments[0] = 0.0;
6158
raw_moments[1] = 0.0;
6259
central_moments[0] = 0.0;
@@ -543,21 +540,25 @@ void ERFilterNM::er_add_pixel(ERStat *parent, int x, int y, int non_border_neigh
543540

544541
if (parent->crossings->size()>0)
545542
{
546-
if (y<parent->bbox[1]) parent->crossings->push_front(2);
547-
else if (y>parent->bbox[3]) parent->crossings->push_back(2);
543+
if (y<parent->rect.y) parent->crossings->push_front(2);
544+
else if (y>parent->rect.br().y-1) parent->crossings->push_back(2);
548545
else {
549-
parent->crossings->at(y - parent->bbox[1]) += 2-2*non_border_neighbours_horiz;
546+
parent->crossings->at(y - parent->rect.y) += 2-2*non_border_neighbours_horiz;
550547
}
551548
} else {
552549
parent->crossings->push_back(2);
553550
}
554551

555552
parent->euler += (d_C1 - d_C2 + 2*d_C3) / 4;
556553

557-
parent->bbox[0] = min(parent->bbox[0],x);
558-
parent->bbox[1] = min(parent->bbox[1],y);
559-
parent->bbox[2] = max(parent->bbox[2],x);
560-
parent->bbox[3] = max(parent->bbox[3],y);
554+
int new_x1 = min(parent->rect.x,x);
555+
int new_y1 = min(parent->rect.y,y);
556+
int new_x2 = max(parent->rect.br().x-1,x);
557+
int new_y2 = max(parent->rect.br().y-1,y);
558+
parent->rect.x = new_x1;
559+
parent->rect.y = new_y1;
560+
parent->rect.width = new_x2-new_x1+1;
561+
parent->rect.height = new_y2-new_y1+1;
561562

562563
parent->raw_moments[0] += x;
563564
parent->raw_moments[1] += y;
@@ -574,29 +575,34 @@ void ERFilterNM::er_merge(ERStat *parent, ERStat *child)
574575
parent->area += child->area;
575576

576577
parent->perimeter += child->perimeter;
578+
577579

578-
for (int i=parent->bbox[1]; i<=min(parent->bbox[3],child->bbox[3]); i++)
579-
if (i-child->bbox[1] >= 0)
580-
parent->crossings->at(i-parent->bbox[1]) += child->crossings->at(i-child->bbox[1]);
580+
for (int i=parent->rect.y; i<=min(parent->rect.br().y-1,child->rect.br().y-1); i++)
581+
if (i-child->rect.y >= 0)
582+
parent->crossings->at(i-parent->rect.y) += child->crossings->at(i-child->rect.y);
581583

582-
for (int i=parent->bbox[1]-1; i>=child->bbox[1]; i--)
583-
if (i-child->bbox[1] < (int)child->crossings->size())
584-
parent->crossings->push_front(child->crossings->at(i-child->bbox[1]));
584+
for (int i=parent->rect.y-1; i>=child->rect.y; i--)
585+
if (i-child->rect.y < (int)child->crossings->size())
586+
parent->crossings->push_front(child->crossings->at(i-child->rect.y));
585587
else
586588
parent->crossings->push_front(0);
587589

588-
for (int i=parent->bbox[3]+1; i<child->bbox[1]; i++)
590+
for (int i=parent->rect.br().y; i<child->rect.y; i++)
589591
parent->crossings->push_back(0);
590592

591-
for (int i=max(parent->bbox[3]+1,child->bbox[1]); i<=child->bbox[3]; i++)
592-
parent->crossings->push_back(child->crossings->at(i-child->bbox[1]));
593+
for (int i=max(parent->rect.br().y,child->rect.y); i<=child->rect.br().y-1; i++)
594+
parent->crossings->push_back(child->crossings->at(i-child->rect.y));
593595

594596
parent->euler += child->euler;
595597

596-
parent->bbox[0] = min(parent->bbox[0],child->bbox[0]);
597-
parent->bbox[1] = min(parent->bbox[1],child->bbox[1]);
598-
parent->bbox[2] = max(parent->bbox[2],child->bbox[2]);
599-
parent->bbox[3] = max(parent->bbox[3],child->bbox[3]);
598+
int new_x1 = min(parent->rect.x,child->rect.x);
599+
int new_y1 = min(parent->rect.y,child->rect.y);
600+
int new_x2 = max(parent->rect.br().x-1,child->rect.br().x-1);
601+
int new_y2 = max(parent->rect.br().y-1,child->rect.br().y-1);
602+
parent->rect.x = new_x1;
603+
parent->rect.y = new_y1;
604+
parent->rect.width = new_x2-new_x1+1;
605+
parent->rect.height = new_y2-new_y1+1;
600606

601607
parent->raw_moments[0] += child->raw_moments[0];
602608
parent->raw_moments[1] += child->raw_moments[1];
@@ -606,14 +612,14 @@ void ERFilterNM::er_merge(ERStat *parent, ERStat *child)
606612
parent->central_moments[2] += child->central_moments[2];
607613

608614
// child region done, we can calculate 1st stage features from the incrementally computable descriptors
609-
child->aspect_ratio = (float)(child->bbox[2]-child->bbox[0]+1)/(child->bbox[3]-child->bbox[1]+1);
615+
child->aspect_ratio = (float)(child->rect.width)/(child->rect.height);
610616
child->compactness = sqrt((float)(child->area))/child->perimeter;
611617
child->num_holes = (float)(1-child->euler);
612618

613619
vector<int> m_crossings;
614-
m_crossings.push_back(child->crossings->at((int)(child->bbox[3]-child->bbox[1]+1)/6));
615-
m_crossings.push_back(child->crossings->at((int)3*(child->bbox[3]-child->bbox[1]+1)/6));
616-
m_crossings.push_back(child->crossings->at((int)5*(child->bbox[3]-child->bbox[1]+1)/6));
620+
m_crossings.push_back(child->crossings->at((int)(child->rect.height)/6));
621+
m_crossings.push_back(child->crossings->at((int)3*(child->rect.height)/6));
622+
m_crossings.push_back(child->crossings->at((int)5*(child->rect.height)/6));
617623
std::sort(m_crossings.begin(), m_crossings.end());
618624
child->med_crossings = (float)m_crossings.at(1);
619625

@@ -750,14 +756,14 @@ ERStat* ERFilterNM::er_tree_filter ( cv::InputArray image, ERStat * stat, ERStat
750756
CV_Assert( src.type() == CV_8UC1 );
751757

752758
//Fill the region and calculate 2nd stage features
753-
Mat region = region_mask(Rect(Point(stat->bbox[0],stat->bbox[1]),Point(stat->bbox[2]+3,stat->bbox[3]+3)));
759+
Mat region = region_mask(Rect(Point(stat->rect.x,stat->rect.y),Point(stat->rect.br().x+2,stat->rect.br().y+2)));
754760
region = Scalar(0);
755761
int newMaskVal = 255;
756762
int flags = 4 + (newMaskVal << 8) + FLOODFILL_FIXED_RANGE + FLOODFILL_MASK_ONLY;
757763
Rect rect;
758764

759-
floodFill( src(Rect(Point(stat->bbox[0],stat->bbox[1]),Point(stat->bbox[2]+1,stat->bbox[3]+1))),
760-
region, Point(stat->pixel%src.cols - stat->bbox[0], stat->pixel/src.cols - stat->bbox[1]),
765+
floodFill( src(Rect(Point(stat->rect.x,stat->rect.y),Point(stat->rect.br().x,stat->rect.br().y))),
766+
region, Point(stat->pixel%src.cols - stat->rect.x, stat->pixel/src.cols - stat->rect.y),
761767
Scalar(255), &rect, Scalar(stat->level), Scalar(0), flags );
762768
rect.width += 2;
763769
rect.height += 2;

0 commit comments

Comments
 (0)