|
-
Jul 2nd, 2005, 12:53 PM
#1
Thread Starter
Junior Member
Comparing Images using Icam
I have used Pino's program Icam and i really like the program....
now i'm just wanna know how to implement motion detection into the program itself?.. how do you copy frame out and then compare it so that it detects motion?... help is really appreciated!
-
Jul 2nd, 2005, 01:07 PM
#2
Re: Comparing Images using Icam
 Originally Posted by nightkids31
I have used Pino's program Icam and i really like the program....
now i'm just wanna know how to implement motion detection into the program itself?.. how do you copy frame out and then compare it so that it detects motion?... help is really appreciated! 
Hey,
Ok to start with to copy a frame we can use the following function
copyFrame(ByVal src As PictureBox, ByVal rect As RectangleF)
- This sub returns an image of the current frame. You need to pass to it the source picture box (Where the camera image is) and then a rectangle specifying size dimensions.
VB Code:
Me.picStill.Image = myCam.copyFrame(Me.picOutput, New RectangleF(0, 0, _
Me.picOutput.Width, Me.picOutput.Height))
The above example assumes your picturebox where the camera image is being displayed is called PicOutput and you wnat to display the image in PicStill.
Ok so now you have 1 frame, if you do this again, saving the images (See the program it has a save ability in it)
Then what we would need to do is loop throuhg the pixels (Most of them) and see if there is a change, we must take into consideration light changes etc)
VB Code:
For x = 0 To myImage.Width - 1
For y = 0 To myImage.Height - 1
Print(CType(myImage.GetPixel(x, y).ToArgb, Integer))
Next
Next
that would loop through and print every pixel in the picture, we would need to save this into an array and then compare it with the next image. I'm going to ask wossy to explain alittl more about the compare process and the getPixel method, (I'm still new to vb>net)
Pino
-
Jul 2nd, 2005, 01:14 PM
#3
Re: Comparing Images using Icam
 Originally Posted by nightkids31
how do you ... compare it so that it detects motion?... help is really appreciated! 
This isn't really a graphics question, graphics is simply the source of the working data.
It all depends on what kind of motion you want to detect. And what sort of objects you expect to be moving. Its easy to write a function that either says "Yes, something moved" or "No, nothing moved". All you have to do is compare the colours and if they don't match then motion did occur. If you want to be able to recognise 1 or more individual objects then you are moving into some hardcore vision algorithms. Extremely difficult to achieve.
You should investigate the Open Source Computer Vision Library (OpenCV). Its free, but its not simple to use. SLH knows more about this than I do.
Can you elaborate as to what kind of motion you want to detect?
I don't live here any more.
-
Jul 2nd, 2005, 01:20 PM
#4
Thread Starter
Junior Member
Re: Comparing Images using Icam
thanks for the reply pino and wossname...
but in that code you gave..you need to manually save two pictures first before comparing them? or could it be done automatically in the program?.... what i want to do is when it detects motion then it'll automatically save a picture in the computer.... i'm sorry..i'm really new in vb.net...
edit : the kind of motion that i want to detect is really simple..let's say if there's more than 5 color pixels that has changed..then motion is detected..just a really simple one..not a complex one
Last edited by nightkids31; Jul 2nd, 2005 at 01:23 PM.
-
Jul 2nd, 2005, 01:25 PM
#5
Fanatic Member
Re: Comparing Images using Icam
personally, I wouldn't even compare all the pixels, more like every fifth or something.
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Jul 2nd, 2005, 01:33 PM
#6
Re: Comparing Images using Icam
You don't need to save the file, just keep them as 2 separate bitmap objects in memory.
Also if you are doing such a simple yes/no motion detector then it would be a good idea to check only a small proportion of the pixels, say check every 5th pixel on every 5th row.
I haven't checked this code but it should give you a rough idea.
VB Code:
Public Function SomethingMoved(BitmapA as bitmap, BitmapB as Bitmap) as boolean
dim x as integer = 0
dim y as integer = 0
dim ChangedPixelCount = 0
'assume both images are the same size
for y = 2 to BitmapA.Height step 5
for x = 2 to BitmapA.Width step 5
If BitmapA.GetPixel(x, y) <> BitmapB.GetPixel(x, y) then
ChangedPixelCount += 1
End If
Next x
Next y
If ChangedPixelCount >= 5 then
'yes, motion detected
return true
else
'nope.
return false
End if
End Function
I don't live here any more.
-
Jul 2nd, 2005, 01:38 PM
#7
Re: Comparing Images using Icam
 Originally Posted by grilkip
personally, I wouldn't even compare all the pixels, more like every fifth or something.
Yeah, both on x and y axis, that will make it 25 times faster!!
There is one MAJOR flaw with this approach, changes in light levels (ie clouds/sunshine) wil trigger off a false reading. The answer to this is to look for pixels that have changed by a certain amount, so you have a fault tolerance.
Problems like this can easily make simple things very complicated.
I don't live here any more.
-
Jul 2nd, 2005, 01:45 PM
#8
Fanatic Member
Re: Comparing Images using Icam
 Originally Posted by wossname
.... so you have a fault tolerance.
Of course
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Jul 2nd, 2005, 01:51 PM
#9
Thread Starter
Junior Member
Re: Comparing Images using Icam
sorry..but now how do you save more than 1 images and put it into the memory to compare?...the above code saves 1 image..so how do you save more than 1?... sorry..really new to this..
edit : i got an error at this line
If BitmapA.GetPixel(x, y) <> BitmapB.GetPixel(x, y) Then
saying that Operator '<>' is not defined for types 'System.Drawing.Color' and 'System.Drawing.Color'.
Last edited by nightkids31; Jul 2nd, 2005 at 02:00 PM.
-
Jul 2nd, 2005, 02:00 PM
#10
Re: Comparing Images using Icam
You could add a "Sensitivity" variable (0-100), and then check each <timerinterval> how many of the pixels have changed (using the above suggestion), and then, if the percentage of pixels changed is greater than the sensitivity, you do whatever.
Bill
-
Jul 2nd, 2005, 02:13 PM
#11
Fanatic Member
Re: Comparing Images using Icam
You probably need to compare each of the color componts (Red, Green, Blue), create a seperate function for that.
VB Code:
Private Function GetDiff(ColorA As Color, ColorB As Color) As Integer
Return Math.Abs(ColorA.R - ColorB.R) + _
Math.Abs(ColorA.G - ColorB.G) + _
Math.Abs(ColorA.B - ColorB.B)
End Function
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Jul 2nd, 2005, 10:20 PM
#12
Thread Starter
Junior Member
Re: Comparing Images using Icam
ok thanks for the reply guys...but i still somehow get an error at this line of coding
If BitmapA.GetPixel(x, y) <> BitmapB.GetPixel(x, y) Then
"Operator '<>' is not defined for types 'System.Drawing.Color' and 'System.Drawing.Color'."
can someone help?
-
Jul 2nd, 2005, 10:44 PM
#13
Fanatic Member
Re: Comparing Images using Icam
I already helped you with that in my previous post. It's a function that gives a difference between two colors (or pixels)
Just realized though:
VB Code:
' you might need to convert those components to Integers first to prevent errors.
Math.Abs([b]CInt[/b](ColorA.R)
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Jul 2nd, 2005, 10:44 PM
#14
Re: Comparing Images using Icam
How about
If not BitmapA.GetPixel(x, y) = BitmapB.GetPixel(x, y) then
instead? equals is in the list of operators for type color in the help files..
Bill
-
Jul 2nd, 2005, 10:47 PM
#15
Fanatic Member
Re: Comparing Images using Icam
Yes but you will want to have a certain tollerance..
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Jul 2nd, 2005, 10:51 PM
#16
Re: Comparing Images using Icam
Wouldn't it be easier like I was sorta getting at above, to use the NUMBER of changed pixels, rather than figure out tolerances on an individual pixel level?
Bill
-
Jul 2nd, 2005, 10:56 PM
#17
Fanatic Member
Re: Comparing Images using Icam
I didn't say that tollerances on each pixel should be evaluated, I think it would be best to use my function (or something like it) and add all the differences. Then judge the combined difference off the whole pic.
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Jul 3rd, 2005, 01:45 AM
#18
Thread Starter
Junior Member
Re: Comparing Images using Icam
 Originally Posted by conipto
How about
If not BitmapA.GetPixel(x, y) = BitmapB.GetPixel(x, y) then
instead? equals is in the list of operators for type color in the help files..
Bill
it doesn't work too..it gives out the same error...
here is the code from the class
Private Sub cmdViewStill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdViewStill.Click
If myCam.iRunning Then
Dim a As FrmImage
a = New FrmImage
a.picImage.Image = myCam.copyFrame(Me.picOutput, New RectangleF(0, 0, _
Me.picOutput.Width, Me.picOutput.Height))
SomethingMoved()
Else
MessageBox.Show("Camera Is Not Running!")
End If
End Sub
Public Function SomethingMoved(ByVal BitmapA As Bitmap, ByVal BitmapB As Bitmap) As Boolean
Dim x As Integer = 0
Dim y As Integer = 0
Dim ChangedPixelCount As Integer = 0
'assume both images are the same size
For y = 2 To BitmapA.Height Step 5
For x = 2 To BitmapA.Width Step 5
If BitmapA.GetPixel(x, y) = BitmapB.GetPixel(x, y) Then
ChangedPixelCount += 1
End If
Next x
Next y
If ChangedPixelCount >= 5 Then
'yes, motion detected
Return True
Else
'nope.
Return False
End If
End Function
now how do i save 2 image and compare pixels? the operators "=" and "<>" doesn't work at all...or is there a way to convert bitmap to integer so that i can use the operators?
Last edited by nightkids31; Jul 3rd, 2005 at 02:18 AM.
-
Jul 3rd, 2005, 12:50 PM
#19
Fanatic Member
Re: Comparing Images using Icam
I've gotten interested in this, so I've made my own attempt. Checkout the screenshot.

The project is attached. Bear in mind that it assumes the resolution is 320x240 like my cam is.
Grid means the spacing between sample points, by increasing this number you will get more speed but less accuracy.
Edit: Oh, and it creates a folder "c:\temp3" and saves the bitmaps there.
Last edited by grilkip; Dec 16th, 2007 at 11:29 AM.
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Jul 4th, 2005, 06:22 AM
#20
Thread Starter
Junior Member
Re: Comparing Images using Icam
hey thanks for posting your project..just what i wanted to check out..thanks!
-
Jul 4th, 2005, 06:59 AM
#21
Re: Comparing Images using Icam
Your saved files will be a nice collection of photographs of blank bits of wall.
I don't live here any more.
-
Jul 4th, 2005, 08:19 AM
#22
Thread Starter
Junior Member
Re: Comparing Images using Icam
it seems a bit too sensitive..nevertheless i like it...thanks so much!
-
Jul 4th, 2005, 08:30 AM
#23
Re: Comparing Images using Icam
Raise the threshold a bit.
I don't live here any more.
-
Jul 4th, 2005, 08:51 AM
#24
Thread Starter
Junior Member
Re: Comparing Images using Icam
ah yeah...thanks wossname!
-
Jul 4th, 2005, 10:49 AM
#25
Fanatic Member
Re: Comparing Images using Icam
It might be interresting to look at the output on an a logarithmic scale.
Right now I'm trying to figure out a way tou get direct access to the pixeldata by using FrameCallBackFunc (or something similar), which would be better than grabbing it from the screen. So far no luck though.
@WossName: Follow your own advice.
Every type of camera and every different situation will require a calibration of the threshhold. And it will probably never be exactly right.
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Jul 20th, 2005, 02:09 AM
#26
Thread Starter
Junior Member
Re: Comparing Images using Icam
do you think you could explain the algorithm for your coding?
-
Jul 20th, 2005, 07:14 AM
#27
Fanatic Member
Re: Comparing Images using Icam
It samples a number of pixels from the grabbed image.
For each pixel the absolute difference of each color component between this pixel and the pixel in the same location in the previous picture is calculated. The three differences are added together(could make an average by dividing by 3 but it doesn't matter)
For all samples the differences are added together to get the combined difference.
The combined difference is compared to the threshhold to determine wether motion has occurred.
I've found a brilliant C# wrapper Class for a webcam somewhere on the web, I'll see if I can dig it up again.
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Jul 20th, 2005, 07:42 AM
#28
Thread Starter
Junior Member
Re: Comparing Images using Icam
Could you explain to me what this two part in checkquesave sub does?
Private Sub CheckSaveQue()
Do
If StopSaveThread Then Exit Do
Try
If SaveQue.Count = 0 Then
Threading.Thread.Sleep(1000)
Else
For Each ImgObj As Object In SaveQue
If StopSaveThread Then Exit Do
Dim savename As String = DirectCast(ImgObj, SavePic).FileName
Debug.WriteLine(savename)
DirectCast(ImgObj, SavePic).Image.Save(savename) Next
SaveQue.Clear()
End If
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try Loop
End Sub
-
Jul 20th, 2005, 07:54 AM
#29
Fanatic Member
Re: Comparing Images using Icam
SaveQue is an ArrayList wich holds all the pictures(and their data). The ChecksaveQue sub your pointing out save's whatever is in SaveQue. It does this on a seperate thread to hopefully prevent the saving from interfering with the capturing.
The CheckSaveQue sub checks if there are any pictures to be saved, if there are then it saves them, if there are none it waits one second and checks again. The sub keeps itself alive untill StopSaveThread is set to False
Its all just botched together, if you wan't a decent program you'd better start from scratch.
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Jul 20th, 2005, 12:02 PM
#30
Thread Starter
Junior Member
Re: Comparing Images using Icam
okay...thanks grilkip...just needed to understand that part..thanks!
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
|