Scripting Resources for DigitalMicrograph™

banner

Dave Mitchell's DigitalMicrograph™ Scripting Website

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

 

Example: Add a Listener to an ROI on a Spectrum
Function

An example script which shows how to attach a listener to a region of interest (ROI) on a 1D image (ie a spectrum or intensity profile). When the ROI is moved, data is obtained from it dynamically, and the position reported in the Results Window. To make this work with an ROI on a 2D image, you need to uncomment the relevant 2D code in the ROIChanged function, and comment out the 1D code.

Version
version:20111229, v1.0
Author
D. R. G. Mitchell
Acknowledgements
-
Comments
Have a spectrum or intensity profile front-most. Draw a ROI on it by clicking and dragging on the spectrum. Then run this script. As the ROI is moved, the left and right positions will be reported in the Results window.
System Requirements
Should be compatible with all recent versions of DigitalMicrograph, but only tested on GMS 1.84 (XP).
Known Issues
As written it will not work with an ROI on a 2D image. However, code is provided - just comment out the 1D code and uncomment (remove the //) the 2D code.
Supported
Yes
Included Files
Main script file.
Source Code

// An example script which shows how to attach a listener to a region of interest (ROI).

// on a 1D image (ie a spectrum or intensity profile). When the ROI is moved, data is

// obtained from it dynamically, and the position reported in the Results Window.

// To make this work with an ROI on a 2D image, you need to uncomment the relevant

// 2D code in the ROIChanged function, and comment out the 1D code.

 

// To use this script have a 1D image (spectrum or intensity profile) front-most

// with an ROI drawn on it. Run this script, then move the ROI whilst monitoring the

// Results window.

 

// D. R. G. Mitchell, adminnospam@dmscripting.com (remove the nospam)

// version:20111229, v1.0, December 2011

 

// Thanks for Vincent Hou for pointing out the DisconnectObject() function.

 

 

// declare the listener object

 

object roilistener

 

 

// This class object contains the functions to set and monitor the ROI

class moveROI : object

{

// Some variables

ROI theroi

number left, right

// A function which returns the ROI

ROI GetWin(object self)

{

return theroi

}

 

// ROIChanged responds to shifts in the ROI on the image/spectrum

// In this case the ROI is passed into the function and the left and right hand

// channel positions are reported in the Results window. This is fine for a 1D

// spectrum. However, if the ROI is in a 2D image then the four vertices of the ROI

// would probably be required (code has been commented out below)

 

void ROIChanged(object self, roi theroi)

{

// If moving the ROI necessitates a lot of processing, the response can become

// very sluggish and clunky. It is useful to include this feature such that

// if the CONTROL key is held down while the ROI is moved, updating will be temporarily

// suspended. This allows large changes to me made quickly. However, since the listener

// only responds to change, it is essential that the ALT key is released before the ROI

// is moved to its final position.

if(controldown())

{

return

}

 

 

// This section works for an ROI on a 1D plot, only the left and right positions

// are needed. If you want to source info from an ROI on a 2D image, comment

// out the three lines below and uncomment equivalent code in this sectio below.

number left, right

theroi.roigetrange(left, right)

result("\nleft="+left+" right="+right)

// For a 2D image, the four vertices of the ROI are required.

// Uncomment the code below (remove the //) to make it work. The code below

// will not work for a ROI on a 1D intensity plot. Comment out the above three lines of code.

//number top, bottom, left, right

//theroi.roigetrectangle(top, left, bottom, right)

//result("\ntop="+top+" left="+left+" bottom="+bottom+" right="+right)

}

 

 

// This function sources the ROI on the passed in image

object init(object self, image spectrum)

{

theroi = spectrum.ImageGetImageDisplay(0).ImageDisplayGetROI(0)

return self

}

 

}

 

 

// main script

 

// Check to make sure that an image is displayed

 

number nodocs=countdocumentwindowsoftype(5)

if(nodocs<1)

{

showalert("Ensure a spectrum/profile with a Region of Interest on it is shown.",2)

exit(0)

}

 

 

// source the front-most image

 

image front:=getfrontimage()

 

 

// Check to make sure there is an ROI on the selected image

 

imagedisplay imgdisp=front.imagegetimagedisplay(0)

number norois=imgdisp.imagedisplaycountrois()

if(norois<1)

{

showalert("Please ensure a ROI is present on the front-most image.",2)

exit(0)

}

 

 

// Allocate the listener object to the front image

 

ROIlistener = alloc(MoveROI).Init(front)

 

 

// Connect the listener to the ROI

 

ConnectObject(ROIlistener.GetWin().ROIGetID(),"changed","selection in image",ROIlistener,"ROIChanged")

 

 

// This listener will be disconnected if the image or the ROI is deleted.

// You can also disconnect the listener with the following code

DisconnectObject(ROIlistener.GetWin().ROIGetID(),"changed","selection in image")

showalert("Listener has been disconnected.",2)