From 8c9e763b22832afc2818aa3eadee05185daa76e2 Mon Sep 17 00:00:00 2001 From: p7cx2jokr <2160925370@qq.com> Date: Sat, 30 Nov 2024 23:22:54 +0800 Subject: [PATCH] ADD file via upload --- 2.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 2.cpp diff --git a/2.cpp b/2.cpp new file mode 100644 index 0000000..242eb0b --- /dev/null +++ b/2.cpp @@ -0,0 +1,51 @@ +#include +#include +#include + +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}; + + //开始进行可分离卷积 + for (int i = 1; i < h - 1; i++) { + // 计算当前行的行内卷积 + for (int j = 1; j < w - 1; j++) { + buf[i][j] = src[i][j-1]*kx[0] + src[i][j]*kx[1] + src[i][j+1]*kx[2]; + } + } + + // 进行行间的卷积得到最终像素值 + for (int i = 1; i < h - 1; i++) { + for (int j = 1; j < w - 1; j++) { + dst[i][j] = buf[i-1][j]*ky[0] + buf[i][j]*ky[1] + buf[i+1][j]*ky[2]; + } + } +} + +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("步骤2运行时间:%f秒\n", (double)(end - start) / CLOCKS_PER_SEC); + + return 0; +}