mirror of
https://github.com/opencv/opencv_contrib.git
synced 2025-10-20 04:25:42 +08:00
text: update detectRegions()
This commit is contained in:
@@ -5,7 +5,6 @@ import os
|
|||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from matplotlib import pyplot as plt
|
|
||||||
|
|
||||||
print('\ntextdetection.py')
|
print('\ntextdetection.py')
|
||||||
print(' A demo script of the Extremal Region Filter algorithm described in:')
|
print(' A demo script of the Extremal Region Filter algorithm described in:')
|
||||||
@@ -50,11 +49,10 @@ for channel in channels:
|
|||||||
#Visualization
|
#Visualization
|
||||||
for r in range(0,np.shape(rects)[0]):
|
for r in range(0,np.shape(rects)[0]):
|
||||||
rect = rects[r]
|
rect = rects[r]
|
||||||
cv2.rectangle(vis, (rect[0],rect[1]), (rect[0]+rect[2],rect[1]+rect[3]), (0, 255, 255), 2)
|
cv2.rectangle(vis, (rect[0],rect[1]), (rect[0]+rect[2],rect[1]+rect[3]), (0, 0, 0), 2)
|
||||||
|
cv2.rectangle(vis, (rect[0],rect[1]), (rect[0]+rect[2],rect[1]+rect[3]), (255, 255, 255), 1)
|
||||||
|
|
||||||
|
|
||||||
#Visualization
|
#Visualization
|
||||||
vis = vis[:,:,::-1] #flip the colors dimension from BGR to RGB
|
cv2.imshow("Text detection result", vis)
|
||||||
plt.imshow(vis)
|
cv2.waitKey(0)
|
||||||
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
|
|
||||||
plt.show()
|
|
||||||
|
@@ -4175,7 +4175,7 @@ void MSERsToERStats(InputArray image, vector<vector<Point> > &contours, vector<v
|
|||||||
void detectRegions(InputArray image, const Ptr<ERFilter>& er_filter1, const Ptr<ERFilter>& er_filter2, CV_OUT vector< vector<Point> >& regions)
|
void detectRegions(InputArray image, const Ptr<ERFilter>& er_filter1, const Ptr<ERFilter>& er_filter2, CV_OUT vector< vector<Point> >& regions)
|
||||||
{
|
{
|
||||||
// assert correct image type
|
// assert correct image type
|
||||||
CV_Assert( image.getMat().type() == CV_8UC1 );
|
CV_Assert( image.type() == CV_8UC1 );
|
||||||
// at least one ERFilter must be passed
|
// at least one ERFilter must be passed
|
||||||
CV_Assert( !er_filter1.empty() );
|
CV_Assert( !er_filter1.empty() );
|
||||||
|
|
||||||
@@ -4189,36 +4189,33 @@ void detectRegions(InputArray image, const Ptr<ERFilter>& er_filter1, const Ptr<
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Convert each ER to vector<Point> and push it to output regions
|
//Convert each ER to vector<Point> and push it to output regions
|
||||||
Mat src = image.getMat();
|
const Mat src = image.getMat();
|
||||||
Mat region_mask = Mat::zeros(src.rows+2, src.cols+2, CV_8UC1);
|
|
||||||
for (size_t i=1; i < ers.size(); i++) //start from 1 to deprecate root region
|
for (size_t i=1; i < ers.size(); i++) //start from 1 to deprecate root region
|
||||||
{
|
{
|
||||||
ERStat* stat = &ers[i];
|
ERStat* stat = &ers[i];
|
||||||
|
|
||||||
//Fill the region and calculate 2nd stage features
|
//Fill the region and calculate 2nd stage features
|
||||||
Mat region = region_mask(Rect(Point(stat->rect.x,stat->rect.y),Point(stat->rect.br().x+2,stat->rect.br().y+2)));
|
Mat region_mask(Size(stat->rect.width + 2, stat->rect.height + 2), CV_8UC1, Scalar(0));
|
||||||
region = Scalar(0);
|
Mat region = region_mask(Rect(1, 1, stat->rect.width, stat->rect.height));
|
||||||
|
|
||||||
int newMaskVal = 255;
|
int newMaskVal = 255;
|
||||||
int flags = 4 + (newMaskVal << 8) + FLOODFILL_FIXED_RANGE + FLOODFILL_MASK_ONLY;
|
int flags = 4 + (newMaskVal << 8) + FLOODFILL_FIXED_RANGE + FLOODFILL_MASK_ONLY;
|
||||||
Rect rect;
|
|
||||||
|
|
||||||
floodFill( src(Rect(Point(stat->rect.x,stat->rect.y),Point(stat->rect.br().x,stat->rect.br().y))),
|
const Point seed_pt(stat->pixel%src.cols, stat->pixel/src.cols);
|
||||||
region, Point(stat->pixel%src.cols - stat->rect.x, stat->pixel/src.cols - stat->rect.y),
|
uchar seed_v = src.at<uchar>(seed_pt);
|
||||||
Scalar(255), &rect, Scalar(stat->level), Scalar(0), flags );
|
CV_Assert((int)seed_v <= stat->level);
|
||||||
rect.width += 2;
|
|
||||||
rect.height += 2;
|
floodFill( src(stat->rect),
|
||||||
region = region(rect);
|
region_mask,
|
||||||
|
seed_pt - stat->rect.tl(),
|
||||||
|
Scalar(255), NULL, Scalar(/*stat->level*/255), Scalar(0), flags );
|
||||||
|
|
||||||
vector<vector<Point> > contours;
|
vector<vector<Point> > contours;
|
||||||
vector<Vec4i> hierarchy;
|
vector<Vec4i> hierarchy;
|
||||||
findContours( region, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE, Point(0, 0) );
|
findContours( region, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE, stat->rect.tl() );
|
||||||
|
|
||||||
for (size_t j=0; j < contours[0].size(); j++)
|
|
||||||
contours[0][j] += (stat->rect.tl()-Point(1,1));
|
|
||||||
|
|
||||||
regions.push_back(contours[0]);
|
regions.push_back(contours[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user