OpenCV for Unity(2.3.2)插件中的Texture2DToMatExample案例(Unity2018.2.6f1)

位置:OpenCVForUnity\Examples\Basic\Texture2DToMatExample

一、功能概括

二、场景结构

三、主要功能脚本

一、功能概括

该案例中主要展示了Texture2D格式与OpenCV中的Mat格式是如何转换的。

场景中又一个立方体,贴图为戴帽子的人这张图片。

二、场景结构

场景中包括四个物体

Cube 立方体主要用来作为展示对比图片的容器,改物体包含实现图形对比的主要脚本。

Main Camera 摄像机

Canvas UI的画布,该案例中主要用来放置Back Button,返回案例列表

EventSystem依附Canvas的必须

三、主要功能脚本

场景中只有Cube物体上有Script,其中TouchController脚本是控制鼠标点击控制立方旋转的,Texture2DToMatExample脚本是实现主要功能的脚本。

Utils.setDebugMode (true);

Utils是OpenCV中的工具类,调用setDebugMode(bool debugMode, bool throwException = false)方法,该方法主要是设置输出模式, debugMode =true,OpenCV的错误日志将显示在Unity编辑器控制台,如果throwException为true,throw CvException 被Debug.logError替代。

//加载图片作为Texture2D
Texture2D imgTexture = Resources.Load ("lena") as Texture2D;
//定义Mat格式
Mat imgMat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC4);
//转换Texture2D为Mat格式
Utils.texture2DToMat (imgTexture, imgMat);

1定义Mat

Mat (int rows, int cols, int type)

rows是Mat的高度,col是Mat的宽度,typeMat的模式,CvType.CV_8UC4是8位无符号四通道带透明色RGB图像

2Texture2D转Mat

texture2DToMat (Texture2D texture2D, Mat mat, bool flip = true, int flipCode = 0)

texture2D要转换的texture2D,mat转换成的Mat,转换要求Mat格式的大小需要与Texture2D的宽高一致,Mat对象的类型必须为'CV_8UC4' (RGBA) , 'CV_8UC3' (RGB) or 'CV_8UC1' (GRAY)等模式;texture2D的TextureFormat必须为RGBA32或者ARGB32;flip和flipCode 结合使用控制图像翻转,当flip=false时,表示不翻转,flipCode值无效,当flip=false时,flipCode = 0,表示绕着X轴镜像翻转,flipCode = 1,表示绕着Y轴镜像翻转,flipCode = -1,表示绕着X和Y轴都镜像翻转。

3Mat 转换Texture2D

//定义Texture来存储转换后的Texture2D纹理
Texture2D texture = new Texture2D (imgMat.cols (), imgMat.rows (), TextureFormat.RGBA32, false);
//Mat转换成Texture格式
Utils.matToTexture2D (imgMat, texture);
//把转换后的Texture2D贴在物体的mainTexture上
gameObject.GetComponent<Renderer> ().material.mainTexture = texture;

matToTexture2D (Mat mat, Texture2D texture2D, bool flip = true, int flipCode = 0, bool flipAfter = false, bool updateMipmaps = false, bool makeNoLongerReadable = false)

mat是需要转换的Mat,texture2D是转换后的Texture,flip和flipCode是基于X、Y轴的映射,

flipAfter =true,Mat先转换后翻转映射

updateMipmaps =true,将重新计算mipmap级别。

makeNoLongerReadable =true,纹理的系统内存副本被释放。

Features: - Since this package is a clone of OpenCV Java, you are able to use the same API as OpenCV Java 4.3.0 (link). - You can image processing in real-time by using the WebCam Text ure capabilities of Unity . (real-time face detection works smoothly on iPhone 5) - Provides a method to interconversion of Unity 's Text ure 2D and OpenCV 's Mat . - IDisposable is implemented in many classes.You can manage the resources with the “using” statement. - Example s of integration with other publisher assets are available.(e.g. PlayMaker, NatDevice, NatCorder) Unity OpenCV 是一个资源插件,可在 Unity 使用 OpenCV 4.4.0。 官方网站 | 示例编码 | Android 演示 WebGL 演示 | 教程和演示视频 | 论坛 | API 引用 | 支持模块 | 免费试用版 - 由于该资源包是 OpenCV Java 的克隆,因此您可以使用与 OpenCV Java 4.4.0(链接)相同的 API。 - 您可以使用 Unity 的 WebCam Text ure 功能进行实时图像处理。 (实时人脸检测可以在 iPhone 5 上流畅运行) - 提供了 Unity Text ure 2D OpenCV Mat 相互转换的方法。 - IDisposable 已在许多类 实现。您可以使用 "using"语句管理资源。 - 提供了与其他发布者资源集成的示例。 下载地址:https:// opencv .org/releases/ 文件目录: opencv -4.1.0-android-sdk/ OpenCV -android-sdk/sdk/native --- jni/include 头文件 --- libs 动态库 Cmake cmake_minimum_required(VERSION 3.4.1) add_library( native-lib SHARE Imgcodecs.imread 获取外部图片【不可以有 文路径】 Utils . text ure 2D ToMat T 2d 转为 Mat Utils . mat To Text ure 2D Mat 转为T 2d Imgproc.resize 重定义尺寸 Imgproc.cvtColor 转化色彩空间为灰度图 Imgproc.threshold 把图片二值化 Imgproc.findConto 写在前面的话:记录 Unity 调用 opencv 里的坑。这是趟了无数的坑之后,写下的满纸的辛酸泪。各种奇怪的错误、闪退折磨了N久之后终于得到的一个好的方法用来在 Unity OpenCV 之间传递图片。PS:作为一个长期使用C#的程序猿,弄C++实在是太痛苦了,如果代码有什么不合理的地方也希望各位大佬指正批评。 1. 关于DLL 注意,本文不使用 OpenCV for Unity ! 关于C#调用C++的DLL,...