From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

Motion Detection Using NI-Vision

Code and Documents

Attachment

Overview

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.

Description

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

Requirements

Software

LabVIEW 8.0 or later

Vision Acquisition Software

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

Hardware

Compatible camera

Additional Images or Video

MDBlockDiagram.png

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

Comments
BlueCheese
NI Employee (retired)
on

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.

FrancoisXavier
Member
Member
on

Hi,

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

Has someone else experienced the same issue ?

lkakavas
Member
Member
on

Yep.

ArtG
NI Employee (retired)
on

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

Art
Applications Engineer
National Instruments
juangl91
Member
Member
on

hi, great example, thank u

shalabyer
Member
Member
on

Thank you and thanks Art for your addition

Contributors