diff --git a/pc_projects/OcrLiteOnnxToNcnn/include/RRLib.h b/pc_projects/OcrLiteOnnxToNcnn/include/RRLib.h index 599b72f..0f285f0 100644 --- a/pc_projects/OcrLiteOnnxToNcnn/include/RRLib.h +++ b/pc_projects/OcrLiteOnnxToNcnn/include/RRLib.h @@ -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 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); diff --git a/pc_projects/OcrLiteOnnxToNcnn/src/OcrLite.cpp b/pc_projects/OcrLiteOnnxToNcnn/src/OcrLite.cpp index 25b72cf..c517d08 100755 --- a/pc_projects/OcrLiteOnnxToNcnn/src/OcrLite.cpp +++ b/pc_projects/OcrLiteOnnxToNcnn/src/OcrLite.cpp @@ -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); diff --git a/pc_projects/OcrLiteOnnxToNcnn/src/OcrUtils.cpp b/pc_projects/OcrLiteOnnxToNcnn/src/OcrUtils.cpp index b117052..42775f5 100755 --- a/pc_projects/OcrLiteOnnxToNcnn/src/OcrUtils.cpp +++ b/pc_projects/OcrLiteOnnxToNcnn/src/OcrUtils.cpp @@ -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; } diff --git a/pc_projects/OcrLiteOnnxToNcnn/src/RRLib.cpp b/pc_projects/OcrLiteOnnxToNcnn/src/RRLib.cpp index 56af081..16e9fcd 100644 --- a/pc_projects/OcrLiteOnnxToNcnn/src/RRLib.cpp +++ b/pc_projects/OcrLiteOnnxToNcnn/src/RRLib.cpp @@ -191,6 +191,53 @@ namespace RRLib { myGetQuadrangleSubPix(img, dst, m); } //---------------------------------------------------------- + // Extracts rotated region and returns it as dst image + //---------------------------------------------------------- + void getRotRectImg(std::vector bbox, Mat &img, Mat& dst) + { + std::vector pt; + std::vector 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(0), mm.at(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)