Simd Library Documentation.

Home | Release Notes | Download | Documentation | Issues | GitHub

The Detection structure provides object detection with using of HAAR and LBP cascade classifiers. More...

#include <SimdDetection.hpp>

Data Structures

struct  Object
 The Object structure describes detected object. More...
 

Public Types

typedef A< uint8_t > Allocator
 
typedef Simd::View< A > View
 
typedef Simd::Point< ptrdiff_t > Size
 
typedef std::vector< SizeSizes
 
typedef Simd::Rectangle< ptrdiff_t > Rect
 
typedef std::vector< RectRects
 
typedef int Tag
 
typedef std::vector< ObjectObjects
 

Public Member Functions

 Detection ()
 
 ~Detection ()
 
bool LoadStringXml (const std::string &xml, Tag tag=UNDEFINED_OBJECT_TAG)
 
bool Load (const std::string &path, Tag tag=UNDEFINED_OBJECT_TAG)
 
bool Init (const Size &imageSize, double scaleFactor=1.1, const Size &sizeMin=Size(0, 0), const Size &sizeMax=Size(INT_MAX, INT_MAX), const View &roi=View(), ptrdiff_t threadNumber=-1)
 
bool Detect (const View &src, Objects &objects, int groupSizeMin=3, double sizeDifferenceMax=0.2, bool motionMask=false, const Rects &motionRegions=Rects())
 

Static Public Attributes

static const Tag UNDEFINED_OBJECT_TAG = -1
 

Detailed Description

template<template< class > class A>
struct Simd::Detection< A >

The Detection structure provides object detection with using of HAAR and LBP cascade classifiers.

Using example (face detection in the image):

#include "Simd/SimdDetection.hpp"
#include "Simd/SimdDrawing.hpp"
int main()
{
image.Load("../../data/image/face/lena.pgm");
Detection detection;
detection.Load("../../data/cascade/haar_face_0.xml");
detection.Init(image.Size());
detection.Detect(image, objects);
for (size_t i = 0; i < objects.size(); ++i)
Simd::DrawRectangle(image, objects[i].rect, uint8_t(255));
image.Save("result.pgm");
return 0;
}
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
The Detection structure provides object detection with using of HAAR and LBP cascade classifiers.
Definition: SimdDetection.hpp:158
bool Load(const std::string &path, Tag tag=UNDEFINED_OBJECT_TAG)
Definition: SimdDetection.hpp:263
std::vector< Object > Objects
Definition: SimdDetection.hpp:206
bool Detect(const View &src, Objects &objects, int groupSizeMin=3, double sizeDifferenceMax=0.2, bool motionMask=false, const Rects &motionRegions=Rects())
Definition: SimdDetection.hpp:313
Detection()
Definition: SimdDetection.hpp:211
bool Init(const Size &imageSize, double scaleFactor=1.1, const Size &sizeMin=Size(0, 0), const Size &sizeMax=Size(INT_MAX, INT_MAX), const View &roi=View(), ptrdiff_t threadNumber=-1)
Definition: SimdDetection.hpp:290
The View structure provides storage and manipulation of images.
Definition: SimdView.hpp:70
Point< ptrdiff_t > Size() const
Definition: SimdView.hpp:1074
bool Load(const std::string &path, Format format=None)
Definition: SimdView.hpp:1268
bool Save(const std::string &path, SimdImageFileType type=SimdImageFileUndefined, int quality=100) const
Definition: SimdView.hpp:1292

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

#include <iostream>
#include <string>
#include "opencv2/opencv.hpp"
#include "opencv2/core/utils/logger.hpp"
#ifndef SIMD_OPENCV_ENABLE
#define SIMD_OPENCV_ENABLE
#endif
#include "Simd/SimdDetection.hpp"
#include "Simd/SimdDrawing.hpp"
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;
}
int W = (int)capture.get(cv::CAP_PROP_FRAME_WIDTH);
int H = (int)capture.get(cv::CAP_PROP_FRAME_HEIGHT);
cv::VideoWriter writer;
if (output.size())
{
writer.open(output, cv::VideoWriter::fourcc('F', 'M', 'P', '4'), capture.get(cv::CAP_PROP_FPS), cv::Size(W, H));
if (!writer.isOpened())
{
std::cout << "Can't open output file '" << output << "' !" << std::endl;
return 1;
}
}
Detection detection;
detection.Load("../../data/cascade/haar_face_0.xml");
detection.Init(Detection::Size(W, H), 1.2, Detection::Size(W, H) / 20);
const char * WINDOW_NAME = "FaceDetection";
cv::namedWindow(WINDOW_NAME, 1);
for (;;)
{
cv::Mat frame;
if (!capture.read(frame))
break;
Detection::View image = frame;
detection.Detect(image, objects);
for (size_t i = 0; i < objects.size(); ++i)
Simd::DrawRectangle(image, objects[i].rect, Simd::Pixel::Bgr24(0, 255, 255));
cv::imshow(WINDOW_NAME, frame);
if (writer.isOpened())
writer.write(frame);
if (cv::waitKey(1) == 27)// "press 'Esc' to break video";
break;
}
return 0;
}
Simd::Point< ptrdiff_t > Size
Definition: SimdDetection.hpp:161
Simd::View< A > View
Definition: SimdDetection.hpp:160
24-bit BGR pixel.
Definition: SimdPixel.hpp:55
Note
This is wrapper around low-level Object Detection API.

Member Typedef Documentation

◆ Allocator

typedef A<uint8_t> Allocator

Allocator type definition.

◆ View

typedef Simd::View<A> View

An image type definition.

◆ Size

typedef Simd::Point<ptrdiff_t> Size

An image size type definition.

◆ Sizes

typedef std::vector<Size> Sizes

A vector of image sizes type definition.

◆ Rect

typedef Simd::Rectangle<ptrdiff_t> Rect

A rectangle type definition.

◆ Rects

typedef std::vector<Rect> Rects

A vector of rectangles type definition.

◆ Tag

typedef int Tag

A tag type definition.

◆ Objects

typedef std::vector<Object> Objects

A vector of objects type defenition.

Constructor & Destructor Documentation

◆ Detection()

Detection ( )

Creates a new empty Detection structure.

◆ ~Detection()

~Detection ( )

A Detection destructor.

Member Function Documentation

◆ LoadStringXml()

bool LoadStringXml ( const std::string &  xml,
Tag  tag = UNDEFINED_OBJECT_TAG 
)

Loads from file classifier cascade. Supports OpenCV HAAR and LBP cascades type. You can call this function more than once if you want to use several object detectors at the same time.

Note
Tree based cascades and old cascade formats are not supported!
Parameters
[in]xml- a string containing XML with cascade.
[in]tag- an user defined tag. This tag will be inserted in output Object structure.
Returns
a result of this operation.

◆ Load()

bool Load ( const std::string &  path,
Tag  tag = UNDEFINED_OBJECT_TAG 
)

Loads from file classifier cascade. Supports OpenCV HAAR and LBP cascades type. You can call this function more than once if you want to use several object detectors at the same time.

Note
Tree based cascades and old cascade formats are not supported!
Parameters
[in]path- a path to cascade.
[in]tag- an user defined tag. This tag will be inserted in output Object structure.
Returns
a result of this operation.

◆ Init()

bool Init ( const Size imageSize,
double  scaleFactor = 1.1,
const Size sizeMin = Size(0, 0),
const Size sizeMax = Size(INT_MAX, INT_MAX),
const View roi = View(),
ptrdiff_t  threadNumber = -1 
)

Prepares Detection structure to work with image of given size.

Parameters
[in]imageSize- a size of input image.
[in]scaleFactor- a scale factor. To detect objects of different sizes the algorithm uses many scaled image. This parameter defines size difference between neighboring images. This parameter strongly affects to performance.
[in]sizeMin- a minimal size of detected objects. This parameter strongly affects to performance.
[in]sizeMax- a maximal size of detected objects.
[in]roi- a 8-bit image mask which defines Region Of Interest. User can restricts detection region with using this mask. The mask affects to the center of detected object.
[in]threadNumber- a number of work threads. It useful for multi core CPU. Use value -1 to auto choose of thread number.
Returns
a result of this operation.

◆ Detect()

bool Detect ( const View src,
Objects objects,
int  groupSizeMin = 3,
double  sizeDifferenceMax = 0.2,
bool  motionMask = false,
const Rects motionRegions = Rects() 
)

Detects objects at given image.

Parameters
[in]src- a input image.
[out]objects- detected objects.
[in]groupSizeMin- a minimal weight (number of elementary detections) of detected image.
[in]sizeDifferenceMax- a parameter to group elementary detections.
[in]motionMask- an using of motion detection flag. Useful for dynamical restriction of detection region to addition to ROI.
[in]motionRegions- a set of rectangles (motion regions) to restrict detection region to addition to ROI. The regions affect to the center of detected object.
Returns
a result of this operation.

Field Documentation

◆ UNDEFINED_OBJECT_TAG

const Tag UNDEFINED_OBJECT_TAG = -1
static

The undefined object tag.