arfoundation-samples icon indicating copy to clipboard operation
arfoundation-samples copied to clipboard

GetRawTextureData or GetPixels from depth map

Open smalleight17 opened this issue 7 years ago • 5 comments

Trying to do some computer vision thing for human segmentation (on CPU side), but it's acting really weird. GetRawTextureData always returns an array of length 0, GetPixels returns array of correct length, but all element is 0. I tried on both stencil map and depth map, neither works. Anyone has luck getting raw data or pixel data from stencil / depth map?

smalleight17 avatar Sep 09 '19 21:09 smalleight17

yea this behaves strange. happened to me too. i got it working with this code and created a readable texture(no idea about performance), hope it helps:

// Make Texture Readable
RenderTexture renderTexture = RenderTexture.GetTemporary(
texture.width,
texture.height,
0,
RenderTextureFormat.RFloat,
RenderTextureReadWrite.Linear);

Graphics.Blit(texture, renderTexture);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = renderTexture;
Destroy(m_readableStencilTexture);
m_readableStencilTexture = new Texture2D(texture.width, texture.height);
m_readableStencilTexture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
m_readableStencilTexture.Apply();
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary(renderTexture);

ghost avatar Sep 16 '19 05:09 ghost

Hey @blurxs , thank you so much for the reply! Messing around with this for a few more days my guess is that since depth and stencil map are all result from machine learning they are already on the GPU, so it's not the normal CPU -> GPU flow. I've tried blitting to a render texture reading from it, my app crashed every a few minutes. Another way is to use AsyncGPUReadback to read texture back to CPU from GPU, which I haven't tested yet. I ended up using compute shader and SetTexture to pass in the depth map, and did my calculation inside compute shader. This works really well for me, hope it helps you as well!

smalleight17 avatar Sep 16 '19 06:09 smalleight17

thank you :) i'm currently waiting for a higher resolution depth-image before doing anything in this area again. I were able to access only the "Standard Resolution" and "half-screen","fullscreen" didn't give me anything back but thanks for your solutions, maybe i'll need them sometime in the future :)

ghost avatar Sep 16 '19 08:09 ghost

The depth image is a GPU texture, so it needs to be transferred from the GPU in order to read the pixel values on the CPU. This is why you need to do a Graphics.Blit to a render texture.

The same issue exists for the camera texture, but there is an API for accessing the camera texture on the CPU. No such API exists for doing that with the depth image, though it is possible. We didn't think anyone would need to access the depth image on the CPU, but sounds like that's what you're trying to do.

Could you provide more details on your use case? What sort of "computer vision thing" are you doing?

tdmowrer avatar Oct 06 '19 06:10 tdmowrer

@tdmowrer Thanks for the reply! I was trying to calculate the average depth of the human and I ended up using compute shader to add up all pixels values and divide by the pixel number that has a value > 0. I might also want to find contour/edge of the segmentation, which I have no idea what to do. Sounds like I want an openCV thing (edge finding, blob detection) for compute shader, because sending the texture back to CPU is just very costly. If you could shed any light on it I would very much appreciate it!

smalleight17 avatar Oct 09 '19 22:10 smalleight17