Code: Select all
#######################################################
## M E A S U R E M E N T A N D A N A L Y S I S
##
## Macro name: Save object stage positions in text file
## Required files: Move measured object to image center.czias
## Required hardware: AxioCam camera
##
## SNAP IMAGE, ANALYSE IMAGE, GET VALUES OF DATA LIST,
## CALCULATE OBJECT STAGE POSITION, SAVE POSITIONS IN TEXT FILE
##
#######################################################
##
##
## Remove all open images
Zen.Application.Documents.RemoveAll()
##
##
## ACQUISITION
## Show live image
Zen.Acquisition.StartLive()
## Set exposure time (AutoExposure)
Zen.Acquisition.AutoExposure()
Zen.Application.Pause("Search field of interest and focus image!")
## Snap image
image = Zen.Acquisition.AcquireImage()
Zen.Application.Documents.Add(image)
## Close live image
Zen.Acquisition.StopLive()
##
## ANALYSIS
## Load analysis setting
ias = ZenImageAnalysisSetting()
ias.Load('Move measured object to image center')
## Analyze the image
Zen.Application.Pause("Do setup for segmentation")
Zen.Analyzing.AnalyzeInteractive(image,ias)
## Create data list with results for all regions
regsTable = Zen.Analyzing.CreateRegionsTable(image)
#Zen.Application.Documents.Add(regsTable)
## Create data list with results for each region
regTable = Zen.Analyzing.CreateRegionTable(image)
#Zen.Application.Documents.Add(regTable)
##
## CREATE TEXT FILE
## Get stage posXY and scaleXY (regions table)
stagePosX = regsTable.GetValue(0,0)
stagePosY = regsTable.GetValue(0,1)
scaleX = regsTable.GetValue(0,2)
scaleY = regsTable.GetValue(0,3)
## Get of image centerXY (meta data)
width2 = int(image.Metadata.Width)/2
height2 = int(image.Metadata.Height)/2
## Get boundbox center positions of each object (region table)
## Calculate stage pos XY of each object
## Write postions to text file
### Note! Stage directions are not taken into account!
for row in xrange(regTable.RowCount):
## Calculate object stage coordinates
id = regTable.GetValue(row,0)
left = regTable.GetValue(row,1)
top = regTable.GetValue(row,2)
## Move object into image center
objectStagePosX = stagePosX + (left - (width2 * scaleX))
objectStagePosY = stagePosY + (top - (height2 * scaleY))
## CREATE TEXT FILE
## Write header
if row == 0:
wFile = open('C:\\OAD\\Output\\ObjectStageCoordinates.txt','w')
wFile.write ('XY Stage coordinates of analyzed objects\n')
wFile.write('\n')
## Write object XY stage coordinate values
wFile.write ('.\t {:6.1f}'.format(float(objectStagePosX)))
wFile.write (', {:6.1f}'.format(float(objectStagePosY)))
wFile.write ('\n')
##
## Close text file
wFile.close()
## Close image
image.Close()
## Show where coordinate list is saved
Zen.Application.Pause("ObjectStageCoordinates.txt is saved in folder: C:\\OAD\\Output")
##
#######################################################