Transparent background on iOS

MLBoy
2 min readOct 4, 2020

--

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

Original
Transparent

procedure

1. Make a mask image.

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

2. Make a transparent image.

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)

3. Synthesize.

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

--

--