Skip to content

重载getRotRectImg()函数,修改文本行裁切方式,保证文本方向一致。 #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: onnx
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pc_projects/OcrLiteOnnxToNcnn/include/RRLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ namespace RRLib {
//----------------------------------------------------------
void getRotRectImg(cv::RotatedRect rr, cv::Mat &img, cv::Mat& dst);
//----------------------------------------------------------
// Extracts rotated region and returns it as dst image
//----------------------------------------------------------
void getRotRectImg(std::vector<cv::Point> bbox, cv::Mat &img, cv::Mat& dst);
//----------------------------------------------------------
// Copies image region (src_roi) from src image, to rotated region on image dst
//----------------------------------------------------------
void copyToRotRectImg(cv::Rect src_roi, cv::RotatedRect rr, cv::Mat &src, cv::Mat& dst);
Expand Down
10 changes: 2 additions & 8 deletions pc_projects/OcrLiteOnnxToNcnn/src/OcrLite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,8 @@ OcrResult OcrLite::detect(const char *path, const char *imgName,
Logger("-----TextBox[%d] score(%f)-----\n", i, textBoxes[i].score);
double startTextBox = getCurrentTime();
cv::Mat partImg;
cv::RotatedRect partRect = getPartRect(textBoxes[i].box, scaleWidth,
scaleHeight);
Logger("partRect(center.x=%f, center.y=%f, width=%f, height=%f, angle=%f)\n",
partRect.center.x, partRect.center.y,
partRect.size.width, partRect.size.height,
partRect.angle);

RRLib::getRotRectImg(partRect, src, partImg);

RRLib::getRotRectImg(bboxs[i], src, partImg);

//drawTextBox
drawTextBox(textBoxPaddingImg, partRect, thickness);
Expand Down
2 changes: 1 addition & 1 deletion pc_projects/OcrLiteOnnxToNcnn/src/OcrUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ cv::Mat matRotateClockWise180(cv::Mat src) {

cv::Mat matRotateClockWise90(cv::Mat src) {
transpose(src, src);
flip(src, src, 1);
flip(src, src, 0);
return src;
}

Expand Down
47 changes: 47 additions & 0 deletions pc_projects/OcrLiteOnnxToNcnn/src/RRLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,53 @@ namespace RRLib {
myGetQuadrangleSubPix(img, dst, m);
}
//----------------------------------------------------------
// Extracts rotated region and returns it as dst image
//----------------------------------------------------------
void getRotRectImg(std::vector<cv::Point> bbox, Mat &img, Mat& dst)
{
std::vector<cv::Point3f> pt;
std::vector<cv::Point2f> r_pt;
cv::Rect rect = cv::boundingRect(bbox);
rect.x -= 50;
rect.y -= 50;
rect.width += 100;
rect.height += 100;
if (rect.x < 0) rect.x = 0;
if (rect.y < 0) rect.y = 0;
if (rect.width+rect.x > img.cols) rect.width = img.cols-rect.x;
if (rect.height+rect.y > img.rows) rect.height = img.rows-rect.y;
cv::Mat roi_bbox = img(cv::Rect(rect.x, rect.y, rect.width, rect.height));
cv::RotatedRect rrect = cv::minAreaRect(bbox);

float ang = rrect.angle;
if (ang < -45) ang += 90;
cv::Point2f center(roi_bbox.cols / 2.0, roi_bbox.rows / 2.0);
cv::Mat M = cv::getRotationMatrix2D(center, ang, 1.0);
for (auto i:bbox)
{
// cout << i.x << " " << i.y << endl;
pt.emplace_back(i.x-rect.x, i.y-rect.y, 1);
}
for(auto i:pt)
{
cv::Mat pt_mat = cv::Mat(i, CV_32F);
cv::Mat m;
cv::Mat mm;
M.convertTo(m, CV_32F);
mm = m*pt_mat;
r_pt.push_back(cv::Point(mm.at<float>(0), mm.at<float>(1)));
}
// cout << r_pt << endl;
cv::Rect rect_roi = cv::boundingRect(r_pt);
if (rect_roi.x < 0) rect_roi.x = 0;
if (rect_roi.y < 0) rect_roi.y = 0;
if (rect_roi.width +rect_roi.x > rect.width) rect_roi.width = rect.width -rect_roi.x;
if (rect_roi.height+rect_roi.y > rect.height) rect_roi.height = rect.height-rect_roi.y;
cv::warpAffine(roi_bbox, dst, M, rect.size());
// cout << "!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
dst = dst(cv::Rect(rect_roi.x, rect_roi.y, rect_roi.width, rect_roi.height));
}
//----------------------------------------------------------
// Copies image region (src_roi) from src image, to rotated region on image dst
//----------------------------------------------------------
void copyToRotRectImg(cv::Rect src_roi, cv::RotatedRect rr, Mat &src, Mat& dst)
Expand Down