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/

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
default = Controls.AddTextBox(125,320)
defaults = LDImage.EffectDefaults
webcam = LDWebCam.Start(320,240)
Sub OnButtonClicked
  If (Controls.LastClickedButton = picture) Then
  ElseIf (Controls.LastClickedButton = pause) Then
  ElseIf (Controls.LastClickedButton = resume) Then
  ElseIf (Controls.LastClickedButton = end) Then
  ElseIf (Controls.LastClickedButton = effect) Then
    LDWebCam.Effect = Math.GetRandomNumber(21)-1
  ElseIf (Controls.LastClickedButton = setDefault) Then
    defaults[LDWebCam.Effect] = Controls.GetTextBoxText(default)
    LDImage.EffectDefaults = defaults
Sub OnComboBoxItemChanged
  LDWebCam.Effect = LDControls.LastComboBoxIndex


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/

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)
Timer.Interval = 1000
Timer.Tick = OnTick
Sub OnTick
  If (working = 0) Then
    working = 1
    img1 = LDWebCam.SnapshotToImageList()
    hist = LDImage.Histogram(img1)
    If (img2 <> "") Then
      img = LDImage.DifferenceImages(img1,img2)
      stats = LDImage.Statistics(img)
      'TextWindow.WriteLine(stats["STD"][1]+" , "+stats["STD"][2]+" , "+stats["STD"][3])
      Total = Math.Floor(stats["Mean"][1]+stats["Mean"][2]+stats["Mean"][3])
      If (Total > sensitivity) Then
    img2 = img1
    working = 0


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 (5)
  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!

  4. Program says:

    Hi ! Do you know if it’s possible to have the webcam video without the window ?

Comments are closed.

Skip to main content