You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <stdio.h>
#include <time.h>
// 应用3x3 GaussianBlur到给定的二维数组src上并将结果存到二维数组dst中
void applyGaussianBlur(float src[][5], float dst[][5], int h, int w, float kernel[3][3]) {
int i, j; // 在函数开头声明循环变量符合C89要求
for (i = 1; i < h - 1; ++i) {
for (j = 1; j < w - 1; ++j) {
dst[i][j] =
src[i - 1][j - 1] * kernel[0][0] + src[i - 1][j] * kernel[0][1] + src[i - 1][j + 1] * kernel[0][2] +
src[i][j - 1] * kernel[1][0] + src[i][j] * kernel[1][1] + src[i][j + 1] * kernel[1][2] +
src[i + 1][j - 1] * kernel[2][0] + src[i + 1][j] * kernel[2][1] + src[i + 1][j + 1] * kernel[2][2];
}
}
}
int main() {
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 kernel[3][3] = {
{1.0f / 16, 2.0f / 16, 1.0f / 16},
{2.0f / 16, 4.0f / 16, 2.0f / 16},
{1.0f / 16, 2.0f / 16, 1.0f / 16}
};
float outputImage[5][5];
int i, j; // 在函数开头声明循环变量符合C89要求
// 计时开始
clock_t start_time = clock();
applyGaussianBlur(inputImage, outputImage, 5, 5, kernel);
// 计时结束
clock_t end_time = clock();
double elapsed_time = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
// 输出结果矩阵
printf("经过GaussianBlur后的图像矩阵\n");
for (i = 0; i < 5; ++i) {
for (j = 0; j < 5; ++j) {
printf("%.2f ", outputImage[i][j]);
}
printf("\n");
}
// 输出运行时间
printf("运行时间:%.6f 秒\n", elapsed_time);
return 0;
}