Use this procedure to apply a morphological filter to an image, including dilate, erode, open, and close.

This routine is obsolete and has been replaced with the BinaryMorphologicalFilter and GrayscaleMorphologicalFilter tasks.


The following example applies an open morphological filter to an image. It displays the filtered image inside a Standard Portal on top of the original image.

; Start ENVI
e = ()
; Open a data file
File = ('qb_boulder_msi', Root_Dir = e.Root_Dir, $
   Subdir = ['data'])
Raster = e.OpenRaster(File)
; Determine an output file
OutFile = e.()
; Return a file ID
fid = (Raster)
; Apply an open morphological filter. 
, fid, DIMS=dims, NB=nb
,'Morph_Doit', $
   FID = fid, $
   DIMS = dims, $
   POS = (nb), $
   METHOD = 2, $
   GRAY = 1, $
   KERNEL = (5,5) + 1, $
   VALUE = (5,5) + 1, $
   OUT_NAME = OutFile, $
   R_FID = r_fid
; Pass the R_FID to an ENVIRaster object.
OutRaster = (r_fid)
; Display the original data and the result
View = e.GetView()
Layer = View.(Raster)
Portal = View.()


ENVI_DOIT, 'MORPH_DOIT', CYCLES=integer, DIMS=array, FID=file ID, /GRAY, /IN_MEMORY, KERNEL=value, METHOD={0 | 1 | 2 | 3}, OUT_BNAME=string array, OUT_NAME=string, POS=array, R_FID=variable, VALUE=array



Use this integer keyword to specify the number of cycles to perform the morphological operation.


The “dimensions” keyword is a five-element array of long integers that defines the spatial subset (of a file or array) to use for processing. Nearly every time you specify the keyword FID, you must also specify the spatial subset of the corresponding file (even if the entire file, with no spatial subsetting, is to be processed).

  • DIMS[0]: A pointer to an open ROI; use only in cases where ROIs define the spatial subset. Otherwise, set to -1L.
  • DIMS[1]: The starting sample number. The first x pixel is 0.
  • DIMS[2]: The ending sample number
  • DIMS[3]: The starting line number. The first y pixel is 0.
  • DIMS[4]: The ending line number

To process an entire file (with no spatial subsetting), define DIMS as shown in the following code example. This example assumes you have already opened a file using ENVI_SELECT or ENVI_PICKFILE:

  , fid, dims=dims


The file ID (FID) is a long-integer scalar with a value greater than 0. An invalid FID has a value of -1. The FID is provided as a named variable by any routine used to open or select a file. Often, the FID is returned from the keyword R_FID in the ENVIRasterToFID routine. Files are processed by referring to their FIDs. If you work directly with the file in IDL, the FID is not equivalent to a logical unit number (LUN).


Set this keyword to perform gray scale, rather than binary, operations. Non-zero elements of the kernel parameter determine the shape of the structuring element (neighborhood). If VALUE is not present, all elements of the structuring element are 0, yielding the neighborhood minimum operator for the erode method and the maximum operator for dilate method.


Set this keyword to specify that output should be stored in memory. If you do not set IN_MEMORY, output will be stored on disk and you must specify OUT_NAME (see below).


Use this keyword to specify a structuring element for the morphological process. The elements are interpreted as binary values, either 0 or 1.


Set this keyword to one of the following values to indicate the type of filter to apply:

  • 0: Erode
  • 1: Dilate
  • 2: Open
  • 3: Close


Use this keyword to specify a string array of output band names.


Use this keyword to specify a string with the output filename for the resulting data. If you set the keyword IN_MEMORY, you do not need to specify OUT_NAME.


Use this keyword to specify an array of band positions, indicating the band numbers on which to perform the operation. This keyword indicates the spectral subset of bands to use in processing. POS is an array of long integers, ranging from 0 to the number of bands minus 1. Specify bands starting with zero (Band 1=0, Band 2=1, etc.) For example, to process only Bands 3 and 4 of a multi-band file, POS=[2, 3].

POS is typically used with individual files. The example code below illustrates the use of POS for a single file with four bands of data:

, '', dims=dims, fid=fid, pos=pos, $
comp_flag=3, dmin=dmin, dmax=dmax, mean=mean, stdv=stdv, hist=hist

But what if you need to create an output file consisting of data from different bands, each from different files? Library routines such as CF_DOIT and ENVI_LAYER_STACKING_DOIT can accomplish this, but they use the POS keyword differently. Suppose you have four files, test1, test2, test3, and test4, with corresponding FIDs of fid1, fid2, fid3, and fid4, respectively. In the following example, you want Band 3 from test1 in the first position, Band 2 from test2 in the second position, Band 6 from test3 in the third position, and Band 4 from test4 in the fourth position. The code should be as follows:

  fid_array = [fid1,fid2,fid3,fid4]
, '', dims=dims, fid=fid_array


ENVI Classic library routines that result in new images also have an R_FID, or “returned FID.” This is simply a named variable containing the file ID to access the processed data. Specifying this keyword saves you the step of opening the new file from disk.


Use this keyword to specify an array of the same dimensions as the kernel providing the values of the structuring element. The presence of this keyword implies gray scale erosion. Each element in the structuring element is either subtracted (eroded) or added (dilated) to the associated pixels. The minimum (erode) or maximum (dilate) is performed.