// Script function: Create a mask with intensity threshold.
// Requirement: Open an image.

// Unless named starting with GlobalSino, EM, My, Self, the function commands are defined 
//by Gatan DM, please see at link: http://www.globalsino.com/EM/page2597.html



image GlobalSinoImage := GetFrontImage()
ImageDisplay EMimage = GlobalSinoImage.ImageGetImageDisplay( 0 )

number EMlow, EMhigh

EMimage.ImageDisplayGetContrastLimits( EMlow, EMhigh )

number EMwidth = GlobalSinoImage.ImageGetDimensionSize( 0 )
number EMheight = GlobalSinoImage.ImageGetDimensionSize( 1 )

EMimage.RasterImageDisplaySetThresholdOn( 1 ) 

// ==============

TagGroup GlobalSino, EMItems
GlobalSino = DLGCreateDialog( "Please confirm your low and high thresholds", EMItems )
TagGroup MyNumber1, MyNumber2, MyNumber3, MyNumber4 

EMItems.DLGAddElement( DLGCreateRealField( "Lowest intensity :", MyNumber1, EMlow, 9, 3 ) )       
EMItems.DLGAddElement( DLGCreateRealField( "Highest intensity :", MyNumber2, EMhigh, 8, 3 ) )       

EMItems.DLGAddElement( DLGCreateRealField( "Low limit :", MyNumber3, EMlow*5, 16, 3 ) )       
EMItems.DLGAddElement( DLGCreateRealField( "High limit :", MyNumber4, EMhigh*0.5, 16, 5 ) )       
if ( !Alloc( UIframe ).Init( GlobalSino ).Pose() ) exit (0)
 
number EMlowLimit = MyNumber3.DLGGetValue(), EMhighLimit = MyNumber4.DLGGetValue()

// =======================

EMimage.RasterImageDisplaySetThresholdLimits( EMlowLimit, EMhighLimit ) 

image EMmask := IntegerImage("Mask", 1, 0, EMwidth, EMheight ) // Binary mask
EMimage.RasterImageDisplayAddThresholdToMask( EMmask, 0, 0, EMheight, EMwidth ) 
EMimage.RasterImageDisplaySetThresholdOn( 0 ) 
ShowImage( EMmask )


