|
-
Nov 6th, 2006, 05:49 PM
#1
Re: movement detection
This is what I used to compare two images, I know it looks a bit **** and so...but keep in mind that it was a long time since i wrote it...and hey, it works for me 
Ive written some explanation comments in the code, please ask if anything is unclear.
VB Code:
Private Sub ProcessImage()
Dim file As String = SavedFilePath
PicCap.Image = Nothing
Dim bm As Bitmap
Try
bm = Image.FromFile(filename)
Catch ex As Exception
MessageBox.Show(ex.Message)
Exit Sub
End Try
Dim y As Integer
Dim r As Double = 0
Dim g As Double = 0
Dim b As Double = 0
Dim pxCount As Long = 0
'Loop through the image and add the rgb values to their respective variable.
For x as Integer = 0 To 319
For y as Integer = 0 To 239
pxCount += 1
r += bm.GetPixel(x, y).R
g += bm.GetPixel(x, y).G
b += bm.GetPixel(x, y).B
Next
Next
'The current NewR, NewG and NewB values comes from the PREVIOUS image, so those values goes into the "old" variables instead.
OldR = NewR
OldG = NewG
OldB = NewB
'And set the "New" variables with the current images R, G and B values divided by the number of pixels in the image.
NewR = r / pxCount
NewG = g / pxCount
NewB = b / pxCount
'Exit the sub if OldR is -1(That means that there was no previous image)
If OldR = -1 Then
Exit Sub
End If
'Here comes the code to compare the old and the new R, G and B values:
If OldR > NewR Then
If (OldR - NewR) >= Picky Then
ListAdd(file)
Exit Sub
End If
ElseIf NewR > OldR Then
If (NewR - OldR) >= Picky Then
ListAdd(file)
Exit Sub
End If
End If
If OldG > NewG Then
If (OldG - NewG) >= Picky Then
ListAdd(file)
Exit Sub
End If
ElseIf NewG > OldG Then
If (NewG - OldG) >= Picky Then
ListAdd(file)
Exit Sub
End If
End If
If OldB > NewB Then
If (OldB - NewB) >= Picky Then
ListAdd(file)
Exit Sub
End If
ElseIf NewB > OldB Then
If (NewB - OldB) >= Picky Then
ListAdd(file)
Exit Sub
End If
End If
'Dispose the bitmap.
bm.Dispose()
End Sub
What im doing if it detects any changes is just to call the ListAdd sub, but you can ofcourse change it to whatever you like. And oh, almost forgot..the "Picky" (hehe) variable is a value that the user sets in settings to change how sensitive it should be. (A value from 1 to 24, where 4 is default, 1 is very sensitive and 25 is very unsensitive)..
Im almost embarrased to show this old code...but if its to any help to you then its good.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|