Small Basic – WebCam Motion Detection


WebCam Extension

There has been a webcam object in the LitDev extension (LDWebCam) for a while, and I recently updated it so it works on WIndows 8 as well as earlier versions of Windows.

There are some effects like grayscale, snow, gamma, contrast, pixelate, fisheye, swirl etc.  The effects can also be applied to an image held in ImageList.

This is a basic sample of the webcam and effects (webcam/webcam.sb).

GraphicsWindow.Width = 400
GraphicsWindow.Height = 400
GraphicsWindow.BackgroundColor = LDColours.DarkGray
 
picture = Controls.AddButton("Take Picture", 20,350)
pause = Controls.AddButton("Pause", 125,350)
resume = Controls.AddButton("Resume", 190,350)
effect = Controls.AddButton("Effect", 270,350)
end = Controls.AddButton("Exit", 335,350)
setDefault = Controls.AddButton("Set Default", 20,320)
Controls.ButtonClicked = OnButtonClicked
 
combo = LDControls.AddComboBox(LDImage.GetEffects(), 100,200)
LDControls.ComboBoxItemChanged = OnComboBoxItemChanged
Shapes.Move(combo,270,320)
default = Controls.AddTextBox(125,320)
Controls.SetSize(default,125,24)
defaults = LDImage.EffectDefaults
 
webcam = LDWebCam.Start(320,240)
Shapes.Move(webcam,40,40)
 
Sub OnButtonClicked
  If (Controls.LastClickedButton = picture) Then
    LDWebCam.Snapshot()
  ElseIf (Controls.LastClickedButton = pause) Then
    LDWebCam.Pause()
  ElseIf (Controls.LastClickedButton = resume) Then
    LDWebCam.Resume()
  ElseIf (Controls.LastClickedButton = end) Then
    LDWebCam.End()
    Program.End()
  ElseIf (Controls.LastClickedButton = effect) Then
    LDWebCam.Effect = Math.GetRandomNumber(21)-1
    LDControls.ComboBoxSelect(combo,LDWebCam.Effect)
    Controls.SetTextBoxText(default,defaults[LDWebCam.Effect])
  ElseIf (Controls.LastClickedButton = setDefault) Then
    defaults[LDWebCam.Effect] = Controls.GetTextBoxText(default)
    LDImage.EffectDefaults = defaults
  EndIf
EndSub
 
Sub OnComboBoxItemChanged
  LDWebCam.Effect = LDControls.LastComboBoxIndex
  Controls.SetTextBoxText(default,defaults[LDWebCam.Effect])
EndSub

 

Motion Detection

A webcam image can be ‘grabbed’ at any point in time and stored as an ImageList image.  With the static ‘grabbed’ image we can do further image processing using methods in LDImage.

One of the more interesting options is LDImage.DifferenceImages which creates a new image with each pixel value equal to the modulus of the difference of the two input images.  In other words it is an image of ‘what has changed’.  We can then further process this image to identify motion detection and even where something is or how it is moving.

This is the source code and a screenshot of a sample motion detection program that also shows a histogram distribution of the image colours (webcam/motion-detection.sb).

GraphicsWindow.Width = 980
GraphicsWindow.Height = 280
GraphicsWindow.Title = "Motion Detection"
movement = Controls.AddTextBox(360,260)
sensitivity = 20
img1 = ""
img2 = ""
working = 0
 
graph = LDGraph.AddGraph(700,0,280,280,"Histogram","Value","Frequency")
webcam = LDWebCam.Start(320,240)
Shapes.Move(webcam,20,20)
 
Timer.Interval = 1000
Timer.Tick = OnTick
 
Sub OnTick
  If (working = 0) Then
    working = 1
    img1 = LDWebCam.SnapshotToImageList()
    hist = LDImage.Histogram(img1)
    LDUtilities.PauseUpdates()
    LDGraph.DeleteSeries(graph,"Red")
    LDGraph.AddSeriesLine(graph,"Red",hist[1],"Red")
    LDGraph.DeleteSeries(graph,"Green")
    LDGraph.AddSeriesLine(graph,"Green",hist[2],"Green")
    LDGraph.DeleteSeries(graph,"Blue")
    LDGraph.AddSeriesLine(graph,"Blue",hist[3],"Blue")
    LDUtilities.ResumeUpdates()
    If (img2 <> "") Then
      img = LDImage.DifferenceImages(img1,img2)
      stats = LDImage.Statistics(img)
      'TextWindow.WriteLine(stats["STD"][1]+" , "+stats["STD"][2]+" , "+stats["STD"][3])
      LDImage.Multiply(img,10,10,10)
      GraphicsWindow.DrawResizedImage(img,360,20,320,240)
      Total = Math.Floor(stats["Mean"][1]+stats["Mean"][2]+stats["Mean"][3])
      Controls.SetTextBoxText(movement,Total)
      If (Total > sensitivity) Then
        LDImage.Save(img1,Program.Directory+"\movement.jpg")
        Sound.PlayClickAndWait()
      EndIf
    EndIf
    LDImage.Remove(img2)
    img2 = img1
    LDImage.Remove(img)
    working = 0
  EndIf
EndSub

 

Working Example

A recent example using this technique was provided by Zock77 to identify movement and control a turret to aim a gun.  He used a very low resolution webcam image 16*16 pixels to simplify the calculation and identification of a moving target’s position. 

The image on the right is the current webcam ‘grabbed’ image with 16*16 resolution expanded to fill a larger area.  The image on the left is the ‘difference’ image showing movement since the initial image was reset (also expanded from 16*16 resolution), and the ‘cross hairs’ are the calculated center of the movement, which is used to control the turret using PPM (Pulse Position Moduation sound pulses and another extension).

The following 2 threads in the small Basic forum follow this development and there is also a YouTube video by Zock of it all working.

Pixelating an image?

Auto aiming sentry turret with Small Basic!!

Comments (3)

  1. I love the example from Zock! That's amazing!

  2. anonymouscommenter says:

    interesting application of your vision, you have attempted in other languages such as Matlab or Labview?

  3. Carlos, no the goal of Small Basic is that it's a programming language specifically made so that anyone can use it, including 8 year old kids!

Skip to main content