- Home /
How can I use `TextureToMat` and `MatToTexture` to reflect the change in texture in real time?
Hi,
I'm trying to make us of OpenCVSharp and do the image processing of the camera view.
I first made a RenderTexture that reflects the camera view in real time.
Since RenderTexture could not be directly converted into Mat type,
I first made a method that converts RenderTexture into a Texture.
Then I made a raw image whose texture can be modified through script. My plan is to update the texture of the raw image every time the RenderTexture is updated.
It seems like my code is working well if I simply convert RenderTexture into Texture;
however if I perform any cv2 works, the texture won't be updated.
I tried a simple gray-scale conversion for example, but the Texturewon't change.
Here is my code:
void Update() { //Load texture texture = toTexture2D(renderTex); Mat image = Unity.TextureToMat(this.texture); Mat grayMat = new Mat(); Cv2.CvtColor(image, grayMat, ColorConversionCodes.BGR2GRAY); Texture2D resultTex = Unity.MatToTexture(grayMat); RawImage rawImage = gameObject.GetComponent<RawImage>(); rawImage.texture = resultTex; }
If I substitute the last line
rawImage.texture = texture;
Then the change is reflected in real time.
How can I use TextureToMat and MatToTexture to reflect the change in texture in real time?
@Klarzahs I'm in almost exactly the same boat as @herocho1997 (our code block is actually pretty much exactly the same), but I haven't had any luck using resultTex.Apply().
Answer by god2001bc · Oct 26, 2020 at 11:22 AM
Did you ever find a solution to this @herocho1997 ? I'm facing the same issue. ,I'm having exactly the same problem. Did you ever find a fix? @herocho1997
Your answer