Chapter 01

Texture2DToMatExampleを読み解く

A
A
2021.12.26に更新


Matインスタンスを作成

Mat imgMat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC4);

Mat()

OpenCVForUnity.CoreModule.Mat.Mat	(	
int 	rows,
int 	cols,
int 	type 
)	

https://enoxsoftware.github.io/OpenCVForUnity/3.0.0/doc/html/class_open_c_v_for_unity_1_1_core_module_1_1_mat.html#a07998ca9da3c7f0f0795a1b1094b36a4

Parameters

rows Number of rows in a 2D array.
cols Number of columns in a 2D array.
type Array type. Use CV_8UC1,..., CV_64FC4 to create 1-4 channel matrices, or CV_8UC(n),..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices.

typeとは

https://tech-blog.s-yoshiki.com/entry/76


Texture2DをMatに変換

Utils.texture2DToMat (imgTexture, imgMat);

texture2DToMat()

static void OpenCVForUnity.UnityUtils.Utils.texture2DToMat(	
Texture2D 	texture2D,
Mat 	mat,
bool 	flip = true,
int 	flipCode = 0 
)	

https://enoxsoftware.github.io/OpenCVForUnity/3.0.0/doc/html/class_open_c_v_for_unity_1_1_unity_utils_1_1_utils.html#a97e3e9b88fb0724aad7584bc2f516c70

Converts Unity Texture2D to OpenCV Mat.

This method converts the Unity Texture2D image to the OpenCV Mat.
The Texture object must have the read/write enabled flag set in the texture import settings, otherwise this function will fail. (GetPixels32() method is needed.)
The Mat object must have the same size as the Texture2D (width * height).
The Mat object must have the types 'CV_8UC4' (RGBA) , 'CV_8UC3' (RGB) or 'CV_8UC1' (GRAY).

Parameters

texture2D the Texture2D object must have the read/write enabled flag set in the texture import settings, otherwise this function will fail. (GetPixels32() method is needed.)
mat the Mat object must have the same size as the Texture2D (width * height). The Mat object must have the types 'CV_8UC4' (RGBA) , 'CV_8UC3' (RGB) or 'CV_8UC1' (GRAY).
flip if true, the mat is fliped after converting.
flipCode a flag to specify how to flip the array; 0 means flipping around the x-axis and positive value (for example, 1) means flipping around y-axis. Negative value (for example, -1) means flipping around both axes.


Texture2Dインスタンスを作成

Texture2D texture = new Texture2D (imgMat.cols (), imgMat.rows (), TextureFormat.RGBA32, false);

Texture2D Constructor

Texture2D(int width, int height, TextureFormat textureFormat = TextureFormat.RGBA32, bool mipChain = true, bool linear = false);

https://docs.unity3d.com/ScriptReference/Texture2D-ctor.html

MatをTexture2Dに変換

Utils.matToTexture2D (imgMat, texture);

matToTexture2D()


static void OpenCVForUnity.UnityUtils.Utils.matToTexture2D(	
Mat 	mat,
Texture2D 	texture2D,
bool 	flip = true,
int 	flipCode = 0,
bool 	flipAfter = false,
bool 	updateMipmaps = false,
bool 	makeNoLongerReadable = false 
)	

https://enoxsoftware.github.io/OpenCVForUnity/3.0.0/doc/html/class_open_c_v_for_unity_1_1_unity_utils_1_1_utils.html#a5372d5ffba5cbeaaf2247d2b2a736653