forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetector.cpp
More file actions
64 lines (53 loc) · 1.77 KB
/
Detector.cpp
File metadata and controls
64 lines (53 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "Detector.h"
CDetector::CDetector(cv::Mat& gray)
{
m_fg = gray.clone();
m_bs = std::make_unique<BackgroundSubtract>(gray.channels());
m_bs->init(gray);
m_minObjectSize.width = std::max(5, gray.cols / 100);
m_minObjectSize.height = m_minObjectSize.width;
}
CDetector::~CDetector(void)
{
}
void CDetector::SetMinObjectSize(cv::Size minObjectSize)
{
m_minObjectSize = minObjectSize;
}
//----------------------------------------------------------------------
// Detector
//----------------------------------------------------------------------
void CDetector::DetectContour(cv::Mat& img, std::vector<cv::Rect>& Rects,std::vector<cv::Point2d>& centers, cv::Rect& croppedRect)
{
m_rects.clear();
m_centers.clear();
std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
cv::Mat edges=img.clone();
//Canny(img, edges, 50, 190, 3);
cv::findContours(edges,contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
if(contours.size()>0)
{
for(int i = 0; i < (int)contours.size(); i++)
{
cv::Rect r=cv::boundingRect(contours[i]);
cv::Point tl = croppedRect.tl()+r.tl();
cv::Point br = croppedRect.tl()+r.br();
r=cv::Rect(tl,br);
m_rects.push_back(r);
m_centers.push_back((r.br()+r.tl())*0.5);
}
}
}
std::pair<std::vector<cv::Point2d>, std::vector<cv::Rect> > CDetector::Detect(cv::Mat& gray, cv::Rect &croppedRect)
{
//m_bs->subtract(gray, m_fg);
cv::dilate(gray, m_fg, getStructuringElement(cv::MORPH_RECT, cv::Size(15,15)));
//imshow("Foreground",fg);
DetectContour(m_fg,m_rects,m_centers,croppedRect);
return make_pair(m_centers,m_rects);
}
const std::vector<cv::Rect>& CDetector::GetDetects() const
{
return m_rects;
}