How to crop image based on binary mask

后端 未结 5 620
盖世英雄少女心
盖世英雄少女心 2021-02-10 03:27

I am using torch with some semantic segmentation algorithms to produce a binary mask of the segmented images. I would then like to crop the images based on that mask. To be clea

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-10 03:58

    For anyone else running into this. I found good luck with converting the torch binary mask tensor into type Double, and then simply multiplying it using torch's cmul function against each of the RGB channels. Basically, because the binary mask has a 1 in place of a segmented pixel, then the value will just remain. Whereas if it is outside the segmentation it has a 0 which when multiplied across the channels produces black. Saransh's answer is also good, and works well for open cv.

提交回复
热议问题