The paper deals with the design and implementation of a stereo algorithm. Disparity map is formulated as a Markov Random Field with a new smoothness constraint depending not only on image derivatives, but also on segmentation results and gradient directions. With these constraints we force disparity continuity inside each segmented object, while its contours are well preserved. Moreover we have designed a modified version of Belief Propagation which gives the solution to the stereo matching problem: the optimization has remarkable improvements and especially with respect to message propagation, which is actually driven by segmentation and boundary knowledge. Preliminary results are presented both on synthetic and benchmark images to demonstrate the effectiveness of our method.