Results 1 to 30 of 30

Thread: Comparing Images using Icam

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    24

    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!

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Comparing Images using Icam

    Quote 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:
    1. Me.picStill.Image = myCam.copyFrame(Me.picOutput, New RectangleF(0, 0, _
    2.                             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:
    1. For x = 0 To myImage.Width - 1
    2.             For y = 0 To myImage.Height - 1
    3.                 Print(CType(myImage.GetPixel(x, y).ToArgb, Integer))
    4.             Next
    5.         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

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Comparing Images using Icam

    Quote 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.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    24

    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.

  5. #5
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    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

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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:
    1. Public Function SomethingMoved(BitmapA as bitmap, BitmapB as Bitmap) as boolean
    2.  
    3. dim x as integer = 0
    4. dim y as integer = 0
    5. dim ChangedPixelCount = 0
    6.  
    7. 'assume both images are the same size
    8. for y = 2 to BitmapA.Height step 5
    9.     for x = 2 to BitmapA.Width step 5
    10.         If BitmapA.GetPixel(x, y) <> BitmapB.GetPixel(x, y) then
    11.             ChangedPixelCount += 1
    12.         End If
    13.     Next x
    14. Next y
    15.  
    16. If ChangedPixelCount >= 5 then
    17.     'yes, motion detected
    18.     return true
    19. else
    20.     'nope.
    21.     return false
    22. End if
    23.  
    24. End Function
    I don't live here any more.

  7. #7
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Comparing Images using Icam

    Quote 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.

  8. #8
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: Comparing Images using Icam

    Quote 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

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    24

    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.

  10. #10
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  11. #11
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    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:
    1. Private Function GetDiff(ColorA As Color, ColorB As Color) As Integer
    2.    Return Math.Abs(ColorA.R - ColorB.R) + _
    3.              Math.Abs(ColorA.G - ColorB.G) + _
    4.              Math.Abs(ColorA.B - ColorB.B)
    5. 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

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    24

    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?

  13. #13
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    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:
    1. ' you might need to convert those components to Integers first to prevent errors.
    2. 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

  14. #14
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  15. #15
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    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

  16. #16
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  17. #17
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    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

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    24

    Re: Comparing Images using Icam

    Quote 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.

  19. #19
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    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

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    24

    Re: Comparing Images using Icam

    hey thanks for posting your project..just what i wanted to check out..thanks!

  21. #21
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    24

    Re: Comparing Images using Icam

    it seems a bit too sensitive..nevertheless i like it...thanks so much!

  23. #23
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Comparing Images using Icam

    Raise the threshold a bit.
    I don't live here any more.

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    24

    Re: Comparing Images using Icam

    ah yeah...thanks wossname!

  25. #25
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    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

  26. #26

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    24

    Re: Comparing Images using Icam

    do you think you could explain the algorithm for your coding?

  27. #27
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    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

  28. #28

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    24

    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

  29. #29
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    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

  30. #30

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    24

    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
  •  



Click Here to Expand Forum to Full Width