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

How to save only segmented object as original colored in jpg or png format ?

Open minkvirparia opened this issue 2 years ago • 8 comments

I have extracted the segmented object from the input image by first creating bounding box around the detected object using YOLOv8 and passed the bounding box and input image to SAM model. Now, I want to save original colored segmented object in jpg or png format. If possible please provide code for this work.

minkvirparia avatar Aug 07 '23 16:08 minkvirparia

Hey, did you find the solution?

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

Nope, I am also still searching for the solution. But I have found out one another way to do it. When you are getting the segmentation mask from SAM output, you try to create binary mask of it and then remove that part only from the original image. Lastly, create an RGBA image and combine with the previous output. So, this way you can get an segment part with all the other things are made transparent.

Remember that the size of the final output will increase, so for that you can use yolo to detect only the segment object first and then crop the image based on bounding box you get from yolo and then pass the cropped image to SAM and follow the above process.

minkvirparia avatar Aug 12 '23 11:08 minkvirparia

But in the demo UI they are showing cut outs at left side, so there must be code for that

chirayu-2001 avatar Aug 16 '23 09:08 chirayu-2001

Yeah it is possible but I didn't find that code from its official repo.

minkvirparia avatar Aug 17 '23 18:08 minkvirparia

Yeah it is possible but I didn't find that code from its official repo.

Hello, maybe this repository can solve your problem: https://github.com/Nomination-NRB/SAM-webui

Save white mask, Save color mask, Save origin image of mask

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

Nope, I am also still searching for the solution. But I have found out one another way to do it. When you are getting the segmentation mask from SAM output, you try to create binary mask of it and then remove that part only from the original image. Lastly, create an RGBA image and combine with the previous output. So, this way you can get an segment part with all the other things are made transparent.

Remember that the size of the final output will increase, so for that you can use yolo to detect only the segment object first and then crop the image based on bounding box you get from yolo and then pass the cropped image to SAM and follow the above process.

How did you create the binary mask from the mask image outputted from SAM?

shrutichakraborty avatar Nov 17 '23 09:11 shrutichakraborty

Nope, I am also still searching for the solution. But I have found out one another way to do it. When you are getting the segmentation mask from SAM output, you try to create binary mask of it and then remove that part only from the original image. Lastly, create an RGBA image and combine with the previous output. So, this way you can get an segment part with all the other things are made transparent. Remember that the size of the final output will increase, so for that you can use yolo to detect only the segment object first and then crop the image based on bounding box you get from yolo and then pass the cropped image to SAM and follow the above process.

How did you create the binary mask from the mask image outputted from SAM?

I also want to know

1983519170 avatar Nov 23 '23 11:11 1983519170

I added a file here which will extract out individual pieces of segmented items and displays them. Instead of displaying, you can save "item" from cmd-11. https://github.com/abhishek0093/segment-anything/blob/main/notebooks/Get_individual_transparent_items.ipynb

abhishek0093 avatar Dec 19 '23 10:12 abhishek0093