-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.c
164 lines (158 loc) · 4.77 KB
/
serial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define height 256
#define width 256
//check conditions a & b
bool conditionsab(int p[3][3]){
int i, j;
int count1 = 0, count2 = 0;
//count the number of black neighbours
for(i = 0; i<3; i++){
for(j = 0; j<3; j++){
if(p[i][j] != 0) {
if(!(i == 1 && j == 1)) count1++;
}
}
}
//count the number of 0 to nonzero transitions
if(p[0][1] == 0 && p[0][2] != 0) count2++;
if(p[0][2] == 0 && p[1][2] != 0) count2++;
if(p[1][2] == 0 && p[2][2] != 0) count2++;
if(p[2][2] == 0 && p[2][1] != 0) count2++;
if(p[2][1] == 0 && p[2][0] != 0) count2++;
if(p[2][0] == 0 && p[1][0] != 0) count2++;
if(p[1][0] == 0 && p[0][0] != 0) count2++;
if(p[0][0] == 0 && p[0][1] != 0) count2++;
//return true if the conditions a & b are met
if(count1 >= 2 && count1 <= 6 && count2 == 1) return true;
else return false;
}
//check conditions a, b, c, d
bool checkconditions1(int p[3][3]){
int i, j, k;
bool check3, check4;
//calculate products according to the conditions c, d
check3 = p[0][1]*p[1][2]*p[2][1] == 0;
check4 = p[1][2]*p[2][1]*p[1][0] == 0;
//return true if the conditions a, b, c, d are met
if(conditionsab(p) && check3 && check4) return true;
else return false;
}
//check conditions a, b, c', d'
bool checkconditions2(int p[3][3]){
int i, j, k;
bool check3, check4;
//calculate products according to the conditions c', d'
check3 = p[0][1]*p[1][2]*p[1][0] == 0;
check4 = p[0][1]*p[2][1]*p[1][0] == 0;
//return true if the conditions a, b, c', d' are met
if(conditionsab(p) && check3 && check4) return true;
else return false;
}
//save the current pixel with indeces i, j and its 8 neighbours in the temp array
void cut(int temp[3][3], unsigned char A[height][width], int i, int j){
int k, m, row, col;
for(k = 0; k<3; k++){
//calculate row of neighbours
row = i-1+k;
for(m = 0; m<3; m++){
//calculate column of neighbour
col = j-1+m;
//check if the neighbour is within the boundaries of the initial array inimg
//if it is then save the value, else save 0
temp[k][m] = (col>-1 && col<width && row>-1 && row<height) ? (int)A[row][col] : 0;
}
}
}
//remove marked pixels in the image array, using the boolean array b
void apply(unsigned char A[height][width], bool b[height][width]){
int i, j;
for(i = 0; i<height; i++){
for(j = 0; j<width; j++){
//if the pixel with indeces i, j is marked then remove it(place a zero) in the image array
if(b[i][j]) A[i][j] = 0;
}
}
}
int main(){
//declare variables
int temp[3][3]; //used for cutting the array to 3x3 windows
int C; //used to count the number of pixels that meet the conditions
bool a[height][width]; //used to mark pixels
bool cond1, cond2; //boolean values, to check if conditions are met
//read from file
char infname[50], outfname[50];
unsigned char inimg[height][width], outimg[height][width];
short i, j;
FILE *infile, *outfile;
printf("input image filename: ");
scanf("%s", infname);
printf("output image filename: ");
scanf("%s",outfname);
infile=fopen(infname,"r");
for (i=0; i<height; i++) fread(inimg[i], 1, width, infile);
fclose(infile);
//flag, to return to this loop if needed
here:
//reset C
C = 0;
//check for each pixel in the image array, if the conditions a, b, c, d are met
for(i = 0; i<height; i++){
for(j = 0; j<width; j++){
//default value for a[i][j] = 0
a[i][j] = 0;
//only check non zero pixels
if(inimg[i][j] != 0){
//cut to 3x3 window
cut(temp, inimg, i, j);
//check conditions
cond1 = checkconditions1(temp);
if(cond1) {
//mark pixel
a[i][j] = 1;
//increase C by 1
C++;
}
}
}
}
//if no pixels found(C == 0) then quit loop
//else continue to the next loop
if(C == 0) goto write;
//remove marked pixels
apply(inimg, a);
//reset C
C = 0;
//check for each pixel in the image array, if the conditions a, b, c', d' are met
for(i = 0; i<height; i++){
for(j = 0; j<width; j++){
//default value for a[i][j] = 0
a[i][j] = 0;
if(inimg[i][j] != 0){
//cut to 3x3 window
cut(temp, inimg, i, j);
//check conditions
cond2 = checkconditions2(temp);
if(cond2) {
//mark pixel
a[i][j] = 1;
//increase C by 1
C++;
}
}
}
}
//remove marked pixels
apply(inimg, a);
//if pixels, that meet the conditions, are found(C != 0) then go to the first loop, and start again
if(C != 0) goto here;
//if C == 0 go here to write the output file
write:
//write
for (i=0; i< height; i++)for (j=0; j<width; j++) outimg[i][j] = inimg[i][j];
outfile=fopen(outfname,"w");
for (i=0; i< height; i++) fwrite(outimg[i], 1, width, outfile);
fclose (outfile);
return 0;
}