How to implement loitering in C#

This article shows you how to detect when an object enters a specified region of interest (ROI) for a specified period of time within camera's view. With creating a ROI on an Onvif IP Camera video stream you will able to monitor the incoming objects and measure the time how long these objects spend in this area.

What does loitering means?

Loitering is the act of remaining in a particular public place for a protracted time without an apparent purpose. Under certain circumstances, it is illegal in various jurisdictions. For example you can distinguish parking vehicles from the ones who just stopped in an area where parking is forbidden. Before you read how to detect loitering, you have to be able implementing ROI, blob detection and handle enter and exit events so be sure to check our previous tutorials if you are not familiar with any of these. You can learn more about these topics in the How to implement object tracking page.

Loitering Detection

loitering detected after the preset loitering time exceded
Figure 1 - Loitering detected at the number 1 object after 30 minutes of loiter time

You need to find all of the foreground objects and recognize them as individual objects. A blob has a position, width and height. Your application should put the blobs into a list as they appear. After an object enters in the ROI, they get a unique identifier one by one, which remains the same while they stay in the intrested region. You should also start the identifiers own private timer. This way you can measure how long it stays in the ROI (Figure 1). When an object's minimum loiter timer elapsed you will be able to react in many different ways (alarm call, take snapshot, capture video).

Set loitering parameters

Each facility and industry may have a different definition of what entails "loitering," as such loitering algorithms typically provide a set of parameters that can be adjusted based on the scene or the specific needs of a particular customer. These parameters include items such as:

  • Minimum targets:

    For many situations a single or two loitering persons can be ignored. However, in some cases, such as in banks, loitering becomes more of an interest when it exceeds a defined number of people in a specific region.

  • Minimum loiter time:

    For many cases (e.g. in parking lots) there is a time limit until there is no reaction needed (e.g. 30 minutes of free parking is given to every visitors in a hospital). With this parameter you can control the trigger of the alarm and set up a time limit after it alerts or reacts.

  • Region of interest:

    Loitering algorithms typically have the ability to be applied to the entire scene, or just to a portion of the scene. Different regions of interest may also have different loitering parameters thereby distinguishing more or less important areas.

  • Real speed / Resting times:

    When applying loitering to a large scene with many targets, the act of loitering may better manifest itself as those objects that are, in general, moving at a slower speed, or coming to rest at a greater frequency for example someone taking just a few steps before a shop window then staring for minutes before taking some steps again.

Related Pages


Below you can find the answers for the most frequently asked questions related to this topic:

  1. How can I get the URL of the camera?

    You can get the URL from the producer of the camera. (In the 10th tutorial you can find information on how to create an own IP camera discoverer program.)

More information