The command CountImages() has unexpected behaviors. I found it can return a number > 0 even if there is no image in sight anywhere. I have also noticed that it can count all displayed images twice. Sometimes, it does what it seems to suggest at first glance: count all the (displayed) images. Greatly appreciate any explanation. Thanks!
1 Answers
CountImages()
command counts all image
objects currently held in memory except so called utility images. This includes all shown images as well as images from other work spaces, images currently not shown (f.e. not shown images of a compound document, or those hidden by the according command).
The command CountAllImages()
includes utility images as well.
These are old commands from the early days of DigitalMicrograph when things were a lot simpler. I would not recommend using it in scripts anymore. However, an example of use would be
ClearResults()
result("\n Count Images:"+CountImages())
result("\n Count All Images:"+CountAllImages())
image img := FirstImage()
number count = 0
while(img.ImageIsValid())
{
count++
result("\n Image #"+Format(count,"%03d")+" = ["+img.ImageGetLabel()+"]:"+img.ImageGetName())
try{ img := NextImage(img); }
catch{ img := NULL; break;}
}
Result("\n DONE")
A better way to count things on a workspace is to count ImageDocuments
which are the containers of one or more images and access data that way, but this might be a separate question.
In any case, I'm attaching a script that counts and lists various things for clarity:
number numImages = CountImages() // Shown or not shown, all workspaces. Excludes utility imgages
number numImages_all = CountAllImages() // Shown or not shown, all workspaces. Includes utility imgages
number numIDocs = CountImageDocuments() // Shown on current workspaces.
number numIDocs_all = CountImageDocumentsOfAllWorkSpaces() // Shown or not shown, any workspace.
ClearResults()
Result("\n Count Images : " + numImages )
Result("\n Count ALL Images: " + numImages_all )
Result("\n Count Image Documents: " + numIDocs )
Result("\n Count Image Documents (all Works spaces): " + numIDocs_all )
// Iterate over all shown Images on current workspace
Result("\n\n*****\nImage Documents on current Workspace:")
for( number i = 0; i<numIDocs; i++ )
{
ImageDocument doc = GetImageDocument(i)
Result("\n\t Doc ID = " + doc.ImageDocumentGetID() + " '" + doc.ImageDocumentGetName() + "'")
number numImg = doc.ImageDocumentCountImages()
for( number j = 0; j<numImg; j++ )
{
image img := doc.ImageDocumentGetImage(j)
Result("\n\t\t Img ID = " + img.ImageGetID() + " ["+img.ImageGetLabel()+"]:'" + img.ImageGetName() + "'")
}
}
// Iterate over all Images via ImageDocuments
Result("\n\n*****\nImage Documents (any Workspace):")
for( number i = 0; i<numIDocs_all; i++ )
{
ImageDocument doc = GetImageDocumentOfAllWorkspaces(i)
Result("\n\t Doc ID = " + doc.ImageDocumentGetID() + " '" + doc.ImageDocumentGetName() + "'")
Result("\t Workspace ID = "+doc.ImageDocumentGetWorkspace())
number numImg = doc.ImageDocumentCountImages()
for( number j = 0; j<numImg; j++ )
{
image img := doc.ImageDocumentGetImage(j)
Result("\n\t\t Img ID = " + img.ImageGetID() + " ["+img.ImageGetLabel()+"]:'" + img.ImageGetName() + "'")
}
}
// Iterate over all Images, excluding not shown utility images
Result("\n\n*****\nAll Images (except utility):")
for( number i = 0; i<numImages; i++ )
{
image img := FindImageByIndex(i)
if (img.ImageIsValid())
Result("\n\t\t Img ID = " + img.ImageGetID() + " ["+img.ImageGetLabel()+"]:'" + img.ImageGetName() + "'")
}
// Iterate over all Images, including not shown utility images
Result("\n\n*****\nAbsolutely All Images:")
for( number i = 0; i<numImages_all; i++ )
{
image img := FindImageByIndex(i)
if (img.ImageIsValid())
Result("\n\t\t Img ID = " + img.ImageGetID() + " ["+img.ImageGetLabel()+"]:'" + img.ImageGetName() + "'")
}

- 6,331
- 1
- 21
- 35