From 5481c0fcf9e1e843aaae34e5bec28837e96b427e Mon Sep 17 00:00:00 2001 From: pqzbgempc <2193876517@qq.com> Date: Fri, 6 Dec 2024 22:52:08 +0800 Subject: [PATCH] ADD file via upload --- GaussianBlur2.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 GaussianBlur2.cpp diff --git a/GaussianBlur2.cpp b/GaussianBlur2.cpp new file mode 100644 index 0000000..2fbc09f --- /dev/null +++ b/GaussianBlur2.cpp @@ -0,0 +1,50 @@ +#include +#include + +#define IDX(n)((n)%3) + +float inputImage[5][5] = { + {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} +}; + +float kx[3] = { 1.0f / 4,2.0f / 4,1.0f / 4 }; +float ky[3] = { 1.0f / 4,2.0f / 4,1.0f / 4 }; +float buf[3][3] = { 0 }; + +void applySeparableGaussianBlur(float src[][5], float dst[][5], int h, int w, float kx[], float ky[3]) { + for (int i = 0; i < 2; ++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) { + buf[IDX(i + 1)][j] = src[i + 1][j - 1] * kx[0] + src[i + 1][j] * kx[1] + src[i + 1][j + 1] * kx[2]; + } + + for (int j = 1; j < w - 1; ++j) { + dst[i][j] = buf[IDX(i - 1)][j] * ky[0] + buf[IDX(i)][j] * ky[1] + buf[IDX(i + 1)][j] * ky[2]; + } + } +} + +int main() { + float outputImage[5][5] = { 0 }; + clock_t start, end; + start = clock(); + applySeparableGaussianBlur(inputImage, outputImage, 5, 5, kx, ky); + end = clock(); + double timeSpent = (double)(end - start) / CLOCKS_PER_SEC; + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 5; j++) { + printf("%-4.1f ", outputImage[i][j]); + } + printf("\n"); + } + printf("The timeSpent is %1.10f second\n", timeSpent); +}