Gatan | AMETEKSkip to Main Content
No options found

Crop image to selection

DigitalMicrograph Script

Crop 2D or 3D image based on selection and displayed slices.(demonstrate data extraction).

Preview

/////////////////////////////////////////////////////////////////////
// (c) Gatan Inc.
/////////////////////////////////////////////////////////////////////
// The script creates a cropped image based on the ROI selection of the image
// If the image is a 3D data block, the behaviour depends on the displayed
// slices. If as single slice is displayed, the image is cropped to the
// XY area over all planes, and the according 3D stack is returned.
// If the slice-tool is used to display the sum of multiple slices, then
// the displayed slice-range is used as a cropping limit in the Z-direction
// as well.
// Pressing the ALT key when running the script displays user-dialogs
// to specify the limits numerically.
// Pressing the SHIFT key will replace the image by the cropped version.
/////////////////////////////////////////////////////////////////////
//	last modified 08-July-2014 BS

void CropImageToSelection( Image img, Number userOptions, Number Replace )
{
	// Perform input data validity checks
	if ( !img.ImageIsValid() )
		Throw( "Invalid image in CropImageToSelection()" )
	number nDim = img.ImageGetNumDimensions() 
	if ( 3 < nDim )
		Throw( "Dimensionality above 3D is not supported by CropImageToSelection()" )
	
	// Read 2D ROI selection and 3D slice-selection
	number sx, sy, t, l, b, r
	number sz=1, first, last
	img.GetSize(sx,sy)
	img.GetSelection(t,l,b,r)
	if ( 3 == nDIm )
	{
		sz = img.ImageGetDimensionSize(2)
		img.ImageGetImageDisplay(0).ImageDisplayGetDisplayedLayers(first,last)
		if ( first == last )
		{
			first = 0
			last = sz-1
		}	
	}
	
	// Display user prompts
	if ( userOptions )
	{
	
		if ( !GetNumber( "Enter cropping coordinates.\nX coordinate of top-left corner limit:",l,l) )
			return
		if ( !GetNumber( "Enter cropping coordinates.\nY coordinate of top-left corner limit:",t,t) )
			return
		if ( !GetNumber( "Enter cropping coordinates.\nX coordinate of bottom-right corner limit:",r,r) )
			return
		if ( !GetNumber( "Enter cropping coordinates.\nY coordinate of bottom-right corner limit:",b,b) )
			return
		if ( 3 == nDIm )
		{
			if ( !GetNumber( "Enter cropping coordinates.\nFirst slice included in sub-volume:",first,first) )
				return
			if ( !GetNumber( "Enter cropping coordinates.\nLast slice included in sub-volume:",last,last) )
				return			
		}
	}
	
	// Clip to sensible input values and perform validity check
	last++					// displayed slices use slice index, not limit
	l = max(0,min(l,sx))
	r = min(sx,max(r,0))
	t = max(0,min(t,sy))
	b = min(sy,max(b,0))
	first = max(0,min(first,sz))
	last  = min(sz,max(last ,0))
	if ( ! ( (l-r)&&(b-t)&&(first-last) ) )
		Throw( "Invalid cropping size in CropImageToSelection().\nCan not crop ["+l+","+t+","+first+";"+r+","+b+","+last+"]." )
		
	// Perform 2D or 3D date extraction
	Image cut
	if ( (3 == nDim) && 1<(last-first) )
		cut := img.Slice3(l,t,first,0,(r-l),1,1,(b-t),1,2,(last-first),1).ImageClone()
	else
		cut := img.Slice2(l,t,first,0,(r-l),1,1,(b-t),1).ImageClone()
	
	// Display cropped data & remove source data (option)
	if (Replace)
	{
		cut.SetName( img.GetName() )
		img.DeleteImage()
	}
	else
		cut.SetName( img.GetName()+"_Cut") 
	Showimage(Cut)
}

// Main script calling the function and applying it to the front most open image
GetFrontImage().CropImageToSelection(OptionDown(),ShiftDown())