This repository has been archived by the owner on Jul 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
process.c
323 lines (273 loc) · 9.43 KB
/
process.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
Author: Thushan Perera
Email: thushan.perera95@gmail.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "pgmfile.h"
#include "malloc_image.h"
#define BLACK 0
#define WHITE 255
#define FALSE 0
#define TRUE !FALSE
#define DEFAULT_SIZE 512
void process_image(int, char *, char *, int, int, int);
int** convolve(unsigned char**, int**, int, int, int);
/* get options and input/output filename from user */
int main(int argc, char *argv[])
{
FILE *fp_in, *fp_out;
char *temp, ch;
int set_file_in, set_file_out, set_sobel, count;
int width, height;
char file_in[255], file_out[255];
int pgmfile;
width = DEFAULT_SIZE;
height = DEFAULT_SIZE;
set_file_in = FALSE;
set_file_out = FALSE;
pgmfile = FALSE;
if (argc > 1) {
count = 0;
do {
count++;
temp = argv[count];
if (*argv[count] == '-') {
ch = *(++temp);
switch (ch) {
case 'i' :
count++;
strcpy(file_in, argv[count]);
set_file_in = TRUE;
pgmfile = FALSE;
break;
case 'p' :
count++;
strcpy(file_in, argv[count]);
set_file_in = TRUE;
pgmfile = TRUE;
break;
case 'o' :
count++;
strcpy(file_out, argv[count]);
set_file_out = TRUE;
break;
case 'w' :
count++;
width = atoi(argv[count]);
break;
case 'h' :
count++;
height = atoi(argv[count]);
break;
case 's' :
count++;
set_sobel = TRUE;
break;
default :
printf("Error on command line\n");
}
}
else {
printf("Error on command line\n");
exit(-1);
}
} while (count < argc - 1);
if (set_file_in == FALSE) {
printf("need input file name\n");
exit(-1);
}
if (set_file_out == FALSE) {
printf("need output file name\n");
exit(-1);
}
if ((fp_in = fopen(file_in,"r")) == NULL) {
printf("Input file %s not found - aborting\n",file_in);
exit(-1);
}
else
fclose(fp_in);
if ((fp_out = fopen(file_out,"w")) == NULL) {
printf("Could not open output file %s - aborting\n",file_out);
exit(-1);
}
else
fclose(fp_out);
/* Main image processing here using process_image */
printf("Reading from file %s\n",file_in);
printf("Writing to file %s\n",file_out);
process_image(pgmfile, file_in, file_out, width, height, set_sobel);
}
else {
printf(" Usage:\n");
printf(" process_ -[ip] file_in -o file_out [options]\n");
printf("\noptions:\n");
printf("\n");
printf("needs -[ip] and -o to work\n");
printf("use -i for raw images (need height and width)\n");
printf("use -p for pgm images\n");
exit(-1);
}
return 0;
}
/* open image file from file_in and write it out to file_out */
void process_image(int pgmfile, char *file_in, char *file_out, int width, int height, int set_sobel)
{
unsigned char **image_in; /* specify image array - char image */
unsigned char **image_out; /* specify image array - char image */
struct pgmfile pg;
/* Robert's cross masks */
int** mask_one;
int** mask_two;
/* Sobel masks */
int** smask_one;
int** smask_two;
int** first;
int** second;
int temp; // Holds current convoluted value
int threshold = 127; // Darker images will require lower thresholds
/* if correct PGM file format then get height and width */
if (pgmfile == TRUE) {
get_pgm_header(file_in,&pg);
height = pg.pgm_height;
width = pg.pgm_width;
}
printf("Image size: Height %d Width %d\n",width,height);
/* allocate memory for image */
image_in = malloc_char_image(width,height);
/* if file in PGM format then read info into predefined structure */
/* else read raw file */
if (pgmfile == TRUE) {
read_pgm_image(image_in,file_in,&pg);
}
else {
read_image(image_in,file_in,width,height);
}
image_out = malloc_char_image(width,height);
/** Do the extra processing here */
/* Allocate memory for the 2 robert's cross masks */
mask_one = malloc(2 * sizeof(int*));
mask_two = malloc(2 * sizeof(int*));
for (int i = 0; i < 2; i++) {
mask_one[i] = malloc(2 * sizeof(int));
mask_two[i] = malloc(2 * sizeof(int));
}
/* Allocate memory for the 2 sobel masks */
smask_one = malloc(3 * sizeof(int*));
smask_two = malloc(3 * sizeof(int*));
for (int i = 0; i < 3; i++) {
smask_one[i] = malloc(3 * sizeof(int));
smask_two[i] = malloc(3 * sizeof(int));
}
/* Assign values of Robert's cross mask one */
mask_one[0][0] = 1;
mask_one[0][1] = 0;
mask_one[1][0] = 0;
mask_one[1][1] = -1;
/* Assign values of Robert's cross mask two */
mask_two[0][0] = 0;
mask_two[0][1] = 1;
mask_two[1][0] = -1;
mask_two[1][1] = 0;
/* Assign values of Sobel vertical mask */
smask_one[0][0] = -1;
smask_one[0][1] = 0;
smask_one[0][2] = 1;
smask_one[1][0] = -2;
smask_one[1][1] = 0;
smask_one[1][2] = 2;
smask_one[2][0] = -1;
smask_one[2][1] = 0;
smask_one[2][2] = 1;
/* Assign values of Sobel horizontal mask */
smask_two[0][0] = 1;
smask_two[0][1] = 2;
smask_two[0][2] = 1;
smask_two[1][0] = 0;
smask_two[1][1] = 0;
smask_two[1][2] = 0;
smask_two[2][0] = -1;
smask_two[2][1] = -2;
smask_two[2][2] = -1;
if (set_sobel == TRUE) { /* Did user select sobel or not? */
printf("\nWill be using Sobel edge detection...\n");
first = convolve(image_in, smask_one, 3, width, height);
second = convolve(image_in, smask_two, 3, width, height);
} else {
printf("\nWill be using Robert's Cross edge detection...\n");
first = convolve(image_in, mask_one, 2, width, height);
second = convolve(image_in, mask_two, 2, width, height);
}
/* Get convolution result and add them to get the final pixel */
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
temp = abs(first[i][j]) + abs(second[i][j]);
if (temp > threshold) {
temp = threshold;
}
image_out[i][j] = (unsigned char)temp;
}
}
/* Memory cleanup */
free(mask_one);
free(mask_two);
free(smask_one);
free(smask_two);
free(first);
free(second);
/* write output image */
if (pgmfile == TRUE) {
write_pgm_image(image_out, file_out, &pg);
}
else
{
write_image(image_out, file_out, width, height);
}
}
/* Given an image and a mask, it will return the convolution result.
Note that each mask needs to be sent separately.
Also note that this method deals with special edge cases by ignoring a 1 pixel border around the image */
int** convolve(unsigned char** image_in, int** mask, int mask_size, int width, int height) {
/* Copy of input image with extra height and width for working */
int** work_image;
/* Results of convolution will be saved to this 2D array */
int** out_image;
int temp;
/* Allocate memory for workable image */
work_image = malloc(height * sizeof(int*));
for (int i = 0; i < height; i++) {
work_image[i] = malloc(width * sizeof(int));
}
/* Copy contents of input image to the workable copy */
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
work_image[i][j] = (int)image_in[i][j];
}
}
/* Allocate memory for output int** */
out_image = malloc(height * sizeof(int*));
for (int i = 0; i < height; i++) {
out_image[i] = malloc(width * sizeof(int));
}
/* We will be ignoring the 1 pixel border around image for edge cases */
for (int i = 1; i < height-1; i++) {
for (int j = 1; j < width-1; j++) {
/* Get pixel and the neighbours and then apply onto the mask matrix */
if (mask_size == 3) { // Multiplies the 3x3 matrix with a pixel and its 8 neighbours
temp = (work_image[i-1][j-1] * mask[0][0]) + (work_image[i-1][j] * mask[0][1]) +
(work_image[i-1][j+1] * mask[0][2]) + (work_image[i][j-1] * mask[1][0]) +
(work_image[i][j] * mask[1][1]) + (work_image[i][j+1] * mask[1][2]) +
(work_image[i+1][j-1] * mask[2][0]) + (work_image[i+1][j] * mask[2][1]) +
(work_image[i+1][j+1] * mask[2][2]);
} else if (mask_size == 2) { // Multiplies the 2x2 matrix with pixel and its 3 neighbours
temp = (work_image[i][j] * mask[0][0]) + (work_image[i+1][j+1] * mask[1][1]) +
(work_image[i][j+1] * mask[0][1]) + (work_image[i+1][j] * mask[1][0]);
}
out_image[i][j] = temp;
}
}
free(work_image);
return out_image;
}