How to crop image based on binary mask

后端 未结 5 617
盖世英雄少女心
盖世英雄少女心 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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-10 04:01

    I've implemented this in Python, assuming that you have your input image and mask available as Mat Objects. Given that src1 is your image and src1_mask is your binary mask:

    src1_mask=cv2.cvtColor(src1_mask,cv2.COLOR_GRAY2BGR)#change mask to a 3 channel image 
    mask_out=cv2.subtract(src1_mask,src1)
    mask_out=cv2.subtract(src1_mask,mask_out)
    

    Now mask_out contains the part of the image src1 located inside the binary mask you defined.

提交回复
热议问题