Search code examples
cmatrixboundary

Fixing boundary issue in C


I'm writing an image smoothing program that uses mean filters, but I can't seem to fix the last couple boundary issues. I'm using integers instead of an image. I've for the 1 left hand corners return the proper values, along with the middle integers, left side integers and the top and bottom integers. Currently I'm stuck on the right hand side set of integers causing boundary issues.

void side2(int a[10][20], int c[10][20], int m, int n)
{
    int i,j;
    for (i=1;i<m-1;i++) {
        for (j=0;j<n-1;j++) {
            c[i][j]=(0+0+a[i-1][j]+a[i+1][j]+a[i][j+1])/3;        
        }         
    } 
}

this is just the function that calculates the right side, not either of the corners.

Here is the output that I get. The user is asked to enter a number for columns and rows, this outputs a random number generated matrix followed by the smoothed image with the results of the mean filter. As you can see the right side is wacky, and I cant seem to find the right values for the boundaries. Can someone please help me find the correct boundaries so I can stop getting these wacky numbers.

please enter number of columns and rows
5 5
66  49  74  73  44
47  73  69  27  97
96  63  79  68  35
82  86  28  22  14
59   3  24   5  22
The smoothed image is
48  71  63  482009141880
78  60  60  79764514
64  79  55  28696626048
80  31  41  29  19
42  56  12  222009815

Solution

  • You need to write something to the border where you can't do the filter. Currently, you're not writing anything to, for example, c[1][n-1], so you just get uninitialized junk.