-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.cpp
101 lines (87 loc) · 2.06 KB
/
helpers.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
#include "helpers.h"
void smoothHist(IntVec & hist, int n) {
if (n <= 0)
return;
IntVec smooth;
smooth.resize(hist.size());
while (n) {
for (int i = 1; i < hist.size() - 1; i++) {
smooth[i] = hist[i-1] / 4 + hist[i+1] / 4 + hist[i] / 2;
}
smooth[0] = smooth[1];
smooth[hist.size() - 1] = smooth[hist.size() - 2];
hist = smooth;
n--;
}
}
void rectSubImg(cv::Mat & src, cv::Mat & dest, cv::Rect & rect) {
dest = src
.rowRange(cv::Range(rect.y, rect.y + rect.height))
.colRange(cv::Range(rect.x, rect.x + rect.width));
}
void imgHist(cv::Mat & img, IntVec & hHist, IntVec & vHist) {
cv::Mat grayImg;
cv::cvtColor(img, grayImg, CV_BGR2GRAY);
cv::equalizeHist(grayImg, grayImg);
vHist.clear();
hHist.clear();
vHist.resize(grayImg.rows);
hHist.resize(grayImg.cols);
for (int r = 0; r < grayImg.rows; r++) {
for (int c = 0; c < grayImg.cols; c++) {
vHist[r] += grayImg.at<unsigned char>(r, c);
hHist[c] += grayImg.at<unsigned char>(r, c);
}
}
vHist[0] = vHist[1];
hHist[0] = hHist[1];
vHist[vHist.size() - 1] = vHist[vHist.size() - 2];
hHist[hHist.size() - 1] = hHist[hHist.size() - 2];
}
void drawHist(cv::Mat & image, IntVec & hist, cv::Point start, char xDir, char yDir, int scale) {
cv::Point end;
for (int i = 0; i < hist.size(); i ++) {
end = start;
switch(yDir) {
case UP:
end.y -= hist[i] / scale;
break;
case DOWN:
end.y += hist[i] / scale;
break;
case LEFT:
end.x -= hist[i] / scale;
break;
case RIGHT:
end.x += hist[i] / scale;
break;
}
cv::line(image, start, end, CV_RGB(255, 255, 255));
switch(xDir) {
case UP:
start.y --;
break;
case DOWN:
start.y ++;
break;
case LEFT:
start.x --;
break;
case RIGHT:
start.x ++;
break;
}
}
}
void splitEyeRect(cv::Rect & eyes, cv::Rect & left, cv::Rect & right) {
int vTrim = eyes.height / 5, hTrim = eyes.width / 10;
eyes.y += vTrim;
eyes.height -= vTrim * 2;
left = eyes;
left.width /= 2;
right = left;
right.x += left.width;
right.x += hTrim;
left.width -= hTrim;
right.width -= hTrim;
}