Skip to content

Commit d632079

Browse files
committed
text: update detectRegions()
1 parent 590dc29 commit d632079

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

modules/text/samples/textdetection.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import cv2
77
import numpy as np
8-
from matplotlib import pyplot as plt
98

109
print('\ntextdetection.py')
1110
print(' A demo script of the Extremal Region Filter algorithm described in:')
@@ -50,11 +49,10 @@
5049
#Visualization
5150
for r in range(0,np.shape(rects)[0]):
5251
rect = rects[r]
53-
cv2.rectangle(vis, (rect[0],rect[1]), (rect[0]+rect[2],rect[1]+rect[3]), (0, 255, 255), 2)
52+
cv2.rectangle(vis, (rect[0],rect[1]), (rect[0]+rect[2],rect[1]+rect[3]), (0, 0, 0), 2)
53+
cv2.rectangle(vis, (rect[0],rect[1]), (rect[0]+rect[2],rect[1]+rect[3]), (255, 255, 255), 1)
5454

5555

5656
#Visualization
57-
vis = vis[:,:,::-1] #flip the colors dimension from BGR to RGB
58-
plt.imshow(vis)
59-
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
60-
plt.show()
57+
cv2.imshow("Text detection result", vis)
58+
cv2.waitKey(0)

modules/text/src/erfilter.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4175,7 +4175,7 @@ void MSERsToERStats(InputArray image, vector<vector<Point> > &contours, vector<v
41754175
void detectRegions(InputArray image, const Ptr<ERFilter>& er_filter1, const Ptr<ERFilter>& er_filter2, CV_OUT vector< vector<Point> >& regions)
41764176
{
41774177
// assert correct image type
4178-
CV_Assert( image.getMat().type() == CV_8UC1 );
4178+
CV_Assert( image.type() == CV_8UC1 );
41794179
// at least one ERFilter must be passed
41804180
CV_Assert( !er_filter1.empty() );
41814181

@@ -4189,36 +4189,33 @@ void detectRegions(InputArray image, const Ptr<ERFilter>& er_filter1, const Ptr<
41894189
}
41904190

41914191
//Convert each ER to vector<Point> and push it to output regions
4192-
Mat src = image.getMat();
4193-
Mat region_mask = Mat::zeros(src.rows+2, src.cols+2, CV_8UC1);
4192+
const Mat src = image.getMat();
41944193
for (size_t i=1; i < ers.size(); i++) //start from 1 to deprecate root region
41954194
{
41964195
ERStat* stat = &ers[i];
41974196

41984197
//Fill the region and calculate 2nd stage features
4199-
Mat region = region_mask(Rect(Point(stat->rect.x,stat->rect.y),Point(stat->rect.br().x+2,stat->rect.br().y+2)));
4200-
region = Scalar(0);
4198+
Mat region_mask(Size(stat->rect.width + 2, stat->rect.height + 2), CV_8UC1, Scalar(0));
4199+
Mat region = region_mask(Rect(1, 1, stat->rect.width, stat->rect.height));
4200+
42014201
int newMaskVal = 255;
42024202
int flags = 4 + (newMaskVal << 8) + FLOODFILL_FIXED_RANGE + FLOODFILL_MASK_ONLY;
4203-
Rect rect;
42044203

4205-
floodFill( src(Rect(Point(stat->rect.x,stat->rect.y),Point(stat->rect.br().x,stat->rect.br().y))),
4206-
region, Point(stat->pixel%src.cols - stat->rect.x, stat->pixel/src.cols - stat->rect.y),
4207-
Scalar(255), &rect, Scalar(stat->level), Scalar(0), flags );
4208-
rect.width += 2;
4209-
rect.height += 2;
4210-
region = region(rect);
4204+
const Point seed_pt(stat->pixel%src.cols, stat->pixel/src.cols);
4205+
uchar seed_v = src.at<uchar>(seed_pt);
4206+
CV_Assert((int)seed_v <= stat->level);
4207+
4208+
floodFill( src(stat->rect),
4209+
region_mask,
4210+
seed_pt - stat->rect.tl(),
4211+
Scalar(255), NULL, Scalar(/*stat->level*/255), Scalar(0), flags );
42114212

42124213
vector<vector<Point> > contours;
42134214
vector<Vec4i> hierarchy;
4214-
findContours( region, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE, Point(0, 0) );
4215-
4216-
for (size_t j=0; j < contours[0].size(); j++)
4217-
contours[0][j] += (stat->rect.tl()-Point(1,1));
4215+
findContours( region, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE, stat->rect.tl() );
42184216

42194217
regions.push_back(contours[0]);
42204218
}
4221-
42224219
}
42234220

42244221
}

0 commit comments

Comments
 (0)