MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Harris.cpp
Go to the documentation of this file.
1#include "Harris.hpp"
2
3#include "Gradient.hpp"
4#include "ImageFilter.hpp"
5
7
8#ifdef MAYAFLUX_ARCH_X64
9#include <immintrin.h>
10#endif
11#ifdef MAYAFLUX_ARCH_ARM64
12#include <arm_neon.h>
13#endif
14
15namespace P = MayaFlux::Parallel;
16
18
19std::vector<float> harris_response(
20 std::span<const float> gray, uint32_t w, uint32_t h,
21 float k, float sigma)
22{
23 const size_t n = static_cast<size_t>(w) * h;
24
25 auto grad = sobel(gray, w, h);
26
27 std::vector<float> ixx(n), iyy(n), ixy(n);
28
29 P::for_each(P::par_unseq,
30 std::views::iota(size_t { 0 }, n).begin(),
31 std::views::iota(size_t { 0 }, n).end(),
32 [&](size_t i) {
33 const float dx = grad.dx[i];
34 const float dy = grad.dy[i];
35 ixx[i] = dx * dx;
36 iyy[i] = dy * dy;
37 ixy[i] = dx * dy;
38 });
39
40 auto sxx = gaussian_blur(ixx, w, h, sigma);
41 auto syy = gaussian_blur(iyy, w, h, sigma);
42 auto sxy = gaussian_blur(ixy, w, h, sigma);
43
44 std::vector<float> response(n);
45
46 P::for_each(P::par_unseq,
47 std::views::iota(size_t { 0 }, n).begin(),
48 std::views::iota(size_t { 0 }, n).end(),
49 [&](size_t i) {
50 const float det = sxx[i] * syy[i] - sxy[i] * sxy[i];
51 const float trace = sxx[i] + syy[i];
52 response[i] = std::max(0.0F, det - k * trace * trace);
53 });
54
55 const float peak = *std::ranges::max_element(response);
56 if (peak > 0.0F) {
57 const float inv = 1.0F / peak;
58 P::transform(P::par_unseq,
59 response.begin(), response.end(), response.begin(),
60 [inv](float v) { return v * inv; });
61 }
62
63 return response;
64}
65
67 std::span<const float> gray,
68 std::span<float> dx, std::span<float> dy, std::span<float> tmp,
69 std::span<float> ixx, std::span<float> iyy, std::span<float> ixy,
70 std::span<float> sxx, std::span<float> syy, std::span<float> sxy,
71 std::span<float> dst,
72 uint32_t w, uint32_t h,
73 float k,
74 std::span<const float> kern)
75{
76 const size_t n = static_cast<size_t>(w) * h;
77
78 sobel(gray, dx, dy, tmp, w, h);
79
80 {
81 const float* dx_ptr = dx.data();
82 const float* dy_ptr = dy.data();
83 float* ixx_ptr = ixx.data();
84 float* iyy_ptr = iyy.data();
85 float* ixy_ptr = ixy.data();
86
87 P::for_each(P::par_unseq,
88 std::views::iota(size_t { 0 }, n).begin(),
89 std::views::iota(size_t { 0 }, n).end(),
90 [&](size_t i) {
91 const float gx = dx_ptr[i];
92 const float gy = dy_ptr[i];
93 ixx_ptr[i] = gx * gx;
94 iyy_ptr[i] = gy * gy;
95 ixy_ptr[i] = gx * gy;
96 });
97 }
98
99 const float* h_src[3] = { ixx.data(), iyy.data(), ixy.data() };
100 float* h_dst[3] = { dx.data(), dy.data(), tmp.data() };
101 filter_horizontal_planes({ h_src, 3 }, { h_dst, 3 }, w, h, kern);
102
103 const float* v_src[3] = { dx.data(), dy.data(), tmp.data() };
104 float* v_dst[3] = { sxx.data(), syy.data(), sxy.data() };
105 filter_vertical_planes({ v_src, 3 }, { v_dst, 3 }, w, h, kern);
106
107 {
108 const float* sxx_ptr = sxx.data();
109 const float* syy_ptr = syy.data();
110 const float* sxy_ptr = sxy.data();
111 float* dst_ptr = dst.data();
112
113#ifdef MAYAFLUX_ARCH_X64
114 const __m256 k_vec = _mm256_set1_ps(k);
115 const __m256 zero = _mm256_setzero_ps();
116
117 size_t i = 0;
118 for (; i + 8 <= n; i += 8) {
119 const __m256 s0 = _mm256_loadu_ps(sxx_ptr + i);
120 const __m256 s1 = _mm256_loadu_ps(syy_ptr + i);
121 const __m256 s01 = _mm256_loadu_ps(sxy_ptr + i);
122
123 const __m256 det = _mm256_sub_ps(
124 _mm256_mul_ps(s0, s1),
125 _mm256_mul_ps(s01, s01));
126 const __m256 trace = _mm256_add_ps(s0, s1);
127 const __m256 resp = _mm256_max_ps(zero,
128 _mm256_fnmadd_ps(k_vec,
129 _mm256_mul_ps(trace, trace),
130 det));
131
132 _mm256_storeu_ps(dst_ptr + i, resp);
133 }
134 for (; i < n; ++i) {
135 const float s0 = sxx_ptr[i];
136 const float s1 = syy_ptr[i];
137 const float s01 = sxy_ptr[i];
138 const float det = s0 * s1 - s01 * s01;
139 const float trace = s0 + s1;
140 dst_ptr[i] = std::max(0.0F, det - k * trace * trace);
141 }
142
143#elif defined(MAYAFLUX_ARCH_ARM64)
144 const float32x4_t k_vec = vdupq_n_f32(k);
145 const float32x4_t zero = vdupq_n_f32(0.0F);
146
147 size_t i = 0;
148 for (; i + 4 <= n; i += 4) {
149 const float32x4_t s0 = vld1q_f32(sxx_ptr + i);
150 const float32x4_t s1 = vld1q_f32(syy_ptr + i);
151 const float32x4_t s01 = vld1q_f32(sxy_ptr + i);
152
153 const float32x4_t det = vsubq_f32(
154 vmulq_f32(s0, s1),
155 vmulq_f32(s01, s01));
156 const float32x4_t trace = vaddq_f32(s0, s1);
157 const float32x4_t resp = vmaxq_f32(zero,
158 vmlsq_f32(det, k_vec, vmulq_f32(trace, trace)));
159
160 vst1q_f32(dst_ptr + i, resp);
161 }
162 for (; i < n; ++i) {
163 const float s0 = sxx_ptr[i];
164 const float s1 = syy_ptr[i];
165 const float s01 = sxy_ptr[i];
166 const float det = s0 * s1 - s01 * s01;
167 const float trace = s0 + s1;
168 dst_ptr[i] = std::max(0.0F, det - k * trace * trace);
169 }
170
171#else
172 for (size_t i = 0; i < n; ++i) {
173 const float s0 = sxx_ptr[i];
174 const float s1 = syy_ptr[i];
175 const float s01 = sxy_ptr[i];
176 const float det = s0 * s1 - s01 * s01;
177 const float trace = s0 + s1;
178 dst_ptr[i] = std::max(0.0F, det - k * trace * trace);
179 }
180#endif
181 }
182}
183
184std::vector<Keypoint> extract_peaks(
185 std::span<const float> response, uint32_t w, uint32_t h,
186 float threshold, uint32_t nms_radius)
187{
188 const auto n = static_cast<size_t>(w) * h;
189 const auto r = static_cast<int32_t>(nms_radius);
190
191 std::vector<Keypoint> keypoints;
192
193 for (uint32_t py = 0; py < h; ++py) {
194 for (uint32_t px = 0; px < w; ++px) {
195 const size_t idx = static_cast<size_t>(py) * w + px;
196 const float val = response[idx];
197
198 if (val < threshold)
199 continue;
200
201 bool is_max = true;
202 for (int32_t dy = -r; dy <= r && is_max; ++dy) {
203 const int32_t ny = static_cast<int32_t>(py) + dy;
204 if (ny < 0 || ny >= static_cast<int32_t>(h))
205 continue;
206 for (int32_t dx = -r; dx <= r && is_max; ++dx) {
207 if (dx == 0 && dy == 0)
208 continue;
209 const int32_t nx = static_cast<int32_t>(px) + dx;
210 if (nx < 0 || nx >= static_cast<int32_t>(w))
211 continue;
212 const size_t ni = static_cast<size_t>(ny) * w + nx;
213 if (response[ni] >= val)
214 is_max = false;
215 }
216 }
217
218 if (!is_max)
219 continue;
220
221 keypoints.push_back({
222 .position = {
223 (static_cast<float>(px) + 0.5F) / static_cast<float>(w),
224 (static_cast<float>(py) + 0.5F) / static_cast<float>(h) },
225 .response = val,
226 .scale = 1.0F,
227 .angle = 0.0F,
228 });
229 }
230 }
231
232 std::ranges::sort(keypoints, [](const Keypoint& a, const Keypoint& b) {
233 return a.response > b.response;
234 });
235
236 return keypoints;
237}
238
239} // namespace MayaFlux::Kinesis::Vision
Gradient and edge detection on normalised float image spans.
Harris corner detection and non-maximum suppression.
2D spatial filtering on normalised float image spans.
uint32_t h
Definition InkPress.cpp:28
size_t a
size_t b
float threshold
uint32_t nms_radius
float k
float sigma
std::vector< Keypoint > extract_peaks(std::span< const float > response, uint32_t w, uint32_t h, float threshold, uint32_t nms_radius)
Extract peaks from a response map via non-maximum suppression.
Definition Harris.cpp:184
std::vector< float > harris_response(std::span< const float > gray, uint32_t w, uint32_t h, float k, float sigma)
Compute the Harris corner response map.
Definition Harris.cpp:19
void sobel(std::span< const float > gray, std::span< float > dx, std::span< float > dy, std::span< float > tmp, uint32_t w, uint32_t h)
Sobel gradient writing dx and dy into caller-supplied buffers.
Definition Gradient.cpp:66
void filter_horizontal_planes(std::span< const float *const > src, std::span< float *const > dst, uint32_t w, uint32_t h, std::span< const float > kernel)
Fused horizontal separable filter over N planes.
std::vector< float > gaussian_blur(std::span< const float > src, uint32_t w, uint32_t h, float sigma)
Separable Gaussian blur.
void filter_vertical_planes(std::span< const float *const > src, std::span< float *const > dst, uint32_t w, uint32_t h, std::span< const float > kernel)
Fused vertical separable filter over N planes.
double peak(const std::vector< double > &data)
Find peak amplitude in single-channel data.
Definition Yantra.cpp:268
Single interest point produced by a corner or blob detector.
Definition Features.hpp:60