I have 2 stereo images with me and the quality of depth map depends on the BlockSize and DisparityRange parameters of the disparity function. Currently I fix them by trying few different values for it.
Is there a better way (code or matlab function) to fix these parameters, maybe also automatically?
First question: which version of MATLAB are you using? Older releases have been using the simple block matching algorithm, which is not very robust. The latest release (R2014a) is using the Semi-Global Block Matching algorithm by default, which is much better.
'DisparityRange'
depends on the distance from the camera to the objects of interest. Disparity is inversely proportional to distance from the camera, so if you want to reconstruct the objects that are close to the camera, you would need to increase the disparity range.
Increasing the 'BlockSize'
helps if your scene has little texture.
Other things you could try is low-pass filter the input stereo images and/or apply the median filter to the output disparity map.