|
Image Processing : Working with Regions of Interest (ROIs) |
|
While most examples in this chapter use interactive methods to define ROIs, a region can also be defined programmatically. The following example uses thresholding and the CONTOUR function to programmatically trace region outlines. After the path information of the regions has been input into ROI objects, the DRAW_ROI procedure displays each region. The example then computes and returns the geometric area and perimeter of each region as well as the number of pixels making up each region when it is displayed. Complete the following steps for a detailed description of the process.
| Example Code See programdefineroi.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
img = READ_PNG(FILEPATH('mineral.png', $
SUBDIRECTORY = ['examples', 'data']))
dims = SIZE(img, /DIMENSIONS)
WINDOW, 0, XSIZE = dims[0], YSIZE = dims[1] TVSCL, img
The following figure displays the initial image.
threshImg = (img LT 50)
| Note See Determining Intensity Values for Threshold and Stretch for a useful strategy to use when determining threshold values. |
strucElem = REPLICATE(1, 3, 3) threshImg = ERODE(DILATE(TEMPORARY(threshImg), $ strucElem), strucElem)
CONTOUR, threshImg, LEVEL = 1, $ XMARGIN = [0, 0], YMARGIN = [0, 0], $ /NOERASE, PATH_INFO = pathInfo, PATH_XY = pathXY, $ XSTYLE = 5, YSTYLE = 5, /PATH_DATA_COORDS
The PATH_INFO variable contains the path information for the contours. When used in conjunction with the PATH_XY variable, containing the coordinates of the contours, the CONTOUR procedure records the outline of closed regions. See CONTOUR in the IDL Reference Guide for full details.
WINDOW, 2, XSIZE = dims[0], YSIZE = dims[1] TVSCL, img LOADCT, 12
FOR I = 0,(N_ELEMENTS(PathInfo) - 1 ) DO BEGIN & $
| Note The & after BEGIN and the $ allow you to use the FOR/DO loop at the IDL command line. These & and $ symbols are not required when the FOR/DO loop in placed in an IDL program as shown in ProgramDefineROI.pro in the examples/doc/image subdirectory of the IDL installation directory. |
line = [LINDGEN(PathInfo(I).N), 0] & $
oROI = OBJ_NEW('IDLanROI', $
(pathXY(*, pathInfo(I).OFFSET + line))[0, *], $
(pathXY(*, pathInfo(I).OFFSET + line))[1, *]) & $
DRAW_ROI, oROI, COLOR = 80 & $
maskResult = oROI -> ComputeMask( $ DIMENSIONS = [dims[0], dims[1]]) & $ IMAGE_STATISTICS, img, MASK = maskResult, $ COUNT = maskArea & $
ROIStats = oROI -> ComputeGeometry( $ AREA = geomArea, PERIMETER = perimeter, $ SPATIAL_SCALE = [1.2, 1.2, 1.0]) & $
| Note The value for SPATIAL _SCALE in the previous statement is used only as an example. The actual spatial scale value is typically known based upon equipment used to gather the data. |
PRINT, ' ' & $ PRINT, 'Region''s mask area = ', $ FIX(maskArea), ' pixels' & $ PRINT, 'Region''s geometric area = ', $ FIX(geomArea), ' mm' & $ PRINT, 'Region''s perimeter = ', $ FIX(perimeter),' mm' & $ WAIT, 3
OBJ_DESTROY, oROI & $
ENDFOR
The outlines of the ROIs recorded by the CONTOUR function have been translated into ROI objects and displayed using DRAW_ROI. Each region's "mask area," (computed using IDLanROI::ComputeMask in conjunction with IMAGE_STATISTICS) shows the number of pixels covered by the region when it is displayed on the screen.
Each region's geometric area and perimeter, (computed using IDLanROI::ComputeGeometry's SPATIAL_SCALE keyword) results in the following geometric area and perimeter measurements in millimeters.
IDL Online Help (March 06, 2007)