yolact icon indicating copy to clipboard operation
yolact copied to clipboard

How to calculate the area of the masked image

Open barryluke96 opened this issue 3 years ago • 3 comments

Hello, is there a way to calculate the area / contour of the masked image? Thanks.

barryluke96 avatar Sep 14 '20 08:09 barryluke96

@barryluke96 Some thing like this

Image of the mask image

with timer.env('Postprocess'):
    save = cfg.rescore_bbox
    cfg.rescore_bbox = True
    classes, scores, boxes, masks  = postprocess(dets_out, w, h, visualize_lincomb = args.display_lincomb,
                                    crop_masks        = args.crop,
                                    score_threshold   = args.score_threshold)
    cfg.rescore_bbox = save

# mask :1 -> 255 -> then copy to cpu -> then convert from tensor into numpy
masks_np = (masks* 255).byte().cpu().numpy()

# Find the contour of first mask 
mask0_np = masks_np[0, :, :]
contours,hierarchy = cv2.findContours(mask0_np,  cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
plt.imshow(cv2.cvtColor(mask1_np, cv2.COLOR_GRAY2RGB))

Note : This code will copy entire mask images from gpu to cpu.

gachiemchiep avatar Oct 29 '20 03:10 gachiemchiep

@barryluke96 Some thing like this

Image of the mask image

with timer.env('Postprocess'):
    save = cfg.rescore_bbox
    cfg.rescore_bbox = True
    classes, scores, boxes, masks  = postprocess(dets_out, w, h, visualize_lincomb = args.display_lincomb,
                                    crop_masks        = args.crop,
                                    score_threshold   = args.score_threshold)
    cfg.rescore_bbox = save

# mask :1 -> 255 -> then copy to cpu -> then convert from tensor into numpy
masks_np = (masks* 255).byte().cpu().numpy()

# Find the contour of first mask 
mask0_np = masks_np[0, :, :]
contours,hierarchy = cv2.findContours(mask0_np,  cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
plt.imshow(cv2.cvtColor(mask1_np, cv2.COLOR_GRAY2RGB))

Note : This code will copy entire mask images from gpu to cpu.

it work for me, thank :)

pongsiri20907 avatar Mar 09 '21 18:03 pongsiri20907

Im late, but is there a way to calculate this on gpu? Copying every mask on cpu will slow process enormously

AtaC29 avatar May 23 '22 08:05 AtaC29