Creating and Displaying an ROI Mask

The IDLanROI::ComputeMask function method defines a 2D mask of a region object, returning an array in which all pixels that lie outside of the region have a value of 0. The mask can then be used to extract the portion of the original image that lies within the ROI. The following example defines an ROI, computes a mask, applies the mask to retain only the portion of the image defined by the ROI, and produces a magnified view of the ROI. Complete the following steps for a detailed description of the process.

Example Code
See scalemask_object.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering scalemask_object at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT scalemask_object.pro.

  1. Select the file, read in the data and get the image dimensions:
  2. file = FILEPATH('md5290fc1.jpg', $ 
       SUBDIRECTORY = ['examples', 'data']) 
    READ_JPEG, file, img, /GRAYSCALE 
    dims = SIZE(img, /DIMENSIONS) 
    
  3. Pass the image to XROI and use the Draw Polygon tool to define the region:
  4. XROI, img, REGIONS_OUT = ROIout, /BLOCK 
     

    Figure 6-11: ROI Definition in XROI

    imgroi11.jpg

    Close the XROI window to save the region object data in the variable, ROIout.

  5. Assign the ROI data to the arrays, x and y:
  6. ROIout -> GetProperty, DATA = ROIdata 
    x = ROIdata[0,*] 
    y = ROIdata[1,*] 
    
  7. Set the properties of the ROI:
  8. ROIout -> SetProperty, COLOR = [255,255,255], THICK = 2 
    
  9. Initialize an IDLgrImage object containing the original image data:
  10. oImg = OBJ_NEW('IDLgrImage', img,$ 
       DIMENSIONS = dims) 
    
  11. Create a window in which to display the image and the ROI:
  12. oWindow = OBJ_NEW('IDLgrWindow', DIMENSIONS = dims, $  
       RETAIN = 2, TITLE = 'Selected ROI') 
    
  13. Create the view plane and initialize the view:
  14. viewRect = [0, 0, dims[0], dims[1]] 
    oView = OBJ_NEW('IDLgrView', VIEWPLANE_RECT = viewRect) 
    
  15. Initialize a model object and add the image and ROI to the model. Add the model to the view and draw the view in the window to display the ROI overlaid onto the original image:
  16. oModel = OBJ_NEW('IDLgrModel') 
    oModel -> Add, oImg 
    oModel -> Add, ROIout 
    oView -> Add, oModel 
    oWindow -> Draw, oView 
    
  17. Use the IDLanROI::ComputeMask function to create a 2D mask of the region. Pixels that fall outside of the ROI will be assigned a value of 0:
  18. maskResult = ROIout -> ComputeMask(DIMENSIONS = dims) 
    
  19. Use the IMAGE_STATISTICS procedure to compute the area of the mask, inputting maskResult as the MASK value. Print count to view the number of pixels occurring within the masked region:
  20. IMAGE_STATISTICS, img, MASK = MaskResult, COUNT = count 
    PRINT, 'area of mask =  ', count,' pixels' 
     
    
    

    Note
    The COUNT keyword to IMAGE_STATISTICS returns the number of pixels covered by the ROI when it is displayed, the same value as that shown in the "# Pixels" field of XROI's ROI Information dialog.

  21. From the ROI mask, create a binary mask, consisting of only zeros and ones. Multiply the binary mask times the original image to retain only the portion of the image that was defined in the original ROI:
  22. mask = (maskResult GT 0) 
    maskImg = img * mask 
    
  23. Using the minimum and maximum values of the ROI array, create a cropped array, cropImg, and get its dimensions:
  24. cropImg = maskImg[min(x):max(x), min(y): max(y)] 
    cropDims = SIZE(cropImg, /DIMENSIONS) 
    
  25. Initialize an image object with the cropped region data:
  26. oMaskImg = OBJ_NEW('IDLgrImage', cropImg, $ 
       DIMENSIONS = dims) 
    
  27. Using the cropped region dimensions, create an offset window. Multiply the x and y dimensions times the value by which you wish to magnify the ROI:
  28. oMaskWindow = OBJ_NEW('IDLgrWindow', $ 
       DIMENSIONS = 2 * cropDims, RETAIN = 2, $ 
       TITLE = 'Magnified ROI', LOCATION = dims) 
    
  29. Create the display objects and display the cropped and magnified ROI:
  30. oMaskView = OBJ_NEW('IDLgrView', VIEWPLANE_RECT = viewRect) 
    oMaskModel = OBJ_NEW('IDLgrModel') 
    oMaskModel -> Add, oMaskImg 
    oMaskView -> Add, oMaskModel 
    OMaskWindow -> Draw, oMaskView 
     

    The original and the magnified view of the ROI are shown in the following figure.

    Figure 6-12: Original and Magnified View of the ROI

    Creating_and_Displaying_an_ROI_Mask-09.jpg

  31. Clean up object references that are not destroyed by the window manager when you close the Object Graphics displays:
  32. OBJ_DESTROY, [oView, oMaskView, ROIout]