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.39k stars 5.61k forks source link

Mask Area/Size after generating masks using prompts #479

Open kelvinlimwei opened 1 year ago

kelvinlimwei commented 1 year ago

I see that automatic mask generator is able to return a dictionary of data about the mask such as area, bounding box, stability scores and etc.

I was wondering if the masks generated from using prompts also returns similar data information? I am interested in getting the mask pixel area for translation into an equivalent physical dimension. Is there a way to convert the output masks from the predictor to the same output format as the automatic mask generator?

heyoeyo commented 1 year ago

If you have access to the segmentation mask itself (i.e. the binary black/white image output from the model), you can get the mask area by taking the sum of all 'pixels' in the mask. The white pixels should have a value of either 1.0 or 255 (depending on the format of the image), so the sum of all the pixels will either be exactly the 'pixel area' or otherwise 255 times larger (so you'd just need to divide it by 255). You also can compare that to the total number of pixels (given by the width x height of the mask) to get the area as a percentage.

Alternatively, the code for generating that dictionary of data can be found in the segment_anything/automatic_mask_generator.py file, so you could maybe take the parts of that code to come up with the same sorts of outputs.