Ask Your Question

Calculating distance from the pixel value of a disparity map

asked 2013-01-09 08:44:10 -0600

Lassan gravatar image


I have been looking for a simple solution for a while, but haven't come across anything so..

I have generated a disparity map using the OpenCV StereoBM and StereoSGBM functions and a pair of cameras. I have all of the camera parameters generated by stereo_calib. Is there a fairly straightforward way to calculate the distance using all of this information given the grayscale value of the pixel?


edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted

answered 2013-01-09 09:25:18 -0600

Victor1234 gravatar image

Why reprojectImageTo3D is not suitable?

edit flag offensive delete link more


I suppose that is possible, but I only need to find the distance of, say, a single pixel, and can ignore the rest of the image. It would be a bit inefficient to calculate the 3D position of all of the image..

Lassan gravatar imageLassan ( 2013-01-09 10:39:03 -0600 )edit

answered 2014-07-09 13:16:26 -0600

bennygato gravatar image

There is a good piece of code from: []

The second piece of code in the question, is the code you need to just get one point's XYZ.

edit flag offensive delete link more

answered 2013-02-26 03:38:07 -0600

Mathias gravatar image

Hey Lassan,

the last line of the documentation for reprojectImageTo3D states "To reproject a sparse set of points {(x,y,d),...} to 3D space, use perspectiveTransform()" perspectiveTransform takes a 3D Point as src, in your case (x,y,disparity(x,y)) which will then be transformed using the Q matrix obtained with stereoRectify() and the result written into dst.

I haven't tested it myself but something like 1) Push all the points you want to transform into a cv::Mat array 2) cv::perspectiveTransform(your_point_array, output_point_array, Qmat) should do the trick for you.

Hope that helped

Greetings Mathias

edit flag offensive delete link more



actually, just Scalar transformed = Q * Scalar(x,y,disparity,1); does the same trick for 1 point as reprojectImageTo3D

berak gravatar imageberak ( 2013-02-26 03:51:34 -0600 )edit

this seems to return the same matrix for every image. a 4x4 matrix. how to get depth from that?

Katri gravatar imageKatri ( 2013-11-29 04:37:16 -0600 )edit

Question Tools

1 follower


Asked: 2013-01-09 08:44:10 -0600

Seen: 10,557 times

Last updated: Jul 09 '14