Simd Library Documentation.

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

Contains Framework for motion detection. More...

Data Structures

class  Detector
 Class Detector. More...
 
struct  Event
 Event structure. More...
 
struct  Metadata
 Metadata structure. More...
 
struct  Model
 Model structure. More...
 
struct  Object
 Object structure. More...
 
struct  Options
 Options structure. More...
 
struct  Position
 Position structure. More...
 

Typedefs

typedef double Time
 Time type.
 
typedef int Id
 ID type.
 
typedef std::string String
 String type.
 
typedef Simd::Point< ptrdiff_t > Size
 screen 2D-size (width and height).
 
typedef Simd::Point< ptrdiff_t > Point
 screen point (x and y).
 
typedef std::vector< PointPoints
 Vector of screen 2D-points.
 
typedef Simd::Rectangle< ptrdiff_t > Rect
 Screen rectangle.
 
typedef Simd::Point< double > FSize
 ONVIF 2D-size (width and height). ONVIF size is restricted by range [0, 2].
 
typedef Simd::Point< double > FPoint
 ONVIF 2D-point (x and y). ONVIF coordinates are restricted by range [-1, 1].
 
typedef std::vector< FPointFPoints
 Vector of ONVIF 2D-points.
 
typedef Simd::View< Simd::AllocatorView
 Image type.
 
typedef Simd::Frame< Simd::AllocatorFrame
 Frame type.
 
typedef std::vector< PositionPositions
 Vector of object positions.
 
typedef std::vector< ObjectObjects
 Vector of objects.
 
typedef std::vector< EventEvents
 Vector of events.
 

Functions

SIMD_INLINE double ScreenToOnvifX (ptrdiff_t x, ptrdiff_t screenWidth)
 Converts screen X-coordinate to ONVIF X-coordinate. More...
 
SIMD_INLINE double ScreenToOnvifY (ptrdiff_t y, ptrdiff_t screenHeight)
 Converts screen Y-coordinate to 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 screen 2D-size to ONVIF 2D-size. More...
 
SIMD_INLINE ptrdiff_t OnvifToScreenX (double x, ptrdiff_t screenWidth)
 Converts ONVIF X-coordinate to screen X-coordinate. More...
 
SIMD_INLINE ptrdiff_t OnvifToScreenY (double y, ptrdiff_t screenHeight)
 Converts ONVIF Y-coordinate to 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 ONVIF 2D-size to screen 2D-size. More...
 
SIMD_INLINE String ToString (Id id)
 Converts ID to string. More...
 

Detailed Description

Contains Framework for motion detection.

Note
This is wrapper around low-level Motion Detection API.

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(0, 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;
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 provides text drawing.
Definition: SimdFont.hpp:64
size_t Height() const
Definition: SimdFont.hpp:131
bool Draw(View &canvas, const String &text, const Point &position, const Color &color) const
Definition: SimdFont.hpp:173
Class Detector.
Definition: SimdMotion.hpp:590
bool SetOptions(const Simd::Motion::Options &options)
Definition: SimdMotion.hpp:613
bool NextFrame(const Frame &input, Metadata &metadata, Frame *output=NULL)
Definition: SimdMotion.hpp:639
bool SetModel(const Model &model)
Definition: SimdMotion.hpp:625
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 line at the image.
Definition: SimdDrawing.hpp:47
SIMD_INLINE void DrawRectangle(View< A > &canvas, const Rectangle< ptrdiff_t > &rect, const Color &color, size_t width=1)
Draws a rectangle at the image.
Definition: SimdDrawing.hpp:201
SIMD_INLINE String ToString(Id id)
Converts ID to string.
Definition: SimdMotion.hpp:326
Contains Framework for motion detection.
Definition: SimdMotion.hpp:201
Simd::Point< double > FSize
ONVIF 2D-size (width and height). ONVIF size is restricted by range [0, 2].
Definition: SimdMotion.hpp:209
Simd::Point< ptrdiff_t > Point
screen point (x and y).
Definition: SimdMotion.hpp:206
Event structure.
Definition: SimdMotion.hpp:367
@ SabotageOff
A disappearing of too big motion on the screen.
Definition: SimdMotion.hpp:378
@ ObjectIn
An appearing of new object.
Definition: SimdMotion.hpp:375
@ ObjectOut
A disappearing of object.
Definition: SimdMotion.hpp:376
@ SabotageOn
An appearing of too big motion on the screen.
Definition: SimdMotion.hpp:377
Metadata structure.
Definition: SimdMotion.hpp:407
Events events
A list of events generated by Simd::Motion::Detector at current frame.
Definition: SimdMotion.hpp:409
Objects objects
A list of objects detected by Simd::Motion::Detector at current frame.
Definition: SimdMotion.hpp:408
Model structure.
Definition: SimdMotion.hpp:419
FSize size
A minimal size of object to detect. ONVIF size is restricted by range [0, 2].
Definition: SimdMotion.hpp:423
Object structure.
Definition: SimdMotion.hpp:353
Options structure.
Definition: SimdMotion.hpp:511
int DifferenceDxFeatureWeight
A weight of X-gradient feature for difference estimation. By default it is equal to 18.
Definition: SimdMotion.hpp:515
double ClassificationShiftMin
A minimal shift (in screen diagonals) of motion region to detect object. By default it is equal to 0....
Definition: SimdMotion.hpp:535
double TrackingAdditionalLinking
A coefficient to boost trajectory linking. By default it is equal to 0.
Definition: SimdMotion.hpp:532
double ClassificationTimeMin
A minimal life time (in seconds) of motion region to detect object. By default it is equal to 1 secon...
Definition: SimdMotion.hpp:536
int DifferenceDyFeatureWeight
A weight of Y-gradient feature for difference estimation. By default it is equal to 18.
Definition: SimdMotion.hpp:516
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:141