Bin 3D image
DigitalMicrograph Script
Perform voxel binning in 3D images (demonstrate Slice3 command).
Preview
/////////////////////////////////////////////////////////////////////
// (c) Gatan Inc.
/////////////////////////////////////////////////////////////////////
// This script performs data binning of a 3D image using the slice3 command.
// Voxel intensities of a [bin x bin x bin] volume are summed into a single voxel.
//////////////////////////////////////////////////////////////////////
// last modified 08-August-2014 BS
image img
if ( !GetFrontImage( img ) ) exit(0) // Exit script if no front-image found
if ( 3 != ImageGetNumDimensions( img ) ) exit(0) // Exit script if the image is not 3D
// Ask user to provide a interger number for binning.
// Refuse entries which are no integers or <=0
// Exit script if user clicks the cancel button
number bin=0
while( (0>=bin) || ( 0 != mod(bin,1) ) )
{
if ( !GetNumber("Enter binning factor as positive integer number.", round(bin), bin ) ) exit(0)
}
number width,height,depth
Get3DSize( img, width, height, depth )
// Check that binning is smaller than any image dimension and exit script
// with an error dialog if not.
if ( ( bin > width ) || ( bin > height ) || ( bin > depth ) ) Throw( "Binning bigger than image dimension." )
// Create a Real 4-byte image with dimensions 1/n of the input image
// Note that the command expects integer values and will automatically truncate any
// number to interger. Hence width/bin will be the smallest suitable number
image BinImg := RealImage( "Binned ("+bin+"x)", 4 , width/bin, height/bin, depth/bin )
ShowImage( BinImg )
// Check if the binning will account for all pixels or warn the user otherwise.
if ( mod(width,bin) || mod(height,bin) || mod(depth,bin) )
OKDialog( "Warning: The chosen binning will miss out some intensity. (No integer factor of width, height or depth)")
// Loop over binning number in X&Y and sum dataset with accordingly extracted subdata.
for ( number ix = 0; ix<bin; ix++ )
{
for ( number iy = 0; iy<bin; iy++ )
{
for ( number iz = 0; iz<bin; iz++ )
{
// Slice3 addresses a 3D subdata set of img.
// The first number triplet specifies a sampling starting point as X/Y/Z
// the second number triplet specifies a sampling direction, sampling lenght and sampling step size for the first output dimension
// the third number triplet specifies a sampling direction, sampling lenght and sampling step size for the second output dimension
// the fourth number triplet specifies a sampling direction, sampling lenght and sampling step size for the third output dimension
BinImg += slice3( img, ix, iy, iz, 0, width/bin, bin, 1, height/bin, bin, 2, depth/bin, bin )
}
}
}