-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathharris.cl
149 lines (119 loc) · 4.61 KB
/
harris.cl
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
__constant sampler_t reflect_sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_MIRRORED_REPEAT | CLK_FILTER_NEAREST;
__constant sampler_t clamp_sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
// Converts ARGB image into a greyscale image
__kernel void Argb32ToFloat (
__read_only image2d_t src,
__write_only image2d_t dest) {
const int2 pos = {get_global_id(0), get_global_id(1)};
const float4 in = read_imagef(src, clamp_sampler, pos);
const float4 out = (float4)(in.x * 0.2126f + in.y * 0.7152f + in.z * 0.0722f);
write_imagef(dest, pos, out);
}
// Runs a gaussian smoothing kernel over an image
__kernel void Smoothing (
__read_only image2d_t src,
__constant float* filterWeights,
__write_only image2d_t dest) {
const int2 pos = {get_global_id(0), get_global_id(1)};
float4 sum = (float4)(0.0f);
int i = 0;
for (int y = -HALF_SMOOTHING; y <= HALF_SMOOTHING; ++y) {
for (int x = -HALF_SMOOTHING; x <= HALF_SMOOTHING; ++x) {
sum += filterWeights[i] * read_imagef(src, reflect_sampler, pos + (int2)(x,y));
++i;
}
}
write_imagef(dest, pos, sum);
}
// Computes dx image
__kernel void DiffX (
__read_only image2d_t src,
__write_only image2d_t dest) {
const int2 pos = {get_global_id(0), get_global_id(1)};
float4 sum = read_imagef(src, reflect_sampler, pos - (int2)(1,0)) - read_imagef(src, reflect_sampler, pos + (int2)(1,0));
write_imagef(dest, (int2)(pos.x, pos.y), sum);
}
// Computes dy image
__kernel void DiffY (
__read_only image2d_t src,
__write_only image2d_t dest) {
const int2 pos = {get_global_id(0), get_global_id(1)};
float4 sum = read_imagef(src, reflect_sampler, pos - (int2)(0,1)) - read_imagef(src, reflect_sampler, pos + (int2)(0,1));
write_imagef (dest, (int2)(pos.x, pos.y), sum);
}
// Computes the structure tensor image from dx and dy images
__kernel void Structure (
__read_only image2d_t i_x,
__read_only image2d_t i_y,
__write_only image2d_t dest) {
const int2 pos = {get_global_id(0), get_global_id(1)};
float4 s = (float4)(0.0f);
for (int y = -HALF_STRUCTURE; y <= HALF_STRUCTURE; ++y) {
for (int x = -HALF_STRUCTURE; x <= HALF_STRUCTURE; ++x) {
const int2 window_pos = pos + (int2)(x,y);
const float s_x = read_imagef(i_x, reflect_sampler, window_pos).x;
const float s_y = read_imagef(i_y, reflect_sampler, window_pos).x;
s.x += s_x * s_x;
s.y += s_y * s_y;
s.z += s_x * s_y;
}
}
write_imagef(dest, pos, s);
}
// Computes the Harris response from the structure tensor image
__kernel void Response (
__read_only image2d_t src,
__write_only image2d_t dest) {
const int2 pos = {get_global_id(0), get_global_id(1)};
const float4 s = read_imagef(src, reflect_sampler, pos);
const float4 r;
r.x = (s.x * s.y - s.z * s.z) - HARRIS_K * (s.x + s.y) * (s.x + s.y);
write_imagef(dest, pos, r);
}
// Find the max value of each row and places it in a row_max_array
__kernel void RowMax (
__read_only image2d_t src,
__global float* row_max_values) {
const int row = get_global_id(0);
const int width = get_image_width(src);
float row_max = 0.0f;
for (int x = 0; x < width; ++x) {
const int2 pos = {x, row};
const float4 s = read_imagef(src, reflect_sampler, pos);
row_max = max(s.x, row_max);
}
row_max_values[row] = row_max;
}
// Finds the max value of an array (Only useful for smallish arrays)
__kernel void Max (
int length,
__global float* row_max_values) {
float row_max = 0.0f;
for (int i = 0; i < length; ++i) {
row_max = max(row_max_values[i], row_max);
}
row_max_values[0] = row_max;
}
// Runs non-maximal suppression with a global minimum threshold
__kernel void NonMaxSuppression (
__read_only image2d_t src,
__constant float* src_max,
__write_only image2d_t dest) {
float threshold = src_max[0] * THRESHOLD_RATIO;
const int2 pos = {get_global_id(0), get_global_id(1)};
const float4 max = read_imagef(src, clamp_sampler, pos);
if (max.x < threshold) {
write_imagef(dest, pos, (float4)0.0f);
return;
}
for (int y = -HALF_SUPPRESSION; y <= HALF_SUPPRESSION; y++) {
for (int x = -HALF_SUPPRESSION; x <= HALF_SUPPRESSION; ++x) {
const float4 r = read_imagef(src, reflect_sampler, pos + (int2)(x,y));
if (r.x > max.x) {
write_imagef(dest, pos, (float4)0.0f);
return;
}
}
}
write_imagef(dest, pos, max);
}