Simd Library Documentation.

Home | Release Notes | Download | Documentation | Issues | GitHub
Simd::Motion Namespace Reference

Contains a C++ framework for motion detection. More...

Data Structures

class  Detector
 Motion detector. More...
 
struct  Event
 An event generated by Simd::Motion::Detector on the current frame. More...
 
struct  Metadata
 Result of Detector::NextFrame for the current frame. More...
 
struct  Model
 Scene model used to calibrate Simd::Motion::Detector. More...
 
struct  Object
 A classified moving object on the current frame. More...
 
struct  Options
 Options used by Simd::Motion::Detector. More...
 
struct  Position
 Position of a detected object at one timestamp. More...
 

Typedefs

typedef double Time
 Time in seconds. Typical usage copies Frame::timestamp (OpenCV CAP_PROP_POS_MSEC * 0.001).
 
typedef int Id
 Object identifier. Object::id and Event::objectId; -1 if the event is not linked to an object.
 
typedef std::string String
 Text type. Event::text and the result of ToString().
 
typedef Simd::Point< ptrdiff_t > Size
 Screen size in pixels (width and height).
 
typedef Simd::Point< ptrdiff_t > Point
 Screen point in pixels (x and y). Origin is the top-left corner. Typical usage is Object::trajectory[].point and DrawLine.
 
typedef std::vector< Point > Points
 Vector of screen points.
 
typedef Simd::Rectangle< ptrdiff_t > Rect
 Screen rectangle in pixels. Typical usage is Object::rect for DrawRectangle.
 
typedef Simd::Point< double > FSize
 ONVIF size (width and height) in range [0, 2]. Model::size; default (0.1, 0.1) is about 0.25% of the screen area.
 
typedef Simd::Point< double > FPoint
 ONVIF point (x and y) in range [-1, 1]. Origin is the screen center; X grows right, Y grows up. Used by Model::roi.
 
typedef std::vector< FPoint > FPoints
 Vector of ONVIF points. Model::roi polygon.
 
typedef Simd::View< Simd::Allocator > View
 Image type. Typical usage wraps OpenCV cv::Mat as Frame input and annotates Object::rect on it.
 
typedef Simd::Frame< Simd::Allocator > Frame
 Video frame. Typical usage is Frame(image, false, timestampInSeconds) as NextFrame input.
 
typedef std::vector< Position > Positions
 Object::trajectory. Typical usage iterates from 1 to size()-1 and draws lines.
 
typedef std::vector< Object > Objects
 Metadata::objects: classified moving objects, including those that disappeared on this frame.
 
typedef std::vector< Event > Events
 Metadata::events of the current frame. Typical usage iterates them after objects.
 

Functions

SIMD_INLINE double ScreenToOnvifX (ptrdiff_t x, ptrdiff_t screenWidth)
 Converts a screen X-coordinate to an ONVIF X-coordinate. More...
 
SIMD_INLINE double ScreenToOnvifY (ptrdiff_t y, ptrdiff_t screenHeight)
 Converts a screen Y-coordinate to an ONVIF Y-coordinate. More...
 
SIMD_INLINE FPoint ScreenToOnvif (const Point &point, const Point &screenSize)
 Converts screen 2D-coordinates to ONVIF 2D-coordinates. More...
 
SIMD_INLINE FSize ScreenToOnvifSize (const Size &size, const Point &screenSize)
 Converts a screen 2D-size to an ONVIF 2D-size. More...
 
SIMD_INLINE ptrdiff_t OnvifToScreenX (double x, ptrdiff_t screenWidth)
 Converts an ONVIF X-coordinate to a screen X-coordinate. More...
 
SIMD_INLINE ptrdiff_t OnvifToScreenY (double y, ptrdiff_t screenHeight)
 Converts an ONVIF Y-coordinate to a screen Y-coordinate. More...
 
SIMD_INLINE Point OnvifToScreen (const FPoint &point, const Point &screenSize)
 Converts ONVIF 2D-coordinates to screen 2D-coordinates. More...
 
SIMD_INLINE Size OnvifToScreenSize (const FSize &size, const Point &screenSize)
 Converts an ONVIF 2D-size to a screen 2D-size. More...
 
SIMD_INLINE String ToString (Id id)
 Converts an object ID to a string. More...
 

Detailed Description

Contains a C++ framework for motion detection.

This is a wrapper around the low-level Motion Detection API.

Typical usage creates a Detector, optionally calls SetModel and SetOptions once, then for every video frame wraps a View as Frame with a timestamp in seconds and calls NextFrame. NextFrame fills Metadata with classified moving objects and events (Event::ObjectIn, Event::ObjectOut, Event::SabotageOn, Event::SabotageOff).

The example annotates Metadata on the input image: DrawRectangle around Object::rect, ToString(Object::id) above the box, DrawLine along Object::trajectory, and a scrolling list of Event::type / Event::objectId. Red marks objects that have an event in the same frame.

SetModel and SetOptions are typically called once before the video loop. Calibration (pyramid scale and ROI mask) runs on the first NextFrame and when the input size changes. The shooting-star example (disabled by #if 0) uses a small Model::size and lowered ClassificationShiftMin / ClassificationTimeMin. A Gray8 Model::mask can restrict ROI (non-zero pixels are inside). Debug annotation is drawn on an optional Bgr24 output Frame of the same size as input.

ONVIF coordinates place the origin at the screen center: X in [-1, 1] (left to right), Y in [-1, 1] with +Y up. Screen coordinates are pixels with (0, 0) at the top-left.

Using example (motion detection in the video captured by OpenCV):

#include <iostream>
#include <string>
#include <list>
#include "opencv2/opencv.hpp"
#include "opencv2/core/utils/logger.hpp"
#ifndef SIMD_OPENCV_ENABLE
#define SIMD_OPENCV_ENABLE
#endif
#include "Simd/SimdMotion.hpp"
using namespace Simd::Motion;
typedef std::list<Event> EventList;
typedef Simd::Pixel::Bgr24 Color;
const Color Red(0, 0, 255), Yellow(0, 255, 255), White(255, 255, 255);
void Annotate(const Metadata & metadata, const Simd::Font & font, EventList & events, View & image)
{
for (size_t i = 0; i < metadata.objects.size(); i++)
{
const Object & object = metadata.objects[i];
bool alarmed = false;
for (size_t j = 0; j < metadata.events.size(); ++j)
{
const Event & event = metadata.events[j];
if (event.objectId == object.id)
{
alarmed = true;
break;
}
}
Color color = alarmed ? Red : Yellow;
int width = alarmed ? 2 : 1;
Simd::DrawRectangle(image, object.rect, color, width);
font.Draw(image, ToString(object.id), Point(object.rect.left, object.rect.top - font.Height()), color);
for (size_t j = 1; j < object.trajectory.size(); ++j)
Simd::DrawLine(image, object.trajectory[j - 1].point, object.trajectory[j].point, color, width);
}
for (size_t i = 0; i < metadata.events.size(); ++i)
{
events.push_front(metadata.events[i]);
if (events.size()*font.Height() > image.height)
events.pop_back();
}
Point location;
for (EventList::const_iterator it = events.begin(); it != events.end(); ++it)
{
std::stringstream ss;
Color color = White;
switch (it->type)
{
ss << "in " << it->objectId;
break;
ss << "out " << it->objectId;
break;
ss << "SABOTAGE ON";
color = Red;
break;
ss << "SABOTAGE OFF";
color = Red;
break;
};
font.Draw(image, ss.str(), location, color);
location.y += font.Height();
}
}
int main(int argc, char * argv[])
{
if (argc < 2)
{
std::cout << "You have to set video source! It can be 0 for camera or video file name." << std::endl;
return 1;
}
std::string source = argv[1], output = argc > 2 ? argv[2] : "";
cv::VideoCapture capture;
cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_ERROR);
if (source == "0")
capture.open(0);
else
capture.open(source);
if (!capture.isOpened())
{
std::cout << "Can't capture '" << source << "' !" << std::endl;
return 1;
}
cv::VideoWriter writer;
if (output.size())
{
writer.open(output, cv::VideoWriter::fourcc('F','M','P','4'), capture.get(cv::CAP_PROP_FPS),
cv::Size((int)capture.get(cv::CAP_PROP_FRAME_WIDTH), (int)capture.get(cv::CAP_PROP_FRAME_HEIGHT)));
if (!writer.isOpened())
{
std::cout << "Can't open output file '" << output << "' !" << std::endl;
return 1;
}
}
EventList events;
Detector detector;
Simd::Font font((int)capture.get(cv::CAP_PROP_FRAME_HEIGHT) / 32);
#if 0
// There is an example of change of parameters to detect shooting star in the night sky:
Model model;
model.size = FSize(0.01, 0.01);
detector.SetModel(model);
Options options;
options.ClassificationShiftMin = 0.01;
options.ClassificationTimeMin = 0.01;
options.BackgroundStatUpdateTime = 0.2;
detector.SetOptions(options);
#endif
const char * WINDOW_NAME = "MotionDetector";
cv::namedWindow(WINDOW_NAME, 1);
for (;;)
{
cv::Mat frame;
if (!capture.read(frame))
break;
View image = frame;
Frame input(image, false, capture.get(cv::CAP_PROP_POS_MSEC) * 0.001);
Metadata metadata;
detector.NextFrame(input, metadata);
Annotate(metadata, font, events, image);
cv::imshow(WINDOW_NAME, frame);
if (writer.isOpened())
writer.write(frame);
if (cv::waitKey(1) == 27)// "press 'Esc' to break video";
break;
}
return 0;
}
The Font class is a C++ wrapper for drawing ASCII text on images.
Definition: SimdFont.hpp:91
size_t Height() const
Definition: SimdFont.hpp:154
bool Draw(View &canvas, const String &text, const Point &position, const Color &color) const
Definition: SimdFont.hpp:204
Motion detector.
Definition: SimdMotion.hpp:712
bool SetOptions(const Simd::Motion::Options &options)
Definition: SimdMotion.hpp:744
bool NextFrame(const Frame &input, Metadata &metadata, Frame *output=NULL)
Definition: SimdMotion.hpp:786
bool SetModel(const Model &model)
Definition: SimdMotion.hpp:760
SIMD_INLINE void DrawLine(View< A > &canvas, ptrdiff_t x1, ptrdiff_t y1, ptrdiff_t x2, ptrdiff_t y2, const Color &color, size_t width=1)
Draws a clipped line segment on an image.
Definition: SimdDrawing.hpp:54
SIMD_INLINE void DrawRectangle(View< A > &canvas, ptrdiff_t left, ptrdiff_t top, ptrdiff_t right, ptrdiff_t bottom, const Color &color, size_t width=1)
Draws a clipped rectangle frame on an image.
Definition: SimdDrawing.hpp:107
SIMD_INLINE String ToString(Id id)
Converts an object ID to a string.
Definition: SimdMotion.hpp:379
Contains a C++ framework for motion detection.
Definition: SimdMotion.hpp:225
Simd::Point< double > FSize
ONVIF size (width and height) in range [0, 2]. Model::size; default (0.1, 0.1) is about 0....
Definition: SimdMotion.hpp:233
Simd::Point< ptrdiff_t > Point
Screen point in pixels (x and y). Origin is the top-left corner. Typical usage is Object::trajectory[...
Definition: SimdMotion.hpp:230
An event generated by Simd::Motion::Detector on the current frame.
Definition: SimdMotion.hpp:436
@ SabotageOff
Motion area fell back below Options::StabilityRegionAreaMax. text is "SabotageOff"; objectId is -1.
Definition: SimdMotion.hpp:447
@ ObjectIn
A new object was classified as moving. text is "ObjectIn"; objectId is Object::id.
Definition: SimdMotion.hpp:444
@ ObjectOut
A classified object disappeared. text is "ObjectOut"; objectId is Object::id.
Definition: SimdMotion.hpp:445
@ SabotageOn
Motion area exceeded Options::StabilityRegionAreaMax. text is "SabotageOn"; objectId is -1.
Definition: SimdMotion.hpp:446
Result of Detector::NextFrame for the current frame.
Definition: SimdMotion.hpp:482
Events events
Events generated at the current frame. NextFrame clears this list before filling it.
Definition: SimdMotion.hpp:484
Objects objects
Classified moving objects at the current frame. Typical usage draws rect, id and trajectory.
Definition: SimdMotion.hpp:483
Scene model used to calibrate Simd::Motion::Detector.
Definition: SimdMotion.hpp:503
FSize size
Minimum object size in ONVIF units [0, 2]. Default (0.1, 0.1). Shooting-star example uses (0....
Definition: SimdMotion.hpp:507
A classified moving object on the current frame.
Definition: SimdMotion.hpp:414
Options used by Simd::Motion::Detector.
Definition: SimdMotion.hpp:615
int DifferenceDxFeatureWeight
Weight of the X-gradient feature in difference estimation. Default 18. 0 disables it (shooting-star e...
Definition: SimdMotion.hpp:619
double BackgroundStatUpdateTime
Interval (seconds) of background statistics updates in normal mode. Default 0.04. Shooting-star examp...
Definition: SimdMotion.hpp:625
double ClassificationShiftMin
Minimum displacement (in screen diagonals) to classify a track as a moving object....
Definition: SimdMotion.hpp:639
double TrackingAdditionalLinking
Extra border (fraction of region size) when linking a region to a track. Default 0....
Definition: SimdMotion.hpp:636
double ClassificationTimeMin
Minimum lifetime (seconds) to classify a track as a moving object. Default 1 second....
Definition: SimdMotion.hpp:640
int DifferenceDyFeatureWeight
Weight of the Y-gradient feature in difference estimation. Default 18. 0 disables it (shooting-star e...
Definition: SimdMotion.hpp:620
24-bit BGR pixel.
Definition: SimdPixel.hpp:55
T y
Specifies the y-coordinate of a point.
Definition: SimdPoint.hpp:76
const size_t height
A height of the image.
Definition: SimdView.hpp:143