Transparent background on iOS

Since the parts other than the foreground (people, etc.) are transparent, it can be used for background composition.

Original
Transparent

procedure

Acquire mask images using segmentation models such as the DeepLab v3 model distributed by Apple.
Please refer to this article.

Create a transparent image with the same size as the original image.
Since the “non-transparent area” is specified in the argument of settingAlphaOne, specify zero to acquire a fully transparent image.

let transparentImage = originalCIImage.settingAlphaOne(in: CGRect.zero)

Foreground = original image, background = transparent image, mask image = mask image of the object you want to keep, and combine with CIFilter.

let compositeImage = CIFilter(name: "CIBlendWithMask", parameters: [
kCIInputImageKey: originalCIImage,
kCIInputBackgroundImageKey:transparentImage,
kCIInputMaskImageKey:mask]).outputImage

Prepare a background image and combine it again to create a composite image.

Composited

🐣

***

Request for work:

rockyshikoku@gmail.com

Freelance iOS developer. You can ask me for a job from any country. rockyshikoku@gmail.com