We propose a methodfor segmenting gray-value images. By segmentation, we mean a map from the set of pixels to a small set of levels such that each connected component of the set of pixels with the same level forms a relatively large and "meaningful" region. The method finds a set of levels with associated gray values byjirstjinding junctions in the image and then seeking a minimum set of threshold values that preserves the junctions. Then it finds a segmentation map that maps each pixel to the level with the closest gray value to the pixel data, within a smoothness construint. For a convex smoothing penalty, we show the global optimal solution for an energy function that fits the data can be obtained in u polynomial time, by a novel use of the muximum-flow algorithm. Our upproach is in contrast to a view in computer vision where segmentation is driven by intensity gradient, usually not yielding closed boundaries.