(self, ref)
| 293 | ax.add_patch(box_plot) |
| 294 | |
| 295 | def getMask(self, ref): |
| 296 | # return mask, area and mask-center |
| 297 | ann = self.refToAnn[ref['ref_id']] |
| 298 | image = self.Imgs[ref['image_id']] |
| 299 | if type(ann['segmentation'][0]) == list: # polygon |
| 300 | rle = mask.frPyObjects(ann['segmentation'], image['height'], |
| 301 | image['width']) |
| 302 | else: |
| 303 | rle = ann['segmentation'] |
| 304 | |
| 305 | # for i in range(len(rle['counts'])): |
| 306 | # print(rle) |
| 307 | m = mask.decode(rle) |
| 308 | m = np.sum( |
| 309 | m, axis=2 |
| 310 | ) # sometimes there are multiple binary map (corresponding to multiple segs) |
| 311 | m = m.astype(np.uint8) # convert to np.uint8 |
| 312 | # compute area |
| 313 | area = sum(mask.area(rle)) # should be close to ann['area'] |
| 314 | return {'mask': m, 'area': area} |
| 315 | # # position |
| 316 | # position_x = np.mean(np.where(m==1)[1]) # [1] means columns (matlab style) -> x (c style) |
| 317 | # position_y = np.mean(np.where(m==1)[0]) # [0] means rows (matlab style) -> y (c style) |
| 318 | # # mass position (if there were multiple regions, we use the largest one.) |
| 319 | # label_m = label(m, connectivity=m.ndim) |
| 320 | # regions = regionprops(label_m) |
| 321 | # if len(regions) > 0: |
| 322 | # largest_id = np.argmax(np.array([props.filled_area for props in regions])) |
| 323 | # largest_props = regions[largest_id] |
| 324 | # mass_y, mass_x = largest_props.centroid |
| 325 | # else: |
| 326 | # mass_x, mass_y = position_x, position_y |
| 327 | # # if centroid is not in mask, we find the closest point to it from mask |
| 328 | # if m[mass_y, mass_x] != 1: |
| 329 | # print 'Finding closes mask point ...' |
| 330 | # kernel = np.ones((10, 10),np.uint8) |
| 331 | # me = cv2.erode(m, kernel, iterations = 1) |
| 332 | # points = zip(np.where(me == 1)[0].tolist(), np.where(me == 1)[1].tolist()) # row, col style |
| 333 | # points = np.array(points) |
| 334 | # dist = np.sum((points - (mass_y, mass_x))**2, axis=1) |
| 335 | # id = np.argsort(dist)[0] |
| 336 | # mass_y, mass_x = points[id] |
| 337 | # # return |
| 338 | # return {'mask': m, 'area': area, 'position_x': position_x, 'position_y': position_y, 'mass_x': mass_x, 'mass_y': mass_y} |
| 339 | # # show image and mask |
| 340 | # I = io.imread(osp.join(self.IMAGE_DIR, image['file_name'])) |
| 341 | # plt.figure() |
| 342 | # plt.imshow(I) |
| 343 | # ax = plt.gca() |
| 344 | # img = np.ones( (m.shape[0], m.shape[1], 3) ) |
| 345 | # color_mask = np.array([2.0,166.0,101.0])/255 |
| 346 | # for i in range(3): |
| 347 | # img[:,:,i] = color_mask[i] |
| 348 | # ax.imshow(np.dstack( (img, m*0.5) )) |
| 349 | # plt.show() |
| 350 | |
| 351 | def showMask(self, ref): |
| 352 | M = self.getMask(ref) |
no test coverage detected