04-04-2013 08:41 PM - edited 04-04-2013 08:42 PM
Hello,
I am new to NI. I have a very strange problem. The project needs 4 cameras to capture the images and process image after that. It is production line and supposed to run without stopping. The image is captured every 10 ms. I used 4 threads to do this work and try to finish at the same time. It is running ok around 50000 times with a tiny memory increased. I think it is caused by each time create and kill thread and .net couldn't release all the resources. The memory doubled up after 50000 times and I can still snap image but can't use any Algorithm to process the image. It keeps giving me E Exception of type 'NationalInstruments.Vision.VisionException' was thrown. (in:Void ThrowError()).
Here is the sample code i use:
Public Function GetImages(ByVal Index As Integer, ByVal ImageSource As eDataSources, ByRef sFolderName As String) As Integer
Dim path As String
Try
If ImageSource = eDataSources.FRESH Then
If Index <= 0 Then
' TAKE FRESH IMAGES
For Each c As NIplugin.NICamera In gCameras
c.Acquire()
Wait(10)
Next
If gCameras.Contains("C1") Then
_image1 = gCameras.Item("C1").WaitImageReady()
End If
If gCameras.Contains("C2") Then
_image2 = gCameras.Item("C2").WaitImageReady()
End If
If gCameras.Contains("C3") Then
_image3 = gCameras.Item("C3").WaitImageReady()
End If
If gCameras.Contains("C4") Then
_image4 = gCameras.Item("C4").WaitImageReady()
End If
End If
Me.AttachImages()
Catch ex As Exception
goTrace.LogException(ex)
End Try
Return 1
End Function
Public Function SnapImage(ByVal settings As NICameraParameters) As VisionImage
Try
Dim params As NICameraParameters = settings
'snap an image
'_session.Snap(SnappedImage)
SyncLock _currImage
_currImage = _session.Snap(_currImage)
End SyncLock
Catch ex As Exception
End Try
Public Sub Acquire()
If Not _thread Is Nothing Then Return
_thread = New System.Threading.Thread(AddressOf Me.SnapImage)
_thread.IsBackground = True
_thread.Start(_settings)
End Sub
Public Function WaitImageReady() As VisionImage
If _thread Is Nothing Then
Throw New Exception("WaitImageReady() called without Acquire() in NICamera.")
End If
_thread.Join()
_thread = Nothing
Return _currImage
End Function
I am using
Algorithms.Copy(image, pimage)
pimage.Overlays.[Default].Clear()
' FIND ROUGH LOCATION
sh = New RectangleContour(iOffset, iOffset, pimage.Width - 2 * iOffset, pimage.Height - 2 * iOffset)
roi = New Roi(sh)
eoptions.Polarity = EdgePolaritySearchMode.All
sereport = Algorithms.StraightEdge(pimage, roi, sd1, eoptions, seoptions)
to process the image.
My guess is after 50000 times, threading causing not release resource issue. Could anyone share some similar information?
Any feedback is appreciated.
04-05-2013 04:45 PM
Does the Exception give you any other information, like what type it is?
04-10-2013 05:52 AM
I couldn't catch the type since it was running as release version. I will do the debug version next..
Here is the function I was using
Protected Function InspectImage(ByVal image As VisionImage, ByVal calTemplate As VisionImage, ByVal eoptions As EdgeOptions, ByVal seoptions As StraightEdgeOptions, ByVal sd1 As SearchDirection, ByVal sd2 As SearchDirection, ByVal bDrawDetails As Boolean) As cPoint
Dim sh As Shape
Dim roi As Roi
Dim sereport As StraightEdgeReport
Dim cPt As New cPoint
Dim iOffset As Integer = _Tool.Offset
Try
If image Is Nothing Then Return cPt
Algorithms.CopyCalibrationInformation(calTemplate, image)
' 'Algorithms.CorrectCalibratedImage(image, pimage, New PixelValue(0), InterpolationMethod.Bilinear)
Using pimage As New VisionImage()
Algorithms.Copy(image, pimage)
pimage.Overlays.[Default].Clear()
' FIND ROUGH LOCATION
sh = New RectangleContour(iOffset, iOffset, pimage.Width - 2 * iOffset, pimage.Height - 2 * iOffset)
roi = New Roi(sh)
eoptions.Polarity = EdgePolaritySearchMode.All
sereport = Algorithms.StraightEdge(pimage, roi, sd1, eoptions, seoptions)
If sereport.StraightEdges.Count = 0 Then
cPt.Y = pimage.Height / 2
Else
cPt.Y = sereport.StraightEdges(0).StraightEdge.Start.Y
End If
sh = New RectangleContour(iOffset, iOffset, pimage.Width - 2 * iOffset, pimage.Height - 2 * iOffset)
I am pretty sure Algorithms.StraightEdge causing the exception. I can still snap the image without any problem.
04-11-2013 06:51 PM
Have you thought about implementing a ring buffer? It sounds like it is running out of usable memory and it is performing the Snap function, but not actually generating an image. This is why the later functions would cause the exception. It would be good to get more information about the exception so we can troubleshoot it better.