Scripting Resources for DigitalMicrograph™

banner

Dave Mitchell's DigitalMicrograph™ Scripting Website

Home | Scripts | Examples | Functions | Recent Updates | Tutorials | Resources | Publications | Consulting | Projects | Contact & Bio |PyJEM| Search

 

Example: Warp and Unwarp Images
Function
Demonstrates the use of the warp function to unfold an image about its centre, and how to reverse the process.
Version
version:20070219, v1.0
Author
D. R. G. Mitchell and Edgar Voelkl
Acknowledgements
-
Comments
Warping effectively unwraps an image about its centre turning concentric features (such as diffraction rings) into vertical stripes in a rectangular image. This is useful for making measurements on annular regions of images, for which no DM function exists. The unwarp function reverses this.
System Requirements
Should be compatible with all recent versions of DigitalMicrograph.
Known Issues
The unwarp function acting on a warped image will not restore information in the corners of the image. The warping process only preserves information out to the mimum radius to the edge of the image. The variable 'samples' in the code affects the error introduced by warping (interpolation effect). I have found a sample size of 2048 appears to result in a minimum error (of a few percent).
Supported
Yes
Included Files
Main script file.
Source Code

// example functions which show how to warp an image to unfold it about a central point

// - the geometric centre of the image, and then rewarp it back into the original form.

// Warping can be useful for carrying out fast radial averaging for SADPs/FFTs etc. Digital Micrograph does not

// have functions for directly extracting/measuring annular regions of images. By warping a circularly symmetric

// image into a rectangular image - the usual rectangular region of interest tools can be applied to calculate/set values

 

// The reverse warp recreates the image, although there is distortion near the edge and of course the corners are lost.

// Thanks to Edgar Voelkl for providing the unwarp function

 

// D. R. G. Mitchell, adminnospam@dmscripting.com (remove the nospam to make this email address work

 

// version:20070219, v1.0, February 2007

 

image warpimage(image front)

{

number xsize, ysize, centrex, centrey

GetSize( front, xsize, ysize )

number samples=ysize

number k=2*Pi()/samples

 

centrex=xsize/2

centrey=ysize/2

 

number minor = min( xsize, ysize )

image linearimg := RealImage( "", 4, minor/2, samples )

linearimg = warp( front, icol * sin(irow * k) + centrex, icol * cos(irow * k) + centrey )

 

return linearimg

}

 

 

 

image unwarpimage(image front)

{

// unwarp function kindly provided by Edgar Voelkl

number xsize, ysize, centrex, centrey

GetSize( front, xsize, ysize );

number k=2*Pi()/ysize;

 

centrex=xsize;

centrey=ysize/2;

 

image unwarpedimg := realImage( "", 4, xsize*2, ysize );

unwarpedimg = warp( front, iradius, 1/k * (Pi() - atan2((icol-centrey),(irow-centrex))));

 

// note the warp function above flips the image horizontally so the following restores it

 

image flipunwarpedimg=imageclone(unwarpedimg)

flipunwarpedimg=0

flipunwarpedimg[icol,ysize-irow]=unwarpedimg

return flipunwarpedimg

}

 

image origimg:=getfrontimage()

setwindowposition(origimg, 142,24)

 

image warpimg=warpimage(origimg)

showimage(warpimg)

setwindowposition(warpimg, 172,54)

setname(warpimg, "Warped Image")

 

image unwarpimg=unwarpimage(warpimg)

showimage(unwarpimg)

setwindowposition(unwarpimg, 202,84)

setname(unwarpimg, "Unwarped Image")