-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithms.h
108 lines (95 loc) · 3.19 KB
/
algorithms.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
#ifndef ALGORITHMS_H
#define ALGORITHMS_H
#include "matrix.h"
matrix<float, 5, 5> gaussian_kernel(float);
void magnitude(QImage&, const QImage&, const QImage&);
template<typename T>
QImage convolution(const T&, const QImage&);
// chanelIndex: 0 - L, 1 - a, 2 - b
template<typename T, typename Y>
Matrix<Y> convolution(const T& kernel, const Matrix<Y>& labChanel, int chanelIndex);
QImage canny(const QImage&, float, float, float);
QImage sobel(const QImage&);
QImage prewitt(const QImage&);
QImage roberts(const QImage&);
QImage scharr(const QImage&);
QImage hysteresis(const QImage&, float, float);
template<typename T>
QImage convolution(const T& kernel, const QImage& image)
{
int kw = kernel[0].size(), kh = kernel.size(),
offsetx = kw / 2, offsety = kw / 2;
QImage out(image.size(), image.format());
float sum;
quint8 *line;
const quint8 *lookup_line;
for (int y = 0; y < image.height(); y++)
{
line = out.scanLine(y);
for (int x = 0; x < image.width(); x++)
{
sum = 0;
for (int j = 0; j < kh; j++)
{
if (y + j < offsety || y + j >= image.height())
continue;
lookup_line = image.constScanLine(y + j - offsety);
for (int i = 0; i < kw; i++)
{
if (x + i < offsetx || x + i >= image.width())
continue;
sum += kernel[j][i] * lookup_line[x + i - offsetx];
}
}
line[x] = qBound(0x00, static_cast<int>(sum), 0xFF);
}
}
return out;
}
template<typename T, typename Y>
Matrix<Y> convolution(const T& kernel, const Matrix<Y>& labChanel, int chanelIndex)
{
int kw = kernel[0].size(), kh = kernel.size(),
offsetx = kw / 2, offsety = kw / 2;
Matrix<Y> out(labChanel.getStrSize(), labChanel.getClmnSize());
float sum;
for (int y = 0; y < labChanel.getStrSize(); y++)
{
for (int x = 0; x < labChanel.getClmnSize(); x++)
{
sum = 0;
for (int j = 0; j < kh; j++)
{
if (y + j < offsety || y + j >= labChanel.getClmnSize())
continue;
for (int i = 0; i < kw; i++)
{
if (x + i < offsetx || x + i >= labChanel.getStrSize())
continue;
sum += kernel[j][i] * labChanel[y + j - offsety][x + i - offsetx];
}
}
out[y][x] = sum;
switch (chanelIndex)
{
case 0:
if (sum < 0) out[y][x] = 0;
else if (sum > 100) out[y][x] = 100;
else out [y][x] = sum;
break;
case 1:
if (sum < -86.185) out[y][x] = -86.185;
else if (sum > 98.254) out[y][x] = 98.254;
else out [y][x] = sum;
break;
case 2:
if (sum < -107.863) out[y][x] = -107.863;
else if (sum > 94.482) out[y][x] = 94.482;
else out [y][x] = sum;
break;
}
}
}
return out;
}
#endif // ALGORITHMS_H