Example Code

Motion Detection Using NI-Vision

Code and Documents



This VI demonstrates how you can use a camera as a means for motion detection. The basic idea here is to get a Boolean output to indicate whether there is motion in the frame or not. You can then use that Boolean output in other areas of your code. For example, you could make a VI that writes an AVI file, and only record frames when movement is detected.


To create this, we initially set up a Grab from a camera. Using low-level VIs, we can set the number of buffers that are allocated for the Grab. Inside the loop, we read out two images for each iteration. In this example, the frames have been specified to be 5 buffers apart. In other words, you will look at every fifth frame. The benefit of configuring the grab beforehand and reading images this way is that we can immediately access the images when we call the Read Image VI without having to wait for acquisition, since the acquisition is continuous.

Now we have two images that are separated in time by a small amount. The next step is to take the absolute value of the difference of these two images. This is a pixel-to-pixel operation. Any pixel values that are different between the two images will be brighter on the difference image. Typically the differences are fairly low values. So, for better visibility, it makes sense to multiply the result by a constant. In this VI, this constant is called "Pixel Gain". The Boolean decision of whether there is motion or not is determined by comparing the mean pixel value of the resulting image to a threshold value. This threshold value may need to be tweaked, based on the ambient conditions in your operating environment. This can be adjusted in this VI by using the control called "Mean Pixel Value Threshold".

Steps to Implement or Execute Code

  1. Select your camera from the IMAQ or IMAQdx Session control on the front panel
  2. Run the VI
  3. Set the Pixel Gain control to a constant that the pixels in the differential image will be multiplied by
  4. Set the Mean Pixel Value Threshold as a means of setting the sensitivity of the motion detector



LabVIEW 8.0 or later

Vision Acquisition Software

Versions for both IMAQ and IMAQdx have been included in the attachments


Compatible camera

Additional Images or Video


**This document has been updated to meet the current required format for the NI Code Exchange. For more details visit this discussion thread**

Wes Pierce
Principal Engineer
Pierce Controls

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

NI Employee (retired)

How you are acquiring two different images seems a little wonky. Your first time through, it will get the first available image for the first image, then get one N/2 (where N is the number of buffers used) in the future for the second image. However, the next time through you'll be acquiring the loop index again, which at this point might not be available and so you might just get the newest image. Then you'll be comparing it with an older one instead of a newer one.

I'd change the code to first get the "next" buffer so you get the newest image available regardless of how long your loop iterations take. Then I'd take the returned buffer number and subtract N/2 from it and get that image. This way you'll ensure you always have constant separation in time.



I am testing it with Labview 2013 and it seems the histogram always returns 0.

Has someone else experienced the same issue ?



NI Employee (retired)

To solve historigram returning a 0 you have to use "IMAQ ExtractSingleColorPlane VI" and extract Luminance.zzzzzzzzzzzzz.PNG

Applications Engineer
National Instruments

hi, great example, thank u


Thank you and thanks Art for your addition