OpenCVforUnity中Texture2D格式转换为OpenCV中的Mat格式——Texture2DToMatExample案例

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轴都镜像翻转。

flip=false

 

flip=true,flipCode=-1
flip=true,flipCode=0

 

flip=true,flipCode=1

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,纹理的系统内存副本被释放。


END

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Unity检测人的轮廓,可以使用OpenCV for Unity插件。以下是一些步骤: 1. 下载和安装OpenCV for Unity插件(https://assetstore.unity.com/packages/tools/integration/opencv-for-unity-21088)。 2. 将图像作为纹理加载到Unity。 3. 使用OpenCV for Unity插件的"TextureToMat"功能将纹理转换为OpenCV Mat对象。 4. 使用OpenCV for Unity的人脸检测功能检测人脸。 5. 使用OpenCV for Unity的轮廓检测功能检测人的轮廓。 6. 将检测到的轮廓绘制到纹理上。 7. 将纹理显示在Unity场景。 下面是一个代码示例,展示如何使用OpenCV for Unity插件进行人的轮廓检测: ``` using UnityEngine; using System.Collections; using OpenCVForUnity.CoreModule; using OpenCVForUnity.ImgprocModule; using OpenCVForUnity.UnityUtils; public class ContourDetectionExample : MonoBehaviour { // Use this for initialization void Start() { // Load the texture into a Texture2D object Texture2D texture = Resources.Load("test") as Texture2D; // Create a new Mat object from the texture Mat rgbaMat = new Mat(texture.height, texture.width, CvType.CV_8UC4); // Convert the texture to a Mat object Utils.texture2DToMat(texture, rgbaMat); // Convert the Mat object to grayscale Mat grayMat = new Mat(); Imgproc.cvtColor(rgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY); // Threshold the image to create a binary image Mat binaryMat = new Mat(); Imgproc.threshold(grayMat, binaryMat, 0, 255, Imgproc.THRESH_BINARY); // Find the contours in the binary image List<MatOfPoint> contours = new List<MatOfPoint>(); Mat hierarchy = new Mat(); Imgproc.findContours(binaryMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); // Draw the contours on the original image Imgproc.drawContours(rgbaMat, contours, -1, new Scalar(0, 255, 0, 255), 2); // Convert the Mat object back to a Texture2D object Texture2D outputTexture = new Texture2D(rgbaMat.cols(), rgbaMat.rows(), TextureFormat.RGBA32, false); Utils.matToTexture2D(rgbaMat, outputTexture); // Display the output texture in the scene GetComponent<Renderer>().material.mainTexture = outputTexture; } } ``` 这个代码示例加载了一个名为“test”的纹理,将其转换为Mat对象,然后使用OpenCV for Unity的轮廓检测功能检测人的轮廓,并在原始图像上绘制轮廓。最后,将Mat对象转换回Texture2D对象并在场景显示它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值