Merge pull request '合并' (#3) from pmfsq5yrv/opcomplex:main into main

main
pi7mcrg2k 8 months ago
commit e8db24479c

@ -0,0 +1,58 @@
#include <stdio.h>
#include <time.h>
void gaussianBlur(float src[5][5], float dst[5][5], int h, int w, float kernel[3][3]) {
int i,j, ki, kj;
for (i = 1; i < h - 1; i++) {
for (j = 1; j < w - 1; j++) {
float sum = 0.0;
for (ki = -1; ki <= 1; ki++) {
for (kj = -1; kj <= 1; kj++) {
sum += src[i + ki][j + kj] * kernel[ki + 1][kj + 1];
}
}
dst[i][j] = sum;
}
}
}
int main() {
float src[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 / 16.0, 2 / 16.0, 1 / 16.0},
{2 / 16.0, 4 / 16.0, 2 / 16.0},
{1 / 16.0, 2 / 16.0, 1 / 16.0}
};
int i, j;
float dst[5][5];
for (i = 0; i < 5; i++){
for (j = 0; j < 5; j++){
dst[i][j] = 0.0;
}
}
clock_t start, end;
start = clock();
gaussianBlur(src, dst, 5, 5, kernel);
end = clock();
double time = (double)(end - start) / CLOCKS_PER_SEC;
printf("Dst matrix:\n");
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
printf("%.2f ", dst[i][j]);
}
printf("\n");
}
printf("Time: %f s\n", time);
return 0;
}

@ -0,0 +1,63 @@
#include <stdio.h>
#include <time.h>
const int h = 5, w = 5;
float kx[3] = {0.25f, 0.5f, 0.25f};
float ky[3] = {0.25f, 0.5f, 0.25f};
void separableGaussianBlur(float src[h][w], float dst[h][w], int h, int w, float kx[3], float ky[3]) {
float buf[3][w];
int x, y;
#define BUF_INDEX(i) ((i) % 3)
for(y = 0; y < h; ++y){
for(x = 0; x < w; ++x){
buf[BUF_INDEX(y)][x] =
src[y][x] * kx[1] +
(x > 0 ? src[y][x - 1] * kx[0] : 0) +
(x < w - 1 ? src[y][x + 1] * kx[2] : 0);
}
if(y >= 2){
for(x = 1; x < w - 1; ++x){
dst[y - 1][x] =
buf[BUF_INDEX(y - 2)][x] * ky[0] +
buf[BUF_INDEX(y - 1)][x] * ky[1] +
buf[BUF_INDEX(y)][x] * ky[2];
}
}
}
}
int main() {
int i, j;
float dst[h][w];
for(i = 0; i < h; i++){
for (j = 0; j < w; j++){
dst[i][j] = 0.0;
}
}
float src[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}
};
clock_t start, end;
start = clock();
separableGaussianBlur(src, dst, h, w, kx, ky);
end = clock();
printf("Blurred result:\n");
for(i = 0; i < h; i++){
for(j = 0; j < w; j++){
printf("%.2f ", dst[i][j]);
}
printf("\n");
}
printf("Time: %f s\n", (float)(end - start) / CLOCKS_PER_SEC);
return 0;
}
Loading…
Cancel
Save