Testing an ROI for Point Containment

The IDLanROI::ContainsPoints function method determines whether a point having given coordinates lies inside, outside, on the boundary of, or on the vertex of a designated ROI. The following example allows the creation of an ROI within an image of the world using XROI. After exiting XROI, a point is selected and tested to determine its relationship to the ROI. The example then creates textual and graphical displays of the results. Complete the following steps for a detailed description of the process.

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

  1. Prepare the display device:
  2. DEVICE, DECOMPOSED = 0, RETAIN = 2 
    
  3. Select and open the image file and get its dimensions:
  4. img = READ_PNG(FILEPATH('avhrr.png', $ 
       SUBDIRECTORY = ['examples', 'data']), R, G, B) 
    dims = SIZE(img, /DIMENSIONS) 
    
  5. Open the file in the XROI utility to create an ROI:
  6. XROI, img, REGIONS_OUT = ROIout, R, G, B, /BLOCK, $ 
       TITLE = 'Create ROI and Close Window' 
     

    After creating any region using the tool of your choice, close the XROI utility to save the ROI object data in the variable, ROIout.

  7. Load the image color table and display the image in a new window:
  8. TVLCT, R, G, B 
    WINDOW, 0, XSIZE = dims[0], YSIZE = dims[1], $ 
       TITLE = 'Left-Click Anywhere in Image' 
    TV, img 
    
  9. The CURSOR function allows you to select and define the coordinates of a point. After entering the following line, position your cursor anywhere in the image window and click the left mouse button to select a point:
  10. CURSOR, xi, yi, /DEVICE 
    
  11. Delete the window after selecting the point:
  12. WDELETE, 0 
    
  13. Using the coordinates returned by the CURSOR function, determine the placement of the point in relation to the ROI object using IDLanROI::ContainsPoints:
  14. ptTest = ROIout -> ContainsPoints(xi,yi) 
    
  15. The value of ptTest, returned by the previous statement, ranges from 0 to 3. Create the following vector of string data where the index value of the string element relates to value of ptTest. Print the actual and textual value of ptTest:
  16. containResults = [ $ 
       'Point lies outside ROI', $ 
       'Point lies inside ROI', $ 
       'Point lies on the edge of the ROI', $ 
       'Point lies on vertex of the ROI'] 
     
    PRINT, 'Result =',ptTest,':   ', containResults[ptTest] 
    
  17. Complete the following steps to create a visual display of the ROI and the point that you have defined. First, create a 7 by 7 ROI indicating the point:
  18. x = LINDGEN(7*7) MOD 7 + xi 
    y = LINDGEN(7*7) / 7 + yi 
    point = x + y * dims[0] 
    
  19. Define the color with which the ROI and point are drawn:
  20. maxClr = !D.TABLE_SIZE - 1 
    TVLCT, 255, 255, 255, maxClr 
    
  21. Draw the point within the original image and display it:
  22. regionPt = img 
    regionPt[point] = maxClr 
    WINDOW, 0, XSIZE = dims[0], YSIZE = dims[1], $ 
       TITLE='Containment Test Results' 
    TV, regionPt 
    
  23. Draw the ROI over the image using DRAW_ROI:
  24. DRAW_ROI, ROIout, COLOR = maxClr, /LINE_FILL, $ 
       THICK = 2, LINESTYLE = 0, ORIENTATION = 315, /DEVICE 
    
  25. Clean up object references that are not destroyed by the window manager:
  26. OBJ_DESTROY, ROIout 
     

    The following figure displays a region covering South America and a point within the African continent. Your results will depend upon the ROI and point you have defined when running this program.

    Figure 6-13: Detail of Point Containment Test

    imgroi13.gif