bowang-lab / MedSAM

Segment Anything in Medical Images
https://www.nature.com/articles/s41467-024-44824-z
Apache License 2.0
2.99k stars 415 forks source link

Question about output 'segs.png' in tutorial_quickstart.ipynb file #267

Closed Lucarqi closed 5 months ago

Lucarqi commented 5 months ago

Thanks for your exsellent work. I have tried tutorial_quickstart.ipynb. I run follow cell.

# abdomen CT
img = "assets/img_demo.png"
bbox_prompt_demo = BboxPromptDemo(medsam_model)
bbox_prompt_demo.show(img)

I segment ROI by adding four boxes, like this image

I click 'save' botton, but i get the nothing output in 'segs.png' like this: image

But in demo.py code, class 'BboxPromptDemo' seems save all segment ROI in 'segs.png', in follow part

def __on_save_button_clicked(b):
      plt.savefig("seg_result.png", bbox_inches='tight', pad_inches=0)
            if len(self.segs) > 0:
                save_seg = np.zeros_like(self.segs[0])
                for i, seg in enumerate(self.segs, start=1):
                    save_seg[seg > 0] = i
                cv2.imwrite("segs.png", save_seg)
                print(f"Segmentation result saved to {getcwd()}")

I think maye value integer is small, vscode png viewer cant show any details. So I range save_seg to [0,255]:

def __on_save_button_clicked(b):
    plt.savefig("seg_result.png", bbox_inches='tight', pad_inches=0)
    if len(self.segs) > 0:
        save_seg = np.zeros_like(self.segs[0])
        for i, seg in enumerate(self.segs, start=1):
            save_seg[seg > 0] = i
            save_seg = (save_seg - save_seg.min()) / (save_seg.max() - save_seg.min()) * 255
        cv2.imwrite("segs.png", save_seg)
        print(f"Segmentation result saved to {getcwd()}")

I run .ipynp file agian, but I only get first box segment ROI in 'segs.png': image

Lucarqi commented 5 months ago

I just correct function 'def __on_save_button_clicked(b)' like this:

def __on_save_button_clicked(b):
    plt.savefig("seg_result.png", bbox_inches='tight', pad_inches=0)
    if len(self.segs) > 0:
        save_seg = np.zeros_like(self.segs[0])
        for i, seg in enumerate(self.segs, start=0):
            save_seg[seg > 0] = i+1
        save_seg = (save_seg - save_seg.min()) / (save_seg.max() - save_seg.min()) * 255
        cv2.imwrite("segs.png", save_seg)
        print(f"Segmentation result saved to {getcwd()}")