- Home /
fast way for converting from opencv mat to texture2d
Hi guys,
I'm trying to read a large-size video (3840*2160) from opencv and then convert it to texture2d in unity frame by
frame, in order to show it as background of a 3d scene. The way I'm using is as follows:
in cpp: mat opencvimg; ... byte* imgdata=new byte[widh*height*3]; memcpy(imgdata,opencvimg.data,widh*height*3);
in c#: Texture2d texture0 texture0.LoadRawTextureData(imgdata); texture0.Apply(); GUITexture backgroundtexture; backgroundtexture.texture=texture0;
When the size of the video is of size 1920*1080, it went smoothly. However, I found that texture0.Apply() is quite quite slow that the video cannot be played normally.
Could anyone please give me some suggestions on how to do in real time please? Thank you!
Answer by kyewong · Jun 03, 2015 at 06:58 AM
let me end this topic myself I've found that the fastest way is to fill the texture2d data in native opengl using glBindTexture & glTexImage2D, instead of using LoadRawTextureDat in c#. For more details, pls refer to http://docs.unity3d.com/Manual/NativePluginInterface.html
Hi, I might suffering from the same problem. and newbie to Unity, sigh. In your LoadRawTexture() method solution. did you new byte data in cpp and passed them back to unity script? I might try the simple one first(I have a opencv mat 1280*1024).
Thanks.
Hi, you don't need to pass the data from cpp to unity script, In contrast, you need to pass the id/pointer of the texture from unity script to cpp, and bind the texture data to the id/pointer inside the cpp. Such an example can be found at: http://docs.unity3d.com/uploads/Examples/RenderingPluginExample52.zip
@kyewong Hi, could you reupload the sample? the link was broken
Your answer
Follow this Question
Related Questions
import video from camera 3 Answers
Using of Texture2D.LoadImage 0 Answers
Smooth Synchronized HD Video Playback 0 Answers
android display h264 byte[] stream 0 Answers