forked from hybridgroup/gocv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objdetect.cpp
127 lines (95 loc) · 3.67 KB
/
objdetect.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "objdetect.h"
// CascadeClassifier
CascadeClassifier CascadeClassifier_New() {
return new cv::CascadeClassifier();
}
void CascadeClassifier_Close(CascadeClassifier cs) {
delete cs;
}
int CascadeClassifier_Load(CascadeClassifier cs, const char* name) {
return cs->load(name);
}
struct Rects CascadeClassifier_DetectMultiScale(CascadeClassifier cs, Mat img) {
std::vector<cv::Rect> detected;
cs->detectMultiScale(*img, detected); // uses all default parameters
Rect* rects = new Rect[detected.size()];
for (size_t i = 0; i < detected.size(); ++i) {
Rect r = {detected[i].x, detected[i].y, detected[i].width, detected[i].height};
rects[i] = r;
}
Rects ret = {rects, (int)detected.size()};
return ret;
}
struct Rects CascadeClassifier_DetectMultiScaleWithParams(CascadeClassifier cs, Mat img,
double scale, int minNeighbors, int flags, Size minSize, Size maxSize) {
cv::Size minSz(minSize.width, minSize.height);
cv::Size maxSz(maxSize.width, maxSize.height);
std::vector<cv::Rect> detected;
cs->detectMultiScale(*img, detected, scale, minNeighbors, flags, minSz, maxSz);
Rect* rects = new Rect[detected.size()];
for (size_t i = 0; i < detected.size(); ++i) {
Rect r = {detected[i].x, detected[i].y, detected[i].width, detected[i].height};
rects[i] = r;
}
Rects ret = {rects, (int)detected.size()};
return ret;
}
// HOGDescriptor
HOGDescriptor HOGDescriptor_New() {
return new cv::HOGDescriptor();
}
void HOGDescriptor_Close(HOGDescriptor hog) {
delete hog;
}
int HOGDescriptor_Load(HOGDescriptor hog, const char* name) {
return hog->load(name);
}
struct Rects HOGDescriptor_DetectMultiScale(HOGDescriptor hog, Mat img) {
std::vector<cv::Rect> detected;
hog->detectMultiScale(*img, detected);
Rect* rects = new Rect[detected.size()];
for (size_t i = 0; i < detected.size(); ++i) {
Rect r = {detected[i].x, detected[i].y, detected[i].width, detected[i].height};
rects[i] = r;
}
Rects ret = {rects, (int)detected.size()};
return ret;
}
struct Rects HOGDescriptor_DetectMultiScaleWithParams(HOGDescriptor hog, Mat img,
double hitThresh, Size winStride, Size padding, double scale, double finalThresh,
bool useMeanshiftGrouping) {
cv::Size wSz(winStride.width, winStride.height);
cv::Size pSz(padding.width, padding.height);
std::vector<cv::Rect> detected;
hog->detectMultiScale(*img, detected, hitThresh, wSz, pSz, scale, finalThresh,
useMeanshiftGrouping);
Rect* rects = new Rect[detected.size()];
for (size_t i = 0; i < detected.size(); ++i) {
Rect r = {detected[i].x, detected[i].y, detected[i].width, detected[i].height};
rects[i] = r;
}
Rects ret = {rects, (int)detected.size()};
return ret;
}
Mat HOG_GetDefaultPeopleDetector() {
return new cv::Mat(cv::HOGDescriptor::getDefaultPeopleDetector());
}
void HOGDescriptor_SetSVMDetector(HOGDescriptor hog, Mat det) {
hog->setSVMDetector(*det);
}
struct Rects GroupRectangles(struct Rects rects, int groupThreshold, double eps) {
std::vector<cv::Rect> vRect;
for (int i = 0; i < rects.length; ++i) {
cv::Rect r = cv::Rect(rects.rects[i].x, rects.rects[i].y, rects.rects[i].width,
rects.rects[i].height);
vRect.push_back(r);
}
cv::groupRectangles(vRect, groupThreshold, eps);
Rect* results = new Rect[vRect.size()];
for (size_t i = 0; i < vRect.size(); ++i) {
Rect r = {vRect[i].x, vRect[i].y, vRect[i].width, vRect[i].height};
results[i] = r;
}
Rects ret = {results, (int)vRect.size()};
return ret;
}