Mat是emgucv的类,需要导入dll
首先需要导入emgucv要用的dll
免积分下载文章来源:https://www.toymoban.com/news/detail-612960.html
Texture2D转Mat
private Mat Texture2DToMat(Texture2D texture2D)
{
using (MemoryStream baseTexture_ms = new MemoryStream(texture2D.EncodeToPNG(), false))
{
Bitmap bitemap = new Bitmap(baseTexture_ms);
Image<Bgr, byte> imageCV = new Image<Bgr, byte>(bitemap);//注意Image的命名空间是Emgu.CV
return imageCV.Mat;
}
}
Mat转Texture2D
private Texture2D MatToTexture2D(Mat mat)//Mat转Texture2D
{
Image<Bgr, byte> resultImage = mat.ToImage<Bgr, byte>();
Texture2D resultTexture = new Texture2D(1, 1);
resultTexture.LoadImage(resultImage.ToJpegData());
resultImage.Dispose();
return resultTexture;
}
这些不需要emgucv的dll,什么都不需要额外导入文章来源地址https://www.toymoban.com/news/detail-612960.html
Texture 转Texture2D
private Texture2D GetTexture2D(Texture texture)
{
Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
RenderTexture renderTexture = new RenderTexture(texture.width, texture.height, 32);
RenderTexture.active = renderTexture;
UnityEngine.Graphics.Blit(texture, renderTexture);//将纹理从Texture渲染到RenderTexuture
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture2D.Apply();
RenderTexture.active = null;
GameObject.Destroy(renderTexture);
return texture2D;
}
到了这里,关于unity emgucv Texture2D转Mat / Mat转Texture2D / Texture 转 Texture2D的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!