segment-anything
segment-anything copied to clipboard
Display Mask Generated without original image?
Hi,
How can I get a jpg or png of just the mask generated by segment anything? I do not want a picture of the mask overlayed over the original image.
from the example you can add two lines of code to extract and save masks.
from PIL import Image
for i, (mask, score) in enumerate(zip(masks, scores)):
plt.figure(figsize=(10,10))
plt.imshow(mask)
# save
im = Image.fromarray(mask)
im.save(f"mask_{i}.jpeg")
plt.title(f"Mask {i+1}, Score: {score:.3f}", fontsize=18)
plt.axis('off')
plt.show()
... A masks, scores, logits = predictor.predict( point_coords=input_point, point_labels=input_label, multimask_output=True, )
...
OR
... B masks, _, _ = predictor.predict( point_coords=input_point, point_labels=input_label, mask_input=mask_input[None, :, :], multimask_output=False, )
...
print(masks.shape) (1, H, W) masks = masks.reshape(H, W, 1) # bool type(False, True) masks = masks.astype(np.uint8) # int(0, 1)
masks[masks==0] = 0 masks[masks==1] = 255 masks = np.squeeze(masks) # shape: (H, W)
your_set_name = 'this_mask' cv2.imwrite(f'{your_set_name}.png', masks)
It doesn't matter if you choose A(masks) or B(masks) above. You can use whatever you want.