-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgmanip.c
executable file
·442 lines (383 loc) · 16.5 KB
/
imgmanip.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "imgio.h"
#include "minkowski.h"
#include "FFT2D.h"
#include "imgsimple.h"
#include "imgfilter.h"
#include "colorspace.h"
#include "noise.h"
#define FILENAME_LENGTH 100
int main(int argc, char **argv) {
int commandArgs = 1;
int i;
char x;
char performContrast = 0;
char fft_filename[FILENAME_LENGTH];
char cdf_filename[FILENAME_LENGTH];
char histo_filename[FILENAME_LENGTH];
float contrastLow = 0.0;
float contrastHigh = 0.0;
float highpasslevel;
float lowpasslevel;
float percentCorrupt;
float sigma;
float brightness;
float sat;
int m;
int replaceWithM;
int performHistogram = 0;
int performCDF = 0;
int performFFT = 0;
int performVectorMedianFilter = 0;
int performMedianFilter = 0;
int performMeanFilter = 0;
int performSpacialFilter = 0;
int performLevelSlicing = 0;
int performEqualize = 0;
int performColorScale = 0;
int performSpatialReduceWidth = 0;
int performSpatialReduceHeigth = 0;
int performHighpass = 0;
int performLowpass = 0;
int performComponentMedianFilter = 0;
int performVectorOrderStatistic = 0;
int performVectorSpacialOrderStatistic = 0;
int performVectorMedianOrderStatistic = 0;
int performMinkowskiAddition = 0;
int performMinkowskiSubtraction = 0;
int performMinkowskiOpening = 0;
int performMinkowskiClosing = 0;
int performEdgeDetectOne = 0;
int performEdgeDetectTwo = 0;
int performEdgeDetectThree = 0;
int performEdgeDetectFour = 0;
int performAddGaussianNoise = 0;
int performAddSaltPepperNoise = 0;
int performSetBrightness = 0;
int performSetSaturation = 0;
int performBrightFilter = 0;
int imageWriteNecessary = 0;
int maskwidth = 0;
int maskheight = 0;
int maskrepeat = 0;
FILE *in = stdin;
FILE *out = stdout;
struct portImage *pi;
if (argc < 3) {
printf("\n");
printf(" Usage: %s [inputFile] ([outputFile]) [option] ([option] ...)\n", argv[0]);
printf("\n");
printf(" InputFile: Either a filename or '-' for stdin.\n");
printf(" OutputFile: Either a filename or '-' for stdout. (Not needed if no output necessary.)\n");
printf("\n");
printf(" Options:\n");
printf(" -ghisto FILENAME Graph Histogram\n");
printf(" -gcdf FILENAME Graph Cumulative Distribution\n");
printf(" -gfft FILENAME Graph FFT plot\n");
printf(" -color n Reduce color scale to n\n");
printf(" -spatial WIDTH-HEIGHT Perform spacial reduction to Width and Height\n");
printf(" -level n Perform level slicing from graylevel n to graylevel n+10\n");
printf(" -con LOW-HIGH Scale image contrast from LOW graylevel percentage to HIGH graylevel percentage\n");
printf(" -equ Histogram Eqaulization\n");
printf(" -medianf n Simple Median Filter of window size n*n\n");
printf(" -meanf n Simple Mean Filter of window size n*n\n");
printf(" -cmf n Component Median Filter of window size n*n\n");
printf(" -vmf n Vector Median Filter of window n*n\n");
printf(" -sf Spacial Filter\n");
printf(" -vos n v Vector Order Stat of window size n*n and value v\n");
printf(" -vmos n m [01] Vector Median Order Stat of window size n*n, m threshold, and 0 or 1(True) replace with m\n");
printf(" -vsos n m [01] Vector Spacial Order Stat of window size n*n, m threshold, and 0 or 1(True) replace with m\n");
printf(" -brightf n Perform an Brightness filter using HSV colorspace on size n*n window.\n");
printf(" -bright %% Set brightness to %% percent\n");
printf(" -sat %% Set saturation to %% percent\n");
printf(" -hp %% Highpass filter of %% percent\n");
printf(" -lp %% Lowpass filter of %% percent\n");
printf(" -ma NxM R Perform Minkowski Addition using NxM mask, repeated R times.\n");
printf(" -ms NxM R Perform Minkowski Subtraction using NxM mask, repeated R times.\n");
printf(" -mo NxM R Perform Minkowski Opening using NxM mask, repeated R times.\n");
printf(" -mc NxM R Perform Minkowski Closing using NxM mask, repeated R times.\n");
printf(" -e1 Perform Edge Detection using X/(X – B)\n");
printf(" -e2 Perform Edge Detection using (X + B)/X\n");
printf(" -e3 Perform Edge Detection using [(X+B)/(X-B)]-B\n");
printf(" -e4 n Experimental Edge Detection on Color Images using n*n window.\n");
printf(" -noiseG p s Add Gaussian noise to p (0 to 1 floating) percent of image with s sigma noise.\n");
printf(" -noiseSP p Add Salt and Pepper noise to p (0 to 1 floating) percent of image.\n");
printf("\n");
return(1);
}
if (strcmp(argv[commandArgs], "-") != 0) {
in = fopen(argv[1],"r");
if (in == NULL) {
fprintf(stderr, "File '%s' failed to open for reading.\n", argv[1]);
exit(1);
}
}
commandArgs++;
if (strcmp(argv[commandArgs], "-") != 0 && argv[commandArgs][0] != '-') {
commandArgs++;
out = fopen(argv[2],"w");
if (out == NULL) {
fprintf(stderr, "File '%s' failed to open for writing.\n", argv[2]);
exit(1);
}
}
for (; commandArgs < argc; commandArgs++) {
if (strcmp(argv[commandArgs], "-color") == 0) {
imageWriteNecessary = 1;
commandArgs++;
sscanf(argv[commandArgs], "%d", &performColorScale);
}
if (strcmp(argv[commandArgs], "-spatial") == 0) {
imageWriteNecessary = 1;
commandArgs++;
sscanf(argv[commandArgs], "%d%c%d", &performSpatialReduceWidth, &x, &performSpatialReduceHeigth);
}
if (strcmp(argv[commandArgs], "-level") == 0) {
imageWriteNecessary = 1;
commandArgs++;
sscanf(argv[commandArgs], "%d", &performLevelSlicing);
}
if (strcmp(argv[commandArgs], "-con") == 0) {
imageWriteNecessary = 1;
commandArgs++;
sscanf(argv[commandArgs], "%f%c%f", &contrastLow, &performContrast, &contrastHigh);
}
if (strcmp(argv[commandArgs], "-vos") == 0) {
imageWriteNecessary = 1;
commandArgs++;
sscanf(argv[commandArgs], "%d", &performVectorOrderStatistic);
commandArgs++;
sscanf(argv[commandArgs], "%d", &m);
}
if (strcmp(argv[commandArgs], "-vmf") == 0) {
imageWriteNecessary = 1;
commandArgs++;
sscanf(argv[commandArgs], "%d", &performVectorMedianFilter);
}
if (strcmp(argv[commandArgs], "-sf") == 0) {
imageWriteNecessary = 1;
performSpacialFilter = 1;
}
if (strcmp(argv[commandArgs], "-vmos") == 0) {
imageWriteNecessary = 1;
commandArgs++;
sscanf(argv[commandArgs], "%d", &performVectorMedianOrderStatistic);
commandArgs++;
sscanf(argv[commandArgs], "%d", &m);
commandArgs++;
sscanf(argv[commandArgs], "%d", &replaceWithM);
}
if (strcmp(argv[commandArgs], "-vsos") == 0) {
imageWriteNecessary = 1;
commandArgs++;
sscanf(argv[commandArgs], "%d", &performVectorSpacialOrderStatistic);
commandArgs++;
sscanf(argv[commandArgs], "%d", &m);
commandArgs++;
sscanf(argv[commandArgs], "%d", &replaceWithM);
}
if (strcmp(argv[commandArgs], "-cmf") == 0) {
imageWriteNecessary = 1;
commandArgs++;
sscanf(argv[commandArgs], "%d", &performComponentMedianFilter);
}
if (strcmp(argv[commandArgs], "-medianf") == 0) {
imageWriteNecessary = 1;
commandArgs++;
sscanf(argv[commandArgs], "%d", &performMedianFilter);
}
if (strcmp(argv[commandArgs], "-meanf") == 0) {
imageWriteNecessary = 1;
commandArgs++;
sscanf(argv[commandArgs], "%d", &performMeanFilter);
}
if (strcmp(argv[commandArgs], "-equ") == 0) {
imageWriteNecessary = 1;
performEqualize = 1;
}
if (strcmp(argv[commandArgs], "-hp") == 0) {
imageWriteNecessary = 1;
commandArgs++;
performHighpass = 1;
sscanf(argv[commandArgs], "%f", &highpasslevel);
}
if (strcmp(argv[commandArgs], "-lp") == 0) {
imageWriteNecessary = 1;
commandArgs++;
performLowpass = 1;
sscanf(argv[commandArgs], "%f", &lowpasslevel);
}
if (strcmp(argv[commandArgs], "-brightf") == 0) {
imageWriteNecessary = 1;
commandArgs++;
sscanf(argv[commandArgs], "%d", &performBrightFilter);
}
if (strcmp(argv[commandArgs], "-bright") == 0) {
imageWriteNecessary = 1;
commandArgs++;
performSetBrightness = 1;
sscanf(argv[commandArgs], "%f", &brightness);
}
if (strcmp(argv[commandArgs], "-sat") == 0) {
imageWriteNecessary = 1;
commandArgs++;
performSetSaturation = 1;
sscanf(argv[commandArgs], "%f", &sat);
}
if (strcmp(argv[commandArgs], "-ghisto") == 0) {
commandArgs++;
performHistogram = 1;
strncpy(histo_filename, argv[commandArgs], FILENAME_LENGTH);
}
if (strcmp(argv[commandArgs], "-gcdf") == 0) {
commandArgs++;
performCDF = 1;
strncpy(cdf_filename, argv[commandArgs], FILENAME_LENGTH);
}
if (strcmp(argv[commandArgs], "-gfft") == 0) {
commandArgs++;
performFFT = 1;
strncpy(fft_filename, argv[commandArgs], FILENAME_LENGTH);
}
if (strcmp(argv[commandArgs], "-ma") == 0) {
imageWriteNecessary = 1;
performMinkowskiAddition = 1;
commandArgs++;
sscanf(argv[commandArgs], "%d%c%d", &maskwidth, &x, &maskheight);
commandArgs++;
sscanf(argv[commandArgs], "%d", &maskrepeat);
}
if (strcmp(argv[commandArgs], "-ms") == 0) {
imageWriteNecessary = 1;
performMinkowskiSubtraction = 1;
commandArgs++;
sscanf(argv[commandArgs], "%d%c%d", &maskwidth, &x, &maskheight);
commandArgs++;
sscanf(argv[commandArgs], "%d", &maskrepeat);
}
if (strcmp(argv[commandArgs], "-mo") == 0) {
imageWriteNecessary = 1;
performMinkowskiOpening = 1;
commandArgs++;
sscanf(argv[commandArgs], "%d%c%d", &maskwidth, &x, &maskheight);
commandArgs++;
sscanf(argv[commandArgs], "%d", &maskrepeat);
}
if (strcmp(argv[commandArgs], "-mc") == 0) {
imageWriteNecessary = 1;
performMinkowskiClosing = 1;
commandArgs++;
sscanf(argv[commandArgs], "%d%c%d", &maskwidth, &x, &maskheight);
commandArgs++;
sscanf(argv[commandArgs], "%d", &maskrepeat);
}
if (strcmp(argv[commandArgs], "-e1") == 0) {
imageWriteNecessary = 1;
performEdgeDetectOne = 1;
}
if (strcmp(argv[commandArgs], "-e2") == 0) {
imageWriteNecessary = 1;
performEdgeDetectTwo = 1;
}
if (strcmp(argv[commandArgs], "-e3") == 0) {
imageWriteNecessary = 1;
performEdgeDetectThree = 1;
}
if (strcmp(argv[commandArgs], "-e4") == 0) {
imageWriteNecessary = 1;
commandArgs++;
sscanf(argv[commandArgs], "%d", &performEdgeDetectFour);
}
if (strcmp(argv[commandArgs], "-noiseG") == 0) {
imageWriteNecessary = 1;
performAddGaussianNoise = 1;
commandArgs++;
sscanf(argv[commandArgs], "%f", &percentCorrupt);
commandArgs++;
sscanf(argv[commandArgs], "%f", &sigma);
}
if (strcmp(argv[commandArgs], "-noiseSP") == 0) {
imageWriteNecessary = 1;
performAddSaltPepperNoise = 1;
commandArgs++;
sscanf(argv[commandArgs], "%f", &percentCorrupt);
}
}
pi = readImage(in);
if (performHighpass || performLowpass || performFFT) {
FFT2D(pi);
if (performHighpass) highpass(pi, highpasslevel);
if (performLowpass) lowpass(pi, lowpasslevel);
if (performFFT) graph_fftlogplot(pi, fft_filename);
IFFT2D(pi);
}
if (performEdgeDetectOne || performEdgeDetectTwo || performEdgeDetectThree ||
performMinkowskiAddition || performMinkowskiSubtraction || performMinkowskiOpening || performMinkowskiClosing)
thresholdImage(pi);
if (performAddGaussianNoise) addGaussianNoise(pi, percentCorrupt, sigma);
if (performAddSaltPepperNoise) addSaltPepperNoise(pi, percentCorrupt);
if (performMedianFilter) simpleMedianFilter(pi, performMedianFilter);
if (performMeanFilter) simpleMeanFilter(pi, performMeanFilter);
if (performComponentMedianFilter) componentMedianFilter(pi, performComponentMedianFilter);
if (performVectorOrderStatistic) vectorOrderStatistic(pi, performVectorOrderStatistic, m);
if (performVectorSpacialOrderStatistic) vectorSpacialOrderStatistic(pi, performVectorSpacialOrderStatistic, m, replaceWithM);
if (performVectorMedianOrderStatistic) vectorMedianOrderStatistic(pi, performVectorMedianOrderStatistic, m, replaceWithM);
if (performVectorMedianFilter) vectorMedianFilter(pi, performVectorMedianFilter);
if (performSpacialFilter) spacialFilter(pi);
if (performBrightFilter) HSV_ValueFilter(pi, performBrightFilter);
if (performColorScale) scale_reduce(pi,performColorScale);
if (performSetBrightness) setBrightness(pi,brightness);
if (performSetSaturation) setSaturation(pi,sat);
if (performSpatialReduceWidth) spacial_reduce(pi,performSpatialReduceWidth, performSpatialReduceHeigth);
if (performContrast) contrast_stretching(pi, contrastLow, contrastHigh);
if (performLevelSlicing) level_slice(pi, performLevelSlicing);
if (performEqualize) equalize(pi);
if (performHistogram) graph_histogram(pi, histo_filename);
if (performCDF) graph_cdf(pi, cdf_filename);
if (performMinkowskiAddition)
for (i = 0; i < maskrepeat; i++)
minkowskiAddition(pi, maskwidth, maskheight);
if (performMinkowskiSubtraction)
for (i = 0; i < maskrepeat; i++)
minkowskiSubtraction(pi, maskwidth, maskheight);
if (performMinkowskiOpening)
for (i = 0; i < maskrepeat; i++)
minkowskiOpening(pi, maskwidth, maskheight);
if (performMinkowskiClosing)
for (i = 0; i < maskrepeat; i++)
minkowskiClosing(pi, maskwidth, maskheight);
if (performEdgeDetectOne) {
struct portImage *pc = copyImage(pi);
imageWriteNecessary = 1;
minkowskiSubtraction(pc, 3, 3);
minkowskiDivision(pi, pc);
freeImage(pc);
}
if (performEdgeDetectTwo) {
struct portImage *pc = copyImage(pi);
imageWriteNecessary = 1;
minkowskiAddition(pi, 3, 3);
minkowskiDivision(pi, pc);
freeImage(pc);
}
if (performEdgeDetectThree) {
struct portImage *pd = copyImage(pi);
maskrepeat = 3;
imageWriteNecessary = 1;
for (i = 0; i < maskrepeat; i++) {
minkowskiAddition(pi, 3, 3);
minkowskiSubtraction(pd, 3, 3);
}
minkowskiDivision(pi, pd);
minkowskiSubtraction(pi, 3, 3);
freeImage(pd);
}
if (imageWriteNecessary)
writeImage(pi, out);
freeImage(pi);
if (in != stdin) fclose(in);
if (out != stdout) fclose(out);
return 0;
} /* End Main */