Creating Image Object Boundaries
The WATERSHED function applies the watershed operation to grayscale images. This operation creates boundaries in an image by detecting borders between poorly distinguished image areas that contain similar pixel values.
To understand the watershed operation, imagine translating the brightness of the image pixels into height. The brightest pixels become tall peaks and the darkest pixels become basins or depressions. Now imagine flooding the image. The watershed operation detects boundaries among areas with nearly the same value or height by noting the points where single pixels separate two similar areas. The points where these areas meet are then translated into boundaries.
Note
Images are usually smoothed before applying the watershed operation. This removes noise and small, unimportant fluctuations in the original image that can produce oversegmentation and a lack of meaningful boundaries.
The following example combines an image containing the boundaries defined by the watershed operation and the original image, a 1982 Landsat satellite image of the Barringer Meteor Crater in Arizona. Complete the following steps for a detailed description of the process.
Example Code
See watershedexample.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering watershedexample at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT watershedexample.pro.
- Prepare the display device and load the grayscale color table:
- Select and open the image of Barringer Meteor Crater, AZ:
- Get the image size and create a window:
- Display the original image, annotating it using the XYOUTS procedure:
- Using /EDGE_TRUNCATE to avoid spikes along the edges, smooth the image to avoid oversegmentation and display the smoothed image:
- Define the radius of the structuring element and create the disk:
- Use the top-hat operation before using watershed to highlight the bright areas within the image.
- Display the image:
- Determine an intensity value with which to stretch the image using an intensity histogram as a guide:
- Stretch the image to set all pixels with a value greater than 70 to the maximum pixel value (white) and display the results:
- Apply the WATERSHED function to the stretched top-hat image. Specify 8-neighbor connectivity to survey the eight closest pixels to the given pixel, resulting in fewer enclosed regions, and display the results:
- Combine the watershed image with the original image and display the result:
file = FILEPATH('meteor_crater.jpg', $
SUBDIRECTORY = ['examples', 'data'])
READ_JPEG, file, img, /GRAYSCALE
smoothImg = smooth(7, /EDGE_TRUNCATE) TVSCL, smoothImg, 1 XYOUTS, (60 + dims[0]), 444, 'Smoothed Image', $ Alignment = .5, /DEVICE, COLOR = 255The following figure shows that the smoothing operation retains the major features within the image.
radius = 3 strucElem = SHIFT(DIST(2*radius+1), radius, radius) LE radiusTip
EnterPRINT, strucElemto view the structure created by the previous statement.
TVSCL, tophatImg, 2 XYOUTS, (60 + 2*dims[0]), 444, 'Top-hat Image', $ Alignment = .5, /DEVICE, COLOR = 255
WINDOW, 2, XSIZE = 400, YSIZE = 300 PLOT, HISTOGRAM(smoothImg)An intensity histogram of the smoothed image is used instead of the top-hat image since it was empirically determined that the top-hat histogram did not provide the required information.
Note
Using an intensity histogram as a guide for determining intensity values is described in the section, Determining Intensity Values for Threshold and Stretch.
WSET, 0 tophatImg = tophatImg < 70 TVSCL, tophatImg XYOUTS, 75, 210, 'Stretched Top-hat Image', $ Alignment = .5, /DEVICE, COLOR = 255The original top-hat image (left) and the results of stretching the image (right) are shown in the following figure.
watershedImg = WATERSHED(tophatImg, CONNECTIVITY = 8) TVSCL, watershedImg, 4 XYOUTS, (70 + dims[0]), 210, 'Watershed Image', $ Alignment = .5, /DEVICE, COLOR = 255
The following display shows all images created in the previous example. The final image, shown in the lower right-hand corner of the following figure, shows the original image with an overlay of the boundaries defined by the watershed operation.


