You need to enable JavaScript to run this app.
导航

绿幕抠图

最近更新时间2023.06.28 14:42:49

首次发布时间2022.02.25 17:16:51

简介

绿幕抠图SDK提供识别绿幕和绿色背景,进行抠图。

技术规格
支持平台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参数

// 设置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);

参数说明

参数名参数类型参数说明
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*算法处理返回结果

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

备注

bef_ai_chroma_keying_ret 详细见bef_effect_ai_chroma_keying.h

/**
 * @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参数

// 设置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)

参数说明

参数名参数类型参数说明
bufferByteBuffer输入图像数据
pixelFormatBytedEffectConstants.PixlFormat输入数据格式
imageWidthint输入图像宽度
imageHeightint输入图像高度
imageStrideint输入图像步长
orientationBytedEffectConstants.Rotation输入图像旋转角

备注

类BefChromaKeyingInfo详细情况可以参考com.bytedance.labcv.effectsdk.BefChromaKeyingInfo.java,并且其定义与C接口bef_ai_chroma_keying_ret保持一致。

/**
 * 绿幕抠图结果
 */
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

错误码

错误码请参考错误码表