db_post_process.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "db_post_process.h" // NOLINT
  15. #include <algorithm>
  16. #include <utility>
  17. void GetContourArea(std::vector<std::vector<float>> box, float unclip_ratio,
  18. float &distance) {
  19. int pts_num = 4;
  20. float area = 0.0f;
  21. float dist = 0.0f;
  22. for (int i = 0; i < pts_num; i++) {
  23. area += box[i][0] * box[(i + 1) % pts_num][1] -
  24. box[i][1] * box[(i + 1) % pts_num][0];
  25. dist += sqrtf((box[i][0] - box[(i + 1) % pts_num][0]) *
  26. (box[i][0] - box[(i + 1) % pts_num][0]) +
  27. (box[i][1] - box[(i + 1) % pts_num][1]) *
  28. (box[i][1] - box[(i + 1) % pts_num][1]));
  29. }
  30. area = fabs(float(area / 2.0));
  31. distance = area * unclip_ratio / dist;
  32. }
  33. cv::RotatedRect Unclip(std::vector<std::vector<float>> box,
  34. float unclip_ratio) {
  35. float distance = 1.0;
  36. GetContourArea(box, unclip_ratio, distance);
  37. ClipperLib::ClipperOffset offset;
  38. ClipperLib::Path p;
  39. p << ClipperLib::IntPoint(static_cast<int>(box[0][0]),
  40. static_cast<int>(box[0][1]))
  41. << ClipperLib::IntPoint(static_cast<int>(box[1][0]),
  42. static_cast<int>(box[1][1]))
  43. << ClipperLib::IntPoint(static_cast<int>(box[2][0]),
  44. static_cast<int>(box[2][1]))
  45. << ClipperLib::IntPoint(static_cast<int>(box[3][0]),
  46. static_cast<int>(box[3][1]));
  47. offset.AddPath(p, ClipperLib::jtRound, ClipperLib::etClosedPolygon);
  48. ClipperLib::Paths soln;
  49. offset.Execute(soln, distance);
  50. std::vector<cv::Point2f> points;
  51. for (int j = 0; j < soln.size(); j++) {
  52. for (int i = 0; i < soln[soln.size() - 1].size(); i++) {
  53. points.emplace_back(soln[j][i].X, soln[j][i].Y);
  54. }
  55. }
  56. cv::RotatedRect res = cv::minAreaRect(points);
  57. return res;
  58. }
  59. std::vector<std::vector<float>> Mat2Vector(cv::Mat mat) {
  60. std::vector<std::vector<float>> img_vec;
  61. std::vector<float> tmp;
  62. for (int i = 0; i < mat.rows; ++i) {
  63. tmp.clear();
  64. for (int j = 0; j < mat.cols; ++j) {
  65. tmp.push_back(mat.at<float>(i, j));
  66. }
  67. img_vec.push_back(tmp);
  68. }
  69. return img_vec;
  70. }
  71. bool XsortFp32(std::vector<float> a, std::vector<float> b) {
  72. if (a[0] != b[0])
  73. return a[0] < b[0];
  74. return false;
  75. }
  76. bool XsortInt(std::vector<int> a, std::vector<int> b) {
  77. if (a[0] != b[0])
  78. return a[0] < b[0];
  79. return false;
  80. }
  81. std::vector<std::vector<int>>
  82. OrderPointsClockwise(std::vector<std::vector<int>> pts) {
  83. std::vector<std::vector<int>> box = pts;
  84. std::sort(box.begin(), box.end(), XsortInt);
  85. std::vector<std::vector<int>> leftmost = {box[0], box[1]};
  86. std::vector<std::vector<int>> rightmost = {box[2], box[3]};
  87. if (leftmost[0][1] > leftmost[1][1])
  88. std::swap(leftmost[0], leftmost[1]);
  89. if (rightmost[0][1] > rightmost[1][1])
  90. std::swap(rightmost[0], rightmost[1]);
  91. std::vector<std::vector<int>> rect = {leftmost[0], rightmost[0], rightmost[1],
  92. leftmost[1]};
  93. return rect;
  94. }
  95. std::vector<std::vector<float>> GetMiniBoxes(cv::RotatedRect box, float &ssid) {
  96. ssid = std::min(box.size.width, box.size.height);
  97. cv::Mat points;
  98. cv::boxPoints(box, points);
  99. auto array = Mat2Vector(points);
  100. std::sort(array.begin(), array.end(), XsortFp32);
  101. std::vector<float> idx1 = array[0], idx2 = array[1], idx3 = array[2],
  102. idx4 = array[3];
  103. if (array[3][1] <= array[2][1]) {
  104. idx2 = array[3];
  105. idx3 = array[2];
  106. } else {
  107. idx2 = array[2];
  108. idx3 = array[3];
  109. }
  110. if (array[1][1] <= array[0][1]) {
  111. idx1 = array[1];
  112. idx4 = array[0];
  113. } else {
  114. idx1 = array[0];
  115. idx4 = array[1];
  116. }
  117. array[0] = idx1;
  118. array[1] = idx2;
  119. array[2] = idx3;
  120. array[3] = idx4;
  121. return array;
  122. }
  123. float BoxScoreFast(std::vector<std::vector<float>> box_array, cv::Mat pred) {
  124. auto array = box_array;
  125. int width = pred.cols;
  126. int height = pred.rows;
  127. float box_x[4] = {array[0][0], array[1][0], array[2][0], array[3][0]};
  128. float box_y[4] = {array[0][1], array[1][1], array[2][1], array[3][1]};
  129. int xmin = clamp(
  130. static_cast<int>(std::floorf(*(std::min_element(box_x, box_x + 4)))), 0,
  131. width - 1);
  132. int xmax =
  133. clamp(static_cast<int>(std::ceilf(*(std::max_element(box_x, box_x + 4)))),
  134. 0, width - 1);
  135. int ymin = clamp(
  136. static_cast<int>(std::floorf(*(std::min_element(box_y, box_y + 4)))), 0,
  137. height - 1);
  138. int ymax =
  139. clamp(static_cast<int>(std::ceilf(*(std::max_element(box_y, box_y + 4)))),
  140. 0, height - 1);
  141. cv::Mat mask;
  142. mask = cv::Mat::zeros(ymax - ymin + 1, xmax - xmin + 1, CV_8UC1);
  143. cv::Point root_point[4];
  144. root_point[0] = cv::Point(static_cast<int>(array[0][0]) - xmin,
  145. static_cast<int>(array[0][1]) - ymin);
  146. root_point[1] = cv::Point(static_cast<int>(array[1][0]) - xmin,
  147. static_cast<int>(array[1][1]) - ymin);
  148. root_point[2] = cv::Point(static_cast<int>(array[2][0]) - xmin,
  149. static_cast<int>(array[2][1]) - ymin);
  150. root_point[3] = cv::Point(static_cast<int>(array[3][0]) - xmin,
  151. static_cast<int>(array[3][1]) - ymin);
  152. const cv::Point *ppt[1] = {root_point};
  153. int npt[] = {4};
  154. cv::fillPoly(mask, ppt, npt, 1, cv::Scalar(1));
  155. cv::Mat croppedImg;
  156. pred(cv::Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1))
  157. .copyTo(croppedImg);
  158. auto score = cv::mean(croppedImg, mask)[0];
  159. return score;
  160. }
  161. float PolygonScoreAcc(std::vector<cv::Point> contour, cv::Mat pred) {
  162. int width = pred.cols;
  163. int height = pred.rows;
  164. std::vector<float> box_x;
  165. std::vector<float> box_y;
  166. for (int i = 0; i < contour.size(); ++i) {
  167. box_x.push_back(contour[i].x);
  168. box_y.push_back(contour[i].y);
  169. }
  170. int xmin =
  171. clamp(int(std::floor(*(std::min_element(box_x.begin(), box_x.end())))), 0,
  172. width - 1);
  173. int xmax =
  174. clamp(int(std::ceil(*(std::max_element(box_x.begin(), box_x.end())))), 0,
  175. width - 1);
  176. int ymin =
  177. clamp(int(std::floor(*(std::min_element(box_y.begin(), box_y.end())))), 0,
  178. height - 1);
  179. int ymax =
  180. clamp(int(std::ceil(*(std::max_element(box_y.begin(), box_y.end())))), 0,
  181. height - 1);
  182. cv::Mat mask;
  183. mask = cv::Mat::zeros(ymax - ymin + 1, xmax - xmin + 1, CV_8UC1);
  184. cv::Point *rook_point = new cv::Point[contour.size()];
  185. for (int i = 0; i < contour.size(); ++i) {
  186. rook_point[i] = cv::Point(int(box_x[i]) - xmin, int(box_y[i]) - ymin);
  187. }
  188. const cv::Point *ppt[1] = {rook_point};
  189. int npt[] = {int(contour.size())};
  190. cv::fillPoly(mask, ppt, npt, 1, cv::Scalar(1));
  191. cv::Mat croppedImg;
  192. pred(cv::Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1))
  193. .copyTo(croppedImg);
  194. float score = cv::mean(croppedImg, mask)[0];
  195. delete[] rook_point;
  196. return score;
  197. }
  198. std::vector<std::vector<std::vector<int>>>
  199. BoxesFromBitmap(const cv::Mat pred, const cv::Mat bitmap,
  200. std::map<std::string, double> Config) {
  201. const int min_size = 3;
  202. const int max_candidates = 1000;
  203. const float box_thresh = static_cast<float>(Config["det_db_box_thresh"]);
  204. const float unclip_ratio = static_cast<float>(Config["det_db_unclip_ratio"]);
  205. const int det_use_polygon_score = int(Config["det_use_polygon_score"]);
  206. int width = bitmap.cols;
  207. int height = bitmap.rows;
  208. std::vector<std::vector<cv::Point>> contours;
  209. std::vector<cv::Vec4i> hierarchy;
  210. cv::findContours(bitmap, contours, hierarchy, cv::RETR_LIST,
  211. cv::CHAIN_APPROX_SIMPLE);
  212. int num_contours =
  213. contours.size() >= max_candidates ? max_candidates : contours.size();
  214. std::vector<std::vector<std::vector<int>>> boxes;
  215. for (int i = 0; i < num_contours; i++) {
  216. float ssid;
  217. if (contours[i].size() <= 2)
  218. continue;
  219. cv::RotatedRect box = cv::minAreaRect(contours[i]);
  220. auto array = GetMiniBoxes(box, ssid);
  221. auto box_for_unclip = array;
  222. // end get_mini_box
  223. if (ssid < min_size) {
  224. continue;
  225. }
  226. float score;
  227. if (det_use_polygon_score) {
  228. score = PolygonScoreAcc(contours[i], pred);
  229. } else {
  230. score = BoxScoreFast(array, pred);
  231. }
  232. // end box_score_fast
  233. if (score < box_thresh)
  234. continue;
  235. // start for unclip
  236. cv::RotatedRect points = Unclip(box_for_unclip, unclip_ratio);
  237. if (points.size.height < 1.001 && points.size.width < 1.001)
  238. continue;
  239. // end for unclip
  240. cv::RotatedRect clipbox = points;
  241. auto cliparray = GetMiniBoxes(clipbox, ssid);
  242. if (ssid < min_size + 2)
  243. continue;
  244. int dest_width = pred.cols;
  245. int dest_height = pred.rows;
  246. std::vector<std::vector<int>> intcliparray;
  247. for (int num_pt = 0; num_pt < 4; num_pt++) {
  248. std::vector<int> a{
  249. static_cast<int>(clamp(
  250. roundf(cliparray[num_pt][0] / float(width) * float(dest_width)),
  251. float(0), float(dest_width))),
  252. static_cast<int>(clamp(
  253. roundf(cliparray[num_pt][1] / float(height) * float(dest_height)),
  254. float(0), float(dest_height)))};
  255. intcliparray.push_back(a);
  256. }
  257. boxes.push_back(intcliparray);
  258. } // end for
  259. return boxes;
  260. }
  261. std::vector<std::vector<std::vector<int>>>
  262. FilterTagDetRes(std::vector<std::vector<std::vector<int>>> boxes, float ratio_h,
  263. float ratio_w, cv::Mat srcimg) {
  264. int oriimg_h = srcimg.rows;
  265. int oriimg_w = srcimg.cols;
  266. std::vector<std::vector<std::vector<int>>> root_points;
  267. for (int n = 0; n < static_cast<int>(boxes.size()); n++) {
  268. boxes[n] = OrderPointsClockwise(boxes[n]);
  269. for (int m = 0; m < static_cast<int>(boxes[0].size()); m++) {
  270. boxes[n][m][0] /= ratio_w;
  271. boxes[n][m][1] /= ratio_h;
  272. boxes[n][m][0] =
  273. static_cast<int>(std::min(std::max(boxes[n][m][0], 0), oriimg_w - 1));
  274. boxes[n][m][1] =
  275. static_cast<int>(std::min(std::max(boxes[n][m][1], 0), oriimg_h - 1));
  276. }
  277. }
  278. for (int n = 0; n < boxes.size(); n++) {
  279. int rect_width, rect_height;
  280. rect_width =
  281. static_cast<int>(sqrt(pow(boxes[n][0][0] - boxes[n][1][0], 2) +
  282. pow(boxes[n][0][1] - boxes[n][1][1], 2)));
  283. rect_height =
  284. static_cast<int>(sqrt(pow(boxes[n][0][0] - boxes[n][3][0], 2) +
  285. pow(boxes[n][0][1] - boxes[n][3][1], 2)));
  286. if (rect_width <= 4 || rect_height <= 4)
  287. continue;
  288. root_points.push_back(boxes[n]);
  289. }
  290. return root_points;
  291. }