OpenCV For Unity 入门教程(一): 实现简单的抠图

1. 需要引入 OpenCVForUnity 命名空间,结构图如下:

2.代码如下:

using OpenCVForUnity;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RemoveImage : MonoBehaviour {
    public int alpha = 150;
    void Start()

    {
        //把图片读进三通道的容器
        var src = Imgcodecs.imread(Application.streamingAssetsPath + "/open.jpg", 1);
        //把尺寸设置为大小
        Imgproc.resize(src, src, new Size(500, 500));
        //建立一个四通道的容器
        var dst = new Mat(src.cols(), src.rows(), CvType.CV_8UC4);
        //转换色彩空间
        Imgproc.cvtColor(src, dst, Imgproc.COLOR_BGR2RGBA);
        // 遍历一下这个 dst 容器, 里面是处理图像的逻辑 会输出一个处理过的 dst 返回出来
        for (int i = 0; i < dst.cols(); i++)
        {
            for (int j = 0; j < dst.rows(); j++)
            {
                //这个150是阈值,你可以自己定义来试试效果
                if (dst.get(j, i)[0] > alpha)
                {
                    dst.put(j, i, 255, 255, 255, 0);
                }
            }
        }
        // 定义一个 Texture2D 对象
        var tex = new Texture2D(dst.cols(), dst.rows(), TextureFormat.RGBA32, false);
        // 将 dst 转换成这个 Texture2D 对象
        Utils.matToTexture2D(dst, tex);
        // 将处理后的图片现实在 RawImage 组件上
        var raw = this.GetComponentInChildren<RawImage>();
        raw.texture = tex;
        raw.SetNativeSize();
    }
}

 

3.效果如下:

4.参考链接如下: https://www.jianshu.com/p/3f1876c62b11

Works with Unity Cloud Build iOS & Android support Windows10 UWP support WebGL support Win & Mac & Linux Standalone support Preview support in the Editor OpenCV for Unity is an Assets Plugin for using OpenCV 3.4.2 from within Unity. Official Site | ExampleCode | Android Demo WebGL Demo | Tutorial & Demo Video | Forum | API Reference | Support Modules Features: - Since this package is a clone of OpenCV Java, you are able to use the same API as OpenCV Java 3.4.2 (link). - You can image processing in real-time by using the WebCamTexture capabilities of Unity. (real-time face detection works smoothly on iPhone 5) - Provides a method to interconversion of Unity's Texture2D and OpenCV's Mat. - IDisposable is implemented in many classes.You can manage the resources with the “using” statement. - Examples of integration with other publisher assets are available.(e.g. PlayMaker, NatCam, NatCorder) ExampleCode using OpenCV for Unity is available. MarkerBased AR Example MarkerLess AR Example FaceTracker Example FaceSwapper Example FaceMask Example RealTime FaceRecognition Example GoogleVR with OpenCV for Unity Example Kinect with OpenCV for Unity Example AVPro with OpenCV for Unity Example HoloLens with OpenCV for Unity Example PlayMakerActions for OpenCVforUnity NatCam with OpenCVForUnity Example NatCorder with OpenCVForUnity Example OpenCV for Unity uses OpenCV under 3-clause BSD License; see Third-Party Notices.txt file in package for details. System Requirements Build Win Standalone & Preview Editor : Windows 7 or later Build Mac Standalone & Preview Editor : OSX 10.9 or later
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值