Previous Getting Started with IDL: Programming in IDL Next

Executing a Simple IDL Program

To show IDL's programming capabilities, the following program removes the bridges from the image of Manhattan Island in New York City using IDL's erosion and dilation capabilities.

  1. From the IDLDE, open a new IDL Editor window by selecting File New Editor (or for Macintosh, simply File New).
  2.  

  3. Type (or copy) the following lines of code into the new Editor window to form a program:
  4. PRO remove_bridges  
    ;  
    ;Read an image of New York.  
    xsize = 768 ; pixels.  
    ysize = 512 ; pixels.  
    img = read_binary( $  
        filepath('nyny.dat', subdir=['examples', 'data']), $  
        data_dims=[xsize, ysize])  
    ;  
    ;Increase image's contrast.  
    img = bytscl(img)  
    ;  
    ;Create an image mask from thresholded image.  
    threshold_level = 70 ; determined empirically.  
    mask = img lt threshold_level  
    ;  
    ;Make a disk-shaped "structuring element."  
    disk_size = 7 ; determined empirically.  
    se = shift(dist(disk_size), disk_size / 2, disk_size / 2)  
    se = se le disk_size / 2  
    ;  
    ;Remove details in the mask's shape.  
    mask = dilate(erode(mask, se), se)  
    ;  
    ;Fuse gaps in the mask's shape.  
    mask = erode(dilate(mask, se), se)  
    ;  
    ;Remove all but the largest region in the mask.  
    label_img = label_region(mask)  
    labels = label_img[where(label_img ne 0)] ; Remove 
    background.  
    label = where(histogram(label_img) eq max(histogram(labels)))  
    mask = label_img eq label[0]  
    ;  
    ;Generate a new image consisting of local area minimums.  
    new_img = dilate(erode(img, se, /gray), se, /gray)  
    ;  
    ;Replace new image with original image, where not masked.  
    new_img[where(mask eq 0)] = img[where(mask eq 0)]  
    ;  
    ;View result, comparing the new image with the original.  
    print, 'Hit any key to end program.'   
    window, xsize=xsize, ysize=ysize  
    flick, img, new_img  
    wdelete  
    END  
      
    

     


    Note
    Semicolons (;) in IDL code are indicators of the beginning of comment lines, which explain what the actual code lines are doing and/or to help you understand your code (while being ignored by IDL itself).

     


    Note
    The dollar sign ($) at the end of the first line is the IDL continuation character. It allows you to enter long IDL commands as multiple lines.

     


    Note
    To change the colors used in the chromacoded editor, select File Preferences and then select the Editor tab.

Saving, Compiling and Running

To view the program at work, IDL requires a few additional steps:

  1. Save the file as remove_bridges.pro by selecting File Save As and then entering "remove_bridges.pro".
  2.  

  3. Compile the program by selecting Run Compile remove_bridges.pro (or on Macintosh, simply Run Compile).
  4.  

  5. Run the program by selecting Run Run remove_bridges.pro (or for Macintosh, simply Run Run).
  6.  


    Note
    If your program encounters an error in running be sure to check your code for typographical errors.

  IDL Online Help (March 06, 2007)