Search code examples
carraysimage2dsmoothing

2d arrays and image smoothing in c


I have to write a program that " will take in an image as input, represented by 2d array pixel values(for simplicity, each pixel can be represented by an integer). Output the resulting smoothed image by applying the mean filter to every pixel in the array".

I'm just learning about arrays, but I am lost as to how to even start this program. Whenever I search this topic it gets very confusing because every example or concept that I find is using or talking about actual images. Since my program is using integers, I'm having a hard time distinguishing what is needed and what is not. Basically I understand the premise(at least i think I do), each number must take the mean value from the 4 numbers around it, but outside the basic concept, I'm at a loss as to what needs to be done. Any help, suggestions, or examples would be great.

thanks


Solution

  • A key concept:

    int values[20][20];
    float intermediates[20][20];
    for (int y = 1; y < 19; y++)
      for (int x = 1; x < 19; x++)
        intermediates = (float)values[y][x];
    
    int means[20][20];
    for (int y = 1; y < 19; y++)
      for (int x = 1; x < 19; x++)
        means[y][x] = (int) ( (float) (intermediates[y-1][x-1] + intermediates[y-1][x] + intermediates[y-1][x+1] + intermediates[y][x-1] + intermediates[y][x] + intermediates[y][x+1] + intermediates[y+1][x-1] + intermediates[y+1][x] + intermediates[y+1][x+1]) / 9.0F); //  Divisor is 9 because we added nine values and we're getting the mean
    

    Now there are 4 corner cases: intermediates[0][0], intermediates[0][19], etc. and then all the sides.

    The values were copied into "intermediates" first because I didn't want to put (float)in front of everything.