Home Image-Pro General Discussions

How to count number of stellar shape vertices using Fourier Descriptor

Image-Pro Premier provides Fourier Descriptor measurement, which represents a spectrum calculated from the shape of the object's outline. The values are translation, rotation and scale invariant and can be used for shape identification. In the following example I will show how it can be used to count the number of vertices of any polygonal shape.

The Fourier Descriptor spectrum contains 32 values (by default). The spectrum is normalized, so the item 1 is the diameter of the normalized shape, it's normalized to 25 by default. Entries from 2 to the end correspond to the frequency spikes calculated along the outline. These spikes show the predominant shape of the object, so if the object is a square, the item 3 will have maximal value , if it's a triangle - item 2. The following macro finds the maximums of the Fourier Descriptor values and changes the object name according to the number of vertices.

The macro also shows how to get multi-dimensional measurement data, which can also be used for Radii, Calipers or Diameters.
Imports MediaCy.Addins.Measurements
Public Module Module1
    'Count number of end-points of stellar shape objects'
    'using Fourier Descriptor'
    Public Sub CountStellarVertices()
        Dim im As McImage=ThisApplication.ActiveImage
        If im Is Nothing Then Exit Sub 'no image'
        Dim md As McMMData=im.MeasurementsData
        If md.Count=0 Then Exit Sub 'no objects'
        'create MeasEntry to get values of Fourier Descriptor'
        Dim fdMeas As MeasEntry=New MeasEntry(eMeasures.aRgnFourierDescriptor,mcmmStatField.Values)
        For Each sf As McMMSubFeature In md.SubFeatures
            'skip non-region features'
            If sf.FeatureType And McMMSubFeature.mcmmsfTypes.mcmmsfRegion=0 Then Continue For
            'the data is 32-item array'
            Dim fdData() As Double=sf.Value(fdMeas)
            Dim maxIndex As Integer=0, maxVal As Double=0
            'loop through spectrum spikes, skip first 2 entries, which correspond to size'
            'cover only the first half of the spectrum (symmetrical)'
            For j As Integer =2 To 16
                Dim v As Double=fdData(j)
                If v>maxVal Then
                    'max spectrum entry corresponds to the number of vertices'
                End If
            'rename measurement according to the number of stellar vertices in shape'
            sf.Name=String.Format("{0} vert",maxIndex-1)
        'update text on image'
    End Sub
End Module
Attached is the macro project and test image.

Sign In or Register to comment.