You need to enable JavaScript to run this app.
导航
绿幕抠图
最近更新时间:2025.03.17 10:51:18首次发布时间:2022.02.25 17:16:51
我的收藏
有用
有用
无用
无用
简介

绿幕抠图SDK提供识别绿幕和绿色背景,进行抠图。
该算法为V423新增算法。
从V450开始,绿幕抠图算法增加绿色保留模式:默认算法效果为了减弱绿幕反射到前景人/物上的绿色,会做去绿处理,导致包含绿色元素的物体产生色偏,新增的绿色保留功能会让前景绿色更接近肉眼所见,但相应不能去除绿幕反光;因此前景无绿色人/物时建议使用默认算法效果,有绿色时可使用绿色保留,主要适合电商带货卖绿色物体场景

技术规格
支持平台Android、iOS
支持输入格式RGBA8888、BGRA8888、BGR888、RGB888
内存占用<42M (测试设备OPPO R11)
检测速度<68ms(测试设备OPPO R11)
C接口说明

详细接口说明查看头文件:bef_effect_ai_chroma_keying.h

1.创建绿幕抠图句柄

BEF_SDK_API bef_effect_result_t  
bef_effect_ai_chroma_keying_create(bef_effect_handle_t *handle);

参数说明

参数名参数类型参数说明
handlebef_effect_handle_t创建的绿幕抠图算法句柄

返回值
成功返回 BEF_RESULT_SUC, 失败返回相应错误码, 具体请参考 bef_effect_ai_public_define.h

2.从文件初始化模型参数

// 当前版本暂不支持模型设置, model_path 设置成空字符串就好
BEF_SDK_API bef_effect_result_t  
bef_effect_ai_chroma_keying_init(
    bef_effect_handle_t handle,  
    const char* model_path = "");

参数说明

参数名参数类型参数说明
handlebef_effect_handle_t绿幕抠图算法的句柄
model_pathconst char*模型文件所在的文件夹(暂不支持),可设置为""

返回值
成功返回 BEF_RESULT_SUC, 失败返回相应错误码, 具体请参考 bef_effect_ai_public_define.h

3.设置SDK参数

// 设置处理参数。该参数为V4.5.0新增,V450以前版本均无此参数
BEF_SDK_API
void bef_effect_ai_chroma_keying_set_processParam(float similar, float smoothness, float correction, float clip_black, float clip_white);

参数说明
该参数为V4.5.0新增,V450以前版本均无此参数

参数名参数类型参数说明
similarfloat相似度(最主要影响抠图强度,一般只建议修改该参数)
smoothnessfloat平滑度(搭配similar的,影响半透明等过渡状态)
correctionfloat去绿强度
clip_blackfloat对最后结果mask做个黑白截断的曲线增强
clip_whitefloat对最后结果mask做个黑白截断的曲线增强
// 设置float类型的参数接口
BEF_SDK_API bef_effect_result_t
bef_effect_ai_chroma_keying_set_paramF(
    bef_effect_handle_t handle,
    bef_ai_chroma_keying_param_type type,
    float value);

参数说明

参数名参数类型参数说明
handlebef_effect_handle_t绿幕抠图算法的句柄
typebef_ai_chroma_keying_param_type绿幕抠图参数类型
valuefloat绿幕抠图参数值

返回值
成功返回 BEF_RESULT_SUC, 失败返回相应错误码, 具体请参考 bef_effect_ai_public_define.h

// 设置int类型的参数接口
BEF_SDK_API bef_effect_result_t
bef_effect_ai_chroma_keying_set_paramI(
    bef_effect_handle_t handle,
    bef_ai_chroma_keying_param_type type,
    int value);

参数说明

参数名参数类型参数说明
handlebef_effect_handle_t绿幕抠图算法的句柄
typebef_ai_chroma_keying_param_type绿幕抠图参数类型
valueint绿幕抠图参数值

返回值
成功返回 BEF_RESULT_SUC, 失败返回相应错误码, 具体请参考 bef_effect_ai_public_define.h
备注
bef_ai_chroma_keying_param_type 详细见bef_effect_ai_chroma_keying.h

/**
 * @brief 参数枚举
 *
 */
typedef enum {
    BEF_AI_CHROMA_KEYING_METHOD = 0,    // 算法key, int。
                                        // 可选值有:0: 移动端实时方案【推荐默认】
                                        //         1: 服务端方案
    BEF_AI_CHROMA_KEYING_CAL_COLOR = 1, // 算法背景色计算key, int。
                                        // 可选值有:0:全图自动主色提取
                                        //         1: 绿幕场景精细化主色提取【默认】
                                        //         2:人工设置主色提取(说明:该选项下返回变量的背景色彩为人工设置色彩)
    BEF_AI_CHROMA_KEYING_BG_R = 2,      // 背景色设置,红色,0~255【默认0】, int。
    BEF_AI_CHROMA_KEYING_BG_G = 3,      // 背景色设置,绿色,0~255【默认255】, int。
    BEF_AI_CHROMA_KEYING_BG_B = 4,      // 背景色设置,蓝色,0~255【默认0】, int。
    BEF_AI_CHROMA_KEYING_BALP = 5,      // 强度调节,float。
                                        // 色彩范围参数 case1: 小于0,自动模式,chroma_keying_cal_color为“1”时候推荐,如【默认-1】
                                        //            case2: 0~1,手动模式,chroma_keying_cal_color为“2”时候推荐设置为0.6~0.7
    BEF_AI_CHROMA_KEYING_KALP = 6,      // 强度调节:锐利度参数【推荐0.15】,建议不调节,float。
    BEF_AI_CHROMA_KEYING_SMOOTH = 7,    // 平滑系数,float。
                                        // case1:小于0时无平滑
                                        // case2:0 仅启用mask时序平滑,估色无时序平滑【推荐0】
                                        // case3:0~1 启用mask时序平滑,参数描述背景色估计平滑下强度(算法内部限制最大为1)
    BEF_AI_CHROMA_KEYING_SHARPEN = 8,   // mask边界锐化key,int。
                                        // 可选有: <=0: 不启用边界锐化
                                        //         >=1: 启用边界锐化【推荐设置为1】
    BEF_AI_CHROMA_KEYING_GET_MASK = 9,  // mask计算并返回,int。
                                        // 可选有:<=0: 不返回mask(仅获得背景色彩估计)
                                        //        >=1: 计算mask,同时获得背景色估计和背景色估值【默认请设1】
} bef_ai_chroma_keying_param_type;

4.进行绿幕抠图操作

BEF_SDK_API bef_effect_result_t
bef_effect_ai_chroma_keying_detect(
    bef_effect_handle_t handle,
    const unsigned char *image,
    bef_ai_pixel_format pixel_format,
    int image_width,
    int image_height,
    int image_stride,
    bef_ai_rotate_type orientation,
    bef_ai_chroma_keying_ret *result,
    bool soft_process);  // V450新增去绿处理

参数说明

参数名参数类型参数说明
handlebef_effect_handle_t绿幕抠图算法的句柄
imageconst unsigned char*为传入图像的大小
pixel_formatbef_ai_pixel_format传入图像的类型
image_widthint传入图像的宽
image_heightint传入图像的高
image_strideint传入图像的步长
orientationbef_ai_rotate_type传入图像旋转角
resultbef_ai_chroma_keying_ret*算法处理返回结果
soft_processbool是否保留原画面绿色,进行弱去绿,V450新增

返回值
成功返回 BEF_RESULT_SUC, 失败返回相应错误码, 具体请参考 bef_effect_ai_public_define.h
备注
bef_ai_chroma_keying_ret 详细见bef_effect_ai_chroma_keying.h
版本4.4.0及以上输出结果:

/**
 * @brief 输出结果
 *
 */
typedef struct
{
    int width;             // 返回宽
    int height;            // 返回高
    unsigned char color_b; // 背景色识别:蓝色
    unsigned char color_g; // 背景色识别:绿色
    unsigned char color_r; // 背景色识别:红色
    int outputTexture; // 输出的纹理rgb为原图大小纹理, alpha 通道为原图和目标图的混合系数
} bef_ai_chroma_keying_ret;

版本4.4.0以前输出结果:

/**
 * @brief 输出结果
 *
 */
typedef struct
{
    int width; // 返回mask宽
        int height; // 返回mask高
        unsigned  char color\_b; // 背景色识别:蓝色
        unsigned  char color\_g; // 背景色识别:绿色
        unsigned  char color\_r; // 背景色识别:红色
        uint8_t* mask; // 绿幕抠图mask数据
} bef_ai_chroma_keying_ret;

5.释放句柄

BEF_SDK_API bef_effect_result_t
bef_effect_ai_chroma_keying_release(bef_effect_handle_t handle);

参数说明

参数名参数类型参数说明
handlebef_effect_handle_t绿幕抠图算法的句柄

返回值
成功返回 BEF_RESULT_SUC, 失败返回相应错误码, 具体请参考 bef_effect_ai_public_define.h

6.绿幕抠图授权

// 离线license检测方式
BEF_SDK_API bef_effect_result_t
bef_effect_ai_chroma_keying_check_license(
    bef_effect_handle_t handle,
    const char *licensePath);

// 在线license检测方式
BEF_SDK_API bef_effect_result_t
bef_effect_ai_chroma_keying_check_online_license(
    bef_effect_handle_t handle,
    const char *licensePath);

参数说明

参数名参数类型参数说明
handlebef_effect_handle_t绿幕抠图算法的句柄
licensePathconst char *授权文件字符串

返回值
成功返回 BEF_RESULT_SUC, 失败返回相应错误码, 具体请参考 bef_effect_ai_public_define.h

Java 接口说明

接口说明
详细接口说明查看文件:com.bytedance.labcv.effectsdk.ChromaKeying.java

1.初始化绿幕抠图句柄

public int init(
    Context context,
    String modelPath,
    String licensePath,
    boolean onlineLicense)

参数说明

参数名参数类型参数说明
contextContext应用上下文
modelpathString模型文件绝对路径
licensePathString授权文件绝对路径
onlineLicenseboolean授权类型

返回值
成功返回BEF_RESULT_SUC,否则返回对应的错误码

2.设置SDK参数

// 设置处理参数。V450新增参数,V450版本以前均无此参数设置
public int setProcessParam(float similar, float smoothness, float correction, float clip_black, float clip_white)

参数说明
V450新增参数,V450版本以前均无此参数设置

参数名参数类型参数说明
similarfloat相似度(最主要影响抠图强度,一般只建议修改该参数)
smoothnessfloat平滑度(搭配similar的,影响半透明等过渡状态)
correctionfloat去绿强度
clip_blackfloat对最后结果mask做个黑白截断的曲线增强
clip_whitefloat对最后结果mask做个黑白截断的曲线增强
// 设置float类型的参数接口
public int setParamF(
    BytedEffectConstants.ChromaKeyingParamType paramType,
    float value)

参数说明

参数名参数类型参数说明
paramTypeBytedEffectConstants.ChromaKeyingParamType输入参数类型
valuefloat输入参数数值
// 设置int类型的参数接口
public int setParamI(
    BytedEffectConstants.ChromaKeyingParamType paramType,
    int value)

参数说明

参数名参数类型参数说明
paramTypeBytedEffectConstants.ChromaKeyingParamType输入参数类型
valueint输入参数数值

返回值
成功返回BEF_RESULT_SUC,否则返回对应的错误码
备注
关于BytedEffectConstants.ChromaKeyingParamType可参考com.bytedance.labcv.effectsdk.BytedEffectConstants.java,并且其定义与C接口bef_ai_chroma_keying_param_type保持一致。

3.绿幕抠图

public BefChromaKeyingInfo detect(
    ByteBuffer buffer,
    BytedEffectConstants.PixlFormat pixelFormat,
    int imageWidth,
    int imageHeight,
    int imageStride,
    BytedEffectConstants.Rotation orientation,
    boolean soft\_process)  //V450新增去绿处理

参数说明

参数名参数类型参数说明
bufferByteBuffer输入图像数据
pixelFormatBytedEffectConstants.PixlFormat输入数据格式
imageWidthint输入图像宽度
imageHeightint输入图像高度
imageStrideint输入图像步长
orientationBytedEffectConstants.Rotation输入图像旋转角
soft_processboolean是否保留原画面绿色,V450新增

备注
类BefChromaKeyingInfo详细情况可以参考com.bytedance.labcv.effectsdk.BefChromaKeyingInfo.java,并且其定义与C接口bef_ai_chroma_keying_ret保持一致。
版本4.4.0及以上输出结果:

/**
 * 绿幕抠图结果
 */
public class BefChromaKeyingInfo {
    private int width;
    private int height;
    private byte color_b;
    private byte color_g;
    private byte color_r;
    private int outputTexture;  // 输出的纹理rgb为原图大小纹理, alpha 通道为原图和目标图的混合系数

    public byte getColor_b() {
        return color_b;
    }

    public byte getColor_g() {
        return color_g;
    }

    public byte getColor_r() {
        return color_r;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }
    
    public int getOutputTexture() {  
            return outputTexture;  
        }

    @Override
    public String toString() {
        return String.format("l: %d w:%d, h:%d", mask.length, width, height);
    }
}

版本4.4.0以前输出结果:

/**
 * 绿幕抠图结果
 */
public class BefChromaKeyingInfo {
    private int width;
    private int height;
    private byte color_b;
    private byte color_g;
    private byte color_r;
    private byte\[\] mask;

    public byte getColor_b() {
        return color_b;
    }

    public byte getColor_g() {
        return color_g;
    }

    public byte getColor_r() {
        return color_r;
    }
    
    public byte[] getMask() {  
            return mask;  
        }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    @Override
    public String toString() {
        return String.format("l: %d w:%d, h:%d", mask.length, width, height);
    }
}

返回值
成功返回 类BefChromaKeyingInfo-绿幕抠图结果

4.释放绿幕抠图句柄

public void release()
FAQ

1.如果出现任何异常,请查看是否有以下类型日志

答:建议首先查看以下tag输出Error类型的日志:
bef_effect_ai 和SMASH_E_LOG 以及 EffectSDK

错误码

错误码请参考错误码表