c - Two dimensional array median filtering -
i'm trying write code implements median filtering on two-dimensional array. here's image illustrate:
the program starts @ beginning of array. maximum array size 100. know can use array like:
int a[100][100];
to store input, , can iterate on part of array using 2 for
loops this:
for(i=0;i<size_filter;i++) for(j=0;j<size_filter;j++) temp[i][j]=a[i][j] // not sure
but how can make code loop on neighbors of every element in array, calculate median, , replace center element median?
for examples of i'm trying do, let's input 5x5 matrix, input size 5. , want run 3x3 median filter on it, i.e. each element should replaced median of 3x3 elements surrounding it.
the program starts @ corner index (0,0). index, scans 3x3 region surrounding (of 4 indexes lie within input array), contains values 0, 0, 1, , 0. median of these values 0, that's code should output array index.
in picture below, number in bold italics center cell, , plain bold numbers neighbors within 3x3 region surrounding it:
0 0 0 0 0 1 0 0 1 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0
here's example, time center index (0,1):
0 0 0 0 0 1 0 0 1 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0
this time, elements in 3x3 region (excluding outside input array) have values 0, 0, 0, 1, 0, , 0, , again, median therefore 0.
here's yet example, time middle of input, @ center index (3,2):
0 0 0 0 0 1 0 0 1 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0
this time, elements within 3x3 region have values 1, 0, 0, 1, 1, 0, 0, 1, , 1, , median in therefore 1.
final example:
<size of array><size filter> <data> 8 3 0 0 0 0 0 0 0 0 0 5 0 0 6 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 5 0 0 0 0 0 0 5 6 0 0 0 0 0 8 5 5 0 0 0 0 0 0 7 0 0 9 0 0 0 0 0 0 0 0 0 output: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 5 0 0 0 0 0 0 5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
it looks you're trying implement two-dimensional median filter. straightforward way implement such filter have four nested loops: 2 outer loops on x , y coordinates of whole image, , 2 inner loops on neighborhood of center pixel.
it's perhaps easier describe in code in text, here's python-esque pseudocode illustrate:
# assumptions: # * image height x width array containing source pixel values # * filtered height x width array store result pixel values in # * size odd number giving diameter of filter region radius = (size - 1) / 2 # size = 3 -> radius = 1 y 0 height-1: top = max(y - radius, 0) bottom = min(y + radius, height-1) x 0 width-1: left = max(x - radius, 0) right = min(x + radius, width-1) values = new list v top bottom: u left right: add image[v][u] values filtered[y][x] = median(values)
translating code c left exercise.
it's possible optimize code noting neighborhoods of adjacent array cells overlap significantly, values of neighboring cells can reused across successive iterations of outer loops. since performance of algorithm on modern cpus limited ram access latency, such reuse can provide significant speedup, large filter sizes.
Comments
Post a Comment