Click or drag to resize

RegionsOperatorsmaRgnFourierDescriptor Method

Array of NumElements complex values of Fourier Descriptor.

Namespace:  MediaCy.IQL.Operators
Assembly:  MediaCy.IQL.Operators (in MediaCy.IQL.Operators.dll) Version: 3.1.0.0
Syntax
VB
<ExtensionAttribute>
Public Shared Function maRgnFourierDescriptor ( 
	regions As McRegions
) As McFourierDescriptor

Parameters

regions
Type: MediaCy.IQL.FeaturesMcRegions

Return Value

Type: McFourierDescriptor

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type McRegions. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Remarks
This measurement's McMeasure.Value property is exposed as a 2 or 3 dimensional array, with the "row" for each region feature holding an array of McFourierDescriptor.NumElements values. The type of output values is defined by the OutputType property. When OutputType is mcfdMagnitudeOnly (default) the output is 2-dimensional, in case of mcfdRealImaginary or mcfdPolar - 3-dimensional (see OutputType for details) This measurement is based on the complex cartesian FFT transform of object's outline, where each outline is treated as a path in the Real,Complex plane. The outline is sub-sampled to the number of X,Y points defined by NumElements. The NumElements Fourier descriptors (cos,sin coefficient pairs or magnitude, depending on OutputType) are filled into the output array in the usual way, namely (when NormalizeFlags is set to mcfdNormalizeNone): the 0th descriptor (Value[0]) is the mean value (zero frequency); in this case, the real part (Value[0,0]) will equal the mean x coordinate while the imaginary part (Value[0,1]) will be the mean y coordinate. The 1st to the (N/2)th descriptors are the cos,sin coefficients (mcfdRealImaginary) or magnitude/phase (mcfdPolar) for the positive frequencies (i.e., Value[1] are coefficients for the fundamental, lowest, frequency; Value[2] are for 2 times the fundamental; Value[3] are for 3 times the fundamental; and so on to Value[N/2] which are coefficients for N/2 times the fundamental. Since this is a complex transform, the (N/2)th to (N-1)th descriptors (the negative frequencies) are not a mirror image of the 1st to (N/2)th descriptors as they would be for a "real" transform. The lowest negative frequency is the last descriptor in the Value array with higher negative harmonics moving down towards the middle of the array of descriptors (the (N/2)th frequency coefficients are always the same for positive and negative frequencies and thus share the (N/2)th descriptor). For this transform, the area is treated as if it had been drawn counter-clockwise, even if it was drawn clockwise. The use of boundary sampled Fourier Descriptors to compare normalized shapes by Euclidian distance is described in: Wallace, T.P. and Wintz, P.A., "An Efficient Three-Dimensional Aircraft Recognition Algorithm Using Normalized Fourier Descriptors." Computer Graphics and Image Processing, 13, pp 99-126 (1980). This technique could be used to automatically classify areas by their normalized boundary shape.
Examples
VB
'This is FourierDescriptorSamples.bas
'The file includes examples of using Fourier Descriptor measurement

Option Explicit

Function LoadDemoImage()
    'load demo image
    Images.Open Path & "\Images\Shapes.tif"
End Function

'draw boundary in annotation overlay
Function DrawBoundary(ByRef Image As McImage, ByRef vPoints As Variant)
    Set ActiveGraphicObject = Image.AnnotationOverlay.Add("McGraphObjPoly")
    ActiveGraphicObject.AddPoints vPoints
    ActiveGraphicObject.NotifyCreationComplete
End Function

Function CalculateEucledianDistance(v1, v2, NumDimensions, NumElements) As Double
    Dim i, Dist As Double
    Dist = 0
    'skip 0-frequency
    For i = 1 To NumElements - 1
        If NumDimensions = 3 Then
            '2D array
            Dist = Dist + (v1(0, i) - v2(0, i)) ^ 2 + (v1(1, i) - v2(1, i)) ^ 2
        Else
            '1D array
            Dist = Dist + (v1(i) - v2(i)) ^ 2
        End If
    Next i
    Dist = Sqr(Dist)
    CalculateEucledianDistance = Dist
End Function

'demonstrate using fourier descriptor
Sub DemoFourierDescriptor()
    'load demo image with shapes
    LoadDemoImage

    MsgBox "On the following steps we will show " & vbCrLf & "shape normalization and shape matching features " & vbCrLf & "of Fourier Descriptor.", vbInformation, "Fourier Descriptor"

    Dim rg As McRegions
    'set regions
    Set rg = ActiveImage.RegionFeatures

    'count bright objects
    rg.Threshold.AutoFindPhase = mcfpBrightest
    rg.Threshold.Execute
    If rg.Count = 0 Then
        MsgBox "Could not find objects"
        Exit Sub
    End If

    Dim i, j
    'set number of elements for Fourier Descriptor
    rg.maRgnFourierDescriptor.NumElements = 128
    rg.maRgnFourierDescriptor.OutputType = mcfdRealImaginary

    Dim vFourierDescr, vdInverted
    'cleanup overlay
    ActiveImage.AnnotationOverlay.RemoveAll

    'Step 1
    'normalize object size and orientation and display normalized object's outlines
    'do not normalize the translation to show the outlines obove the objects
    rg.maRgnFourierDescriptor.NormalizeFlags = mcfdNormalizeRotation Or mcfdNormalizeScale

    For i = 0 To rg.Count - 1
        'get normalized Fourier Descriptor
        vFourierDescr = rg.maRgnFourierDescriptor.Value(i)
        'apply inverse FFT transform to Fourier Descriptor to get normalized object outline
        ActiveImage.fft.Inverse1D vFourierDescr, vdInverted, mcfoRealImaginaryDouble
        'draw outline defined by descriptor
        DrawBoundary ActiveImage, vdInverted
    Next i

    MsgBox "Normalized object outlines are shown on the image." & vbCrLf & "Note that outlines of similar objects have the same size, " & vbCrLf & "orientation and starting point.", vbInformation, "Fourier Descriptor"


    'Step 2
    'Calculate matching degree between shapes
    'using Complex Eucleadian Distance between FourierDescriptors of objects
    Dim ReferenceObjectID, vRefFD

    'use first reference object
    ReferenceObjectID = 0

    'set only Magnitude as output, simplyfy calculation of Fourier Descriptor
    rg.maRgnFourierDescriptor.OutputType = mcfdMagnitudeOnly

    'get Fourier Descriptor of reference object
    vRefFD = rg.maRgnFourierDescriptor.Value(ReferenceObjectID)

    For i = 0 To rg.Count - 1
      'calculate eucledian distance between current and reference object and set it as label
      rg.SourceData(i) = CalculateEucledianDistance(vRefFD, rg.maRgnFourierDescriptor.Value(i), rg.maRgnFourierDescriptor.ValueMcObject.Shape(mcobjSIC_NofDims), rg.maRgnFourierDescriptor.ValueMcObject.Shape(mcobjSIC_SizeDim1))
    Next i
    rg.DisplayedObjects.SetLabelText "%f", mcsltDisplaySourceData Or mcsltLabelCenter, &H4000FF

    MsgBox "Object " & ReferenceObjectID + 1 & " is selected as reference object" & vbCrLf & "The labels show matching degree of all objects to the reference object." & vbCrLf & "Matching degree is calculated using Euclidian distance between " & vbCrLf & "magnitude components of Fourier descriptors.", vbInformation, "Fourier Descriptor"

    'Step 3
    'Classification using 4 first objects
    'Mark all objects below threshold

    Dim ClassThreshold
    Dim Colors
    Colors = Array(&HFF00&, &H80&, &HFF0000, &HFF00FF)
    'set threshold to 5
    ClassThreshold = 5

    For j = 0 To 3
        'use first 4 reference objects
        ReferenceObjectID = j
        vRefFD = rg.maRgnFourierDescriptor.Value(ReferenceObjectID)

        For i = 0 To rg.Count - 1
          'calculate Euclidian distance between current and reference object and set it as label
          Dim Diff
          Diff = CalculateEucledianDistance(vRefFD, rg.maRgnFourierDescriptor.Value(i), rg.maRgnFourierDescriptor.ValueMcObject.Shape(mcobjSIC_NofDims), rg.maRgnFourierDescriptor.ValueMcObject.Shape(mcobjSIC_SizeDim1))
          If Diff < ClassThreshold Then
            'change color and width
            rg.DisplayedObjects.Item(i).BorderColor = Colors(j)
            rg.DisplayedObjects.Item(i).BorderWidth = 2
          End If
        Next i
    Next j
    MsgBox "All objects are classified and colored according to character shape." & vbCrLf & vbCrLf & "End of the demo", vbInformation, "Fourier Descriptor"
End Sub
See Also

Reference

RegionsOperatorsmaRgnFourierDescriptor(McRegions)
McFourierDescriptor