image_tools.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding:utf-8 -*-
  2. import math
  3. import numpy as np
  4. # from neighbor import neighbor_change
  5. from Near import neighbor_change
  6. import numba as nb
  7. def get_range(bboxs):
  8. # 坐标转化
  9. ranges = []
  10. for i in bboxs:
  11. xs = i[::2]
  12. ys = i[1::2]
  13. box = [min(xs), min(ys), max(xs), max(ys)]
  14. ranges.append(box)
  15. return ranges
  16. # @nb.jit
  17. def check_range(pixel_points, ranges):
  18. """
  19. :param pixel_points: 一团联通坐标
  20. :param ranges: 所有textbox坐标
  21. :return:
  22. """
  23. pixel_point = pixel_points[0]
  24. for i in ranges:
  25. if i[2] >= pixel_point[0] >= i[0] and i[3] >= pixel_point[1] >= i[1]:
  26. return 1
  27. return 0
  28. def is_text(bboxs, contours):
  29. '''
  30. doc
  31. 判断一个实物点在不在textbox中
  32. :param bboxs: text的区域
  33. :param contours: 所有区域,[array[一个x坐标,一个y坐标]]
  34. :return: 插图区域
  35. '''
  36. ranges = bboxs
  37. illustration_box = []
  38. for region in contours:
  39. contain = [check_range(pixel_points, ranges) for pixel_points in region]
  40. contain = sum(contain) / len(contain)
  41. if contain < 0.2: # 交并比 iou
  42. # 坐标结构化
  43. pixel = [math.inf, math.inf, -1, -1]
  44. for i in region:
  45. if i[0][0] < pixel[0]:
  46. pixel[0] = i[0][0]
  47. if i[0][0] > pixel[2]:
  48. pixel[2] = i[0][0]
  49. if i[0][1] < pixel[1]:
  50. pixel[1] = i[0][1]
  51. if i[0][1] > pixel[3]:
  52. pixel[3] = i[0][1]
  53. x_min, y_min, x_max, y_max = pixel
  54. if (x_max - x_min) * (y_max - y_min) > 30: # 30是一个参数 像素点超过30
  55. illustration_box.append(pixel)
  56. return illustration_box
  57. def processed(text_boxes, i_box):
  58. # 去除大图片中的小图片
  59. x_min, y_min, x_max, y_max = 0, 1, 2, 3 # index
  60. if len(i_box):
  61. W = i_box[-1][2] - i_box[-1][0] * 0.9
  62. i_box = [i for i in i_box if i[2] - i[0] < W]
  63. tmp_bbx = []
  64. for i in range(len(i_box)):
  65. for j in range(len(i_box)):
  66. if i_box[i][x_min] > i_box[j][x_min] and i_box[i][y_min] > i_box[j][y_min] \
  67. and i_box[i][x_max] < i_box[j][x_max] and i_box[i][y_max] < i_box[j][y_max]:
  68. tmp_bbx.append(i)
  69. inx = [i for i in range(len(i_box)) if i not in tmp_bbx]
  70. i_box_c = [i_box[i] for i in inx]
  71. text_boxes, i_box_c = neighbor_change(text_boxes, i_box_c)
  72. return i_box_c, text_boxes