Ezaldeen99 / BackgroundRemoval

Background Removal written with swift using u2net model
Apache License 2.0
204 stars 34 forks source link

Improve results will give me Mask not image #5

Closed X901 closed 1 year ago

X901 commented 1 year ago

Hi,

I tried your example Improve results

but the result is The mask not the image after remove background how can I get the image itself without background ?

Ezaldeen99 commented 1 year ago

you can refer to this function used in the library functions to mask the image from a mask, you can use it as an extension for your input image and mask it with the output mask

`

 /// mask the input image with our mask to get the final result

func maskImage(withMask maskImage: UIImage) -> UIImage {

    let maskRef = maskImage.cgImage

    let mask = CGImage(
        maskWidth: maskRef!.width,
        height: maskRef!.height,
        bitsPerComponent: maskRef!.bitsPerComponent,
        bitsPerPixel: maskRef!.bitsPerPixel,
        bytesPerRow: maskRef!.bytesPerRow,
        provider: maskRef!.dataProvider!,
        decode: nil,
        shouldInterpolate: false)

    let masked = self.cgImage!.masking(mask!)
    let maskedImage = UIImage(cgImage: masked!)

    // No need to release. Core Foundation objects are automatically memory managed.
    return maskedImage

}`