Home Image-Pro Automation (Macros, Apps, Reports)

running a macro on invisible images

Hello,

For some background,  I am relatively new writing macros and have been building all of my macros from recordings and then modifying as I learn more. I have found little documentation to learn from other than this community. If there is a good source to learn more about writing macros in Image Pro, I would like to know where it is!

I have a macro that flattens an image, counting objects, collect measurement data in data collector, generates a mask of the objects for an image, saves the mask, and then closes the mask and original image. I want to batch process this macro on 1200 images. When the batch processing starts the macro takes about 3 seconds to run, however, by the end of the batch the macro takes about 13 seconds to run on an image. The macro only works on visible images at the moment and I suspect that there could be some hidden images that it holds on too but I haven't figured out how to use the debugging macros to diagnose the problem. I have also read on here that processing on hidden images is faster but I am unclear about how to do that. Is there help document that walks through the process of setting up a macro to work on invisible images? I am hoping that working entirely on hidden images perhaps reduce the chance Image Pro is keeping other hidden images in memory. 

Thanks!

Answers

  • edited July 7
    Hi misbrit2128,

    The way you create the macros is correct, but there are many different nuances in optimizing the recorded macros and editing them. The documentation about this is included into the help, though it could be improved. You can also use the pre-installed macros (in the Scripts folder) and the apps, which could be downloaded form our app center, as examples. (look at https://www.mediacy.com/imagepro/learn/media and search for "macro")
     I would also recommend checking the Forum for specific programming issues, where we try to address all programmers' questions.

    Regarding running the batch processing on invisible images: we already have several topics on the forum that discuss it (search for "Invisible"), but here is the summary:

    You can modify the recorded macro to make it a LoopMacro for batch processing adding an argument (doc), which is the invisible document passed by batch macro:

    Public Sub LoopMacro(doc As Object)
    ...

    Set the Visible property to False for commands that create new images, in you case it's Mask:
            With Measure.Measurements.CreateCommands.Mask(Counting_3)
                .Visible=False 'don't show the mask
                .Run(doc1, image1)'image1 is the mask
            End With
    
    Some commands, such as QuickSaveSelectedForPublication, do not work with invisible images, so you have to use alternatives.

    I've modified your macro to work in with batch processing with "Display Documents" off:

        'process invisible image from the Batch
        'doc - loaded image document
        Public Sub LoopMacro(doc As Object)
            Dim Counting_3 = New SimpleScript
            Dim doc1 As IMcDocument, image0, image1 As McImage, doc2
    
            'use the document passed from the Batch macro
            doc1=doc
    
            With Process.Filter.EnhancementCommands.Flatten(Counting_3)
                .BrightOnDark = False
                .FeatureWidth = 20
                .Run(doc1, image0)
            End With
    
            With Measure.ThresholdToolCommands.Open(Counting_3)
                .FileName = ThisApplication.Path(mcPathType.mcptConfigurationFiles) & "Threshold, 6-21 vent-S7.rge"
                .FilterIndex = 1
                .Run(doc1)
            End With
    
            With Measure.MeasurementsCommands.ExecuteCount(Counting_3)
                .Run(doc1)
            End With
    
            With Measure.Data.CollectorCommands.Collect(Counting_3)
                .Run(doc1)
            End With
    
            With Measure.Measurements.CreateCommands.Mask(Counting_3)
                .Visible=False 'don't show the mask
                .Run(doc1, image1)'image1 is the mask
            End With
    
            'QuickSaveSelectedForPublication doesn't work with hidden images        
            'save mask directly
            Dim fName As String=doc1.FileName
            'use default QuickSaveForPublications path
            Dim outPath As String=ThisApplication.Settings("QuickSave", "Publication").Get("Path", ThisApplication.Path(mcPathType.mcptWritableImages))
            'generate mask name, save it to the same folder as the original image, but with _Mask suffix in JPG format
            Dim maskName As String=outPath & System.IO.Path.GetFileNameWithoutExtension(fName) & "_Mask" & ".jpg"
            image1.SaveAs(maskName)
            image1.Close'close the image
    End Sub
    
    I've also attached the complete project.

    I can recommend other ways to improve batch performance, such as Data Collection. In your macro you collect data from every image, I don't know what you collect, but if you just use the default Measurements table and your images may contain thousands of object, data collection will take longer time with every iteration, as multiple rows will get merged in Data Collector tables. In this case I recommend collecting only Statistical values of the measurements, use Measurement Stats table and collect Count, Mean (and other stats) of your values, so you get only 1 row of data per image. You may also create a separate "RunBefore" macro to setup and initialize data collector. (check https://www.mediacy.com/imagepro/learn/media and search for "Data Collector").

    Best regards,

    Yuri

Sign In or Register to comment.