2006
04.25
04.25
13. Digital images, particularly those radioed back from spacecraft, may have glitches. Add a de-glitching function to programming exercise 12. It should compare each value to its immediate neighbors to the left and right, above and below. If the value differs by more than 1 from each of its neighbors, replace the value with the average of the neighboring values. You should round the average to the nearest integer value. Note that the points along the boundaries have fewer than four neighbors, so they require special handling
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define ROWS 20 /* 原始檔案有 20 行 */
#define COLS 30 /* 原始檔案有 20 欄位 */
#define SELF picIn[i][j]
#define LEFT picIn[i][j-1]
#define RIGHT picIn[i][j+1]
#define UP picIn[i-1][j]
#define DOWN picIn[i+1][j]
void deglitch(int picIn[][COLS]) /* de-glitching 用 */
{
int i, j, k = 0, l = 0, m = 0, n = 0, o=0; /* i,j 走訪陣列用,其餘計算修正次數*/
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
if(i == 0 && j ==0) /* 最左上角的 */
{
if ( abs(RIGHT - SELF) > 1 && abs(DOWN - SELF) > 1)
{
SELF = RIGHT + LEFT / 2;
printf("De-glitched the upper-left element.\n");
}
}
else if (i == ROWS - 1 && j == 0) /* 最左下角的 */
{
if ( abs(RIGHT - SELF) > 1 && abs(UP - SELF) > 1)
{
SELF = RIGHT + UP / 2;
printf("De-glitched the bottom-left element.\n");
}
}
else if (i == 0 && j == COLS - 1) /* 最右上角的 */
{
if ( abs(LEFT - SELF) > 1 && abs(DOWN - SELF) > 1)
{
SELF = LEFT + DOWN / 2;
printf("De-glitched the upper-right element.\n\n");
}
}
else if (i == ROWS - 1 && j == COLS - 1) /* 最右下角的 */
{
if ( abs(LEFT - SELF) > 1 && abs(UP - SELF) > 1)
{
SELF = LEFT + UP / 2;
printf("De-glitched the bottom-right element.\n\n");
}
}
else if (i == 0) /* 最上面的 ROW */
{
if ( abs(LEFT - SELF) > 1 && abs(RIGHT - SELF) > 1)
{
SELF = LEFT + RIGHT / 2;
k++;
}
}
else if (i == ROWS - 1) /* 最下面的 ROW */
{
if ( abs(LEFT - SELF) > 1 && abs(RIGHT - SELF) > 1)
{
SELF = LEFT + UP / 2;
l++;
}
}
else if ( j == 0 ) /* 最左邊的 COLUNM */
{
if ( abs(RIGHT - SELF) > 1 && abs(UP - SELF) > 1 && abs(DOWN - SELF) > 1)
{
SELF = LEFT + UP + DOWN / 3;
m++;
}
}
else if ( j == COLS - 1) /* 最右邊的 COLUNM */
{
if ( abs(LEFT - SELF) > 1 && abs(UP - SELF) > 1 && abs(DOWN - SELF) > 1)
{
SELF = LEFT + UP + DOWN / 3;
n++;
}
}
else /* 正常比對 */
{
if( abs(UP - SELF) > 1 && abs(DOWN - SELF) >1 && abs(LEFT - SELF) > 1 && abs(RIGHT - SELF) > 1)
{
SELF = ( UP + DOWN + LEFT + RIGHT ) / 4 ;
o++;
}
}
}
}
printf("The elements of the up row de-glitched %d times.\n", k);
printf("The elements of the bottom row de-glitched %d times.\n", l);
printf("The elements of the leftest colunm de-glitched %d times.\n", m);
printf("The elements of the rightest colunm de-glitched %d times.\n", n);
printf("others de-glitched %d times.\n", o);
printf("The following message has been de-glitched:\n");
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
printf("%2d", picIn[i][j]);
printf("\n");
}
/* 在螢幕上列印結果 */
}
int main(void)
{
int row, col; /* 走訪陣列用的 row 和 colunm */
int picIn[ROWS][COLS]; /* 讀入的整數陣列 */
char fileName[40]; /* 檔案名稱最大 39 個字 */
FILE * infile; /* 輸入的檔案指標 */
printf("Enter name of file: ");
scanf("%s", fileName);
if ((infile = fopen(fileName, "r")) == NULL)
{
fprintf(stderr, "Open file error.\n");
exit(EXIT_FAILURE);
}
/* 讓使用者輸入要讀入的檔案名稱,錯誤則回傳錯誤訊息 */
for (row = 0; row < ROWS; row++)
for (col = 0; col < COLS; col++)
fscanf(infile, "%d", &picIn[row][col]);
/* 以巢狀迴圈依序讀入檔案至 picIn 這個陣列*/
if (ferror(infile))
{
fprintf(stderr, "Error getting data from file.\n");
exit(EXIT_FAILURE);
}
/* 假設讀取或寫入有錯誤時,ferror()回傳非零,否則傳回零,用以判斷讀取是否正確 */
deglitch(picIn);
/* */
return 0;
}

No Comment.
Add Your Comment