Macro to save snapshot/ outline files based image name after performing particle measurements
Hello Image-Pro Plus programmers: I just recently started to use Image-Pro Plus (version 7) to perform particle analysis on images captured using light microscopy. My goal is to write a macro that reads images from a folder (perhaps up to 100+ images/ folder), perform pre-filtering (e.g. HiPass), threshold, measure 10+ parameters for each particle in the image, save a snapshot of the particle outlines/ numbers, save the outlines file (.scl), record the particle measurements in the Data Recorder, and finally export the measurement data to a text file for analysis in R. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
I have used the macro recorder, Data Collector and the Batch Process plug-in to get most of the way there (see code below). The issue that I have is how to write a for loop (or other coding method) that saves the snapshot image and outlines file as the image name including the addition of “_outlines” to the name: e.g. Image1_outlines.scl and Image1_outlines.tif. Note the image names are more complex; I simplified them for the example.
Note the lines for saving the image/ outline files are hard-coded to save only a single name, not one that assigns a name that is based on the actual image/ file name.
ret = IpSCalSelect("10x_0.3NA_LU_
ret = IpFltHiPass(3, 10, 1) ‘ applies an initial hi-pass filter to image
ret = IpBlbCount() ‘performs particle size/ count measurements
ret = IpBlbUpdate(0) ‘
ret = IpDcSet(DC_AUTO, 0) ‘Data Collector
ret = IpDcUpdate(DC_FETCH) ’collects data using Data Collector
ret =
IpBlbSaveOutline("C:\Documents And Settings\jhietpas\Desktop\
ret = IpSnap() ‘snaps an image of the particle outlines/ numbers
ret = IpWsSaveAs("C:\Documents And
Settings\jhietpas\Desktop \Images\Processed\
ret = IpDocClose() ‘ closes the image after analysis
Thanks very much for any information/ help you can provide,
-Jack
Comments
-
Hi Jack,You can use batch processing macro to do your loop. Check this link http://forums.mediacy.com/discussion/comment/304/#Comment_304The ProcessDirectory() macro will load images one by one, you will have file names from there (fName). You just append the extension you need (".scl") to save your output files.Yuri0
-
What you are asking for is essentially how to work with the file name strings to generate the names you wish. Attached is some sample code for string handling.Output:fileName: C:\IPWIN70\Images\DemoBlue.TIFextension: TIFbaseName: C:\IPWIN70\Images\DemoBluedirName: C:\IPWIN70\Images\name_Script: C:\IPWIN70\Images\DemoBlue_outlines.sclname_Image: C:\IPWIN70\Images\DemoBlue_outlines.TIF0
-
-
Hi Kevin- Thank you very much for your help, I really appreciate it! Sorry for the delay in getting back to you. I have incorporated your code into mine (below). It is working very well, the image and the outline files are being properly labeled and saved! The only question that I have is that I have to select each image individually from the directory. Is there a way to only select one of the images and have the code use all the images in the selected directory? I have been trying to figure this out myself using parts of the Batch_Process script (Yuri's suggestion)... but failing miserably. Do you have any suggestions? Thanks again for your help.
-jh
'Dim Firstfile As String
'Dim Formattype As String
'Firstfile = "C:\Documents and Settings\jhietpas\Desktop\Images\Image1.TIF"
'Formattype = "TIF"
'ret = IpWsLoad(Firstfile, Formattype)
Dim fileName As String, dirName As String
Dim extension As String, baseName As String
Dim name_Script As String, name_Image As String
Dim delim As Integer
fileName = GetFilePath(, , , , 0)
If fileName = "" Then Exit Sub
' Chop up the file name using searches for "." and "\" delimiters
delim = InStrRev(fileName, ".")
extension = Right(fileName, Len(fileName)- delim)
baseName = Left(fileName, delim - 1)
delim = InStrRev(fileName, "\")
dirName = Left(fileName, delim)
' Build new file names for later use
name_Script = baseName + "_outlines" + "." + "scl"
name_Image = baseName + "_outlines" + "." + extension
' Output results
'Debug.Print "fileName: "; vbTab; fileName
'Debug.Print "extension: "; vbTab; extension
' Debug.Print "baseName: "; vbTab; baseName
' Debug.Print "dirName: "; vbTab; dirName
' Debug.Print "name_Script: "; vbTab; name_Script
' Debug.Print "name_Image: "; vbTab; name_Image
' Debug.Print ""
'ret = IpSCalSelect("4x_0.2_NA_Nikon")
'ret = IpSCalSelect("10x_0.45_NA_APO_Nikon")
ret = IpSCalSelect("20x_0.75_NA_APO_Nikon")
ret = IpFltHiPass(3, 10, 1)
ret = IpBlbSetAttr(BLOB_CLEANBORDER,1)
ret = IpBlbCount()
ret = IpBlbUpdate(0)
ret = IpDcShow(3)
ret = IpDcSet(DC_AUTO, 0)
ret = IpDcUpdate(DC_FETCH)
ret = IpBlbSaveOutline(name_Script)
ret = IpSnap()
ret = IpWsSaveAs(name_Image,"TIF")
ret = IpDocClose()0 -
The code in Batch_Process.ipm, specifically Sub ProcessDirectory( ), implements just such a loop. Calling Dir(directory + "*.*) returns the first matching file, calling Dir( ) again gives the next in alphabetical order. In that macro the following steps are performed:
- Initialize to a reasonable starting path
- Select a file in the desired directory
- Extract the directory path from the file
- Search for files in that directory, looping through all
- For each of those files call a processing macro
- End when no more files can be found (returned file name is "")
You can use the ProcessDirectory( ) routine as a base for your work - copy it into your script file, put your setup code into one macro, your calibration/filtering/counting into another, and insert calls to those in the lines of ProcessDirectory( ) marked with '***'. You will be replacing the example DoStuff call with your own. That's how that example code is intended to be used - you need no other changes to do looping over all files in a directory.As a programming note, I strongly suggest that you break your code up into separate routines. One for overall loop control, one for setup (printing your column labels, for example), one for processing that's called for each image, and if desired one for cleanup/reporting. Smaller pieces are easier to understand, and to swap in/out as needed. Add comments (lots!) describing what you are doing with the code, plus empty lines and indentation to show the workflow.A single unindented block of code rapidly becomes unreadable, and impossible to maintain or reuse. And you may end up just rewriting it for the next experiment, discarding all the effort, because you cannot understand what it's doing.0
Categories
- All Categories
- 961 Image-Pro v9 and higher
- 9 Image-Pro FAQs
- 18 Image-Pro Download & Install
- 448 Image-Pro General Discussions
- 486 Image-Pro Automation (Macros, Apps, Reports)
- 20 AutoQuant Deconvolution
- 2 AutoQuant Download & Install
- 18 AutoQuant General Discussions
- 195 Image-Pro Plus v7 and lower
- 3 Image-Pro Plus Download & Install
- 106 Image-Pro Plus General Discussions
- 86 Image-Pro Plus Automation with Macros
- 19 Legacy Products
- 16 Image-Pro Premier 3D General Discussions
- 26 Image-Pro Insight General Discussions