The ZegoEffects SDK provides various background segmentation features that can be used in a wide range of use cases, such as online education, video conferencing, short videos, and more.
Prerequisites
Before you begin to use the SDK's background segmentation feature in your project, make sure you complete the following steps:
Integrate the ZegoEffects SDK into your project. For details, see Integrate the SDK.
Background segmentation includes portrait segmentation, green screen segmentation, background blur, background mosaic, and other features.
Portrait segmentation
Note
Before using the portrait segmentation feature, please import the corresponding "SegmentationModel" model file first, otherwise the feature will not work. For details, see Quick Start - Import resources and models.
// Set the background path for portrait segmentation (choose either of the two)
[self.effects setPortraitSegmentationBackgroundPath: @"MY_BACKGROUND_PATH" mode:ZegoEffectsScaleModeAspectFill];
// Set the background buffer for portrait segmentation (choose either of the two)
[self.effects setPortraitSegmentationBackgroundBuffer:pixelbuffer mode:ZegoEffectsScaleModeAspectFill];
// Set the background path for portrait segmentation (choose either of the two)
[self.effects setPortraitSegmentationBackgroundPath: @"MY_BACKGROUND_PATH" mode:ZegoEffectsScaleModeAspectFill];
// Set the background buffer for portrait segmentation (choose either of the two)
[self.effects setPortraitSegmentationBackgroundBuffer:pixelbuffer mode:ZegoEffectsScaleModeAspectFill];
// Set the background path for portrait segmentation (choose one of the three)
mEffects.setPortraitSegmentationBackgroundPath("MY_BACKGROUND_PATH", ZegoEffectsScaleMode.ASPECT_FILL);
// Set the background buffer for portrait segmentation (choose one of the three)
mEffects.setPortraitSegmentationBackgroundBuffer(buffer, bufferLength, param, ZegoEffectsScaleMode.ASPECT_FILL);
// Set the background texture for portrait segmentation (choose one of the three)
mEffects.setPortraitSegmentationBackgroundTexture(textureID, param, ZegoEffectsScaleMode.ASPECT_FILL);
// Set the background path for portrait segmentation (choose one of the three)
mEffects.setPortraitSegmentationBackgroundPath("MY_BACKGROUND_PATH", ZegoEffectsScaleMode.ASPECT_FILL);
// Set the background buffer for portrait segmentation (choose one of the three)
mEffects.setPortraitSegmentationBackgroundBuffer(buffer, bufferLength, param, ZegoEffectsScaleMode.ASPECT_FILL);
// Set the background texture for portrait segmentation (choose one of the three)
mEffects.setPortraitSegmentationBackgroundTexture(textureID, param, ZegoEffectsScaleMode.ASPECT_FILL);
// Set the background path for portrait segmentation (choose one of the three methods: path, buffer, or texture)
zego_effects_set_portrait_segmentation_background_path(handle, "MY_BACKGROUND_PATH", zego_effects_scale_mode_aspect_fill);
// Set the background buffer for portrait segmentation (choose one of the three methods: path, buffer, or texture)
zego_effects_set_portrait_segmentation_background_buffer(handle, buffer, buffer_length, param, zego_effects_scale_mode_aspect_fill);
// Set the background texture for portrait segmentation (choose one of the three methods: path, buffer, or texture)
zego_effects_set_portrait_segmentation_background_texture(handle, texture_id, param, zego_effects_scale_mode_aspect_fill);
// Set the background path for portrait segmentation (choose one of the three methods: path, buffer, or texture)
zego_effects_set_portrait_segmentation_background_path(handle, "MY_BACKGROUND_PATH", zego_effects_scale_mode_aspect_fill);
// Set the background buffer for portrait segmentation (choose one of the three methods: path, buffer, or texture)
zego_effects_set_portrait_segmentation_background_buffer(handle, buffer, buffer_length, param, zego_effects_scale_mode_aspect_fill);
// Set the background texture for portrait segmentation (choose one of the three methods: path, buffer, or texture)
zego_effects_set_portrait_segmentation_background_texture(handle, texture_id, param, zego_effects_scale_mode_aspect_fill);
Call the method to set the background path for portrait segmentation.
// Set the background path for portrait segmentation
ZegoEffectsPlugin.instance.setPortraitSegmentationBackgroundPath('MY_BACKGROUND_PATH', ZegoEffectsScaleMode.AspectFill);
// Set the background path for portrait segmentation
ZegoEffectsPlugin.instance.setPortraitSegmentationBackgroundPath('MY_BACKGROUND_PATH', ZegoEffectsScaleMode.AspectFill);
// Set the background path for portrait segmentation
mEffects.setPortraitSegmentationBackgroundPath("MY_BACKGROUND_PATH", ZegoEffectsScaleMode.ASPECT_FILL);
// Set the background path for portrait segmentation
mEffects.setPortraitSegmentationBackgroundPath("MY_BACKGROUND_PATH", ZegoEffectsScaleMode.ASPECT_FILL);
// Set the background path for green screen segmentation (choose one of the two methods: path or Buffer)
[self.effects setChromaKeyBackgroundPath: @"MY_BACKGROUND_PATH" mode:ZegoEffectsScaleModeAspectFill];
// Set the background buffer for green screen segmentation (choose one of the two methods: path or Buffer)
[self.effects setChromaKeyBackgroundBuffer:pixelbuffer mode:ZegoEffectsScaleModeAspectFill];
// Set the background path for green screen segmentation (choose one of the two methods: path or Buffer)
[self.effects setChromaKeyBackgroundPath: @"MY_BACKGROUND_PATH" mode:ZegoEffectsScaleModeAspectFill];
// Set the background buffer for green screen segmentation (choose one of the two methods: path or Buffer)
[self.effects setChromaKeyBackgroundBuffer:pixelbuffer mode:ZegoEffectsScaleModeAspectFill];
// Set the background path for green screen segmentation (choose one of the three methods: path, buffer, or texture)
mEffects.setChromaKeyBackgroundPath("MY_BACKGROUND_PATH", ZegoEffectsScaleMode.ASPECT_FILL);
// Set the background buffer for green screen segmentation (choose one of the three methods: path, buffer, or texture)
mEffects.setChromaKeyBackgroundBuffer(buffer, bufferLength, param, ZegoEffectsScaleMode.ASPECT_FILL);
// Set the background texture for green screen segmentation (choose one of the three methods: path, buffer, or texture)
mEffects.setChromaKeyBackgroundTexture(textureID, param, ZegoEffectsScaleMode.ASPECT_FILL);
// Set the background path for green screen segmentation (choose one of the three methods: path, buffer, or texture)
mEffects.setChromaKeyBackgroundPath("MY_BACKGROUND_PATH", ZegoEffectsScaleMode.ASPECT_FILL);
// Set the background buffer for green screen segmentation (choose one of the three methods: path, buffer, or texture)
mEffects.setChromaKeyBackgroundBuffer(buffer, bufferLength, param, ZegoEffectsScaleMode.ASPECT_FILL);
// Set the background texture for green screen segmentation (choose one of the three methods: path, buffer, or texture)
mEffects.setChromaKeyBackgroundTexture(textureID, param, ZegoEffectsScaleMode.ASPECT_FILL);
// Set the background path for green screen segmentation (choose one of the three methods: path, buffer, or texture)
zego_effects_set_chroma_key_background_path(handle, "MY_BACKGROUND_PATH", zego_effects_scale_mode_aspect_fill);
// Set the background buffer for green screen segmentation (choose one of the three methods: path, buffer, or texture)
zego_effects_set_chroma_key_background_buffer(handle, buffer, buffer_length, param, zego_effects_scale_mode_aspect_fill);
// Set the background texture for green screen segmentation (choose one of the three methods: path, buffer, or texture)
zego_effects_set_chroma_key_background_texture(handle, texture_id, param, zego_effects_scale_mode_aspect_fill);
// Set the background path for green screen segmentation (choose one of the three methods: path, buffer, or texture)
zego_effects_set_chroma_key_background_path(handle, "MY_BACKGROUND_PATH", zego_effects_scale_mode_aspect_fill);
// Set the background buffer for green screen segmentation (choose one of the three methods: path, buffer, or texture)
zego_effects_set_chroma_key_background_buffer(handle, buffer, buffer_length, param, zego_effects_scale_mode_aspect_fill);
// Set the background texture for green screen segmentation (choose one of the three methods: path, buffer, or texture)
zego_effects_set_chroma_key_background_texture(handle, texture_id, param, zego_effects_scale_mode_aspect_fill);
Call the method to set the background path for green screen segmentation.
// Set the background path for green screen segmentation
ZegoEffectsPlugin.instance.setChromaKeyBackgroundPath("MY_BACKGROUND_PATH", ZegoEffectsScaleMode.AspectFill);
// Set the background path for green screen segmentation
ZegoEffectsPlugin.instance.setChromaKeyBackgroundPath("MY_BACKGROUND_PATH", ZegoEffectsScaleMode.AspectFill);
// Set the background path for green screen segmentation
mEffects.setChromaKeyBackgroundPath("MY_BACKGROUND_PATH", ZegoEffectsScaleMode.ASPECT_FILL);
// Set the background path for green screen segmentation
mEffects.setChromaKeyBackgroundPath("MY_BACKGROUND_PATH", ZegoEffectsScaleMode.ASPECT_FILL);
Call the enableChromaKey method to enable the green screen segmentation feature, and then call the setChromaKeyParam method to set the parameters, adjust the color tolerance, edge smoothness index, and other parameters.
// Enable the green screen segmentation feature
[self.effects enableChromaKey:YES]
// Set the green screen segmentation parameters
ZegoEffectsChromaKeyParam *param = [[ZegoEffectsChromaKeyParam alloc] init];
[self.effects setChromaKeyParam:param];
// Enable the green screen segmentation feature
[self.effects enableChromaKey:YES]
// Set the green screen segmentation parameters
ZegoEffectsChromaKeyParam *param = [[ZegoEffectsChromaKeyParam alloc] init];
[self.effects setChromaKeyParam:param];
// Enable the green screen segmentation feature
mEffects.enableChromaKey(true);
// Set the green screen segmentation parameters
ZegoEffectsChromaKeyParam param = new ZegoEffectsChromaKeyParam();
mEffects.setChromaKeyParam(param);
// Enable the green screen segmentation feature
mEffects.enableChromaKey(true);
// Set the green screen segmentation parameters
ZegoEffectsChromaKeyParam param = new ZegoEffectsChromaKeyParam();
mEffects.setChromaKeyParam(param);
// Enable the green screen segmentation feature
zego_effects_enable_chroma_key(handle, true);
// Get the default value of the green screen segmentation parameters and set it
zego_effects_chroma_key_param param{};
zego_effects_get_default_chroma_key_param(handle, ¶m);
zego_effects_set_chroma_key_param(param);
// Enable the green screen segmentation feature
zego_effects_enable_chroma_key(handle, true);
// Get the default value of the green screen segmentation parameters and set it
zego_effects_chroma_key_param param{};
zego_effects_get_default_chroma_key_param(handle, ¶m);
zego_effects_set_chroma_key_param(param);
// Enable the green screen segmentation feature
ZegoEffectsPlugin.instance.enableChromaKey(true);
// Set the green screen segmentation parameters
ZegoEffectsChromaKeyParam param = ZegoEffectsChromaKeyParam();
param.smoothness = 80;
param.opacity = 100;
param.keyColor = 65280;
param.similarity = 67;
ZegoEffectsPlugin.instance.setChromaKeyParam(param);
// Enable the green screen segmentation feature
ZegoEffectsPlugin.instance.enableChromaKey(true);
// Set the green screen segmentation parameters
ZegoEffectsChromaKeyParam param = ZegoEffectsChromaKeyParam();
param.smoothness = 80;
param.opacity = 100;
param.keyColor = 65280;
param.similarity = 67;
ZegoEffectsPlugin.instance.setChromaKeyParam(param);
// Enable the green screen segmentation feature
mEffects.enableChromaKey(true);
// Set the green screen segmentation parameters
let param = new ZegoEffectsChromaKeyParam();
param.smoothness = 80;
param.opacity = 100;
param.keyColor = 65280;
param.similarity = 67;
mEffects.setChromaKeyParam(param);
// Enable the green screen segmentation feature
mEffects.enableChromaKey(true);
// Set the green screen segmentation parameters
let param = new ZegoEffectsChromaKeyParam();
param.smoothness = 80;
param.opacity = 100;
param.keyColor = 65280;
param.similarity = 67;
mEffects.setChromaKeyParam(param);
After enabling the green screen segmentation feature, you can further set background blur, background mosaic, and other features according to your needs.
Background blur for green screen segmentation
Call the enableChromaKeyBackgroundBlur method to enable the background blur feature for green screen segmentation. After enabling the background blur feature, you can call the setChromaKeyBackgroundBlurParam method to set the "intensity" parameter of the ZegoEffectsBlurParam object to adjust the intensity of background blur.
Note
After enabling the background blur feature for green screen segmentation, it will override the custom background and background mosaic features.
// Enable background blur for green screen segmentation
[self.effects enableChromaKeyBackgroundBlur:YES];
// Set the background blur parameter for green screen segmentation
ZegoEffectsBlurParam *param = [[ZegoEffectsBlurParam alloc] init];
param.intensity = 100;
[self.effects setChromaKeyBackgroundBlurParam:param];
// Enable background blur for green screen segmentation
[self.effects enableChromaKeyBackgroundBlur:YES];
// Set the background blur parameter for green screen segmentation
ZegoEffectsBlurParam *param = [[ZegoEffectsBlurParam alloc] init];
param.intensity = 100;
[self.effects setChromaKeyBackgroundBlurParam:param];
// Enable background blur for green screen segmentation
mEffects.enableChromaKeyBackgroundBlur(true);
// Set the background blur parameter for green screen segmentation
ZegoEffectsBlurParam param = new ZegoEffectsBlurParam();
param.intensity = 100;
mEffects.setChromaKeyBackgroundBlurParam(param);
// Enable background blur for green screen segmentation
mEffects.enableChromaKeyBackgroundBlur(true);
// Set the background blur parameter for green screen segmentation
ZegoEffectsBlurParam param = new ZegoEffectsBlurParam();
param.intensity = 100;
mEffects.setChromaKeyBackgroundBlurParam(param);
// Enable background blur for green screen segmentation
zego_effects_enable_chroma_key_background_blur(handle, true);
// Set the background blur parameter for green screen segmentation
zego_effects_blur_param param{};
param.intensity = 100;
zego_effects_set_chroma_key_background_blur_param(handle, ¶m);
// Enable background blur for green screen segmentation
zego_effects_enable_chroma_key_background_blur(handle, true);
// Set the background blur parameter for green screen segmentation
zego_effects_blur_param param{};
param.intensity = 100;
zego_effects_set_chroma_key_background_blur_param(handle, ¶m);
// Enable background blur for green screen segmentation
ZegoEffectsPlugin.instance.enableChromaKeyBackgroundBlur(true);
// Set the background blur parameter for green screen segmentation
ZegoEffectsBlurParam param = ZegoEffectsBlurParam();
param.intensity = 100;
ZegoEffectsPlugin.instance.setChromaKeyBackgroundBlurParam(param);
// Enable background blur for green screen segmentation
ZegoEffectsPlugin.instance.enableChromaKeyBackgroundBlur(true);
// Set the background blur parameter for green screen segmentation
ZegoEffectsBlurParam param = ZegoEffectsBlurParam();
param.intensity = 100;
ZegoEffectsPlugin.instance.setChromaKeyBackgroundBlurParam(param);
// Enable background blur for green screen segmentation
mEffects.enableChromaKeyBackgroundBlur(true);
// Set the background blur parameter for green screen segmentation
mEffects.setChromaKeyBackgroundBlurParam({ intensity: 100 });
// Enable background blur for green screen segmentation
mEffects.enableChromaKeyBackgroundBlur(true);
// Set the background blur parameter for green screen segmentation
mEffects.setChromaKeyBackgroundBlurParam({ intensity: 100 });
After enabling the background mosaic feature for green screen segmentation, it will override the custom background and background blur features.
// Enable background mosaic for green screen segmentation
[self.effects enableChromaKeyBackgroundMosaic:YES];
// Set the background mosaic parameter for green screen segmentation
ZegoEffectsMosaicParam *param = [[ZegoEffectsMosaicParam alloc] init];
param.intensity = 100;
[self.effects setChromaKeyBackgroundMosaicParam:param];
// Enable background mosaic for green screen segmentation
[self.effects enableChromaKeyBackgroundMosaic:YES];
// Set the background mosaic parameter for green screen segmentation
ZegoEffectsMosaicParam *param = [[ZegoEffectsMosaicParam alloc] init];
param.intensity = 100;
[self.effects setChromaKeyBackgroundMosaicParam:param];
// Enable background mosaic for green screen segmentation
mEffects.enableChromaKeyBackgroundMosaic(true);
// Set the background mosaic parameter for green screen segmentation
ZegoEffectsMosaicParam param = new ZegoEffectsMosaicParam();
param.intensity = 100;
mEffects.setChromaKeyBackgroundMosaicParam(param);
// Enable background mosaic for green screen segmentation
mEffects.enableChromaKeyBackgroundMosaic(true);
// Set the background mosaic parameter for green screen segmentation
ZegoEffectsMosaicParam param = new ZegoEffectsMosaicParam();
param.intensity = 100;
mEffects.setChromaKeyBackgroundMosaicParam(param);
// Enable background mosaic for green screen segmentation
zego_effects_enable_chroma_key_background_mosaic(handle, true);
// Set the background mosaic parameter for green screen segmentation
zego_effects_mosaic_param param;
param.intensity = 100;
param.type = zego_effects_mosaic_type::zego_effects_mosaic_type_square;
zego_effects_set_chroma_key_background_mosaic_param(handle, ¶m);
// Enable background mosaic for green screen segmentation
zego_effects_enable_chroma_key_background_mosaic(handle, true);
// Set the background mosaic parameter for green screen segmentation
zego_effects_mosaic_param param;
param.intensity = 100;
param.type = zego_effects_mosaic_type::zego_effects_mosaic_type_square;
zego_effects_set_chroma_key_background_mosaic_param(handle, ¶m);
// Enable background mosaic for green screen segmentation
ZegoEffectsPlugin.instance.enableChromaKeyBackgroundMosaic(true);
// Set the background mosaic parameter for green screen segmentation
ZegoEffectsBlurParam param = ZegoEffectsBlurParam();
param.intensity = 100;
ZegoEffectsPlugin.instance.setChromaKeyBackgroundMosaicParam(param);
// Enable background mosaic for green screen segmentation
ZegoEffectsPlugin.instance.enableChromaKeyBackgroundMosaic(true);
// Set the background mosaic parameter for green screen segmentation
ZegoEffectsBlurParam param = ZegoEffectsBlurParam();
param.intensity = 100;
ZegoEffectsPlugin.instance.setChromaKeyBackgroundMosaicParam(param);
// Enable background mosaic for green screen segmentation
mEffects.enableChromaKeyBackgroundMosaic(true);
// Set the background mosaic parameter for green screen segmentation
mEffects.setChromaKeyBackgroundMosaicParam({ intensity: 100 });
// Enable background mosaic for green screen segmentation
mEffects.enableChromaKeyBackgroundMosaic(true);
// Set the background mosaic parameter for green screen segmentation
mEffects.setChromaKeyBackgroundMosaicParam({ intensity: 100 });