-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathedge3.c
executable file
·227 lines (179 loc) · 5.3 KB
/
edge3.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
/***********************************************
*
* file d:\cips\edge3.c
*
* Functions: This file contains
* gaussian_edge
* enhance_edges
*
* Purpose:
* These functions implement several
* types of advanced edge detection.
*
* External Calls:
* none
*
* Modifications:
* 26 March 1991 - created
* May 10, 1998 - modified routines to work
* with an entire image in one array.
*
***********************************************/
#include "cips.h"
short enhance_mask[3][3] = {
{-1, 0, -1},
{ 0, 4, 0},
{-1, 0, -1} };
short g7[7][7] = {
{ 0, 0, -1, -1, -1, 0, 0},
{ 0, -2, -3, -3, -3, -2, 0},
{ -1, -3, 5, 5, 5, -3, -1},
{ -1, -3, 5, 16, 5, -3, -1},
{ -1, -3, 5, 5, 5, -3, -1},
{ 0, -2, -3, -3, -3, -2, 0},
{ 0, 0, -1, -1, -1, 0, 0}};
short g9[9][9] = {
{ 0, 0, 0, -1, -1, -1, 0, 0, 0},
{ 0, -2, -3, -3, -3, -3, -3, -2, 0},
{ 0, -3, -2, -1, -1, -1, -2, -3, 0},
{ -1, -3, -1, 9, 9, 9, -1, -3, -1},
{ -1, -3, -1, 9, 19, 9, -1, -3, -1},
{ -1, -3, -1, 9, 9, 9, -1, -3, -1},
{ 0, -3, -2, -1, -1, -1, -2, -3, 0},
{ 0, -2, -3, -3, -3, -3, -3, -2, 0},
{ 0, 0, 0, -1, -1, -1, 0, 0, 0}};
/************************************************
*
* gaussian_edge(...
*
*
*************************************************/
gaussian_edge(the_image, out_image,
rows, cols, bits_per_pixel,
size, threshold, high)
int high, size, threshold;
short **the_image,
**out_image;
long rows, cols, bits_per_pixel;
{
char response[80];
long sum;
int a, b, absdiff, absmax, diff, i, j,
length, lower, max, new_hi, new_low,
scale, starti, stopi, startj, stopj,
upper, 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;
if(size == 7){
lower = -3;
upper = 4;
starti = 3;
startj = 3;
stopi = rows-3;
stopj = cols-3;
scale = 2;
}
if(size == 9){
lower = -4;
upper = 5;
starti = 4;
startj = 4;
stopi = rows-4;
stopj = cols-4;
scale = 2;
}
for(i=0; i<rows; i++)
for(j=0; j<cols; j++)
out_image[i][j] = 0;
for(i=starti; i<stopi; i++){
if ( (i%10) == 0) printf(" i=%d", i);
for(j=startj; j<stopj; j++){
sum = 0;
for(a=lower; a<upper; a++){
for(b=lower; b<upper; b++){
if(size == 7)
sum = sum + the_image[i+a][j+b] *
g7[a+3][b+3];
if(size == 9)
sum = sum + the_image[i+a][j+b] *
g9[a+4][b+4];
} /* ends loop over a */
} /* ends loop over b */
if(sum < 0) sum = 0;
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 */
} /* ends gaussian_edge */
/*******************************************
*
* enhance_edges(...
*
* This function enhances the edges in an
* input image and writes the enhanced
* result to an output image. It operates
* much the same way as detect_edges
* except it uses only one type of mask.
*
* The threshold and high parameters perform
* a different role in this function. The
* threshold parameter does not exist. The
* high parameter determines if the edge is
* strong enough to enhance or change the
* input image.
*
*******************************************/
enhance_edges(the_image, out_image,
rows, cols, bits_per_pixel, high)
int high;
short **the_image,
**out_image;
long rows, cols, bits_per_pixel;
{
int a, b, i, j, k,
length, max, new_hi,
new_lo, sum, width;
max = 255;
if(bits_per_pixel == 4)
max = 16;
/* Do convolution over image array */
for(i=1; i<rows-1; i++){
if( (i%10) == 0) printf("%d ", i);
for(j=1; j<cols-1; j++){
sum = 0;
for(a=-1; a<2; a++){
for(b=-1; b<2; b++){
sum = sum +
the_image[i+a][j+b] *
enhance_mask[a+1][b+1];
}
}
if(sum < 0) sum = 0;
if(sum > max) sum = max;
if(sum > high)
out_image[i][j] = max;
else
out_image[i][j] = the_image[i][j];
} /* ends loop over j */
} /* ends loop over i */
} /* ends enhance_edges */