facebookresearch / segment-anything

The repository provides code for running inference with the SegmentAnything Model (SAM), links for downloading the trained model checkpoints, and example notebooks that show how to use the model.
Apache License 2.0
47.53k stars 5.62k forks source link

Can I cut out photos? And change the background to white or other colors. The result is then reported locally. #221

Open cristianohello opened 1 year ago

cristianohello commented 1 year ago

its like ? image

image

qiantubu commented 1 year ago

好玩

Github-Yilei commented 1 year ago

you may use the binary mask to extract the object pixels from the original image:

img_arr = cv2.imread(img_path)
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_BGR2RGB)

mask_generator = SamAutomaticMaskGenerator(sam)
predictor = mask_generator.generate(img_arr)

 # choose the first masks
mask = predictor[1]['segmentation']

# Remove background by turn it to white
img_arr[mask==False] = [255,255,255] 

plt.imshow(img_arr)
plt.axis('off')
chirayu-2001 commented 1 year ago

Hey, I don't want the background. I just want the cut out image of object as they have shown in demo.

Nomination-NRB commented 1 year ago

Hey, I don't want the background. I just want the cut out image of object as they have shown in demo.

You can try on this repository: https://github.com/Nomination-NRB/SAM-webui

You can save the color mask, white mask and cut out image of object

ChiaN-Yang commented 11 months ago

I just choose the one with the largest area

img_arr = cv2.imread(img_path)
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_BGR2RGB)

mask_generator = SamAutomaticMaskGenerator(sam)
masks = mask_generator.generate(img_arr)

 # choose the biggest area
masks = sorted(masks, key=lambda x: x['area'], reverse=True)
mask = masks[0]['segmentation']