C.中的图像平滑问题



好的,所以我正在尝试使用带均值滤波器的整数编写一个图像平滑程序。出于某种原因,尽管只有角落部分以及顶部和底部部分是正确的。

例如,一个有四个邻居的数字应该返回这四个邻居中的平均值,而不是。一个有3个邻居的数字应该返回这3个邻居中的平均值。

不过,这只适用于角值和上下值。两边和中间的计算不正确。

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

正如你所看到的,第一个矩阵中的1有83和87作为它的邻居,在第二个矩阵中正确地返回85,第二个数字87有1,27,54作为它的近邻,但错误地返回14作为平均值。有人能看看我下面的代码吗?请通过编辑或详细说明来解决这个问题,我已经看了几个小时了,似乎无法理解这个问题。如果你能解决这个问题,我将永远感激你!

#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 rowsn");
scanf("%d %d", &m,&n);
fillArray(a,m,n);
printarray (a,m,n);
printf("The smoothed image isn");

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;
}

好吧,我们似乎在同一个班上,因为我目前正在做同一个项目(我看到你之前的问题,你逐字引用了他的作业PDF)。我还没有完成,但我知道有一个问题是你设置得完全错误,使用了太多的函数。它应该是一个带有一堆if语句的平滑函数。同样,我还没有完成它,所以我没有任何代码可以给你看,但当我完成时,如果可能的话,我会编辑这篇文章(编辑:这是可能的,所以这就是我要做的)。

编辑:好吧,我终于完成了。我花了很长时间才意识到我把边界弄得太大了。与其发布整个代码,我只发布应该使用的单个平滑函数。

int smoothen(int mArray[100][100], int uX, int uY)  {
int tempArr[100][100];
int x, y;
for(x = 0; x < uX; x++)
{
for(y = 0; y < uY; y++)
{
// Multiple if/else statements to test pixel location
if ((x == 0) && (y == 0))
{
tempArr[x][y] = (mArray[x][y] + mArray[x+1][y] + mArray[x][y+1]) / 3;
}
else if ((x > 0 && x < uX-1) && (y == 0))
{
tempArr[x][y] = (mArray[x][y] + mArray[x+1][y] + mArray[x-1][y] + mArray[x][y+1]) / 4;
}
else if ((x == uX-1) && (y == 0))
{
tempArr[x][y] = (mArray[x][y] + mArray[x-1][y] + mArray[x][y+1]) / 3;
}
else if ((x == uX-1) && (y > 0 && y < uY-1))
{
tempArr[x][y] = (mArray[x][y] + mArray[x-1][y] + mArray[x][y+1] + mArray[x][y-1]) / 4;
}
else if ((x == uX-1) && (y == uY-1))
{
tempArr[x][y] = (mArray[x][y] + mArray[x-1][y] + mArray[x][y-1]) / 3;
}
else if ((x > 0 && x < uX-1) && (y == uY-1))
{
tempArr[x][y] = (mArray[x][y] + mArray[x+1][y] + mArray[x-1][y] + mArray[x][y-1]) / 4;
}
else if ((x == 0) && (y == uY-1))
{
tempArr[x][y] = (mArray[x][y] + mArray[x+1][y] + mArray[x][y-1]) / 3;
}
else if ((x == 0) && (y > 0 && y < uY-1))
{
tempArr[x][y] = (mArray[x][y] + mArray[x+1][y] + mArray[x][y+1] + mArray[x][y-1]) / 4;
}
else if ((x > 0 && x < uX-1) && (y > 0 && y < uY-1))
{
tempArr[x][y] = (mArray[x][y] + mArray[x+1][y] + mArray[x-1][y] + mArray[x][y+1] + mArray[x][y-1]) / 5;
}
}
}
printArray(tempArr, uX, uY);  }

您在一些函数中切换了维度:

void middle(int a[10][20], int c[10][20], int n, int m)
...
void side1 (int a[10][20],int c[10][20], int n, int m)
...
void corner2(int a[10][20], int c[10][20], int m, int n)

最新更新