forked from pi7mcrg2k/opcomplex
parent
37f55b0e7e
commit
b4bbaa9e9b
@ -0,0 +1,61 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#define IDX(n) ((n) % 3)
|
||||
|
||||
void applySeparableGaussianBlur(void*, void*, int, int, float[3], float[3]);
|
||||
void print(void*, int h, int w);
|
||||
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 kx[3] = {0.25f, 0.5f, 0.25f};
|
||||
float ky[3] = {0.25f, 0.5f, 0.25f};
|
||||
float outputImage[5][5] = {0};
|
||||
clock_t start = clock();
|
||||
applySeparableGaussianBlur(inputImage, outputImage, 5, 5, kx, ky);
|
||||
clock_t end = clock();
|
||||
printf("Time: %lf s\n", (double)(end-start) / CLOCKS_PER_SEC);
|
||||
print(outputImage, 5, 5);
|
||||
}
|
||||
|
||||
void applySeparableGaussianBlur(void* _src, void* _dst,
|
||||
int h, int w, float kx[3], float ky[3]) {
|
||||
float buf[3][101] = {0};
|
||||
int i, j;
|
||||
float (*src)[w] = (float(*)[w])_src;
|
||||
float (*dst)[w] = (float(*)[w])_dst;
|
||||
// 计算前两行的行内卷积
|
||||
for(i=0; i<2; i++)
|
||||
for(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];
|
||||
}
|
||||
// 利用buf进行累计
|
||||
for(i=1; i<h-1; i++) {
|
||||
for(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(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];
|
||||
}
|
||||
}
|
||||
|
||||
void print(void* _a, int h, int w) {
|
||||
float (*a)[w] = (float(*)[w])_a;
|
||||
int i, j;
|
||||
for(i=0; i<h; i++) {
|
||||
for(j=0; j<w; j++) {
|
||||
printf("%5.1f ", a[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
Loading…
Reference in new issue