nndeploy C++ API  0.2.0
nndeploy C++ API
utils.h
Go to the documentation of this file.
1 
2 namespace nndeploy {
3 namespace ocr {
4 
5 cv::Mat GetRotateCropImage(const cv::Mat& srcimage,
6  const std::array<int, 8>& box) {
7  cv::Mat image;
8  srcimage.copyTo(image);
9 
10  std::vector<std::vector<int>> points;
11 
12  for (int i = 0; i < 4; ++i) {
13  std::vector<int> tmp;
14  tmp.push_back(box[2 * i]);
15  tmp.push_back(box[2 * i + 1]);
16  points.push_back(tmp);
17  }
18  int x_collect[4] = {box[0], box[2], box[4], box[6]};
19  int y_collect[4] = {box[1], box[3], box[5], box[7]};
20  int left = int(*std::min_element(x_collect, x_collect + 4));
21  int right = int(*std::max_element(x_collect, x_collect + 4));
22  int top = int(*std::min_element(y_collect, y_collect + 4));
23  int bottom = int(*std::max_element(y_collect, y_collect + 4));
24 
25  cv::Mat img_crop;
26  image(cv::Rect(left, top, right - left, bottom - top)).copyTo(img_crop);
27 
28  for (int i = 0; i < points.size(); i++) {
29  points[i][0] -= left;
30  points[i][1] -= top;
31  }
32 
33  int img_crop_width = int(sqrt(pow(points[0][0] - points[1][0], 2) +
34  pow(points[0][1] - points[1][1], 2)));
35  int img_crop_height = int(sqrt(pow(points[0][0] - points[3][0], 2) +
36  pow(points[0][1] - points[3][1], 2)));
37 
38  cv::Point2f pts_std[4];
39  pts_std[0] = cv::Point2f(0., 0.);
40  pts_std[1] = cv::Point2f(img_crop_width, 0.);
41  pts_std[2] = cv::Point2f(img_crop_width, img_crop_height);
42  pts_std[3] = cv::Point2f(0.f, img_crop_height);
43 
44  cv::Point2f pointsf[4];
45  pointsf[0] = cv::Point2f(points[0][0], points[0][1]);
46  pointsf[1] = cv::Point2f(points[1][0], points[1][1]);
47  pointsf[2] = cv::Point2f(points[2][0], points[2][1]);
48  pointsf[3] = cv::Point2f(points[3][0], points[3][1]);
49 
50  cv::Mat M = cv::getPerspectiveTransform(pointsf, pts_std);
51 
52  cv::Mat dst_img;
53  cv::warpPerspective(img_crop, dst_img, M,
54  cv::Size(img_crop_width, img_crop_height),
55  cv::BORDER_REPLICATE);
56 
57  if (float(dst_img.rows) >= float(dst_img.cols) * 1.5) {
58  cv::Mat srcCopy = cv::Mat(dst_img.rows, dst_img.cols, dst_img.depth());
59  cv::transpose(dst_img, srcCopy);
60  cv::flip(srcCopy, srcCopy, 0);
61  return srcCopy;
62  } else {
63  return dst_img;
64  }
65 }
66 
67 } // namespace ocr
68 } // namespace nndeploy
Point< float > Point2f
Definition: type.h:179
cv::Mat GetRotateCropImage(const cv::Mat &srcimage, const std::array< int, 8 > &box)
Definition: utils.h:5
base::Status pow(device::Tensor *input, device::Tensor *exponent, device::Tensor *output)
base::Status sqrt(device::Tensor *input, device::Tensor *output)
base::Status transpose(device::Tensor *input, std::shared_ptr< ir::TransposeParam > param, device::Tensor *output)