Converting ImageSegment to OpenCV

I have a model for image segmentation. When I make a prediction, it returns an ImageSegment object. I need to convert the generated mask to an OpenCV image. How do I achieve that?

Your ImageSegement object has a .save() method, and you can then convert it from the PIL to a OpenCV as described here:

One option

I don’t want to save and load the image. I’m trying to build a small pipeline in which I have to process the segmented image using open cv. Since it is binary segmentation, I am having trouble converting the predicted ImageSegment object (size: (256, 256, 1)) to an opencv image.

@BBloggsbott, I need something similar. After digging in the fastai repo. I found the following that might do the job.

numpy_array_image_mask = image2np(

Then you can easily convert that to opencv image

Hope, this helps.

1 Like

Thank you @Pager07