segment-anything icon indicating copy to clipboard operation
segment-anything copied to clipboard

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

Open cristianohello opened this issue 2 years ago • 5 comments

its like ? image

image

cristianohello avatar Apr 15 '23 08:04 cristianohello

好玩

qiantubu avatar Apr 17 '23 00:04 qiantubu

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')

Github-Yilei avatar Jun 30 '23 07:06 Github-Yilei

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

chirayu-2001 avatar Aug 11 '23 08:08 chirayu-2001

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

Nomination-NRB avatar Oct 12 '23 03:10 Nomination-NRB

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']

ChiaN-Yang avatar Nov 25 '23 08:11 ChiaN-Yang