| 193 | |
| 194 | # CPU post process |
| 195 | def fast_show_mask( |
| 196 | annotation, |
| 197 | ax, |
| 198 | random_color=False, |
| 199 | bbox=None, |
| 200 | points=None, |
| 201 | point_label=None, |
| 202 | retinamask=True, |
| 203 | target_height=960, |
| 204 | target_width=960, |
| 205 | ): |
| 206 | msak_sum = annotation.shape[0] |
| 207 | height = annotation.shape[1] |
| 208 | weight = annotation.shape[2] |
| 209 | # 将annotation 按照面积 排序 |
| 210 | areas = np.sum(annotation, axis=(1, 2)) |
| 211 | sorted_indices = np.argsort(areas) |
| 212 | annotation = annotation[sorted_indices] |
| 213 | |
| 214 | index = (annotation != 0).argmax(axis=0) |
| 215 | if random_color == True: |
| 216 | color = np.random.random((msak_sum, 1, 1, 3)) |
| 217 | else: |
| 218 | color = np.ones((msak_sum, 1, 1, 3)) * np.array( |
| 219 | [30 / 255, 144 / 255, 255 / 255] |
| 220 | ) |
| 221 | transparency = np.ones((msak_sum, 1, 1, 1)) * 0.6 |
| 222 | visual = np.concatenate([color, transparency], axis=-1) |
| 223 | mask_image = np.expand_dims(annotation, -1) * visual |
| 224 | |
| 225 | show = np.zeros((height, weight, 4)) |
| 226 | h_indices, w_indices = np.meshgrid( |
| 227 | np.arange(height), np.arange(weight), indexing="ij" |
| 228 | ) |
| 229 | indices = (index[h_indices, w_indices], h_indices, w_indices, slice(None)) |
| 230 | # 使用向量化索引更新show的值 |
| 231 | show[h_indices, w_indices, :] = mask_image[indices] |
| 232 | if bbox is not None: |
| 233 | x1, y1, x2, y2 = bbox |
| 234 | ax.add_patch( |
| 235 | plt.Rectangle( |
| 236 | (x1, y1), x2 - x1, y2 - y1, fill=False, edgecolor="b", linewidth=1 |
| 237 | ) |
| 238 | ) |
| 239 | # draw point |
| 240 | if points is not None: |
| 241 | plt.scatter( |
| 242 | [point[0] for i, point in enumerate(points) if point_label[i] == 1], |
| 243 | [point[1] for i, point in enumerate(points) if point_label[i] == 1], |
| 244 | s=20, |
| 245 | c="y", |
| 246 | ) |
| 247 | plt.scatter( |
| 248 | [point[0] for i, point in enumerate(points) if point_label[i] == 0], |
| 249 | [point[1] for i, point in enumerate(points) if point_label[i] == 0], |
| 250 | s=20, |
| 251 | c="m", |
| 252 | ) |