r/computervision • u/minhduc66532 • Feb 19 '21
Help Required Depth map to 3D point cloud with OpenCV ?
So let's say we have a depth map like this:

Now I want to "remap" this depth map into a 3D point cloud (for obstacles avoidance). I did lots of googling to find a way to solve this problem but most of them are quite hard to understand. It would be great if you can give me some pointers on this problem ? Thank you
18
Upvotes
8
u/drsimonz Feb 19 '21
This should be pretty easy depending on the properties you want the point cloud to have. You need to determine the following:
The simple approach is to iterate over each pixel and compute the 3D location of that pixel, which then becomes a point in your point cloud. This requires a little trigonometry but nothing above highschool level. The x, y image coordinates of the pixel define the point at which the "view ray" intersects the image plane. If you know the size of the image plane, you will be able to compute the 3D coordinates in "real" units. Otherwise, the units will be relative to the size of the image plane.
You could also try to formulate it in terms of a 4x4 projection matrix. In 3D rendering, the projection matrix is computed from the camera's position and orientation, and it maps positions in 3D space onto a 2D image. I believe this is a degenerate matrix since it's reducing the number of dimensions from 3 to 2 (or 4 to 3 if you include the homogenous coordinate), so it probably can't be inverted. But since you have the depth at each pixel, it ought to be possible. If performance is important you definitely want to formulate this using matrices if possible, but doing it point-by-point is probably easier to understand.
2
u/_d0s_ Feb 19 '21
Considering the previously posted image https://cs.lmu.edu/~ray/images/perspective.png, what you need to compute is the normal vector for each pixel on the projection plane. Multiplying the normal vector by the depth in the depth map at the respective pixel position gets you to the 3d position of this point. This is the simplest way to think about this problem without knowing anything about camera matrices.
The most important unknown in your setup is the depthmap. it is scaled to 0-255 for visualization purposes, and probably is only valid up to scale with the real depth values.
2
u/kigurai Feb 19 '21
Given internal camera calibration matrix K the 3D point that corresponds to a certain pixel (u,v) is computed as
Here
D(u,v)is the depth map value at that pixel.The produced 3d points are located in the local camera coordinate frame. If you have the camera pose (extrinsic camera matrix) you can get the points also in a world coordinate frame.
The angles, focal length and everything you saw in previous equations are encoded into the
Kmatrix.2
u/dimsycamore Feb 19 '21 edited Feb 20 '21
I had to solve this problem using a Kinect sensor a couple years back. This thread was a lifesaver for me: https://stackoverflow.com/questions/41241236/vectorizing-the-kinect-real-world-coordinate-processing-algorithm-for-speed
Your camera parameters should be different but the overall math and optimizations should apply to your situation.
2
u/aNormalChinese Feb 20 '21
This is a disparity map(white color means closer), not a depth map (black color means closer)
https://imgur.com/5KIAP3q
quick demo in ros(a piece of code from long time ago), you have to adjust your camera matrix.
-1
u/tim_gabie Feb 19 '21
write yourself a converter:
- take the image as greyscale bitmap
- then just invert the brightness value and the the further back it is the brighter it should get