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

Getting file location of user-selected file

Hello everyone,

In Image-Pro10.0.4 Build 6912, I am attempting to load a file directly to the "Global Recipe" as in the following post on this forum to automate smart segmentation. 

https://forums.mediacy.com/discussion/comment/444#Comment_444

However in my case there is a need to change the input .isg file due to differences in the image sets examined (different lighting conditions, coloring, for instance). I could have users generate their own .isg file in the correct folder and rename it so the macro can find it, but I would prefer if I could use a dialog box to locate the file and get the file address somehow.

How could I go about doing this?

Best Answer

  • Answer ✓
    Hi cjhung,

    Yes, applying a recipe or measurement options require an image, so it should be done in LoopOn macro, but the selectiton of the file name can be done only once in RunBefore macro, like this:

        Public OptionsFileName As String ' global variable that contains file name and that is used by RunBefore and LoopOn macros
        Public Sub RunBefore()
            With Measure.Measurements.OptionsCommands.Open(Nothing)
                .FileName = ""
                .FilterIndex = 1
                .Run(Nothing)
                OptionsFileName=.FileName'read selected file name
                Debug.Print(OptionsFileName)'just for checking
            End With
        End Sub
    
    
        Public Sub LoopOn()
            With Measure.Measurements.OptionsCommands.Open(Nothing)
                .FileName = OptionsFileName'use global file name set in RunBefore
                .FilterIndex = 1
                .Run(ThisApplication.ActiveImage)
            End With
            'add other steps to execute Count using the options loaded from OptionsFileName
        End Sub

    Yuri

Answers

  • 2024-04-30-171341

    CJHUNG --

    I believe a solution to your problem would be something like the following . . . 

    Let's say that you have a COOKIE FOLDER full of COOKIE IMAGES

    -- You open one of the COOKIE IMAGES from the COOKIE FOLDER and create a SEGMENTATION FILE named

        COOKIE.isg

    as shown here



    and the ISG FILE is stored in the COOKIE FOLDER as shown here.


    -- Then you add code similar to this to your APP so that when the APP needs an ISG FILE for a COOKIE IMAGE, it uses the ISG FILE that is in the COOKIE FOLDER.

        Public Function NewMacro() As SimpleScript
            NewMacro = New SimpleScript
            Dim image1
    
            With Application.DocumentCommands.ActiveImage(NewMacro)
                .Run(image1)
            End With
    
            Dim MyString1 As String = _
                ThisApplication.ActiveDocument.FileName
    
            Debug.Print _
                MyString1
    
            Dim MyString2 As String = _
                Left(ThisApplication.ActiveDocument.FileName,InStrRev(MyString1,"\"))
    
            Debug.Print _
                MyString2
    
            Dim MyString3 As String = _
                MyString2 & "COOKIE.isg"
    
            Debug.Print _
                MyString3
    
            With Measure.SmartSegmentation.RecipeCommands.Open(NewMacro)
                '.FileName = "C:\Users\mbatc\Desktop\New folder\COOKIE.isg"
                .FileName = MyString3
                .FilterIndex = 1
                .Settings = Nothing
                .FilterInput = True
                .Run(image1)
            End With

    I hope this information is helpful.

    -- Matt




  • Hi cjhun,

    If you are trying to apply a recipe from file to the active image, you can use RecoipeCommads.Open with empty FileName property, it will invoke File Open dialog: 
    Public Sub LoadRecipeWithPrompt
        With Measure.SmartSegmentation.RecipeCommands.Open(Nothing)
            .FileName = "" 'leave File Name empty To Invoke File Open Dialog
            .FilterIndex = 1
            .FilterInput = True
            .Run(ThisApplication.ActiveImage)
        End With
    End Sub
    
    
    Also, instead of ISG I would recommend using IQO files (Measurement Options) as it includes the recipe and all other segmentation properties, so you should resave your active recipes to IQO files (Measurements - Save options button) and then use this command to open IQO file with a prompt:
        Public Function LoadSegmentationOptions() As SimpleScript
            LoadSegmentationOptions = New SimpleScript
            Dim doc1
    
            With Application.DocumentCommands.Active(LoadSegmentationOptions)
                .Run(doc1)
            End With
    
            With Measure.Measurements.OptionsCommands.Open(LoadSegmentationOptions)
                .FileName = ""
                .FilterIndex = 1
                .Run(doc1)
            End With
        End Function
    

    Regards,

    Yuri

  • Thank you Matt and Yuri for your responses. Both provide insight, but I am still stuck. I believe because both solutions assume an image is open.

    In the "Run Before" step in batch processing, I am trying to load the ISG to the "Global Recipe" (I didn't know about IQO, will have to try it out) before having an image open like in the referenced forum post. I then call on Measure.SmartSegmentation.RecipeCommands.ApplyGlobal() in the "Loop On" macro. 

    In "Run Before", I am currently trying to use Measure.SmartSegmentation.RecipeCommands.Open() and Measure.SmartSegmentation.RecipeCommands.SetGlobal() to do this, but it doesn't seem to run properly without an image open. 

    To get around this, I tried to use the following as it works without an open image.
    With Automate.ScriptingCommands.CodeCommand(Script)
                If .Run() Then
                    ' load recipe to Global
                    Dim globalRecipe As MediaCy.Addins.LearningSegmentation.SegmRecipe=MediaCy.Addins.LearningSegmentation.Globals.m_SegmRecipe
                    Dim FileName As String=ThisApplication.Path(mcPathType.mcptConfigurationFiles) & "ISG.isg"
                    globalRecipe.Load(FileName,MediaCy.Addins.LearningSegmentation.McLearningSegmentation.ThisAddin.LS.Options)
                End If
            End With
    But I'm running into issues with the FileName since that may change. My thought is if I can have the user identify an ISG file, I could then feed the address into the above script.
  • 2024-05-01-142236

    cjhung --

    If you put the CODE that I provided into your LOOP ON MACRO in an appropriate location, then there should an IMAGE open for the CODE to use to get a PATH to the ISG FILE.

    You can resolve the FLOATING FILE NAME ISSUE with DIR COMMAND something like NEWMACRO2 CODE below.

    When RUN on the COOKIE FOLDER, the OUTPUT to the DEBUG WINDOW is

    ---------------------------
    O:\ALCES\Media Cy Forum Misc\2024-04-30-172126 -- ISG FILE NAME QUESTION\AA_Crop.tif
    O:\ALCES\Media Cy Forum Misc\2024-04-30-172126 -- ISG FILE NAME QUESTION\
    COOKIE.isg
    O:\ALCES\Media Cy Forum Misc\2024-04-30-172126 -- ISG FILE NAME QUESTION\COOKIE.isg
    ---------------------------

    It is important to note that if there is more than ONE ISG FILE in the FOLDER, the DIR COMMAND will find the FIRST ISG FILE in the FOLDER.  I have tried to highlight that in the REMARKS that I added to this CODE.

    I hope this information is helpful

    -- Matt

        Public Function NewMacro2() As SimpleScript
            
            'CLEAR THE DEBUG WINDOW
            DEBUG.Clear 
    
            'ADMINISTRATIVE STUF
            NewMacro2 = New SimpleScript
            Dim image1
    
            'CONNECT WITH THE CURRENT IMAGE
            With Application.DocumentCommands.ActiveImage(NewMacro2)
                .Run(image1)
            End With
    
            'DETERMINE THE FULL FILE NAME OF THE CURRENT IMAGE
            Dim MyString1 As String = _
                ThisApplication.ActiveDocument.FileName
    
            'PRINT STRING TO DEBUG WINDOW
            Debug.Print _
                MyString1
    
            'DETERMINE THE PATH FOR THE CURRENT IMAGE
            Dim MyString2 As String = _
                Left(ThisApplication.ActiveDocument.FileName,InStrRev(MyString1,"\"))
    
            'PRINT STRING TO DEBUG WINDOW
            Debug.Print _
                MyString2
    
            'FIND THE NAME OF THE (FIRST) ISG FILE IN THE SAME FOLDER AS THE CURRENT IMAGE
            Dim MyString4 As String = _
                Dir (MyString2 & "*.isg")
    
            'PRINT STRING TO DEBUG WINDOW
            Debug.Print _
                MyString4
    
    '        Dim MyString3 As String = _
    '            MyString2 & "COOKIE.isg"
    
            'BUILD THE FULL FILE NAME FOR THE (FIRST) ISG FILE IN THE SAME FOLDER AS THE CURRENT IMAGE
            Dim MyString3 As String = _
                MyString2 & MyString4
    
            'PRINT STRING TO DEBUG WINDOW
            Debug.Print _
                MyString3
    
            'OPEN THE (FIRST) ISG FILE IN THE SAME FOLDER AS THE CURRENT IMAGE FILE
            With Measure.SmartSegmentation.RecipeCommands.Open(NewMacro2)
                '.FileName = "C:\Users\mbatc\Desktop\New folder\COOKIE.isg"
                .FileName = MyString3
                .FilterIndex = 1
                .Settings = Nothing
                .FilterInput = True
                .Run(image1)
            End With
    
        End Function
    




  • Thank you Matt. It's useful to know you can select files with wildcard characters, though I do prefer Yuri's solution in this case.

    I was able to make it work with the global variable. Thanks!
  • 2024-05-02-130849

    cjhung --

    I am glad the information was helpful.

    Good luck on your project.

    -- Matt

Sign In or Register to comment.