Re: Masking using user defined ROIs [message #32681] |
Thu, 31 October 2002 09:35  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
John Dickson (j.dickson@rfc.ucl.ac.uk) writes:
> Thanks David, your code worked beautifully. Just to help me understand
> the code a bit better, how would I change the code to mask everything
> outside the roi to zero.
Here is the same code, with a REVERSEMASK keyword.
Set this keyword to return an image where everything
outside the ROIs is masked out.
IDL> TV, ExampleROIMask(/ReverseMask)
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
;*********************************************************** ******
FUNCTION ExampleROIMask, image, ReverseMask=reversemask
IF N_Elements(image) EQ 0 THEN BEGIN
; Get an image, if needed.
filename = Filepath(Subdir=['examples','data'], 'mr_knee.dcm')
image = Read_DICOM(filename)
ENDIF
; Draw ROI's on image. (Use the freehand PENCIL tool, for example.)
XROI, image, Regions_Out=rois, /Block
; Create an image mask from the ROIs you just created.
dim = Size(image, /Dimensions)
IF Keyword_Set(reversemask) THEN $
mask = BytArr(dim[0], dim[1]) ELSE $
mask = BytArr(dim[0], dim[1]) + 1B
; Cycle through the ROIs.
FOR j=0, N_Elements(rois)-1 DO BEGIN
thisROI = rois[j]
IF Obj_Valid(thisROI) THEN BEGIN
thisROI -> GetProperty, Data=polygon
indices = PolyFillV(polygon[0,*], polygon[1,*], dim[0], dim[1])
IF indices[0] NE -1 THEN BEGIN
IF Keyword_Set(reversemask) THEN $
mask[indices] = 1 ELSE $
mask[indices] = 0
ENDIF
Obj_Destroy, thisROI
ENDIF
ENDFOR
; Apply the mask to the image and return it.
RETURN, image * mask
END
;*********************************************************** ******
|
|
|