前言

OpenCV Plus Unity 有关的教程实在是少之又少,Opencv的有很多,但是在Unity上应用的相关教程很少,比如付费的OpenCV For Unity ,就已经很少了,目前经济有限,只能选择更加小众的OpenCV Plus Unity 国内甚至搜不到相关的概念,更别提学习教程了。真的就是纯靠自己一点点摸索尝试出来的。很不容易,不过成功之后也是成就感满满。

1、导入OpenCV Plus Unity包

2、创建摄像机纹理

创建摄像机纹理并将其拖放到需要读取的摄像机上边

3、创建脚本

创建一个脚本:Get_cam_new,并将其附加到需处理的摄像机上边

4、代码分析

(1)引入包
using OpenCvSharp;
(2)所需变量
    //このScriptはMainCameraにアタッチしてください
    public RenderTexture renderTexture;             //mainCameraにつけるRendertexture(アタッチしてね)
    Texture2D kakunin, dstTexture;                  
    Camera mainCamera;
    GameObject hand;
(3)获取摄像机及所调用函数
    private void Update()
    {
        mainCamera = GetComponent<Camera>();
        kakunin = CreateTexture2D(renderTexture);
        Tex2D_to_Mat_show(kakunin);
    }
(4)将摄像机图像转 Texture2D 的函数定义
Texture2D CreateTexture2D(RenderTexture rt)
    {
        //Texture2Dを作成
        Texture2D texture2D = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false, false);

        //subCameraにRenderTextureを入れる
        mainCamera.targetTexture = rt;

        //手動でカメラをレンダリングします
        mainCamera.Render();

        RenderTexture.active = rt;
        texture2D.ReadPixels(new UnityEngine.Rect(0, 0, rt.width, rt.height), 0, 0);
        texture2D.Apply();

        ////元に戻す別のカメラを用意してそれをRenderTexter用にすれば下のコードはいらないです。
        //mainCamera.targetTexture = null;
        //RenderTexture.active = null;
        return texture2D;
    }
(5)将 Texture2D 转 Mat 并经过 OpenCV 灰度处理输出到RawImage
    void Tex2D_to_Mat_show(Texture2D tex)
    {
        //Texture2D -> Mat
        Mat srcMat = OpenCvSharp.Unity.TextureToMat(tex);
        
        Mat grayMat = new Mat();
        Cv2.CvtColor(srcMat, grayMat, ColorConversionCodes.RGBA2GRAY);

        // Mat → Texture2D
        if (this.dstTexture == null)
        {
            this.dstTexture = new Texture2D(grayMat.Width, grayMat.Height, TextureFormat.RGBA32, false);
        }

        OpenCvSharp.Unity.MatToTexture(grayMat, this.dstTexture);

        // 表示
        GameObject.FindObjectOfType<RawImage>().texture = this.dstTexture;
    }

5、回到Unity,将渲染器纹理附加脚本上

6、创建一个cube,加上旋转脚本使其自转

旋转代码 Rotate:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
///
/// </summary>
public class Rotate : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }
    public float speed = 90f;
    // Update is called once per frame
    void Update()
    {
        this.transform.Rotate(Vector3.up * Time.deltaTime * speed);
    }
}

7、添加RawImage

   

8、运行测试

被灰度化后的图像上的Cube在不断旋转,成功

9、Get_cam_new源代码

using UnityEngine;
using UnityEngine.UI;
using OpenCvSharp;

public class Get_cam_new : MonoBehaviour
{

    //このScriptはMainCameraにアタッチしてください
    public RenderTexture renderTexture;             //mainCameraにつけるRendertexture(アタッチしてね)
    Texture2D kakunin, dstTexture;                  
    Camera mainCamera;
    GameObject hand;

    void Start()
    {
        //mainCamera = GetComponent<Camera>();
        //kakunin = CreateTexture2D(renderTexture);
        //Tex2D_to_Mat_show(kakunin);
    }
    private void Update()
    {
        mainCamera = GetComponent<Camera>();
        kakunin = CreateTexture2D(renderTexture);
        Tex2D_to_Mat_show(kakunin);
    }

    /// <summary>
    /// ここでTextur2Dに変換しているよ
    /// </summary>
    /// <param name="rt"></param>
    /// <returns></returns>
    Texture2D CreateTexture2D(RenderTexture rt)
    {
        //Texture2Dを作成
        Texture2D texture2D = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false, false);

        //subCameraにRenderTextureを入れる
        mainCamera.targetTexture = rt;

        //手動でカメラをレンダリングします
        mainCamera.Render();

        RenderTexture.active = rt;
        texture2D.ReadPixels(new UnityEngine.Rect(0, 0, rt.width, rt.height), 0, 0);
        texture2D.Apply();

        ////元に戻す別のカメラを用意してそれをRenderTexter用にすれば下のコードはいらないです。
        //mainCamera.targetTexture = null;
        //RenderTexture.active = null;
        return texture2D;
    }

    void Tex2D_to_Mat_show(Texture2D tex)
    {
        //Texture2D -> Mat
        Mat srcMat = OpenCvSharp.Unity.TextureToMat(tex);
        
        Mat grayMat = new Mat();
        Cv2.CvtColor(srcMat, grayMat, ColorConversionCodes.RGBA2GRAY);

        // Mat → Texture2D
        if (this.dstTexture == null)
        {
            this.dstTexture = new Texture2D(grayMat.Width, grayMat.Height, TextureFormat.RGBA32, false);
        }

        OpenCvSharp.Unity.MatToTexture(grayMat, this.dstTexture);

        // 表示
        GameObject.FindObjectOfType<RawImage>().texture = this.dstTexture;
    }
}

以上

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/HanaKoo/p/16206089.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!