advancedskrald/ChessAR/Assets/OpenCVForUnity/Examples/Basic/Texture2DToMatExample/Texture2DToMatExample.cs

53 lines
1.5 KiB
C#
Raw Normal View History

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using OpenCVForUnity.CoreModule;
using OpenCVForUnity.UnityUtils;
namespace OpenCVForUnityExample
{
/// <summary>
/// Texture2DToMat Example
/// An example of converting a Texture2D image to OpenCV's Mat format.
/// </summary>
public class Texture2DToMatExample : MonoBehaviour
{
// Use this for initialization
void Start ()
{
//if true, The error log of the Native side OpenCV will be displayed on the Unity Editor Console.
Utils.setDebugMode (true);
Texture2D imgTexture = Resources.Load ("lena") as Texture2D;
Mat imgMat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC4);
Utils.texture2DToMat (imgTexture, imgMat);
Debug.Log ("imgMat.ToString() " + imgMat.ToString ());
Texture2D texture = new Texture2D (imgMat.cols (), imgMat.rows (), TextureFormat.RGBA32, false);
Utils.matToTexture2D (imgMat, texture);
gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
Utils.setDebugMode (false);
}
// Update is called once per frame
void Update ()
{
}
/// <summary>
/// Raises the back button click event.
/// </summary>
public void OnBackButtonClick ()
{
SceneManager.LoadScene ("OpenCVForUnityExample");
}
}
}