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

Parent objects

thank you Yuri,
your answer is clear but I need to get the values inside my APP in order to be processed locally.
Is there any possibility to read out from image measurements the required data?
ciao
Maurizio

Best Answer

  • edited January 2015 Accepted Answer
    Hi Maurizio,

    You can read the measurements in a loop and sort them by eMeasures.AnyParentName and eMeasures.AnyClassName.

    Here is the macro that does it:

        'class to collect stats
        Public Class ClassStats
            Public dict As New System.Collections.Generic.Dictionary(Of String,Integer)
            Public Sub New(clName As String)
                dict.Add(clName,1)
            End Sub
        End Class
    
    Public Module Module1
        'Report parent-class statistics
        Public Sub ReportParentStats
            Dim im As McImage=ThisApplication.ActiveImage
            If im Is Nothing Then Exit Sub
            Dim md As McMMData=im.MeasurementsData
            Dim parents As New System.Collections.Generic.Dictionary(Of String,ClassStats)
            For Each sf As McMMSubFeature In md.SubFeatures
                'get parent name
                Dim par As String=sf.Value(eMeasures.AnyParentName)'get parent
                If par="" Then Continue For'ignore objects withot parents (parents themselves)
                Dim clName As String=sf.Value(eMeasures.AnyClassName)'get class
                If Not parents.ContainsKey(par) Then
                    'create new entry
                    parents.Add(par,New ClassStats(clName))
                Else
                    'add to existing
                    Dim cls As ClassStats=parents(par)
                    If cls.dict.ContainsKey(clName) Then
                        cls.dict(clName)=cls.dict(clName)+1
                    Else
                        cls.dict.Add(clName,1)
                    End If
                End If
            Next
            'report objects of each class
            ThisApplication.Output.Show
            For Each kvKey As String In parents.Keys
                ThisApplication.Output.PrintMessage(String.Format("Number of objects in parent {0}",kvKey))
                Dim parVal As ClassStats=parents(kvKey)
                For Each kvCl As System.Collections.Generic.KeyValuePair(Of String,Integer) In parVal.dict
                    ThisApplication.Output.PrintMessage(String.Format("{0} = {1}",kvCl.Key,kvCl.Value))
                Next
            Next
        End Sub
    
    
    End Module
    


    The output looks like this:

    Number of objects in parent P1
    Class 2 = 2
    Class 3 = 3
    Number of objects in parent P2
    Class 2 = 3
    Class 3 = 6


    I've also attached the project.

    Regards,

    Yuri

Answers

  • It looks like I forgot to attach the project. Here it is.
    Also, I've adjusted project for 9.1.4 (Sub New couldn't have parameters)

    
    Imports MediaCy.Addins.Measurements
    
    'class to collect stats
    Class ClassStats
        Public dict As New System.Collections.Generic.Dictionary(Of String,Integer)
    End Class
    
    Public Module Module1
    
        'Report parent-class statistics
        Public Sub ReportParentStats
            Dim im As McImage=ThisApplication.ActiveImage
            If im Is Nothing Then Exit Sub
            Dim md As McMMData=im.MeasurementsData
            Dim parents As New System.Collections.Generic.Dictionary(Of String,ClassStats)
            For Each sf As McMMSubFeature In md.SubFeatures
                'get parent name
                Dim par As String=sf.Value(eMeasures.AnyParentName)'get parent
                If par="" Then Continue For'ignore objects withot parents (parents themselve)
                Dim clName As String=sf.Value(eMeasures.AnyClassName)'get class
                If Not parents.ContainsKey(par) Then
                    'create new entry
                    Dim cl As New ClassStats
                    cl.dict.Add(clName,1)
                    parents.Add(par,cl)
                Else
                    'add to existing
                    Dim cls As ClassStats=parents(par)
                    If cls.dict.ContainsKey(clName) Then
                        cls.dict(clName)=cls.dict(clName)+1
                    Else
                        cls.dict.Add(clName,1)
                    End If
                End If
            Next
            'report objects of each class
            ThisApplication.Output.Show
            For Each kvKey As String In parents.Keys
                ThisApplication.Output.PrintMessage(String.Format("Number of objects in parent {0}",kvKey))
                Dim parVal As ClassStats=parents(kvKey)
                For Each kvCl As System.Collections.Generic.KeyValuePair(Of String,Integer) In parVal.dict
                    ThisApplication.Output.PrintMessage(String.Format("{0} = {1}",kvCl.Key,kvCl.Value))
                Next
            Next
        End Sub
    End Module
    
    Yuri
Sign In or Register to comment.