Search code examples
opencvcomputer-visionstereo-3d

how to access the disparity value in opencv


Which of the option is correct

disparity.at<short>(X,Y)
disparity.at<uchar>(X,Y)

Do we have to divide the disparity by 16.0 to get the correct disparity. some people in their blog uses 1. Some have used 2 .The type is of short using the function disparity.type(). The disparity value which I am getting by accessing using 1 is very high.


Solution

  • If you use OpenCV 2.4.x you have to look at http://docs.opencv.org/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#stereobm-operator which tells you:

    disparity – Output disparity map. It has the same size as the input images. When disptype==CV_16S, the map is a 16-bit signed single-channel image, containing disparity values scaled by 16. To get the true disparity values from such fixed-point representation, you will need to divide each disp element by 16. If disptype==CV_32F, the disparity map will already contain the real disparity values on output.

    So if you've chosen disptype = CV_16S during computation, you can access a pixel at pixel-position (X,Y) by:

    short pixVal = disparity.at<short>(Y,X);
    

    while the disparity value is

    float disparity = pixVal / 16.0f;
    

    if you've chosen disptype = CV_32F during computation, you can access the disparity directly:

    float disparity = disparity.at<float>(Y,X);
    

    Accessing the disparity matrix with .at<uchar> should definitely be wrong!

    please be aware that there might be differences for different OpenCV versions!