* add deskew to plateMserLocate function.

v1.6alpha
liuruoze 9 years ago
parent 02d2c1a9c9
commit 79de3be046

@ -1997,3 +1997,13 @@ Recall:59.393%, Precise:77.2109%, Fscore:67.1399%.
Recall:66.8796%, Precise:82.0482%, Fscore:73.6914%.
0-error:31.4607%, 1-error:37.0787%, Chinese-precise:37.6405%
总时间:886秒, 平均执行时间:4.43秒
2016-06-29 17:17:42
总图片数:50, Plates count:52, 未识出图片:14, 定位率:73.0769%
Recall:65.5408%, Precise:74.0896%, Fscore:69.5535%.
0-error:26.3158%, 1-error:28.9474%, Chinese-precise:39.4737%
总时间:598秒, 平均执行时间:11.96秒
2016-06-29 17:36:23
总图片数:50, Plates count:52, 未识出图片:14, 定位率:73.0769%
Recall:65.9247%, Precise:76.1797%, Fscore:70.6822%.
0-error:34.2105%, 1-error:39.4737%, Chinese-precise:42.1053%
总时间:211秒, 平均执行时间:4.22秒

@ -46,7 +46,8 @@ class CPlateLocate {
//! 抗扭斜处理
int deskew(const Mat& src, const Mat& src_b,
std::vector<RotatedRect>& inRects, std::vector<CPlate>& outPlates);
std::vector<RotatedRect>& inRects, std::vector<CPlate>& outPlates,
bool useDeteleArea = true);
//! 是否偏斜
//! 输入二值化图像,输出判断结果

Binary file not shown.

Before

Width:  |  Height:  |  Size: 76 KiB

After

Width:  |  Height:  |  Size: 286 KiB

File diff suppressed because it is too large Load Diff

@ -1059,7 +1059,7 @@ void rotatedRectangle(InputOutputArray image, RotatedRect rrect, const Scalar& c
}
void searchWeakSeed(const std::vector<CCharacter>& charVec, std::vector<CCharacter>& mserCharacter,
void searchWeakSeed(const std::vector<CCharacter>& charVec, std::vector<CCharacter>& mserCharacter, double thresh1, double thresh2,
const Vec4f& line, Point& boundaryPoint, const Rect& maxrect, Rect& plateResult, Mat result, CharSearchDirection searchDirection) {
float k = line[1] / line[0];
@ -1071,7 +1071,7 @@ void searchWeakSeed(const std::vector<CCharacter>& charVec, std::vector<CCharact
for (auto weakSeed : charVec) {
Rect weakRect = weakSeed.getCharacterPos();
cv::rectangle(result, weakRect, Scalar(255, 0, 255));
//cv::rectangle(result, weakRect, Scalar(255, 0, 255));
Point weakCenter(weakRect.tl().x + weakRect.width / 2, weakRect.tl().y + weakRect.height / 2);
float x_2 = (float)weakCenter.x;
@ -1092,7 +1092,7 @@ void searchWeakSeed(const std::vector<CCharacter>& charVec, std::vector<CCharact
float y_diff_ratio = abs(y_2l - y_2) / maxrect.height;
if (y_diff_ratio < 0.1) {
if (y_diff_ratio < thresh1) {
float width_1 = float(maxrect.width);
float height_1 = float(maxrect.height);
@ -1102,7 +1102,7 @@ void searchWeakSeed(const std::vector<CCharacter>& charVec, std::vector<CCharact
float height_diff = abs(height_1 - height_2);
double height_diff_ratio = height_diff / min(height_1, height_2);
if (height_diff_ratio < 0.15) {
if (height_diff_ratio < thresh1) {
searchWeakSeedVec.push_back(weakSeed);
}
else {
@ -1132,11 +1132,13 @@ void searchWeakSeed(const std::vector<CCharacter>& charVec, std::vector<CCharact
firstWeakRect.tl().y + firstWeakRect.height / 2);
float ratio = (float)abs(firstWeakCenter.x - boundaryPoint.x) / (float)maxrect.height;
//std::cout << "ratio:" << ratio << std::endl;
if (ratio > 2.f)
if (ratio > thresh2) {
if (0) {
std::cout << "search seed ratio:" << ratio << std::endl;
}
return;
}
mserCharacter.push_back(firstWeakSeed);
plateResult |= firstWeakRect;
boundaryPoint = firstWeakCenter;
@ -1165,9 +1167,9 @@ void searchWeakSeed(const std::vector<CCharacter>& charVec, std::vector<CCharact
float x_margin_diff = abs(x_margin_left - x_margin_right);
double x_margin_diff_ratio = x_margin_diff / min(height_1, height_2);
if (x_margin_diff_ratio > 2.0) {
if (x_margin_diff_ratio > thresh2) {
if (0) {
std::cout << "x_margin_diff_ratio:" << x_margin_diff_ratio << std::endl;
std::cout << "search seed x_margin_diff_ratio:" << x_margin_diff_ratio << std::endl;
}
break;
}
@ -1371,13 +1373,13 @@ void removeRightOutliers(std::vector<CCharacter>& charGroup, std::vector<CCharac
float slope_1 = slopeVec.at(slopeVec_i);
float slope_2 = slopeVec.at(slopeVec_i+1);
float slope_diff = abs(slope_1 - slope_2);
if (1) {
if (0) {
std::cout << "slope_diff:" << slope_diff << std::endl;
}
if (slope_diff <= thresh1) {
uniformity_count++;
}
if (1) {
if (0) {
std::cout << "slope_1:" << slope_1 << std::endl;
std::cout << "slope_2:" << slope_2 << std::endl;
}
@ -1388,7 +1390,7 @@ void removeRightOutliers(std::vector<CCharacter>& charGroup, std::vector<CCharac
}
}
}
if (1) {
if (0) {
std::cout << "uniformity_count:" << uniformity_count << std::endl;
std::cout << "outlier_index:" << outlier_index << std::endl;
}
@ -1400,7 +1402,7 @@ void removeRightOutliers(std::vector<CCharacter>& charGroup, std::vector<CCharac
}
}
if (1) {
if (0) {
std::cout << "end:" << std::endl;
}
}
@ -1441,7 +1443,7 @@ void reFoundAndCombineRect(std::vector<CCharacter>& mserCharacter, float min_thr
});
int comparDist = dist[0] * dist[0] + dist[1] * dist[1];
if (1) {
if (0) {
std::cout << "comparDist:" << comparDist << std::endl;
}
@ -1461,7 +1463,7 @@ void reFoundAndCombineRect(std::vector<CCharacter>& mserCharacter, float min_thr
// distance between two centers
int distance2 = x_diff * x_diff + y_diff * y_diff;
if (1) {
if (0) {
std::cout << "distance2:" << distance2 << std::endl;
}
@ -1716,7 +1718,7 @@ Mat mserCharMatch(const Mat &src, Mat &match, std::vector<CPlate>& out_plateVec,
fitLine(Mat(points), line, CV_DIST_L2, 0, 0.01, 0.01);
float k = line[1] / line[0];
float angle = atan(k) * 180 / (float)CV_PI;
//float angle = atan(k) * 180 / (float)CV_PI;
//std::cout << "k:" << k << std::endl;
//std::cout << "angle:" << angle << std::endl;
//std::cout << "cos:" << 0.3 * cos(k) << std::endl;
@ -1824,7 +1826,10 @@ Mat mserCharMatch(const Mat &src, Mat &match, std::vector<CPlate>& out_plateVec,
}
if (mserCharacter.size() < 7) {
searchWeakSeed(searchCandidate, searchRightWeakSeed, line, rightPoint, maxrect, plateResult, result, CharSearchDirection::RIGHT);
double thresh1 = 0.1;
double thresh2 = 2.0;
searchWeakSeed(searchCandidate, searchRightWeakSeed, thresh1, thresh2, line, rightPoint,
maxrect, plateResult, result, CharSearchDirection::RIGHT);
if (1 && showDebug) {
std::cout << "searchRightWeakSeed:" << searchRightWeakSeed.size() << std::endl;
}
@ -1833,7 +1838,8 @@ Mat mserCharMatch(const Mat &src, Mat &match, std::vector<CPlate>& out_plateVec,
mserCharacter.push_back(seed);
}
searchWeakSeed(searchCandidate, searchLeftWeakSeed, line, leftPoint, maxrect, plateResult, result, CharSearchDirection::LEFT);
searchWeakSeed(searchCandidate, searchLeftWeakSeed, thresh1, thresh2, line, leftPoint,
maxrect, plateResult, result, CharSearchDirection::LEFT);
if (1 && showDebug) {
std::cout << "searchLeftWeakSeed:" << searchLeftWeakSeed.size() << std::endl;
}
@ -1932,7 +1938,8 @@ Mat mserCharMatch(const Mat &src, Mat &match, std::vector<CPlate>& out_plateVec,
rotatedRectangle(result, platePos, Scalar(0, 0, 255), 1);
plate.setPlatePos(platePos);
plate.setPlateColor(color);
plate.setPlateLocateType(CMSER);
out_plateVec.push_back(plate);
}
@ -1948,7 +1955,7 @@ Mat mserCharMatch(const Mat &src, Mat &match, std::vector<CPlate>& out_plateVec,
}
if (1 && showDebug) {
if (0 /*&& showDebug*/) {
imshow("result", result);
waitKey(0);
destroyWindow("result");

@ -738,7 +738,7 @@ void DeleteNotArea(Mat &inmat) {
int CPlateLocate::deskew(const Mat &src, const Mat &src_b,
vector<RotatedRect> &inRects,
vector<CPlate> &outPlates) {
vector<CPlate> &outPlates, bool useDeteleArea) {
Mat mat_debug;
src.copyTo(mat_debug);
@ -811,7 +811,8 @@ int CPlateLocate::deskew(const Mat &src, const Mat &src_b,
plate_mat.create(HEIGHT, WIDTH, TYPE);
// haitungaga添加删除非区域这个函数影响了25%的完整定位率
// DeleteNotArea(deskew_mat);
if (useDeteleArea)
DeleteNotArea(deskew_mat);
// 这里对deskew_mat进行了一个筛选
// 使用了经验数值2.3和6
@ -830,7 +831,7 @@ int CPlateLocate::deskew(const Mat &src, const Mat &src_b,
plate.setPlatePos(roi_rect);
plate.setPlateMat(plate_mat);
if (1) {
if (0) {
imshow("plate_mat", plate_mat);
waitKey(0);
destroyWindow("plate_mat");
@ -1092,58 +1093,51 @@ int CPlateLocate::plateMserLocate(Mat src, vector<CPlate> &candPlates, int img_i
// vector<RotatedRect> rects;
mserSearch(image, color, src_b, plates, img_index, false);
std::vector<CPlate> deskewPlate;
std::vector<CPlate> mserPlate;
// deskew for rotation and slope image
/*for (auto plate : plates) {
RotatedRect rrect = plate.getPlatePos();
rects_mser.push_back(rrect);
}
deskew(image, src_b, rects_mser, candPlates);*/
for (auto plate : plates) {
//RotatedRect rrect = plate.getPlatePos();
//rects_mser.push_back(rrect);
// no deskew
for (size_t j = 0; j < plates.size(); ++j) {
CPlate plate = plates.at(j);
RotatedRect rrect = plate.getPlatePos();
RotatedRect scaleRect = scaleBackRRect(rrect, (float)scale_ratio);
plate.setPlatePos(scaleRect);
Rect_<float> outputRect;
calcSafeRect(scaleRect, src, outputRect);
Mat plate_mat;
plate_mat.create(HEIGHT, WIDTH, TYPE);
resize(src(outputRect), plate_mat, plate_mat.size(), 0, 0, INTER_AREA);
plate.setPlateMat(plate_mat);
plate.setPlateColor(color);
candPlates.push_back(plate);
rects_mser.push_back(scaleRect);
mserPlate.push_back(plate);
}
}
//for (size_t i = 0; i < rects_mser.size(); ++i) {
// Rect_<float> outputRect;
// calcSafeRect(rects_mser[i], src, outputRect);
Mat resize_src_b;
resize(src_b, resize_src_b, Size(channelImage.cols, channelImage.rows));
deskew(src, resize_src_b, rects_mser, deskewPlate, false);
// if (0) {
// std::stringstream ss(std::stringstream::in | std::stringstream::out);
// ss << "resources/image/tmp/plate_" << i << ".jpg";
// imwrite(ss.str(), src(outputRect));
// }
for (auto dplate : deskewPlate) {
RotatedRect drect = dplate.getPlatePos();
Mat dmat = dplate.getPlateMat();
Rect_<float> safe_dRect;
calcSafeRect(drect, src, safe_dRect);
// CPlate plate;
// plate.setPlateLocateType(CMSER);
// plate.setPlateMat(src(outputRect));
// plate.setPlatePos(rects_mser[i]);
// candPlates.push_back(plate);
//}
for (auto splate : mserPlate) {
RotatedRect srect = splate.getPlatePos();
Rect_<float> safe_sRect;
calcSafeRect(srect, src, safe_sRect);
//deskew(src, src_b, rects_mser_blue, plates);
Rect inter = safe_dRect & safe_sRect;
Rect urect = safe_dRect | safe_sRect;
//for (size_t i = 0; i < plates.size(); i++) {
// candPlates.push_back(plates[i]);
//}
float iou = (float)inter.area() / (float)urect.area();
std::cout << "iou" << iou << std::endl;
if (iou > 0.95) {
splate.setPlateMat(dmat);
candPlates.push_back(splate);
break;
}
}
}
}
return 0;
}

Loading…
Cancel
Save