如何访问opencv中的视差值

how to access the disparity value in opencv

哪个选项是正确的

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

我们是否必须将视差除以 16.0 才能得到正确的视差。 有些人在他们的博客中使用了 1。有些人使用了 2。类型很短,使用函数 disparity.type()。 我通过使用 1 访问得到的视差值非常高。

使用 StereoBM 输出差异可以是 CV_16SCV_32F。当视差的类型为CV_16S时,它是一个16位有符号的单通道图像,包含缩放16的视差值。要从这种定点表示中获得真正的视差值,您需要将每个元素除以到 16. 如果视差的类型是 CV_32F,那么视差图将已经包含输出的真实视差值。

如果你使用 OpenCV 2.4.x 你必须看看 http://docs.opencv.org/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#stereobm-operator 它告诉你:

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.

因此,如果您在计算期间选择了 disptype = CV_16S,则可以通过以下方式访问像素位置 (X,Y) 处的像素:

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

而视差值为

float disparity = pixVal / 16.0f;

如果您在计算过程中选择了disptype = CV_32F,您可以直接访问视差:

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

.at<uchar>访问视差矩阵肯定是错误的!

请注意,不同的 OpenCV 版本可能存在差异!