Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

MatchPattern2 memory leak

Hi,

I am using the following software:

VB 2008 Express

NI Vision 8.5

NI  Vision Acquisition Software 8.6

 

My program takes an image, locates a pattern, and shows an overlay based on where the patterns were found in real time.  I am having a problem with the memory continuously increasing.  When I comment out the calls to MatchPattern2, the memory usage stays stable.  Below is the relevant code.  Can anyone confirm this issue or offer work-arounds?  Thanks

 

 

Public Class LocatePosts 

Dim templateImage As New NationalInstruments.CWIMAQControls.CWIMAQImage

Dim patternMatchOptions As New NationalInstruments.CWIMAQControls.CWIMAQMatchPatternOptions

Dim patternMatchReport As New NationalInstruments.CWIMAQControls.CWIMAQPatternMatchReport 

Dim post1ROI As New NationalInstruments.CWIMAQControls.CWIMAQRectangle

Dim post2ROI As New NationalInstruments.CWIMAQControls.CWIMAQRectangle  

 

   Private Sub LocatePosts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

post1ROI.Initialize(My.Settings.post1ROIleft, My.Settings.post1ROItop, My.Settings.post1ROIwidth, My.Settings.post1ROIheight)

post2ROI.Initialize(My.Settings.post2ROIleft, My.Settings.post2ROItop, My.Settings.post2ROIwidth, My.Settings.post2ROIheight) 

 

AxCWIMAQVision1.ReadImage(templateImage, My.Settings.postTemplatePath)

AxCWIMAQVision1.LearnPattern2(templateImage)

 

patternMatchOptions.MatchMode = NationalInstruments.CWIMAQControls.CWIMAQMatchModes.cwimaqMatchShiftInvariant

patternMatchOptions.NumMatchesRequested = 1

patternMatchOptions.MinimumMatchScore = 700

patternMatchOptions.SubPixelAccuracy = False 

  ...

SnapImageTimer.Enabled = True 

   end sub 

  

 

   Private Function analyzeImage() As Integer  'locate the posts

        Dim numberOfPostsFound As Integer = 0

         

        If AxCWIMAQVision1.MatchPattern2(myImage, templateImage, patternMatchOptions, patternMatchReport, post1ROI) <> 0 Then 'causing memory leak

            MessageBox.Show("error finding post 1")

            Return -1

        End If

 

        If patternMatchReport.Count = 1 Then

            post1.X = patternMatchReport.Item(1).Position.X

            post1.Y = patternMatchReport.Item(1).Position.Y

            numberOfPostsFound += 1

        End If

 

        If AxCWIMAQVision1.MatchPattern2(myImage, templateImage, patternMatchOptions, patternMatchReport, post2ROI) <> 0 Then 'causing memory leak

            MessageBox.Show("error finding post 2")

            Return -2

        End If

 

        If patternMatchReport.Count = 1 Then

            post2.X = patternMatchReport.Item(1).Position.X

            post2.Y = patternMatchReport.Item(1).Position.Y

            numberOfPostsFound += 1

        End If

 

        Return numberOfPostsFound

    End Function

 

 

   Private Sub SnapImageTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SnapImageTimer.Tick     '25ms interval

        errorCode = CWIMAQ1394.Grab2CW(sid, 1, BufferNumber, myImage)

        If (errorCode <> CWIMAQ1394.ErrorCodes.IMG1394_ERR_GOOD) Then

            DisplayError(errorCode)

        Else

            If analyzeImage() <> 2 Then

                'MessageBox.Show("unable to locate both posts)

            End If

 

            'displayOverlays()

        End If

    End Sub 

End Class

0 Kudos
Message 1 of 4
(3,559 Views)

Hi Brad,

 

Could you post an attachment of the simplest code that reproduces the issue on your side?

Jeff | LabVIEW Software Engineer
0 Kudos
Message 2 of 4
(3,538 Views)

Hi Jeff,

Thanks for your reply.  I have attached a sample project that illustrates the problem.  I think that I have more or less figured it out now.  Looks like you need to pass a new CWIMAQPatternMatchReport to MatchPattern2 every time that it is called or else the memory usage will increase.  I'm not sure if this is expected or not, but it seems unexpected because you only get the latest match result in your CWIMAQPatternMatchReport when you use it multiple times.

In the attached project if I change the CWIMAQPatternMatchReport to be a local (used only once) instead of global (used multiple times) the memory usage seems to stabilize.

Regards,

Brad 

0 Kudos
Message 3 of 4
(3,516 Views)
Ok, I will try it out myself and see if I can reproduce it.
Jeff | LabVIEW Software Engineer
0 Kudos
Message 4 of 4
(3,489 Views)