-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsobel.cpp
76 lines (59 loc) · 1.43 KB
/
sobel.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
#include <stdint.h>
#include "sobel.hpp"
static inline float getpx(float * buf, int x, int y)
{
return buf[y * 640 + x];
}
static inline float kernel2(float * buf, int x, int y)
{
constexpr float gx[] = {
1, 0, -1, /* fr0 , _ , xf12 */
2, 0, -2, /* fr1 , _ , xf13 */
1, 0, -1, /* fr2, _ , xf14 */
};
constexpr float gy[] = {
1, 2, 1, /* fr0, fr1, fr2 */
0, 0, 0,
-1, -2, -1, /* fr4, fr5, fr6 */
};
float a = getpx(buf, x - 1, y - 1);
float b = getpx(buf, x , y - 1);
float c = getpx(buf, x + 1, y - 1);
float d = getpx(buf, x - 1, y );
float e = getpx(buf, x , y );
float f = getpx(buf, x + 1, y );
float g = getpx(buf, x - 1, y + 1);
float h = getpx(buf, x , y + 1);
float i = getpx(buf, x + 1, y + 1);
float sx = 0;
float sy = 0;
sx += a * gx[0];
//sx += b * gx[1];
sx += c * gx[2];
sx += d * gx[3];
//sx += e * gx[4];
sx += f * gx[5];
sx += g * gx[6];
//sx += h * gx[7];
sx += i * gx[8];
sy += a * gy[0];
sy += b * gy[1];
sy += c * gy[2];
//sy += d * gy[3];
//sy += e * gy[4];
//sy += f * gy[5];
sy += g * gy[6];
sy += h * gy[7];
sy += i * gy[8];
return sx * sx + sy * sy;
}
void convolve(float * in, uint32_t * out)
{
for (int y = 1; y < 480 - 1; y++) {
for (int x = 1; x < 640 - 1; x++) {
float c = kernel2(in, x, y);
int d = c > 100.f ? 0 : 0xffffffff;
out[y * 640 + x] = d;
}
}
}