
// Script function: Calculate an area above a given intensity-threshold on the image
// 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
 
if ( !GetFrontImage( GlobalSinoImage ) )
Throw( "Please open an image" )

GlobalSinoImage:=GetFrontImage()

number EMxScale, EMyScale
GetScale(GlobalSinoImage, EMxScale, EMyScale)

number EMxSize, EMySize, EMx, EMy
getsize(GlobalSinoImage, EMxSize, EMySize)
number EMValue

TagGroup GlobalSino, EMItems
GlobalSino = DLGCreateDialog( "Please confirm your own threshold", EMItems )
 
TagGroup MyNumber1, MyNumber2, MyNumber3

Number GlobalSinoMax = max(GlobalSinoImage)
Number GlobalSinoMin = min(GlobalSinoImage)
  
EMItems.DLGAddElement( DLGCreateRealField( "Maximum intensity:               ", MyNumber1, GlobalSinoMax, 10, 3 ) )       
EMItems.DLGAddElement( DLGCreateRealField( "Minimum intensity:                ", MyNumber2, GlobalSinoMin, 10, 5 ) )       

number GlobalSionMedium = (GlobalSinoMax + GlobalSinoMin)/2
EMItems.DLGAddElement( DLGCreateRealField( "My threshold:                       ", MyNumber3, GlobalSionMedium, 10, 8 ) )    

  
if ( !Alloc( UIframe ).Init( GlobalSino ).Pose() ) exit (0)


for (EMy = 0; EMy < EMySize; EMy++) {
for (EMx = 0; EMx < EMxSize; EMx++) {
EMValue = GetPixel(GlobalSinoImage, EMx, EMy);
if (EMValue > MyNumber3.DLGGetValue())
EMValue = 1
else
EMValue = 0
SetPixel(GlobalSinoImage, EMx, EMy, EMValue)
}
}

number EMPixelArea
EMPixelArea = EMxScale*EMyScale*sum (GlobalSinoImage)
String EMLengthUnits = GetUnitString(GlobalSinoImage)
Result ("\n" + EMPixelArea + "\t" + EMLengthUnits+"^2")

GlobalSinoImage.SetName("Included area from " + GlobalSinoImage.GetName())
ShowImage(GlobalSinoImage)
