Image Processing : Extracting and Analyzing Shapes |
The following example uses a variety of morphological operations to remove bridges from a satellite image of New York waterways. Complete the following steps for a detailed description of the process.
Example Code See removebridges.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
xsize = 768 ysize = 512 img = READ_BINARY(FILEPATH('nyny.dat', $ SUBDIRECTORY = ['examples', 'data']), $ DATA_DIMS = [xsize, ysize])
img = BYTSCL(img) WINDOW, 1, TITLE = 'Original Image' TVSCL, img
WINDOW, 4, XSIZE = 400, YSIZE = 300 PLOT, HISTOGRAM(img)
Note Using an intensity histogram as a guide for determining threshold values is described in the section, Determining Intensity Values for Threshold and Stretch. |
maskImg = img LT 70
side = 3 strucElem = DIST(side) LE side
maskImg = MORPH_OPEN(maskImg, strucElem)
maskImg = MORPH_CLOSE(maskImg, strucElem) WINDOW, 1, title='Mask After Opening and Closing' TVSCL, maskImg
This results in the following figure:
|
labelImg = LABEL_REGION(maskImg)
regions = labelImg[WHERE(labelImg NE 0)]
mainRegion = WHERE(HISTOGRAM(labelImg) EQ $ MAX(HISTOGRAM(regions)))
maskImg = labelImg EQ mainRegion[0] Window, 3, TITLE = 'Final Masked Image' TVSCL, maskImg
This results in a mask of the largest region, the waterways, as shown in the following figure.
newImg = MORPH_OPEN(img, strucElem, /GRAY)
newImg[WHERE(maskImg EQ 0)] = img[WHERE(maskImg EQ 0)]
WINDOW, 0, XSIZE = xsize, YSIZE = ysize FLICK, img, newImg
Hit any key to stop the image from flickering. Details of the two images are shown in the following figure.
IDL Online Help (March 06, 2007)