How to get the depth of a particular pixel coordinate from depth buffer

Typically: "How do I... ", "How can I... " questions
Post Reply
Aadhithya K
Posts: 9
Joined: 05 Apr 2021, 16:26

How to get the depth of a particular pixel coordinate from depth buffer

Post by Aadhithya K »

I had got the depth buffer from my vision sensor using python remote api with the simxGetVisionSensorDepthBuffer() function and got a list with the depth buffer.
Now by seeing the length of the list it seems that the size is equal to the number of the pixels of the resolution of the image.(I have the vision sensor resolution as 512x512, so I got a depth buffer list of length 262144)
Now to get the depth of a particular pixel of coordinate (x,y), for example (256,256) and get the distance of the pixel from sensor what should we do?
If I know how the list ordered the pixel depth values (like row wise of column wise) at least I would be able to get the depth value of the needed pixel.

Thanks in advance,
Aadhithya K

Site Admin
Posts: 8516
Joined: 14 Dec 2012, 00:25

Re: How to get the depth of a particular pixel coordinate from depth buffer

Post by coppelia »

Hello and sorry for the delay,

if you call simxGetVisionSensorDepthBuffer, then you'll get a value between 0 and 1 for each depth value. 0 is the distance of the near clipping plane, 1 is the distance of the far clipping plane:

Code: Select all

you can use simxGetFloatParameter to get the value of the near and far clipping plane for your vision sensor, or check it in the dialog of the vision sensor and hard code the value.

A better alternative would be probably to use something like:

Code: Select all

function getDepthBuffer(inInts,inFloats,inStrings,inBuffer)
    local sensorHandle=inInts[1] 
    local packedData=sim.getVisionSensorDepthBuffer(sensorHandle+sim.handleflag_codedstring+sim.handleflag_depthbuffermeters)
	return {},{},{},packedData
You can call above function via simxCallScriptFunction. It will return the depth buffer, in meters, but packed. To unpack the data, use simxUnpackFloats.

When you look in the direction of the vison sensor (i.e. along its Z-axis, with the Y-axis pointing up), then the first value corresponds to the bottom left of the image, the last value to the top right, traversing the x-direction first: i.e. reading the image from left to right and bottom to top.


Post Reply