matlab的disparity函数

disparity

Disparity map between stereo images

Syntax

disparityMap = disparity(I1,I2)
d = disparity(I1,I2,Name,Value)

Description

example

disparityMap = disparity(I1,I2) returns the disparity map, disparityMap, for a pair of stereo images, I1 and I2.

d = disparity(I1,I2,Name,Value) Additional control for the disparity algorithm requires specificationof parameters and corresponding values. One or more Name,Value pair arguments specifies an additional option.

Examples

collapse all

Compute Disparity Map for a Pair of Stereo Images

Load the images and convert them to grayscale.

I1 = imread('scene_left.png');
I2 = imread('scene_right.png');

Show stereo anaglyph. Usered-cyan(红—青色) stereo glasses to view image in 3-D.

figure
imshow(stereoAnaglyph(I1,I2));
title('Red-cyan composite view of the stereo images');

Compute the disparity map.

disparityRange = [-6 10];
disparityMap = disparity(rgb2gray(I1),rgb2gray(I2),'BlockSize',...
    15,'DisparityRange',disparityRange);

Display the disparity map. For better visualization, use the disparity range as the display range for imshow.

figure
imshow(disparityMap,disparityRange);
title('Disparity Map');
colormap jet
colorbar

Input Arguments

collapse all

I1 — Input image 1
M-by-N 2-D grayscale image

Input image referenced as I1 corresponding to camera 1, specified in 2-D grayscale. The stereo images, I1 and I2, must be rectified such that the corresponding points are located on the same rows. You can perform thisrectification with the rectifyStereoImages function.

You can improve the speed of the function by setting the class of I1 and I2 to uint8, and the number of columns to be divisible by 4. Input images I1 and I2 must be real, finite, andnonsparse. They must be the same class.

Data Types: uint8 | uint16 | int16 | single | double

I2 — Input image 2
M-by-N 2-D grayscale image

Input image referenced as I2 corresponding to camera 2, specified in 2-D grayscale. The input images must be rectified such that the corresponding points are located on the same rows. You can improve the speed of the function by setting the class of I1 and I2 to uint8, and the number of columns to be divisible by 4. Input images I1 and I2 must be real, finite, and nonsparse. They must be the same class.

Data Types: uint8 | uint16 | int16 | single | double

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'Method','BlockMatching', specifies the 'Method' property be set to 'BlockMatching'.

collapse all

'Method' — Disparity estimation algorithm
'SemiGlobal' (default) | 'BlockMatching'

Disparity estimation algorithm, specified as the comma-separated pair consisting of 'Method' and the character vector 'BlockMatching' or 'SemiGlobal'. The disparity function implements the basic Block Matching[1] and the Semi-Global Block Matching[3] algorithms. In the 'BlockMatching' method, the function computes disparity by comparing the sum of absolute differences (SAD) of each block of pixels in the image. In the 'SemiGlobal' matching method, the function additionally forces similar disparity on neighboring blocks. This additional constraint results in a more complete disparity estimate than in the 'BlockMatching' method.

The algorithms perform these steps:

  1. Compute a measure of contrast of the image by using theSobel filter.

  2. Compute the disparity for each pixel in I1.

  3. Mark elements of the disparity map, disparityMap, that were not computed reliably. The function uses –realmax('single') to mark these elements.

'DisparityRange' — Range of disparity
[0 64] (default) | two-element vector

Range of disparity, specified as the comma-separated pair consisting of 'DisparityRange' and a two-element vector. The two-element vector must be in the format [MinDisparityMaxDisparity]. Both elements must be an integer and can be negative. MinDisparity and MaxDisparity must be in the range [-image widthimage width]. The difference between MaxDisparity and MinDisparity must be divisible by 16DisparityRange must be real, finite, and nonsparse. If the camera used to take I1 was to the right of the camera used to take I2, then MinDisparity must be negative.

The disparity range depends on the distance between the two cameras and the distance between the cameras and the object of interest.Increase the DisparityRange when the cameras are far apart or the objects are close to the cameras. To determine a reasonable disparity for yourconfiguration, display thestereo anaglyph of the input images in imtool and use the Distance tool to measure distances between pairs of corresponding points. Modify the MaxDisparity to correspond to the measurement.

'BlockSize' — Square block size
15 (default) | odd integer

Square block size, specified as the comma-separated pair consisting of 'BlockSize' and an odd integer in the range [5,255]. This value sets the width for the square block size. The function uses thesquare block of pixels for comparisons between I1 and I2. BlockSize must be real, finite, and nonsparse.

'ContrastThreshold' — Contrast threshold range
0.5 (default) | scalar value

Contrast threshold range, specified as the comma-separated pair consisting of 'ContrastThreshold' and a scalar value in the range (0,1]. The contrast threshold defines an acceptable range of contrast values.Increasing this parameter results in fewer pixels being marked as unreliable.ContrastThreshold must be real, finite, and nonsparse.

'UniquenessThreshold' — Minimum value of uniqueness
15 (default) | non-negative integer

Minimum value of uniqueness, specified as the comma-separated pair consisting of 'UniquenessThreshold' and a nonnegative integer. Increasing this parameter results in the function marking more pixels unreliable. When the uniqueness value for a pixel is low, the disparity computed for it is less reliable. Setting the threshold to 0 disables uniqueness thresholdingUniquenessThresholdmust be real, finite, and nonsparse.

The function defines uniqueness as a ratio of theoptimal disparity estimation and the less optimal disparity estimation. For example:

Let K be thebest estimated disparity, and let V be the corresponding SAD (Sum of Absolute Difference) value.
Consider V as the smallest SAD value over the whole disparity range, and v as the smallest SAD value over the whole disparity range, excluding KK-1, and K+1.
If v < V * (1+0.01*UniquenessThreshold), then the function marks the disparity for the pixel as unreliable.

'DistanceThreshold' — Maximum distance for left-to-right image checking
[] (disabled) (default) | non-negative integer

Maximum distance for left-to-right image checking between two points, specified as the comma-separated pair consisting of 'DistanceThreshold' and a nonnegative integer. Increasing this parameter results in fewer pixels being marked as unreliable. Conversely,when you decrease the value of the distance threshold, you increase the reliability of the disparity map. You can set this parameter to an empty matrix [] to disable it. DistanceThreshold must be real, finite, and nonsparse.

The distance threshold specifies the maximum distance between a point in I1 and the same point found from I2. The function finds the distance and marks the pixel in the following way:

Let p1 be a point in image I1.
Step 1: The function searches for point p1's best match in image I2 (left-to-right check) and finds point p2.
Step 2: The function searches for p2's best match in image I1 (right-to-left check) and finds point p3.
If the search returns a distance between p1 and p3 greater than DistanceThreshold, the function marks the disparity for the point p1 as unreliable.

'TextureThreshold' — Minimum texture threshold
0.0002 (default) | scalar value

Minimum texture threshold, specified as the comma-separated pair consisting of 'TextureThreshold' and a scalar value in the range [0, 1]. The texture threshold defines the minimum texture value for a pixel to be reliable.The lower the texture for a block of pixels, the less reliable the computed disparity is for the pixels. Increasing this parameter results in more pixels being marked as unreliable. You can set this parameter to 0 to disable it. This parameter applies only when you set Method to 'BlockMatching'.

The texture of a pixel is defined as the sum of the saturated contrast computed over the BlockSize-by-BlockSize window around the pixel. The function considers the disparity computed for the pixel unreliable and marks it, when the texture falls below the value defined by:

Texture < XTextureThreshold * BlockSize2

X represents the maximum value supported by the class of the input images, I1 and I2.

TextureThreshold must be real, finite, and nonsparse.

Output Arguments

collapse all

disparityMap — Disparity map
M-by-N 2-D grayscale image

Disparity map for a pair of stereo images, returned as an M-by-N 2-D grayscale image. The function returns the disparity map with the same size as the input images, I1 and I2. Each element of the output specifies the disparity for the corresponding pixel in the image references as I1. The returned disparity values are rounded to 1/16 th pixel.

The function computes the disparity map in three steps:

  1. Compute a measure of contrast of the image by using the Sobel filter.

  2. Compute the disparity for each of the pixels by using block matching and the sum of absolute differences (SAD).

  3. Optionally, mark the pixels which contain unreliable disparity values. The function sets the pixel to the value returned by -realmax('single').

Tips

If your resulting disparity map looks noisy, try modifying the DisparityRange. The disparity range depends on the distance between the two cameras and the distance between the cameras and the object of interest. Increase the DisparityRange when the cameras are far apart or the objects are close to the cameras. To determine a reasonable disparity for your configuration, display the stereo anaglyph of the input images in imtool and use the Distance tool to measure distances between pairs of corresponding points. Modify the MaxDisparity to correspond to the measurement.

References

[1] Konolige, K., Small Vision Systems: Hardware and Implementation, Proceedings of the 8th International Symposium in Robotic Research, pages 203-212, 1997.

[2] Bradski, G. and A. Kaehler, Learning OpenCV : Computer Vision with the OpenCV Library, O'Reilly, Sebastopol, CA, 2008.

[3] Hirschmuller, H., Accurate and Efficient Stereo Processing by Semi-Global Matching and Mutual Information, International Conference on Computer Vision and Pattern Recognition, 2005.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MATLAB自带的disparity函数是用于计算视差图的函数,常用于立体视觉中的深度估计。下面是disparity函数的语法和说明: ``` [dmap, cim] = disparity(left, right, 'Method', method, 'BlockSize', blocksize, 'DisparityRange', disp_range, 'Uniqueness', uniqueness) ``` 输入参数: - left:左视图灰度图像; - right:右视图灰度图像; - method:视差计算方法,可选的值包括'SSD'(和平方差法)和'AD'(绝对差法),默认值为'SSD'; - blocksize:匹配块大小,必须为正奇数,表示匹配块的边长,例如blocksize=3表示3x3的匹配块,默认值为9; - disp_range:视差范围,表示最大视差值与最小视差值之间的差值,必须为正整数,例如disp_range=64表示最大视差值为64,最小视差值为0,默认值为64; - uniqueness:唯一性约束阈值,表示匹配块的最小相似度,取值范围为0到1之间的实数,例如uniqueness=0.1表示相似度小于0.1的匹配块会被排除,默认值为0.5。 输出参数: - dmap:视差图,表示每个像素的视差值,值越大表示距离越远; - cim:一致性图,表示每个像素匹配的一致性,值越大表示匹配越可靠。 下面是一个简单的disparity函数的示例: ```matlab % 读取左右视图图像 left = imread('left.png'); right = imread('right.png'); % 转为灰度图像 left_gray = rgb2gray(left); right_gray = rgb2gray(right); % 计算视差图 [dmap, cim] = disparity(left_gray, right_gray, 'BlockSize', 15, 'DisparityRange', [0 64], 'Uniqueness', 0.5); % 显示视差图 figure; imshow(dmap, []); colorbar; ``` 在这个示例中,我们首先将输入的彩色左右视图图像转换为灰度图像。然后,使用disparity函数计算视差图。在计算过程中,我们将匹配块大小设置为15x15,视差范围设置为0到64,唯一性约束阈值设置为0.5。最后,使用imshow函数显示视差图。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值