|
Image Processing : Working with Regions of Interest (ROIs) |
|
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. |
file = FILEPATH('md5290fc1.jpg', $
SUBDIRECTORY = ['examples', 'data'])
READ_JPEG, file, img, /GRAYSCALE
dims = SIZE(img, /DIMENSIONS)
XROI, img, REGIONS_OUT = ROIout, /BLOCK
Close the XROI window to save the region object data in the variable, ROIout.
ROIout -> GetProperty, DATA = ROIdata x = ROIdata[0,*] y = ROIdata[1,*]
ROIout -> SetProperty, COLOR = [255,255,255], THICK = 2
oImg = OBJ_NEW('IDLgrImage', img,$
DIMENSIONS = dims)
oWindow = OBJ_NEW('IDLgrWindow', DIMENSIONS = dims, $
RETAIN = 2, TITLE = 'Selected ROI')
viewRect = [0, 0, dims[0], dims[1]]
oView = OBJ_NEW('IDLgrView', VIEWPLANE_RECT = viewRect)
oModel = OBJ_NEW('IDLgrModel')
oModel -> Add, oImg
oModel -> Add, ROIout
oView -> Add, oModel
oWindow -> Draw, oView
maskResult = ROIout -> ComputeMask(DIMENSIONS = dims)
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. |
mask = (maskResult GT 0) maskImg = img * mask
cropImg = maskImg[min(x):max(x), min(y): max(y)] cropDims = SIZE(cropImg, /DIMENSIONS)
oMaskImg = OBJ_NEW('IDLgrImage', cropImg, $
DIMENSIONS = dims)
oMaskWindow = OBJ_NEW('IDLgrWindow', $
DIMENSIONS = 2 * cropDims, RETAIN = 2, $
TITLE = 'Magnified ROI', LOCATION = dims)
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.
![]() |
OBJ_DESTROY, [oView, oMaskView, ROIout]
IDL Online Help (March 06, 2007)