前言
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;
}
}
以上
Love for Ever Day轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/467977.html
標籤:其他
