forked from Biswajee/DenoisingAssignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noisyfy.c
100 lines (81 loc) · 2.23 KB
/
noisyfy.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
/* Adding Salt pepper noise to image to convert them to noisy images */
#include <stdio.h>
#include <stdlib.h>
// Generate random numbers in a range...
int randomize(int lower, int upper) {
int num = (rand() % (upper - lower + 1)) + lower;
return num;
}
int main() {
// input RAW image file
FILE *fin = fopen("test_images\\feep.pgm", "r");
if(fin == NULL) {
printf("Could not read from source !\n");
return 0;
}
// output RAW image file
FILE *fout = fopen("outputs\\noisy_feep.pgm", "w");
if(fout == NULL) {
printf("Could not write to file !\n");
return 0;
}
// variable declaration
int c, ch, i, j, m, n, ten_percent_noise, twenty_percent_noise, fifty_percent_noise;
printf("Enter column and row values of PGM image [col row]\n");
scanf("%d %d", &m, &n);
printf("m: %d, n: %d\n", m , n);
fflush(stdin);
unsigned int data[n][m];
//fseek(fin, 22, 0);
c = getc(fin);
for(i=0; i<n; i++) {
for(j=0; j<m; j++) {
data[i][j] = c; // following lines converted to pixel values (unsigned int) [only incase of uchar PGMs]
c = getc(fin);
printf("%d", c);
}
}
// Salt pepper noise to the PGM Image...
ten_percent_noise = 0.10 * m * n;
twenty_percent_noise = 0.20 * m * n;
fifty_percent_noise = 0.50 * m * n;
printf("How much noise do you wish to add \n");
printf("1. 10%% noise \n2. 20%% noise \n3. 50%% noise \n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch) {
case 1:
// adding 10% noise to image
printf("10%% Salt-pepper going in, Sir !");
while(ten_percent_noise--) {
data[randomize(0,n)][randomize(0,m)] = randomize(3, 15);
}
break;
case 2:
printf("20%% Salt-pepper going in ! That's spicy");
while(twenty_percent_noise--) {
data[randomize(0,n)][randomize(0,m)] = randomize(3, 15);
}
break;
case 3:
printf("50%% Salt-pepper, Jeez !");
while(fifty_percent_noise--) {
data[randomize(0,n)][randomize(0,m)] = randomize(3, 15);
}
break;
default:
printf("\nAm I a joke to you ?\n");
}
printf("\n");
// writing data array to output file...
for(i=0; i<n; i++) {
for(j=0; j<m; j++) {
fprintf(fout, "%d ", data[i][j]);
}
fprintf(fout,"\n");
}
// closing input and output files
fclose(fin);
fclose(fout);
return 0;
}