|
Image Processing : Extracting and Analyzing Shapes |
|
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. |
DEVICE, DECOMPOSED = 0, RETAIN = 2 LOADCT, 0
file = FILEPATH('meteor_crater.jpg', $
SUBDIRECTORY = ['examples', 'data'])
READ_JPEG, file, img, /GRAYSCALE
dims = SIZE(img, /DIMENSIONS) WINDOW, 0, XSIZE = 3*dims[0], YSIZE = 2*dims[1]
TVSCL, img, 0 XYOUTS, 50, 444, 'Original Image', Alignment = .5, $ /DEVICE, COLOR = 255
smoothImg = smooth(7, /EDGE_TRUNCATE) TVSCL, smoothImg, 1 XYOUTS, (60 + dims[0]), 444, 'Smoothed Image', $ Alignment = .5, /DEVICE, COLOR = 255
The 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 radius
| Tip Enter PRINT, strucElem to view the structure created by the previous statement. |
tophatImg = MORPH_TOPHAT(smoothImg, strucElem)
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 = 255
The 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
img [WHERE (watershedImg EQ 0)]= 0 TVSCL, img, 5 XYOUTS, (70 + 2*dims[0]), 210, 'Watershed Overlay', $ 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.
![]() |
IDL Online Help (March 06, 2007)