Gatan | AMETEKSkip to Main Content
No options found

Scale 2D image

DigitalMicrograph Script

Scale a 2D image by a requested factor either using sampling or bi-linear interpolation (demonstrate warp command).

Preview

/////////////////////////////////////////////////////////////////////
// (c) Gatan Inc.
/////////////////////////////////////////////////////////////////////
// This script performs data scaling of a 2D image.
// Pixels are either sampled/repeated or bi-linearly interpolated.
//////////////////////////////////////////////////////////////////////
//	last modified 08-August-2014 BS

image img
if ( !GetFrontImage( img ) ) exit(0)				// Exit script if no front-image found
if ( 2 != ImageGetNumDimensions( img ) ) exit(0)	// Exit script if the image is not 2D

// Ask user to provide a scale factor.
// Refuse entries which are <=0.
// Exit script if user clicks the cancel button.
number scale=0
while( (0>=scale)  )
{
	if ( !GetNumber("Enter scale factor as positive number.", scale, scale) ) exit(0)
}

// Ask user if data should be interpolated
number interpolate = TwoButtonDialog( "Should the data be...", "interpolated", "sampled" )

// Create identical copy of input image, including tags, calibration etc...
image dst := ImageClone(img)
ShowImage(dst)

// Resize image to given dimension.
// This command keeps the field of view (FOV) the same, i.e. adjusted spatial calibrations
// while it adjusts the pixel-dimensions of the image.
// However, the actual data is set to zero-value.
number width,height
GetSize( img, width, height )
ImageResize( dst, 2, width*scale, height*scale )

// Now either use sampling (i.e. nearest neighbour interpolation)
// or bilinear interpolation
// to fill the data container

if ( interpolate )
{
	SetName( dst, GetName(dst) + " (bilinear)")
	dst = warp( img, icol/scale, irow/scale )
	// Warp interpolates the value at img[X,Y] based on the 4 nearest integer coordinates.
	// Hence for X = icol/scale and Y = irow/scale it will perform bilinear interpolation
	// if X/Y is not an integer value. icol/irow run over the dimensionality of the image 'dst'.
}
else
{
	SetName( dst, GetName(dst) + " (sampling)")
	dst = img[ icol/scale, irow/scale ]
	// Direct coordinate addressing at img[X,Y] will truncate the coordinates X/Y to integer
	// values.
	// Hence for X = icol/scale and Y = irow/scale data is simply sampled/repeated at truncated
	// value.
	// For a "nearest neighbor" sampling one could simply use
	// dst = img[ round(icol/scale), round(irow/scale) ]
	// to replaces truncation by value-rounding.
}