MCPcopy Create free account
hub / github.com/Topdu/OpenOCR / draw_border_map

Method draw_border_map

opendet/preprocess/db_label_encode.py:99–150  ·  view source on GitHub ↗
(self, polygon, canvas, mask)

Source from the content-addressed store, hash-verified

97 return data
98
99 def draw_border_map(self, polygon, canvas, mask):
100 polygon = np.array(polygon)
101 assert polygon.ndim == 2
102 assert polygon.shape[1] == 2
103
104 polygon_shape = Polygon(polygon)
105 if polygon_shape.area <= 0:
106 return
107 distance = (polygon_shape.area * (1 - np.power(self.shrink_ratio, 2)) /
108 polygon_shape.length)
109 subject = [tuple(l) for l in polygon]
110 padding = pyclipper.PyclipperOffset()
111 padding.AddPath(subject, pyclipper.JT_ROUND,
112 pyclipper.ET_CLOSEDPOLYGON)
113
114 padded_polygon = np.array(padding.Execute(distance)[0])
115 cv2.fillPoly(mask, [padded_polygon.astype(np.int32)], 1.0)
116
117 xmin = padded_polygon[:, 0].min()
118 xmax = padded_polygon[:, 0].max()
119 ymin = padded_polygon[:, 1].min()
120 ymax = padded_polygon[:, 1].max()
121 width = xmax - xmin + 1
122 height = ymax - ymin + 1
123
124 polygon[:, 0] = polygon[:, 0] - xmin
125 polygon[:, 1] = polygon[:, 1] - ymin
126
127 xs = np.broadcast_to(
128 np.linspace(0, width - 1, num=width).reshape(1, width),
129 (height, width))
130 ys = np.broadcast_to(
131 np.linspace(0, height - 1, num=height).reshape(height, 1),
132 (height, width))
133
134 distance_map = np.zeros((polygon.shape[0], height, width),
135 dtype=np.float32)
136 for i in range(polygon.shape[0]):
137 j = (i + 1) % polygon.shape[0]
138 absolute_distance = self._distance(xs, ys, polygon[i], polygon[j])
139 distance_map[i] = np.clip(absolute_distance / distance, 0, 1)
140 distance_map = distance_map.min(axis=0)
141
142 xmin_valid = min(max(0, xmin), canvas.shape[1] - 1)
143 xmax_valid = min(max(0, xmax), canvas.shape[1] - 1)
144 ymin_valid = min(max(0, ymin), canvas.shape[0] - 1)
145 ymax_valid = min(max(0, ymax), canvas.shape[0] - 1)
146 canvas[ymin_valid:ymax_valid + 1, xmin_valid:xmax_valid + 1] = np.fmax(
147 1 - distance_map[ymin_valid - ymin:ymax_valid - ymax + height,
148 xmin_valid - xmin:xmax_valid - xmax + width, ],
149 canvas[ymin_valid:ymax_valid + 1, xmin_valid:xmax_valid + 1],
150 )
151
152 def _distance(self, xs, ys, point_1, point_2):
153 """

Callers 1

__call__Method · 0.95

Calls 1

_distanceMethod · 0.95

Tested by

no test coverage detected