-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathedge2.c
executable file
·472 lines (385 loc) · 11.6 KB
/
edge2.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/***********************************************
*
* file edge2.c
*
* Functions: This file contains
* homogeneity
* difference_edge
* contrast_edge
* range
* variance
*
* Purpose:
* These functions implement several
* types of advanced edge detection.
*
* External Calls:
* utility.c - sort_elements
*
* Modifications:
* 26 March 1991 - created
* 30 December 1992 - added the range and
* variance edge detectors.
* May 10, 1998 - modified routines to work
* with an entire image in one array.
*
*************************************************/
#include "cips.h"
int sort_elements();
short e_mask[3][3] = {
{-9, 0, -9},
{ 0, 36, 0},
{-9, 0, -9} };
short contrast[3][3] = {
{ 1, 1, 1},
{ 1, 1, 1},
{ 1, 1, 1}};
/**************************************************
*
* homogeneity(...
*
* This function performs edge detection by looking
* for the absence of an edge. The center of a
* 3x3 area is replaced by the absolute value of
* the max difference between the center point
* and its 8 neighbors.
*
***************************************************/
int homogeneity(the_image, out_image,
rows, cols, bits_per_pixel,
threshold, high)
int high, threshold;
short **the_image, **out_image;
long rows, cols, bits_per_pixel;
{
int a, b, absdiff, absmax, diff, i, j,
length, max, max_diff, new_hi, new_low, width;
new_hi = 250;
new_low = 16;
if(bits_per_pixel == 4){
new_hi = 10;
new_low = 3;
}
max = 255;
if(bits_per_pixel == 4)
max = 16;
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
out_image[i][j] = 0;
}
}
for(i=1; i<rows-1; i++){
if( (i%10) == 0) printf("%4d", i);
for(j=1; j<cols-1; j++){
max_diff = 0;
for(a=-1; a<=1; a++){
for(b=-1; b<=1; b++){
diff = the_image[i][j] -
the_image[i+a][j+b];
absdiff = abs(diff);
if(absdiff > max_diff)
max_diff = absdiff;
} /* ends loop over b */
} /* ends loop over a */
out_image[i][j] = max_diff;
} /* ends loop over j */
} /* ends loop over i */
/* if desired, threshold the output image */
if(threshold == 1){
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
if(out_image[i][j] > high){
out_image[i][j] = new_hi;
}
else{
out_image[i][j] = new_low;
}
}
}
} /* ends if threshold == 1 */
return(1);
} /* ends homogeneity */
/**************************************************
*
* difference_edge(...
*
* This function performs edge detection by looking
* at the differences in the pixels that surround
* the center point of a 3x3 area. It replaces the
* center point with the absolute value of the
* max difference of:
* upper left - lower right
* upper right - lower left
* left - right
* top - bottom
*
***************************************************/
int difference_edge(the_image, out_image,
rows, cols, bits_per_pixel,
threshold, high)
int high, threshold;
short **the_image, **out_image;
long rows, cols, bits_per_pixel;
{
int a, b, absdiff, absmax, diff, i, j,
length, max, max_diff, new_hi, new_low, width;
new_hi = 250;
new_low = 16;
if(bits_per_pixel == 4){
new_hi = 10;
new_low = 3;
}
max = 255;
if(bits_per_pixel == 4)
max = 16;
for(i=0; i<rows; i++)
for(j=0; j<cols; j++)
out_image[i][j] = 0;
for(i=1; i<rows-1; i++){
if( (i%10) == 0) printf("%4d", i);
for(j=1; j<cols-1; j++){
max_diff = 0;
absdiff = abs(the_image[i-1][j-1] -
the_image[i+1][j+1]);
if(absdiff > max_diff) max_diff = absdiff;
absdiff = abs(the_image[i-1][j+1] -
the_image[i+1][j-1]);
if(absdiff > max_diff) max_diff = absdiff;
absdiff = abs(the_image[i][j-1] -
the_image[i][j+1]);
if(absdiff > max_diff) max_diff = absdiff;
absdiff = abs(the_image[i-1][j] -
the_image[i+1][j]);
if(absdiff > max_diff) max_diff = absdiff;
out_image[i][j] = max_diff;
} /* ends loop over j */
} /* ends loop over i */
/* if desired, threshold the output image */
if(threshold == 1){
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
if(out_image[i][j] > high){
out_image[i][j] = new_hi;
}
else{
out_image[i][j] = new_low;
}
}
}
} /* ends if threshold == 1 */
return(1);
} /* ends difference_edge */
/**************************************************
*
* contrast_edge(...
*
* The edge detector uses the basic quick edge
* detector mask and then divides the result
* by a contrast smooth mask. This implements
* Johnson's contrast based edge detector.
*
***************************************************/
int contrast_edge(the_image, out_image,
rows, cols, bits_per_pixel,
threshold, high)
int high, threshold;
short **the_image, **out_image;
long rows, cols, bits_per_pixel;
{
int ad, d;
int a, b, absdiff, absmax, diff, i, j,
length, max, new_hi, new_low,
sum_d, sum_n, width;
new_hi = 250;
new_low = 16;
if(bits_per_pixel == 4){
new_hi = 10;
new_low = 3;
}
max = 255;
if(bits_per_pixel == 4)
max = 16;
for(i=0; i<rows; i++)
for(j=0; j<cols; j++)
out_image[i][j] = 0;
for(i=1; i<rows-1; i++){
if( (i%10) == 0) printf("%4d", i);
for(j=1; j<cols-1; j++){
sum_n = 0;
sum_d = 0;
for(a=-1; a<2; a++){
for(b=-1; b<2; b++){
sum_n = sum_n + the_image[i+a][j+b] *
e_mask[a+1][b+1];
sum_d = sum_d + the_image[i+a][j+b] *
contrast[a+1][b+1];
}
}
d = sum_d / 9;
if(d == 0)
d = 1;
out_image[i][j] = sum_n/d;
if(out_image[i][j] > max)
out_image[i][j] = max;
if(out_image[i][j] < 0)
out_image[i][j] = 0;
} /* ends loop over j */
} /* ends loop over i */
/* if desired, threshold the output image */
if(threshold == 1){
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
if(out_image[i][j] > high){
out_image[i][j] = new_hi;
}
else{
out_image[i][j] = new_low;
}
}
}
} /* ends if threshold == 1 */
return(1);
} /* ends contrast_edge */
/*******************************************
*
* range(..
*
* This edge detector performs the
* range operation.
* It replaces the pixel at the center of a
* 3x3, 5x5, etc. area with the max - min
* for that area.
*
*******************************************/
int range(the_image, out_image,
rows, cols, bits_per_pixel,
size, threshold, high)
int high, threshold, size;
short **the_image,
**out_image;
long rows, cols, bits_per_pixel;
{
int a, b, count, i, j, k,
new_hi, new_low, length,
sd2, sd2p1, ss, width;
short *elements;
sd2 = size/2;
sd2p1 = sd2 + 1;
/**********************************************
*
* Allocate the elements array large enough
* to hold size*size shorts.
*
**********************************************/
ss = size*size;
elements = (short *) malloc(ss * sizeof(short));
new_hi = 250;
new_low = 16;
if(bits_per_pixel == 4){
new_hi = 10;
new_low = 3;
}
/***************************
*
* Loop over image array
*
****************************/
printf("\n");
for(i=sd2; i<rows-sd2; i++){
if( (i%10) == 0) printf("%4d ", i);
for(j=sd2; j<cols-sd2; j++){
count = 0;
for(a=-sd2; a<sd2p1; a++){
for(b=-sd2; b<sd2p1; b++){
elements[count] = the_image[i+a][j+b];
count++;
}
}
sort_elements(elements, &ss);
out_image[i][j] = elements[ss-1]-elements[0];
} /* ends loop over j */
} /* ends loop over i */
/* if desired, threshold the output image */
if(threshold == 1){
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
if(out_image[i][j] > high){
out_image[i][j] = new_hi;
}
else{
out_image[i][j] = new_low;
}
}
}
} /* ends if threshold == 1 */
free(elements);
return(1);
} /* ends range */
/**************************************************
*
* variance(...
*
* This function replaces the pixel in the center
* of a 3x3 area with the square root of the sum
* of squares of the differences between the
* center pixel and its eight neighbors.
*
***************************************************/
int variance(the_image, out_image,
rows, cols, bits_per_pixel,
threshold, high)
int high, threshold;
short **the_image,
**out_image;
long rows, cols, bits_per_pixel;
{
int a, b, i, j, length,
max, new_hi, new_low, width;
long diff;
unsigned long sum, tmp;
new_hi = 250;
new_low = 16;
if(bits_per_pixel == 4){
new_hi = 10;
new_low = 3;
}
max = 255;
if(bits_per_pixel == 4)
max = 16;
for(i=1; i<rows-1; i++){
if( (i%10) == 0) printf("%4d", i);
for(j=1; j<cols-1; j++){
sum = 0;
for(a=-1; a<=1; a++){
for(b=-1; b<=1; b++){
if( a!=0 && b!=0){
diff = 0;
diff = the_image[i][j] -
the_image[i+a][j+b];
tmp = diff*diff;
sum = sum + tmp;
}
}
}
if(sum < 0)
printf("\nWHAT? sum < 0, %ld ,diff=%d ", sum, diff);
sum = sqrt(sum);
if(sum > max) sum = max;
out_image[i][j] = sum;
} /* ends loop over j */
} /* ends loop over i */
/* if desired, threshold the output image */
if(threshold == 1){
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
if(out_image[i][j] > high){
out_image[i][j] = new_hi;
}
else{
out_image[i][j] = new_low;
}
}
}
} /* ends if threshold == 1 */
return(1);
} /* ends variance */