好吧,所以我試圖讓用戶輸入列數和行數,然後讓輸出帶出一個隨機數矩陣,然後是一個平滑的圖像矩陣,找到每個像素的平均濾波器陣列。現在我已經到了它可以給我一個隨機數矩陣的地方,但平滑後的矩陣完全沒有問題。現在我寫的代碼只關注具有4個鄰居的整數,但顯然我做了錯誤的事情。圖像平滑在c
#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 smooth (int a[10][20], int m, int n)
{
int i,j;
for (i=1;i<m;i++)
{
for (j=1;j<n;j++)
{
a[i][j]=a[i][j]=(a[i-1][j]+a[i][j-1]+a[i+1][j]+a[i][j+1])/4;
printf("%4d",a[i][j]);
}
printf("\n");
}
}
int main()
{
int a[10][20];
int m,n;
srand(time(NULL));
//User input
printf("please enter number of columns and rows");
scanf("%d %d", &m,&n);
fillArray(a,m,n);
printarray (a,m,n);
printf("The smoothed image is\n");
smooth(a,m,n);
getch();
return 0;
}
現在如果我進入3和3我會得到這樣的事情:
23 43 54
12 76 89
56 98 24
The smoothed image is
347373849493234343
12 23345345345
2132334565645645645
任何人有什麼想法?
我感到驚訝的是,當你平滑角落和邊緣時,你的程序沒有遇到錯誤;這些應該是特殊的casses,對待不同。 –
@coraldoe,我已經修復了大部分程序,現在我唯一的問題是我似乎無法修復右邊界和右邊兩個邊界的邊界問題。 – DatDudeJC