
// Script function: Set pixel values to zero if the values are lower than a threshold and modify the values of other pixels by multiplying a factor (i.e. Contrast factor)
// Note: You can change "My threshold" and "Contrast factor".
// 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 EMxSize, EMySize, EMx, EMy
getsize(GlobalSinoImage, EMxSize, EMySize)
number EMValue

TagGroup GlobalSino, EMItems
GlobalSino = DLGCreateDialog( "Please confirm or enter your own numbers", EMItems )
 
TagGroup MyNumber1, MyNumber2, MyNumber3, MyNumber4, MyNumber5

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 ) )    

EMItems.DLGAddElement( DLGCreateRealField( "Contrast factor:                                                              ", MyNumber4, 9, 10, 8 ) )    

EMItems.DLGAddElement( DLGCreateRealField( "Set 0 to remove low values or 1 to remove high values :", MyNumber5, 0, 9, 3 ) )    
   
if ( !Alloc( UIframe ).Init( GlobalSino ).Pose() ) exit (0)


if (MyNumber5.DLGGetValue() == 0)
{
for (EMy = 0; EMy < EMySize; EMy++) {
for (EMx = 0; EMx < EMxSize; EMx++) {
EMValue = GetPixel(GlobalSinoImage, EMx, EMy);
if (EMValue < MyNumber3.DLGGetValue())
EMValue = 0
else
EMValue = EMValue*(MyNumber4.DLGGetValue())
SetPixel(GlobalSinoImage, EMx, EMy, EMValue)
}
}
}

if (MyNumber5.DLGGetValue() == 1)
{
for (EMy = 0; EMy < EMySize; EMy++) {
for (EMx = 0; EMx < EMxSize; EMx++) {
EMValue = GetPixel(GlobalSinoImage, EMx, EMy);
if (EMValue > MyNumber3.DLGGetValue())
EMValue = 0
else
EMValue = EMValue*(MyNumber4.DLGGetValue())
SetPixel(GlobalSinoImage, EMx, EMy, EMValue)
}
}
}


GlobalSinoImage.SetName("Image with threshold from " + GlobalSinoImage.GetName())
ShowImage(GlobalSinoImage)
