RegionsOperatorsmaRgnFourierDescriptor Method |
![]() |
Namespace: MediaCy.IQL.Operators
<ExtensionAttribute> Public Shared Function maRgnFourierDescriptor ( regions As McRegions ) As McFourierDescriptor
'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