For the demonstration a simple image is taken and the various stages of processing are shown:
Step 1: Threshold the image for a simple segmentation of your image to identify background and foreground regions. For our sample image it results as :
Step 2: We are now certain of the background and the foreground but the region we are uncertain about is the boundary region. Now for this we use the processing functions - erode and dilate. Erosion removes the boundary pixels, so we are certain of the pixels that are inside the object. Dilation increases the region's boundary to the background.Now we use distance transformation for gray level intensities of points inside region with respect to the boundary.
Step 3: Finally mark the boundary regions and it is done !!