Hi, from my cam I analyze each frame to detect the movements. The code I wrote works fine but I can only analyze 6 / 7 frames per second.
I tried using a loop Parallel.For to exploit multi-core but I have not had many advantages. ow can I improve my code?
Thanks.
I tried using a loop Parallel.For to exploit multi-core but I have not had many advantages. ow can I improve my code?
VB.NET:
Private fronte As Boolean
Private rgbValues_old() As Byte
Private Function ControlImageBiZoneThread(ByVal immagine As Image) As Boolean
Try
Dim esito As Boolean
Dim bmp As New Bitmap(immagine)
Dim bitmap As System.Drawing.Imaging.BitmapData
Dim rect As Rectangle
Dim ptr As IntPtr
Dim bytes As Integer
Dim pixelBytes As Integer = 3
Dim rgbValues_new() As Byte
Dim rgbValues_tmp() As Byte
rect = New Rectangle(0, 0, bmp.Width, bmp.Height)
bitmap = bmp.LockBits(rect, Drawing.Imaging.ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
' Get the address of the first line.
ptr = bitmap.Scan0
' This code is specific to a bitmap with 24 bits per pixels.
bytes = bmp.Width * bmp.Height * 3
ReDim rgbValues_new(bytes - 1)
ReDim Preserve rgbValues_old(bytes - 1)
ReDim rgbValues_tmp(bytes - 1)
' Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues_new, 0, bytes)
rgbValues_tmp = DirectCast(rgbValues_new.Clone, Byte())
If fronte = False Then GoTo salta
Dim changedX(bmp.Width - 1, bmp.Height - 1) As Integer
Dim scanPixel As Integer
Dim lumaPixel As Double
Dim Max As Double
Dim Min As Double
Dim temp As Integer
Dim listaPointX As New List(Of Integer)
Dim listaPointY As New List(Of Integer)
PixelChanged = 0
scanPixel = 0
Dim PixelChanged2 As Integer
'*********************************************************************************
Parallel.For(0, bmp.Height - 1, Sub(Y)
For X As Integer = 0 To 639
Dim pos As Integer = (Y * bitmap.Stride) + (X * pixelBytes)
scanPixel += 1
'Calcolo luminosità
lumaPixel = rgbValues_new(pos) / 255
Max = 0
Min = 1
If lumaPixel > Max Then Max = lumaPixel
If lumaPixel < Min Then Min = lumaPixel
'temp += System.Math.Min(((Max + Min) / 2 * 255), 255)
System.Threading.Interlocked.Add(temp, CInt(System.Math.Min(((Max + Min) / 2 * 255), 255)))
If (rgbValues_new(pos) >= SensibiltàColoriView.B AndAlso System.Math.Abs(CInt(rgbValues_new(pos)) - CInt(rgbValues_old(pos))) > SensibilitaColore) Then
System.Threading.Interlocked.Add(PixelChanged2, CInt(1 + ((480 - Y) * (pesoPixel / 480))))
listaPointX.Add(X)
listaPointY.Add(Y)
If _showPixelChanged Then
rgbValues_new(pos + 2) = PixelChangedColor.R 'Red
rgbValues_new(pos + 1) = PixelChangedColor.G 'Green
rgbValues_new(pos) = PixelChangedColor.B 'Blue
End If
If esito = False AndAlso PixelChanged2 > SensilitàPixel Then esito = True : Exit For
End If
Next
End Sub)
'*********************************************************************************
luminosity = temp / scanPixel
listaPointX.Sort()
listaPointY.Sort()
System.Runtime.InteropServices.Marshal.Copy(rgbValues_new, 0, ptr, bytes)
bmp.UnlockBits(bitmap)
Dim myPoint(1) As Point
If listaPointX.Count > 0 Then myPoint(0).X = listaPointX(0)
If listaPointY.Count > 0 Then myPoint(0).Y = listaPointY(0)
If listaPointX.Count > 0 Then myPoint(1).X = listaPointX(listaPointX.Count - 1)
If listaPointY.Count > 0 Then myPoint(1).Y = listaPointY(listaPointY.Count - 1)
RaiseEvent PixelChangedViewControllo(bmp, bmp, esito, PixelChanged2, luminosity, myPoint)
salta: rgbValues_old = DirectCast(rgbValues_tmp.Clone, Byte())
fronte = True
bmp.Dispose()
bmp = Nothing
rect = Nothing
listaPointX = Nothing
listaPointY = Nothing
Return esito
Catch ex As Exception
'MsgBox(ex.Message)
End Try
End Function
Public Sub DoControl()
Try
myTask = New System.Threading.Thread(AddressOf DoControltask)
myTask.IsBackground = True
myTask.Priority = System.Threading.ThreadPriority.Highest
myTask.SetApartmentState(Threading.ApartmentState.STA)
myTask.Start()
Catch ex As Exception
End Try
End Sub
Private Sub DoControltask()
Do While _StartStopElaboration
Try
countF += 1
SendMessage(hHwndFinestra, WM_CAP_EDIT_COPY, 0, 0)
System.Threading.Thread.Sleep(threadSlep)
If System.Environment.TickCount - tempElapsed >= 1000 Then
tempElapsed = System.Environment.TickCount
countFrame = countF
countF = 0
End If
RaiseEvent EsitoControllo(ControlImageBiZoneThread(My.Computer.Clipboard.GetImage), PixelChanged, countFrame)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Loop
End Sub
Thanks.