Unity3d无绿幕抠像处理

1.unity3d可以借助体感工具进行抠图,但是会有毛刺,借助第三方SDK在无绿幕情况下实现抠图处理,习惯性先上效果: (效果处理的很完美)

2.本文借助腾讯云人像分割,进入腾讯云后搜索:人像分割

 3.具体大家可以去研究,先上代码

void CutoutPicture()
    {
        try
        {
            Credential cred = new Credential
            {
                SecretId = TencentKey.SecretId,
                SecretKey = TencentKey.SecretKey,
            };
            // 实例化一个client选项,可选的,没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.Endpoint = ("bda.tencentcloudapi.com");
            clientProfile.HttpProfile = httpProfile;

            // 实例化要请求产品的client对象,clientProfile是可选的
            BdaClient client = new BdaClient(cred, "ap-shanghai", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SegmentPortraitPicRequest req = new SegmentPortraitPicRequest();

            //req.Image=""接受图片两种格式:
            //1.一种是服务地址:https://
            //2.base64格式:/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJ...

            //本文采用将本地图片转化为base64
            req.Image = ImageOrBase.Instance.ImgToBase64String(imgPath);
            // 返回的resp是一个SegmentPortraitPicResponse的实例,与请求对象对应
            SegmentPortraitPicResponse resp = client.SegmentPortraitPicSync(req);

            //将resp.ResultImage返回的base64转化为图片
            ImageOrBase.Instance.Base64ToImg(resp.ResultImage);
        }
        catch (Exception e)
        {
            Debug.Log(e.ToString());
        }

4.图片转base64,base64转图片代码如下:

/// <summary>
    /// 图片转换成base64编码
    /// </summary>
    public string ImgToBase64String(string path)
    {
        try
        {
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, (int)fs.Length);
            string base64String = Convert.ToBase64String(buffer);
            return base64String;
        }
        catch (Exception e)
        {
            Debug.Log("ImgToBase64String 转换失败:" + e.Message);
        }
        return null;
    }

    /// <summary>
    /// base64编码文本转换成图片
    /// </summary>
    public void Base64ToImg(string recordBase64String)
    {
        string base64 = recordBase64String;
        byte[] bytes = Convert.FromBase64String(base64);
        Texture2D tex2D = new Texture2D(100, 100);
        tex2D.LoadImage(bytes);
        Sprite s = Sprite.Create(tex2D, new Rect(0, 0, tex2D.width, tex2D.height), new Vector2(0.5f, 0.5f));
        imagePic.sprite = s;
        imagePic.SetNativeSize();
        Resources.UnloadUnusedAssets();
    }

5.密匙可以在腾讯云中获取

 6.注:图片分辨率不得超过2000*2000,大小不得超过5M,支持PNG、JPG、JPEG、BMP,不支持 GIF 图片。

猜你喜欢

转载自blog.csdn.net/m0_38116456/article/details/130990273