// Script function: Set the pixel values (contrast) to zero if the values are out of the range of needs with the reference of the average (threshold) 
// contrast of the image and create an image which are in binary contrast by setting the needed values to 1.
// 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

// =======================

 
for (EMy = 0; EMy < EMySize; EMy++) {
for (EMx = 0; EMx < EMxSize; EMx++) {
EMValue = GetPixel(GlobalSinoImage, EMx, EMy);
if (EMValue < 0)
EMValue = 0
else
EMValue
SetPixel(GlobalSinoImage, EMx, EMy, EMValue)
}
}

 
GlobalSinoImage.SetName(GlobalSinoImage.GetName())
ShowImage(GlobalSinoImage)


// =======================
// =======================


Image TwoPercent := ImageClone(GlobalSinoImage)
ShowImage(TwoPercent)

number TwoPercentEMxSize, TwoPercentEMySize, TwoPercentEMx, TwoPercentEMy
getsize(TwoPercent, TwoPercentEMxSize, TwoPercentEMySize)
number TwoPercentEMValue

for (TwoPercentEMy = 0; TwoPercentEMy < TwoPercentEMySize; TwoPercentEMy++) {
for (TwoPercentEMx = 0; TwoPercentEMx < TwoPercentEMxSize; TwoPercentEMx++) {
TwoPercentEMValue = GetPixel(TwoPercent, TwoPercentEMx, TwoPercentEMy);
if (TwoPercentEMValue < 0.04*mean(GlobalSinoImage))
TwoPercentEMValue = 0
else
TwoPercentEMValue
SetPixel(TwoPercent, TwoPercentEMx, TwoPercentEMy, TwoPercentEMValue)
}
}


TwoPercent.SetName("2 % Cut of " + GlobalSinoImage.GetName())
ShowImage(TwoPercent)

// =======================

 
Image TwoPercentBinary := ImageClone(GlobalSinoImage)
ShowImage(TwoPercentBinary)

number TwoPercentBinaryEMxSize, TwoPercentBinaryEMySize, TwoPercentBinaryEMx, TwoPercentBinaryEMy
getsize(TwoPercentBinary, TwoPercentBinaryEMxSize, TwoPercentBinaryEMySize)
number TwoPercentBinaryEMValue

for (TwoPercentBinaryEMy = 0; TwoPercentBinaryEMy < TwoPercentBinaryEMySize; TwoPercentBinaryEMy++) {
for (TwoPercentBinaryEMx = 0; TwoPercentBinaryEMx < TwoPercentBinaryEMxSize; TwoPercentBinaryEMx++) {
TwoPercentBinaryEMValue = GetPixel(TwoPercentBinary, TwoPercentBinaryEMx, TwoPercentBinaryEMy);
if (TwoPercentBinaryEMValue < 0.04*mean(GlobalSinoImage))
TwoPercentBinaryEMValue = 0
else
TwoPercentBinaryEMValue = 1
SetPixel(TwoPercentBinary, TwoPercentBinaryEMx, TwoPercentBinaryEMy, TwoPercentBinaryEMValue)
}
}

 
TwoPercentBinary.SetName("2 % Binary of " + GlobalSinoImage.GetName())
ShowImage(TwoPercentBinary)

 

// =======================
// =======================

 

Image FivePercent := ImageClone(GlobalSinoImage)
ShowImage(FivePercent)

number FivePercentEMxSize, FivePercentEMySize, FivePercentEMx, FivePercentEMy
getsize(FivePercent, FivePercentEMxSize, FivePercentEMySize)
number FivePercentEMValue


for (FivePercentEMy = 0; FivePercentEMy < FivePercentEMySize; FivePercentEMy++) {
for (FivePercentEMx = 0; FivePercentEMx < FivePercentEMxSize; FivePercentEMx++) {
FivePercentEMValue = GetPixel(FivePercent, FivePercentEMx, FivePercentEMy);
if (FivePercentEMValue < 0.1*mean(GlobalSinoImage))
FivePercentEMValue = 0
else
FivePercentEMValue
SetPixel(FivePercent, FivePercentEMx, FivePercentEMy, FivePercentEMValue)
}
}

FivePercent.SetName("5 % Cut of " + GlobalSinoImage.GetName())
ShowImage(FivePercent)


// =======================

Image FivePercentBinary := ImageClone(GlobalSinoImage)
ShowImage(FivePercentBinary)

number FivePercentBinaryEMxSize, FivePercentBinaryEMySize, FivePercentBinaryEMx, FivePercentBinaryEMy
getsize(FivePercentBinary, FivePercentBinaryEMxSize, FivePercentBinaryEMySize)
number FivePercentBinaryEMValue

for (FivePercentBinaryEMy = 0; FivePercentBinaryEMy < FivePercentBinaryEMySize; FivePercentBinaryEMy++) {
for (FivePercentBinaryEMx = 0; FivePercentBinaryEMx < FivePercentBinaryEMxSize; FivePercentBinaryEMx++) {
FivePercentBinaryEMValue = GetPixel(FivePercentBinary, FivePercentBinaryEMx, FivePercentBinaryEMy);
if (FivePercentBinaryEMValue < 0.1*mean(GlobalSinoImage))
FivePercentBinaryEMValue = 0
else
FivePercentBinaryEMValue = 1
SetPixel(FivePercentBinary, FivePercentBinaryEMx, FivePercentBinaryEMy, FivePercentBinaryEMValue)
}
}


FivePercentBinary.SetName("5 % Binary of " + GlobalSinoImage.GetName())
ShowImage(FivePercentBinary)


// =======================
// =======================

 
Image TenPercent := ImageClone(GlobalSinoImage)
ShowImage(TenPercent)

number TenPercentEMxSize, TenPercentEMySize, TenPercentEMx, TenPercentEMy
getsize(TenPercent, TenPercentEMxSize, TenPercentEMySize)
number TenPercentEMValue

for (TenPercentEMy = 0; TenPercentEMy < TenPercentEMySize; TenPercentEMy++) {
for (TenPercentEMx = 0; TenPercentEMx < TenPercentEMxSize; TenPercentEMx++) {
TenPercentEMValue = GetPixel(TenPercent, TenPercentEMx, TenPercentEMy);
if (TenPercentEMValue < 0.2*mean(GlobalSinoImage))
TenPercentEMValue = 0
else
TenPercentEMValue
SetPixel(TenPercent, TenPercentEMx, TenPercentEMy, TenPercentEMValue)
}
}

TenPercent.SetName("10 % Cut of " + GlobalSinoImage.GetName())
ShowImage(TenPercent)

// =======================

 
Image TenPercentBinary := ImageClone(GlobalSinoImage)
ShowImage(TenPercentBinary)

number TenPercentBinaryEMxSize, TenPercentBinaryEMySize, TenPercentBinaryEMx, TenPercentBinaryEMy
getsize(TenPercentBinary, TenPercentBinaryEMxSize, TenPercentBinaryEMySize)
number TenPercentBinaryEMValue

for (TenPercentBinaryEMy = 0; TenPercentBinaryEMy < TenPercentBinaryEMySize; TenPercentBinaryEMy++) {
for (TenPercentBinaryEMx = 0; TenPercentBinaryEMx < TenPercentBinaryEMxSize; TenPercentBinaryEMx++) {

TenPercentBinaryEMValue = GetPixel(TenPercentBinary, TenPercentBinaryEMx, TenPercentBinaryEMy);
if (TenPercentBinaryEMValue < 0.2*mean(GlobalSinoImage))
TenPercentBinaryEMValue = 0
else
TenPercentBinaryEMValue = 1
SetPixel(TenPercentBinary, TenPercentBinaryEMx, TenPercentBinaryEMy, TenPercentBinaryEMValue)
}
}

TenPercentBinary.SetName("10 % Binary of " + GlobalSinoImage.GetName())
ShowImage(TenPercentBinary)

// =======================

// =======================


Image fifteenPercent := ImageClone(GlobalSinoImage)
ShowImage(fifteenPercent)

number fifteenPercentEMxSize, fifteenPercentEMySize, fifteenPercentEMx, fifteenPercentEMy
getsize(fifteenPercent, fifteenPercentEMxSize, fifteenPercentEMySize)
number fifteenPercentEMValue


for (fifteenPercentEMy = 0; fifteenPercentEMy < fifteenPercentEMySize; fifteenPercentEMy++) {
for (fifteenPercentEMx = 0; fifteenPercentEMx < fifteenPercentEMxSize; fifteenPercentEMx++) {
fifteenPercentEMValue = GetPixel(fifteenPercent, fifteenPercentEMx, fifteenPercentEMy);
if (fifteenPercentEMValue < 0.3*mean(GlobalSinoImage))
fifteenPercentEMValue = 0
else
fifteenPercentEMValue
SetPixel(fifteenPercent, fifteenPercentEMx, fifteenPercentEMy, fifteenPercentEMValue)
}
}

fifteenPercent.SetName("15 % Cut of " + GlobalSinoImage.GetName())
ShowImage(fifteenPercent)

// =======================

Image fifteenPercentBinary := ImageClone(GlobalSinoImage)
ShowImage(fifteenPercentBinary)

number fifteenPercentBinaryEMxSize, fifteenPercentBinaryEMySize, fifteenPercentBinaryEMx, fifteenPercentBinaryEMy
getsize(fifteenPercentBinary, fifteenPercentBinaryEMxSize, fifteenPercentBinaryEMySize)
number fifteenPercentBinaryEMValue


for (fifteenPercentBinaryEMy = 0; fifteenPercentBinaryEMy < fifteenPercentBinaryEMySize; fifteenPercentBinaryEMy++) {
for (fifteenPercentBinaryEMx = 0; fifteenPercentBinaryEMx < fifteenPercentBinaryEMxSize; fifteenPercentBinaryEMx++) {
fifteenPercentBinaryEMValue = GetPixel(fifteenPercentBinary, fifteenPercentBinaryEMx, fifteenPercentBinaryEMy);
if (fifteenPercentBinaryEMValue < 0.3*mean(GlobalSinoImage))
fifteenPercentBinaryEMValue = 0
else
fifteenPercentBinaryEMValue = 1
SetPixel(fifteenPercentBinary, fifteenPercentBinaryEMx, fifteenPercentBinaryEMy, fifteenPercentBinaryEMValue)
}
}

fifteenPercentBinary.SetName("15 % Binary of " + GlobalSinoImage.GetName())
ShowImage(fifteenPercentBinary)

// =======================

// =======================

 

Image FiftyPercent := ImageClone(GlobalSinoImage)
ShowImage(FiftyPercent)

number FiftyPercentEMxSize, FiftyPercentEMySize, FiftyPercentEMx, FiftyPercentEMy
getsize(FiftyPercent, FiftyPercentEMxSize, FiftyPercentEMySize)
number FiftyPercentEMValue

for (FiftyPercentEMy = 0; FiftyPercentEMy < FiftyPercentEMySize; FiftyPercentEMy++) {
for (FiftyPercentEMx = 0; FiftyPercentEMx < FiftyPercentEMxSize; FiftyPercentEMx++) {
FiftyPercentEMValue = GetPixel(FiftyPercent, FiftyPercentEMx, FiftyPercentEMy);
if (FiftyPercentEMValue < mean(GlobalSinoImage))
FiftyPercentEMValue = 0
else
FiftyPercentEMValue
SetPixel(FiftyPercent, FiftyPercentEMx, FiftyPercentEMy, FiftyPercentEMValue)
}
}

FiftyPercent.SetName("50 % Cut of " + GlobalSinoImage.GetName())
ShowImage(FiftyPercent)

// =======================

Image FiftyPercentBinary := ImageClone(GlobalSinoImage)
ShowImage(FiftyPercentBinary)

number FiftyPercentBinaryEMxSize, FiftyPercentBinaryEMySize, FiftyPercentBinaryEMx, FiftyPercentBinaryEMy
getsize(FiftyPercentBinary, FiftyPercentBinaryEMxSize, FiftyPercentBinaryEMySize)
number FiftyPercentBinaryEMValue

 
for (FiftyPercentBinaryEMy = 0; FiftyPercentBinaryEMy < FiftyPercentBinaryEMySize; FiftyPercentBinaryEMy++) {
for (FiftyPercentBinaryEMx = 0; FiftyPercentBinaryEMx < FiftyPercentBinaryEMxSize; FiftyPercentBinaryEMx++) {
FiftyPercentBinaryEMValue = GetPixel(FiftyPercentBinary, FiftyPercentBinaryEMx, FiftyPercentBinaryEMy);
if (FiftyPercentBinaryEMValue < mean(GlobalSinoImage))
FiftyPercentBinaryEMValue = 0
else
FiftyPercentBinaryEMValue = 1
SetPixel(FiftyPercentBinary, FiftyPercentBinaryEMx, FiftyPercentBinaryEMy, FiftyPercentBinaryEMValue)
}
}

FiftyPercentBinary.SetName("50 % Binary of " + GlobalSinoImage.GetName())
ShowImage(FiftyPercentBinary)

// =======================

// =======================

 
Image SixtyPercent := ImageClone(GlobalSinoImage)
ShowImage(SixtyPercent)

number SixtyPercentEMxSize, SixtyPercentEMySize, SixtyPercentEMx, SixtyPercentEMy
getsize(SixtyPercent, SixtyPercentEMxSize, SixtyPercentEMySize)
number SixtyPercentEMValue

for (SixtyPercentEMy = 0; SixtyPercentEMy < SixtyPercentEMySize; SixtyPercentEMy++) {
for (SixtyPercentEMx = 0; SixtyPercentEMx < SixtyPercentEMxSize; SixtyPercentEMx++) {
SixtyPercentEMValue = GetPixel(SixtyPercent, SixtyPercentEMx, SixtyPercentEMy);

if (SixtyPercentEMValue < 1.2*mean(GlobalSinoImage))
SixtyPercentEMValue = 0
else
SixtyPercentEMValue
SetPixel(SixtyPercent, SixtyPercentEMx, SixtyPercentEMy, SixtyPercentEMValue)
}
}

 
SixtyPercent.SetName("60 % Cut of " + GlobalSinoImage.GetName())
ShowImage(SixtyPercent)

// =======================

Image SixtyPercentBinary := ImageClone(GlobalSinoImage)
ShowImage(SixtyPercentBinary)

number SixtyPercentBinaryEMxSize, SixtyPercentBinaryEMySize, SixtyPercentBinaryEMx, SixtyPercentBinaryEMy
getsize(SixtyPercentBinary, SixtyPercentBinaryEMxSize, SixtyPercentBinaryEMySize)
number SixtyPercentBinaryEMValue

for (SixtyPercentBinaryEMy = 0; SixtyPercentBinaryEMy < SixtyPercentBinaryEMySize; SixtyPercentBinaryEMy++) {
for (SixtyPercentBinaryEMx = 0; SixtyPercentBinaryEMx < SixtyPercentBinaryEMxSize; SixtyPercentBinaryEMx++) {
SixtyPercentBinaryEMValue = GetPixel(SixtyPercentBinary, SixtyPercentBinaryEMx, SixtyPercentBinaryEMy);
if (SixtyPercentBinaryEMValue < 1.2*mean(GlobalSinoImage))
SixtyPercentBinaryEMValue = 0
else
SixtyPercentBinaryEMValue = 1
SetPixel(SixtyPercentBinary, SixtyPercentBinaryEMx, SixtyPercentBinaryEMy, SixtyPercentBinaryEMValue)
}
}

SixtyPercentBinary.SetName("60 % Binary of " + GlobalSinoImage.GetName())
ShowImage(SixtyPercentBinary)

// =======================

// =======================

 
Image SevenPercent := ImageClone(GlobalSinoImage)
ShowImage(SevenPercent)


number SevenPercentEMxSize, SevenPercentEMySize, SevenPercentEMx, SevenPercentEMy
getsize(SevenPercent, SevenPercentEMxSize, SevenPercentEMySize)
number SevenPercentEMValue

for (SevenPercentEMy = 0; SevenPercentEMy < SevenPercentEMySize; SevenPercentEMy++) {
for (SevenPercentEMx = 0; SevenPercentEMx < SevenPercentEMxSize; SevenPercentEMx++) {
SevenPercentEMValue = GetPixel(SevenPercent, SevenPercentEMx, SevenPercentEMy);
if (SevenPercentEMValue < 1.4*mean(GlobalSinoImage))
SevenPercentEMValue = 0
else
SevenPercentEMValue
SetPixel(SevenPercent, SevenPercentEMx, SevenPercentEMy, SevenPercentEMValue)
}
}

SevenPercent.SetName("70 % Cut of " + GlobalSinoImage.GetName())
ShowImage(SevenPercent)

// =======================

 
Image SevenPercentBinary := ImageClone(GlobalSinoImage)
ShowImage(SevenPercentBinary)

number SevenPercentBinaryEMxSize, SevenPercentBinaryEMySize, SevenPercentBinaryEMx, SevenPercentBinaryEMy
getsize(SevenPercentBinary, SevenPercentBinaryEMxSize, SevenPercentBinaryEMySize)
number SevenPercentBinaryEMValue

for (SevenPercentBinaryEMy = 0; SevenPercentBinaryEMy < SevenPercentBinaryEMySize; SevenPercentBinaryEMy++) {
for (SevenPercentBinaryEMx = 0; SevenPercentBinaryEMx < SevenPercentBinaryEMxSize; SevenPercentBinaryEMx++) {
SevenPercentBinaryEMValue = GetPixel(SevenPercentBinary, SevenPercentBinaryEMx, SevenPercentBinaryEMy);
if (SevenPercentBinaryEMValue < 1.4*mean(GlobalSinoImage))
SevenPercentBinaryEMValue = 0
else
SevenPercentBinaryEMValue = 1
SetPixel(SevenPercentBinary, SevenPercentBinaryEMx, SevenPercentBinaryEMy, SevenPercentBinaryEMValue)
}
}


SevenPercentBinary.SetName("70 % Binary of " + GlobalSinoImage.GetName())
ShowImage(SevenPercentBinary)

// =======================

// =======================