3 Replies - 641 Views - Last Post: 03 October 2012 - 07:18 PM Rate Topic: -----

#1 jcmoney1010  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-October 12

Image smoothing Issues

Posted 03 October 2012 - 05:43 PM

ok so i'm trying to write an image smoothing program using integers with a mean filter. For some reason though only the corner sections, and the top, bottom sections are out putting correctly.

for example a number with four neighbors should return the average of those 4 neighbors, instead it is not. A number with 3 neighbors should return the average of those 3 neighbors.

this though is only working for corner values and top bottom values. The sides and the middle are not calculating correctly. Here is the output.

please enter number of columns and rows
5 5
1 83 92 66 38
87 27 98 36 80
54 55 33 97 5
26 93 40 79 55
21 34 54 85 25
The smoothed image is
85 40 82 55 73
14 89 51 81 20
71 38 83 24 76
73 40 68 64 52
30 56 53 52 70

as you can see the 1 in the first matrix has 83 and 87 as its neighbors and returns 85 correctly in the 2nd matrix, the second number 87 has 1,27,54 as its neighbors but incorrectly returns 14 as the average. Can someone take a look at my code below and please fix this with either an edit, or detailed instructions, I've been looking at this for hours and can't seem to understand the problem. I will be eternally grateful if you can fix this!

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
// function that randomly generates numbers 
void fillArray(int a[10][20], int m, int n)
{
 int random;
  int i,j;  
      for (i=0;i<m;i++)
      {
          for (j=0;j<n;j++)
          {
              random=rand()%100;
              a[i][j]=random;
          }
      }
 }
// function that prints the first matrix of random numbers
void printarray (int a[10][20], int m, int n)
{
 int i,j;
for (i=0;i<m;i++) 
{
    for (j=0;j<n;j++)
        {
        printf("%4d", a[i][j]);
        }
        printf("\n");
    }
}
// function that finds the mean for any number and its 4 nieghbors 
void corner1 (int a[10][20], int c[10][20], int n, int m)
{
int i,j;
 for (i=0;i<m;i++) 
{
    for (j=0;j<n;j++)
    {
        if (i<=0&&j<=0){
           c[i][j]=(a[i+1][j]+a[i][j+1])/2;
          }
      }
  }
}
void middle(int a[10][20], int c[10][20], int n, int m)
{
int i,j;
 for (i=1;i<m-1;i++) 
{
    for (j=1;j<n-1;j++)
    {

        c[i][j]=(a[i-1][j]+a[i][j-1]+a[i+1][j]+a[i][j+1])/4;
    }
}

}

void side1 (int a[10][20],int c[10][20], int n, int m)
{
int i,j;
 for (i=1;i<m-1;i++) 
{
    for (j=0;j<n-1;j++)
    {
      if (i>=1&&j<=0){

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

        if (i>=1 && j>=0){

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


void bottom (int a[10][20], int c[10][20], int m, int n)
{
int i,j;
 for (i=1;i<m;i++) 
{
    for (j=1;j<n;j++)
    {
        c[i][j]=(a[i][j-1]+a[i-1][j]+a[i][j+1])/3;
    }
  }   
}
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;j++)
    {
      c[i][n-1]=(0+0+a[i-1][j]+a[i+1][j]+a[i][j-1])/3;        
    }         
  } 
}     


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

int a[10][20];
int c[10][20];
int m,n;
srand(time(NULL));
//User input
printf("please enter number of columns and rows\n");
scanf("%d %d", &m,&n);
fillArray(a,m,n);
printarray (a,m,n);
printf("The smoothed image is\n");



corner1(a,c,m,n);
side1(a,c,m,n);
middle (a,c,m,n);
corner2(a,c,m,n);
top(a,c,m,n);
bottom(a,c,m,n);
side2(a,c,m,n);
corner3(a,c,m,n);
corner4(a,c,m,n);
printarray(c,m,n);

getch();
return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Image smoothing Issues

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: Image smoothing Issues

Posted 03 October 2012 - 05:49 PM

check your conditions
if (i<=0&&j<=0){
   c[i][j]=(a[i+1][j]+a[i][j+1])/2;
}


Was This Post Helpful? 0
  • +
  • -

#3 jcmoney1010  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-October 12

Re: Image smoothing Issues

Posted 03 October 2012 - 05:53 PM

View Postjjl, on 03 October 2012 - 05:49 PM, said:

check your conditions
if (i<=0&&j<=0){
   c[i][j]=(a[i+1][j]+a[i][j+1])/2;
}



I've checked them so many times. I did each function one at a time, and checked to make sure they worked before I went to the next function. I can't seemed to figure out why after they worked before, they no longer work. After I discovered the problem, I went through and double,triple and quadruple checked them. Still can't find the problem. Any other ideas
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1298
  • View blog
  • Posts: 4,468
  • Joined: 19-February 09

Re: Image smoothing Issues

Posted 03 October 2012 - 07:18 PM

The way you are doing it seems over complicated.

Why not write a function that calculates the average for one element.

The average is the sum of the neighbors divided by the number of neighbors.

Lets say the first neighbor is the array[current_row+1][current_col]. If current_row+1 is < m_rows (it is valid), then add its value to sum and increment the number of neighbors.

Do the same for rest of the neighbors.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1