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 | /*
This file tests all the edge detection algorithms implemented in
edge_detectors.c. It also leads with input/output and with the
parameters handling.
Copyright (c) 2011-2013, Haldo Sponton <haldos@fing.edu.uy>
Copyright (c) 2011-2013, Juan Cardelino <juanc@fing.edu.uy>
This program is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include "classic_edge_detectors.h" // edge detection algorithms
#include "io_png.h" // image input/output
/* Display help function */
void displayhelp(void) {
printf("Usage: edges [options] input.png\n"
" -r <th> Roberts' algorithm\n"
" th threshold (float in [0,1])\n"
" -p <th> Prewitt's algorithm\n"
" th threshold (float in [0,1])\n"
" -s <th> Sobel's algorithm\n"
" th threshold (float in [0,1])\n"
" -m <sigma> <n> <th> Marr-Hildreth algorithm (Gaussian)\n"
" sigma std dev of Gaussian kernel\n"
" n size of Gaussian kernel (int)\n"
" th threshold (float in [0,1])\n"
" -l <sigma> <n> <th> Marr-Hildreth algorithm (LoG)\n"
" sigma std dev of LoG kernel\n"
" n size of LoG kernel (int)\n"
" th threshold (float in [0,1])\n"
" -h <th> Haralick's algorithm\n"
" th threshold (float in [0,1])\n"
" -H Display this help\n"
"\nMore than one option can be used together. "
"Output is written to image files.\n");
}
/* Main function */
int main(int argc, char *argv[]) {
/* Start */
printf("A review of classic edge detection algorithms\n");
printf("Haldo Sponton & Juan Cardelino, IPOL 2013\n\n");
/* Options and parameters handling */
int argc_sobel = 0; //
int argc_prewitt = 0; //
int argc_roberts = 0; // indexes in argv[]
int argc_mh = 0; //
int argc_mhl = 0; //
int argc_haralick = 0; //
int padding_method = 1; // Reflection of image boundary. Hard-coded.
// Can be changed to 0 (zero-padding)
for(int n = 1; n<argc; n++ ) { // scan through args
switch( (int)argv[n][0] ) { // check for option character
case '-':
switch( (int)argv[n][1] ) {
case 'r': argc_roberts = n;
break;
case 'p': argc_prewitt = n;
break;
case 's': argc_sobel = n;
break;
case 'm': argc_mh = n;
break;
case 'l': argc_mhl = n;
break;
case 'h': argc_haralick = n;
break;
case 'H': /* Display help! */
displayhelp();
exit(1);
break;
default: printf("Error: Invalid option -> %s. Valid"
" options are -r, -p, -s, -h, -m and -l"
" (-H for help).\n", argv[n]);
exit(1);
break;
}
/* Error message if an option is the last input parameter
or if the next parameter after an option is another option */
if( n==argc-1 ) {
printf("Error: Missing parameter(s) for %s option.\n",
argv[n]);
exit(1);
break;
} else if( argv[n+1][0]=='-' ) {
printf("Error: Missing parameter(s) for %s option.\n",
argv[n]);
exit(1);
break;
}
/* In the case of -m and -l, three parameters are needed */
if( (argv[n][1]=='m') || (argv[n][1]=='l') ) {
if( n+3>argc-1 ) {
printf("Error: Missing parameter(s) for %s option.\n",
argv[n]);
exit(1);
break;
} else if( (argv[n+2][0]=='-') || (argv[n+3][0]=='-') ) {
printf("Error: Missing parameter(s) for %s option.\n",
argv[n]);
exit(1);
break;
}
}
default:
break;
}
}
/* Check for total number of parameters */
int nparam = 2;
if(argc_roberts!=0) nparam+=2;
if(argc_prewitt!=0) nparam+=2;
if(argc_sobel!=0) nparam+=2;
if(argc_mh!=0) nparam+=4;
if(argc_mhl!=0) nparam+=4;
if(argc_haralick!=0) nparam+=2;
if(nparam!=argc) {
printf("Error: Wrong number of arguments (%d instead of %d)\n"
"Usage: %s [options] input.png\n"
"For help type %s -H.\n",argc-1,nparam-1,argv[0],argv[0]);
exit(1);
}
/* Load input image */
size_t w, h;
float *im = io_png_read_f32_gray(argv[argc-1], &w, &h);
printf("Input image: %s\n",argv[argc-1]);
printf("Size: %zd x %zd\n\n",w,h);
/* Roberts edge detection algorithm */
if(argc_roberts!=0) {
float th_roberts = atof(argv[argc_roberts+1]); // threshold
printf("Running Roberts, threshold=%.2f",th_roberts);
float *im_roberts = edges_roberts(im, w, h, th_roberts, padding_method);
io_png_write_f32("out_roberts.png", im_roberts, w, h, 1);
printf(" output: out_roberts.png\n");
free(im_roberts);
}
/* Prewitt edge detection algorithm */
if(argc_prewitt!=0) {
float th_prewitt = atof(argv[argc_prewitt+1]); // threshold
printf("Running Prewitt, threshold=%.2f",th_prewitt);
float *im_prewitt = edges_prewitt(im, w, h, th_prewitt, padding_method);
io_png_write_f32("out_prewitt.png", im_prewitt, w, h, 1);
printf(" output: out_prewitt.png\n");
free(im_prewitt);
}
/* Sobel edge detection algorithm */
if(argc_sobel!=0) {
float th_sobel = atof(argv[argc_sobel+1]); // threshold
printf("Running Sobel, threshold=%.2f",th_sobel);
float *im_sobel = edges_sobel(im, w, h, th_sobel, padding_method);
io_png_write_f32("out_sobel.png", im_sobel, w, h, 1);
printf(" output: out_sobel.png\n");
free(im_sobel);
}
/* Marr-Hildreth (Gaussian) edge detection algorithm */
if(argc_mh!=0) {
float sigma_m = atof(argv[argc_mh+1]); // Gaussian kernel's std dev.
int n_m = atoi(argv[argc_mh+2]); // kernel size
float tzc_m = atof(argv[argc_mh+3]); // threshold for zero-crossing
printf("Running Marr-Hildreth (Gaussian), sigma=%.2f, N=%d, TZC=%.2f",
sigma_m, n_m, tzc_m);
float *im_mh = edges_mh(im, w, h, sigma_m, n_m, tzc_m, padding_method);
io_png_write_f32("out_mh.png", im_mh, w, h, 1);
printf(" output: out_mh.png\n");
free(im_mh);
}
/* Marr-Hildreth (LoG) edge detection algorithm */
if(argc_mhl!=0) {
float sigma_l = atof(argv[argc_mhl+1]); // Gaussian kernel's std dev.
int n_l = atoi(argv[argc_mhl+2]); // kernel size
float tzc_l = atof(argv[argc_mhl+3]); // threshold for zero-crossing
printf("Running Marr-Hildreth (LoG), sigma=%.2f, N=%d, TZC=%.2f",
sigma_l, n_l, tzc_l);
float *im_mhl = edges_mh_log(im, w, h,
sigma_l, n_l, tzc_l, padding_method);
io_png_write_f32("out_mhl.png", im_mhl, w, h, 1);
printf(" output: out_mhl.png\n");
free(im_mhl);
}
/* Haralick edge detection algorithm */
if(argc_haralick!=0) {
float rhozero = atof(argv[argc_haralick+1]); // threshold
printf("Running Haralick, rhozero=%.2f",rhozero);
float *im_haralick = edges_haralick(im, w, h, rhozero, padding_method);
io_png_write_f32("out_haralick.png", im_haralick, w, h, 1);
printf(" output: out_haralick.png\n");
free(im_haralick);
}
/* Memory free */
free(im);
exit(EXIT_SUCCESS);
}
|