opencv Mat 转成unity3d 的texture2D怎么操作?

Alisa6 2017-12-15 05:09:24
http://m.blog.csdn.net/ychl87/article/details/13294963
来自这种方式把unity3d中图像导出到OpenCV中,那么如何将生成的Mat再导入unity3d呢?

参考内容原贴如下:
首先从相机或者Texture中获得存储图像块
Color32[] pixels = Texture2D.GetPixels32();
//或者WebCamTexture.GetPixels32 ();
再使用GCHandle获得块的指针
GCHandle pixelHandle = GCHandle.Alloc(pixels, GCHandleType.Pinned);
IntPtr pixelPointer = pixelHandle.AddrOfPinnedObject();
然后将此指针传递给OpenCV使用即可。以dll方式进行数据传递。
unity3d中调用如下
[DllImport(@"Unity2OpenCVImage.dll")]
private static extern void Unity2OpenCVImage(IntPtr data,int width,int height,int stride);
opencv中的dll函数为

void Unity2OpenCVImage(char* imageData,int width,int height,int stride)
{
if(NULL==imageData)
{
return;
}
cv::Mat opencvImage(height,width,CV_8UC4);
memcpy(opencvImage.data,imageData,stride*height);
cv::cvtColor(opencvImage,opencvImage,CV_BGR2RGB);
// cv::imshow("11",opencvImage);
// cv::waitKey(1);
}
}


...全文
852 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
drunkVermouth 2019-12-13
  • 打赏
  • 举报
回复
用opencvforunity插件
夫人的泡泡鱼 2017-12-29
  • 打赏
  • 举报
回复
使用插件中Utils.mat2Texture(mat,texture,colors);
夫人的泡泡鱼 2017-12-29
  • 打赏
  • 举报
回复
http://download.csdn.net/download/yzx5452830/9948107
Works with Unity Cloud Build iOS & Android support Windows10 UWP support WebGL support Win & Mac & Linux Standalone support Preview support in the Editor OpenCV for Unity is an Assets Plugin for using OpenCV 3.4.2 from within Unity. Official Site | ExampleCode | Android Demo WebGL Demo | Tutorial & Demo Video | Forum | API Reference | Support Modules Features: - Since this package is a clone of OpenCV Java, you are able to use the same API as OpenCV Java 3.4.2 (link). - You can image processing in real-time by using the WebCamTexture capabilities of Unity. (real-time face detection works smoothly on iPhone 5) - Provides a method to interconversion of Unity's Texture2D and OpenCV's Mat. - IDisposable is implemented in many classes.You can manage the resources with the “using” statement. - Examples of integration with other publisher assets are available.(e.g. PlayMaker, NatCam, NatCorder) ExampleCode using OpenCV for Unity is available. MarkerBased AR Example MarkerLess AR Example FaceTracker Example FaceSwapper Example FaceMask Example RealTime FaceRecognition Example GoogleVR with OpenCV for Unity Example Kinect with OpenCV for Unity Example AVPro with OpenCV for Unity Example HoloLens with OpenCV for Unity Example PlayMakerActions for OpenCVforUnity NatCam with OpenCVForUnity Example NatCorder with OpenCVForUnity Example OpenCV for Unity uses OpenCV under 3-clause BSD License; see Third-Party Notices.txt file in package for details. System Requirements Build Win Standalone & Preview Editor : Windows 7 or later Build Mac Standalone & Preview Editor : OSX 10.9 or later

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧