|
|
|
|
@ -0,0 +1,89 @@
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <arm_neon.h>
|
|
|
|
|
|
|
|
|
|
// 应用可分离的高斯模糊
|
|
|
|
|
// src: 输入矩阵
|
|
|
|
|
// dst: 输出矩阵
|
|
|
|
|
// h: 矩阵高度
|
|
|
|
|
// w: 矩阵宽度
|
|
|
|
|
// kx: 水平方向卷积核
|
|
|
|
|
// ky: 垂直方向卷积核
|
|
|
|
|
void applySeparableGaussianBlur(float src[5][5], float dst[5][5], int h, int w, float kx[3], float ky[3]) {
|
|
|
|
|
// 临时缓冲区
|
|
|
|
|
float buf[5][5] = {0};
|
|
|
|
|
|
|
|
|
|
// 检查输入参数合法性
|
|
|
|
|
if (src == NULL || dst == NULL || kx == NULL || ky == NULL) {
|
|
|
|
|
printf("Error: NULL pointer passed to applySeparableGaussianBlur\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (h <= 0 || w <= 0) {
|
|
|
|
|
printf("Error: Invalid matrix size\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将卷积核加载到Neon寄存器
|
|
|
|
|
float32x4_t kx_vec = vld1q_f32(kx); // 加载kx
|
|
|
|
|
float32x4_t ky_vec = vld1q_f32(ky); // 加载ky
|
|
|
|
|
|
|
|
|
|
// 水平卷积
|
|
|
|
|
for (int i = 1; i < h - 1; i++) {
|
|
|
|
|
for (int j = 1; j < w - 1; j += 4) {
|
|
|
|
|
// 加载水平方向上的3个点
|
|
|
|
|
float32x4_t left = vld1q_f32(&src[i][j - 1]);
|
|
|
|
|
float32x4_t mid = vld1q_f32(&src[i][j]);
|
|
|
|
|
float32x4_t right = vld1q_f32(&src[i][j + 1]);
|
|
|
|
|
|
|
|
|
|
// 水平方向卷积
|
|
|
|
|
float32x4_t result = vmulq_lane_f32(left, vget_low_f32(kx_vec), 0); // kx[0] * left
|
|
|
|
|
result = vmlaq_lane_f32(result, mid, vget_low_f32(kx_vec), 1); // + kx[1] * mid
|
|
|
|
|
result = vmlaq_lane_f32(result, right, vget_high_f32(kx_vec), 0); // + kx[2] * right
|
|
|
|
|
|
|
|
|
|
vst1q_f32(&buf[i][j], result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 垂直卷积
|
|
|
|
|
for (int i = 1; i < h - 1; i++) {
|
|
|
|
|
for (int j = 1; j < w - 1; j++) {
|
|
|
|
|
// 加载垂直方向上的3个点
|
|
|
|
|
float top = buf[i - 1][j];
|
|
|
|
|
float mid = buf[i][j];
|
|
|
|
|
float bottom = buf[i + 1][j];
|
|
|
|
|
|
|
|
|
|
// 垂直方向卷积
|
|
|
|
|
float result = ky[0] * top + ky[1] * mid + ky[2] * bottom;
|
|
|
|
|
dst[i][j] = result; // 存储到dst矩阵
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
float src[5][5] = {
|
|
|
|
|
{0, 0, 0, 0, 0},
|
|
|
|
|
{0, 1, 2, 3, 0},
|
|
|
|
|
{0, 4, 5, 6, 0},
|
|
|
|
|
{0, 7, 8, 9, 0},
|
|
|
|
|
{0, 0, 0, 0, 0}
|
|
|
|
|
};
|
|
|
|
|
float dst[5][5] = {0};
|
|
|
|
|
float kx[3] = {0.25, 0.5, 0.25};
|
|
|
|
|
float ky[3] = {0.25, 0.5, 0.25};
|
|
|
|
|
|
|
|
|
|
clock_t start = clock();
|
|
|
|
|
applySeparableGaussianBlur(src, dst, 5, 5, kx, ky);
|
|
|
|
|
clock_t end = clock();
|
|
|
|
|
|
|
|
|
|
printf("矩阵结果:\n");
|
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
|
for (int j = 0; j < 5; j++) {
|
|
|
|
|
printf("%.2f ", dst[i][j]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
printf("步骤3运行时间:%f秒\n", (double)(end - start) / CLOCKS_PER_SEC);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|