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

 

Add and Position ROI
Function
Adds and positions a region of interest to theforemost image.
Version
version:20030722, v1.0
Author
D. R. G. Mitchell
Acknowledgements
Comments
System Requirements
Should be compatible with all recent versions of DigitalMicrograph.
Known Issues
Requires an image to be foremost when called.
Supported
Yes
Included Files
Main script file.
Source Code

// Script to add a rectangular region of interest (ROI) to an image.

// The ROI dimensions are requested and then the ROI is positioned

// either with reference to the top left position of the ROI or its centre.

// You can use this to place an ROI of specific size on a particular location

// version:20030722

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

// version 1.0

 

 

image img:=getfrontimage()

string imgname=getname(img)

number xdim, ydim, top, left, bottom, right, xcentre, ycentre

 

if(!twobuttondialog("Create a rectangular region of interest (ROI) on image : "+imgname+"?", "Yes", "Cancel")) exit(0)

 

imagedisplay imgdisp = img.ImageGetImageDisplay(0)

 

if (imgdisp.ImageDisplayCountROIs() > 0)

{

beep()

if(!twobuttondialog("An ROI already exists on this image. Continue or cancel?", "Continue", "Cancel")) exit(0)

 

}

 

getnumber("Enter x Width of the ROI in pixels",0,xdim)

getnumber("Enter y height of the ROI in pixels",0,ydim)

ROI roi_1 = NewROI()

 

if(twobuttondialog("Do you want to position the ROI by its top/left or by its centre?", "Top/Left", "Centre"))

{

 

// This positions the ROI by on the basis of its top left position

getnumber("Enter Top position of ROI",0,top)

getnumber("Enter Left position of ROI",0,left)

bottom=top+ydim

right=left+xdim

xcentre=left+((right-left)/2)

ycentre=top+((bottom-top)/2)

ROISetRectangle( roi_1, top, left, bottom, right )

imgdisp.ImageDisplayAddROI( roi_1)

imagedisplaysetroiselected(imgdisp, roi_1,1)

}

 

else

 

{

 

// This positions the ROI by on the basis of its centre position

getnumber("Enter Centre x position of ROI",0,xcentre)

getnumber("Enter Centre y position of ROI",0,ycentre)

top=ycentre-(ydim/2)

bottom=top+ydim

left=xcentre-(xdim/2)

right=left+xdim

ROISetRectangle( roi_1, top, left, bottom, right )

imgdisp.ImageDisplayAddROI( roi_1)

imagedisplaysetroiselected(imgdisp, roi_1,1)

}

 

result("ROI created on image: "+imgname+"\n ROI Width = "+xdim+" ROI height = "+ydim+"\n")

result("Top = "+top+" Left = "+left+" Bottom = "+bottom+" Right = "+right+"\nCentre x = "+xcentre+" Centre y = "+ycentre+"\n")