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

Is there an elegant way to eliminate all but the largest object found within an image?

All --

I'm working on a project that has an requirement to work on only the largest image in the image.

Finding the objects is not a problem but only the largest object needs to be analyzed.

I have a method that resets the LOWER AREA LIMIT of MEASUREMENT FILTER RANGES to just under the AREA for the object with the MAXIMUM AREA for the group using STATS(3) from:

        '-- Determine which object is the largest

        'Learn the areas as a measurement
        Dim MyMeasure As New MeasEntry
        MyMeasure.Measurement=eMeasures.RgnArea

        'Learn the statistics of the areas as a measurement
        Dim stats(9) As Double
        MyMeasurementData.CalcStatistics(MyMeasure,stats)

        '0 -- mcStatsMean -- mean value
        '1 -- mcStatsStDev -- standard deviation
        '2 -- mcStatsMin -- minimum value
        '3 -- mcStatsMax -- maximum value
        '4 -- mcStatsRange -- range
        '5 -- mcStatsSum -- sum
        '6 -- mcStatsIMin -- index Of minimum
        '7 -- mcStatsIMax -- index Of maximum
        '8 -- mcStatsNItems -- number Of items

This method seems like a bit of a "cheat".

Using STATS(7) from above, the software knows the index for the MAX AREA OBJECT so I'm looking for a more elegant method to eliminate all of the other objects.  Perhaps something like SELECTING the MAX OBJECT, INVERTING the SELECTION, and then DELETING the SELECTED OBJECTS.

What method would be recommended by the PREMIER GURUS?

Thanks.

-- Matt

Best Answer

  • Answer ✓
    Hi Matt,

    Your idea is good, you can use statistics to get largest object index and delete other objects.

    Here is the macro that will do that:

        'Leave only largest object on image
        Public Sub ShowLargestObject
            Dim im As McImage=ThisApplication.ActiveImage
            If im Is Nothing Then Exit Sub
    
            Dim md As McMMData=im.MeasurementsData
            Dim indexOfLargest As Integer=md.Statistics(eMeasures.RgnArea).IndexOfMax-1'0-based
            'disable updates to speed up
            md.BeginUpdateBlock(True)
            'select all
            For i As Integer=0 To md.SubFeatures.Count-1
                'select all but largest
                If i<>indexOfLargest Then
                    md.SubFeatures(i).Selected=True
                End If
            Next
            md.DeleteSelected
            'enable updates
            md.BeginUpdateBlock(False)
        End Sub

    I've also attached a complete project.

    Regards,

    Yuri

Answers

  • Yuri --

    Thank you for the code and the assistance.

    It not only solves my issue but demonstrates several PREMIER automation features.

    Thanks again.

    -- Matt

Sign In or Register to comment.