[unity] opencv plus unity obtain the camera in unity and call OpenCV to grayscale the instance

Time:2022-5-2

preface

There are few tutorials related to opencv plus unity. There are many of OpenCV, but there are few relevant tutorials applied on unity. For example, there are few paid opencv for unity. At present, the economy is limited, so we can only choose a smaller number of OpenCV plus unity. There are even no relevant concepts in China, let alone learning tutorials. It’s really just a little bit of groping and trying by yourself. It’s not easy, but it’s also full of achievement after success.

1. Import opencv plus unity package

[unity] opencv plus unity obtain the camera in unity and call OpenCV to grayscale the instance

2. Create camera texture

Create a camera texture and drag it onto the camera you want to read

[unity] opencv plus unity obtain the camera in unity and call OpenCV to grayscale the instance

3. Create script

Create a script: get_ cam_ Attach it to the new side of the camera and

[unity] opencv plus unity obtain the camera in unity and call OpenCV to grayscale the instance

4. Code analysis

(1) Introduction package
using OpenCvSharp;
(2) Required variables
//このScriptはMainCameraにアタッチしてください
    public RenderTexture renderTexture;             //mainCameraにつけるRendertexture(アタッチしてね)
    Texture2D kakunin, dstTexture;                  
    Camera mainCamera;
    GameObject hand;
(3) Get the camera and the called function
private void Update()
    {
        mainCamera = GetComponent();
        kakunin = CreateTexture2D(renderTexture);
        Tex2D_to_Mat_show(kakunin);
    }
(4) Function definition of converting camera image to texture2d
Texture2D CreateTexture2D(RenderTexture rt)
    {
        //Texture2d を
        Texture2D texture2D = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false, false);

        //Subcamera rendertexture
        mainCamera.targetTexture = rt;

        //Manual operation
        mainCamera.Render();

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

        ////Yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan.
        //mainCamera.targetTexture = null;
        //RenderTexture.active = null;
        return texture2D;
    }
(5) Convert texture2d to mat and output it to rawimage after opencv gray processing
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);

        //Express
        GameObject.FindObjectOfType().texture = this.dstTexture;
    }

5. Back to unity, attach the renderer texture to the script

[unity] opencv plus unity obtain the camera in unity and call OpenCV to grayscale the instance

6. Create a cube and rotate the script to make it rotate

[unity] opencv plus unity obtain the camera in unity and call OpenCV to grayscale the instance

Rotation code rotate:

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

/// 
///
/// 
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. Add rawimage

[unity] opencv plus unity obtain the camera in unity and call OpenCV to grayscale the instance    [unity] opencv plus unity obtain the camera in unity and call OpenCV to grayscale the instance

8. Run test

The cube on the grayed image is rotating continuously and successfully

[unity] opencv plus unity obtain the camera in unity and call OpenCV to grayscale the instance

9、Get_ cam_ New source code

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();
        //kakunin = CreateTexture2D(renderTexture);
        //Tex2D_to_Mat_show(kakunin);
    }
    private void Update()
    {
        mainCamera = GetComponent();
        kakunin = CreateTexture2D(renderTexture);
        Tex2D_to_Mat_show(kakunin);
    }

    /// 
    ///Click textur2d to select textur2d
    /// 
    /// 
    /// 
    Texture2D CreateTexture2D(RenderTexture rt)
    {
        //Texture2d を
        Texture2D texture2D = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false, false);

        //Subcamera rendertexture
        mainCamera.targetTexture = rt;

        //Manual operation
        mainCamera.Render();

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

        ////Yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan, yuan.
        //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);

        //Express
        GameObject.FindObjectOfType().texture = this.dstTexture;
    }
}

above

Recommended Today

A front-end developer's Vim is the same as an IDE

Here is my new configurationjaywcjlove/vim-webI've been grinding it, and it's basically ready to use. Take it out and cheat the star Install The latest version of Vim 7.4+ uses (brew install macvim) installation, vim version updatebrew install macvim –override-system-vim View configuration locations # Enter vim and enter the following characters :echo $MYVIMRC download vim-web Download […]