-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathLib.h
124 lines (104 loc) · 2.99 KB
/
MathLib.h
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
#ifndef MATHLIB_Header_AD
#define MATHLIB_Header_AD
#pragma once
#include <vector>
#include <algorithm>
#include <numeric>
// stdev
template<typename T>
double getStDev(const std::vector<T> & inVec)
{
double sum = std::accumulate(std::begin(inVec), std::end(inVec), 0.0);
double mean = sum / inVec.size(); //mean
double accum = 0.0;
std::for_each(std::begin(inVec), std::end(inVec), [&](const double d) {
accum += (d - mean)*(d - mean);
});
double stdev = sqrt(accum / (inVec.size() - 1)); //·½std
return stdev;
}
// max
template<typename T>
T getMax(const std::vector<T> & inVec)
{
auto maxPosition = std::max_element(inVec.begin(), inVec.end());
return inVec[maxPosition - inVec.begin()];
}
// max
template<typename T>
T getMin(const std::vector<T> & inVec)
{
auto maxPosition = std::min_element(inVec.begin(), inVec.end());
return inVec[maxPosition - inVec.begin()];
}
// Sobel
template<typename T, typename T1>
void getSobel(const T* value, T1 * sobData, int cols, int rows)
{
T1 Gx;
T1 Gy;
for (int row = 1; row < rows - 1; ++row)
{
for (int col = 1; col < cols - 1; ++col)
{
Gx = value[(row - 1) * cols + col - 1] + 2 * value[(row - 1)* cols + col] + value[(row - 1) * cols + col + 1]
- value[(row + 1) * cols + col - 1] - 2 * value[(row + 1)* cols + col] - value[(row + 1) * cols + col + 1];
Gy = value[(row + 1) * cols + col + 1] + 2 * value[(row)* cols + col + 1] + value[(row - 1) * cols + col + 1]
- value[(row + 1) * cols + col - 1] - 2 * value[(row)* cols + col - 1] - value[(row - 1) * cols + col - 1];
sobData[row*cols + col] = abs(Gx) + abs(Gy);
}
}
}
// Laplace and Sobel, LS
template<typename T, typename T1>
void getSL(const T* value, T1 * LSData, int cols, int rows)
{
for (int row = 1; row < rows - 1; ++row)
{
for (int col = 1; col < cols - 1; ++col)
{
LSData[row*cols + col] = 4 * value[row*cols + col] - (value[(row + 1)*cols + col] + value[(row - 1)*cols + col] + value[row*cols + col + 1]
+ value[row *cols + col - 1]);
}
}
}
// Laplace
template<typename T, typename T1>
void getLaplacian(const T* value, T1 * lapData, int cols, int rows)
{
for (int row = 1; row < rows - 1; ++row)
{
for (int col = 1; col < cols - 1; ++col)
{
lapData[row*cols + col] = 4 * value[row*cols + col] - (value[(row + 1)*cols + col] + value[(row - 1)*cols + col] + value[row*cols + col + 1]
+ value[row *cols + col - 1]);
}
}
}
// Find the value in the data that is equal to the condition and get the value of the value that matches the first value
template<typename T, typename T1>
int getEqualValue(const T* inVal, const T& val, const size_t length, std::vector<T1>& tmpIndex)
{
int count = 0;
for (size_t i = 0; i < length; ++i)
{
if (inVal[i] == val)
{
++count;
tmpIndex.push_back(i);
}
}
return count;
}
// Reset array traversal to a value
template<typename T>
int resetValue(T* inVal, const T& val, const size_t length)
{
int count = 0;
for (size_t i = 0; i < length; ++i)
{
inVal[i] = val;
}
return 0;
}
#endif // !FUNCTIONS_Header_AD