-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimpackage.f90
464 lines (349 loc) · 8.34 KB
/
impackage.f90
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
subroutine grayscale(image,x,y,output) !return grayscale image
!f2py intent(in) :: image,x,y
!f2py intent(out) output
integer :: x,y
REAL :: image(1:x,1:y,1:3)
REAL :: output(1:x,1:y)
REAL :: R(1:x,1:y),G(1:x,1:y),B(1:x,1:y)
REAL :: red,green,blue
integer :: i,j
R = image(:,:,1)
G = image(:,:,2)
B = image(:,:,3)
do j = 1,y
do i = 1,x
red = R(i,j)*0.2989
green = G(i,j)*0.5870
blue = B(i,j)*0.114
output(i,j) = red + blue + green
end do
end do
end subroutine grayscale
subroutine canny(h,l,image,x,y,output) !apply canny edge detection
!f2py intent(in) :: image,x,y,h,l
!f2py intent(out) :: output
INTEGER :: x
INTEGER :: y
REAL :: image(1:x,1:y)
REAL :: h,l
Integer :: output(1:x,1:y)
REAL :: pad(1:x+4,1:y+4)
REAL :: conv(1:x,1:y)
REAL :: convpad(1:x+1,1:y+1)
REAL :: smallwindow(1:3,1:3)
REAL :: fsmallwindow(1:9)
REAL :: gfilter(1:5,1:5)
REAL :: fgauss(1:25)
REAL :: window(1:5,1:5)
REAL :: fwindow(1:25)
REAL :: point(1:25),point2(1:9),point3(1:9)
REAL :: kx(1:3,1:3),ky(1:3,1:3)
REAL :: fkx(1:9),fky(1:9)
REAL :: filtx(1:x,1:y), filty(1:x,1:y)
REAL :: IX,IY
REAL :: G(1:x,1:y), theta(1:x,1:y)
REAL :: PI = 3.1415926535897
REAL :: angle
REAL :: highThresh, lowThresh
REAL :: thresh(1:x,1:y)
INTEGER :: i,j,k,q,u
REAL :: b1,b2
!initialize filter
gfilter(:,1) = (/ 2.,4.,5.,4.,2. /)
gfilter(:,2) = (/ 4.,9.,12.,9.,4. /)
gfilter(:,3) = (/ 5.,12.,15.,12.,5. /)
gfilter(:,4) = (/ 4.,9.,12.,9.,4. /)
gfilter(:,5) = (/ 2.,4.,5.,4.,2. /)
do j = 1,5
do i = 1,5
gfilter(i,j) = gfilter(i,j)/159.0
end do
end do
fgauss = pack(gfilter, .TRUE.)
!pad the image
pad = 0.0
do j = 3,y+2
do i = 3,x+2
pad(i,j) = image(i-2,j-2)
end do
end do
!convolve
do j = 3,y+2
do i = 3,x+2
q=-2
do k = 1,5
window(:,k) = pad(i-2:i+2,j+q)
q = q+1
end do
fwindow = pack(window, .TRUE.)
do u = 1,25
point(u) = fwindow(u)*fgauss(u)
end do
conv(i-2,j-2) = sum(point)
end do
end do
!gradient -> convolve again
kx(:,1) = (/1,2,1/)
kx(:,2) = (/0,0,0/)
kx(:,3) = (/-1,-2,-1/)
ky(:,1) = (/-1,0,1/)
ky(:,2) = (/-2,0,2/)
ky(:,3) = (/-1,0,1/)
fkx = pack(kx, .TRUE.)
fky = pack(ky, .TRUE.)
convpad = 0.0
do j = 2,y+1
do i = 2,x+1
convpad(i,j) = conv(i-1,j-1)
end do
end do
do j = 2,y+1
do i = 2,x+1
q = -1
do k = 1,3
smallwindow(:,k) = convpad(i-1:i+1,j+q)
q = q+1
end do
fsmallwindow = pack(smallwindow, .TRUE.)
do u = 1,9
point2(u) = fsmallwindow(u) * fkx(u)
point3(u) = fsmallwindow(u) * fky(u)
end do
filtx(i-1,j-1) = sum(point2)
filty(i-1,j-1) = sum(point3)
end do
end do
do j = 1,y
do i = 1,x
IX = filtx(i,j)
IY = filty(i,j)
G(i,j) = sqrt((IX*IX) + (IY*IY))
theta(i,j) = ATAN2(IY,IX) * 180./PI
if (theta(i,j) < 0)THEN
theta(i,j) = theta(i,j) + 180
end if
end do
end do
!non max suppression
thresh = 0.0
do j = 2,y-1
do i = 2,x-1
angle = theta(i,j)
if (((angle >= 0) .and. (angle < 22.5)) .or. ((angle >= 15.*180./8.) .and. (angle <= 360.)))THEN
b1 = G(i,j-1)
b2 = G(i,j+1)
else if ((angle >= 22.5) .and. (angle < 3.*180./8.).or.((angle >= 9.*180./8.).and.(angle<11.*180./8.)))THEN
b1 = G(i+1,j-1)
b2 = G(i-1,j+1)
else if ((angle >= 3.*180./8.).and.(angle<5.*180./8.).or.((angle >= 11.*180./8.).and.(angle<13.*180./8.)))THEN
b1 = G(i-1,j)
b2 = G(i+1,j)
else
b1 = G(i-1,j-1)
b2 = G(i+1,j+1)
end if
if ((G(i,j) >= b1) .and. (G(i,j) >= b2)) THEN
thresh(i,j) = G(i,j)
else
thresh(i,j) = 0.0
end if
end do
end do
!Double Thresholding
highThresh = h*255.
lowThresh = l*255.
do j = 1,y
do i = 1,x
if (thresh(i,j) >= highThresh) THEN
thresh(i,j) = 255
else if (thresh(i,j) < lowThresh) THEN
thresh(i,j) = 0
else if ((thresh(i,j) < highThresh) .and. (thresh(i,j) >= lowThresh)) THEN
thresh(i,j) = lowThresh
end if
end do
end do
!hysteresis
smallwindow = 0.0
output = 0.0
do j = 2,y-1
do i = 2,x-1
output(i,j) = INT(thresh(i,j)/255)
if (thresh(i,j) == lowThresh)THEN
q = -1
do k = 1,3
smallwindow(:,k) = thresh(i-1:i+1,j+q)
q = q+1
end do
fsmallwindow = pack(smallwindow, .TRUE.)
if (maxval(fsmallwindow) == 255) THEN
output(i,j) = 1
else
output(i,j) = 0
end if
end if
end do
end do
end subroutine canny
subroutine hough(MD,image,x,y,output)
!f2py intent(in) :: MD,image,x,y
!f2py intent(out) :: output
INTEGER :: x,y
INTEGER :: image(1:x,1:y)
REAL :: theta(1:180)
INTEGER :: MD
REAL :: output(1:2*MD,1:180)
REAL :: PI = 3.1415926535897
INTEGER :: j,i,k,r,t
t = -90
do k = 1,180
theta(k) = t * PI/180.
t = t+1
end do
output = 0.0
do i = 1,x
do j = 1,y
if (image(i,j) > 0) THEN
do k = 1,180
r = (i-1)*COS(theta(k))+(j-1)*SIN(theta(k))
output(int(r)+MD,k) = output(int(r)+MD,k)+1
end do
end if
end do
end do
end subroutine hough
!overlay subroutine
!overlay black portions onto grayscale image
subroutine overlay3(image,filt,x,y,output)
!f2py intent(in) :: image,filt, x, y
!f2py intent(out) :: output
INTEGER :: x,y,k
INTEGER :: image(1:x,1:y,1:3)
INTEGER :: filt(1:x,1:y)
INTEGER :: output(1:x,1:y,1:3)
do j = 1,y
do i = 1,x
if (filt(i,j) == 1) THEN
do k = 1,3
output(i,j,k) = image(i,j,k)
end do
else
do k = 1,3
output(i,j,k) = 255!image(i,j,k)
end do
end if
end do
end do
end subroutine overlay3
subroutine overlay(filt,image,x,y,output)
!f2py intent(in) :: image,filt, x, y
!f2py intent(out) :: output
INTEGER :: x,y
REAL :: image(1:x,1:y),filt(1:x,1:y)
REAL :: output(1:x,1:y)
do j = 1,y
do i = 1,x
if (filt(i,j) == 1) THEN
image(i,j) = 1
end if
end do
end do
output = image
end subroutine overlay
subroutine flip(image,x,y,output)
!f2py intent(in):: image,x,y
!f2py intent(out):: output
INTEGER :: x,y
INTEGER :: image(1:x,1:y)
INTEGER :: output(1:x,1:y)
INTEGER :: i,j
do j = 1,y
do i = 1,x
if (image(i,j) == 0)THEN
output(i,j) = 1
else
output(i,j) = 0
end if
end do
end do
end subroutine flip
subroutine dilate(lim,image,x,y,output)
!f2py intent(in) :: lim,image,x,y
!f2py intent(out) :: output
INTEGER :: x,y,lim
INTEGER :: image(1:x,1:y),pad(1:x+4,1:y+4)
INTEGER :: output(1:x,1:y)
INTEGER :: i,j,k,q
INTEGER :: window(1:5,1:5)
INTEGER :: fwindow(1:25)
pad = 1
output = image
do j = 3,y+2
do i = 3,x+2
pad(i,j) = image(i-2,j-2)
end do
end do
do j = 3,y+2
do i = 3,x+2
q=-2
do k = 1,5
window(:,k) = pad(i-2:i+2,j+q)
q = q+1
end do
fwindow = pack(window, .TRUE.)
if (sum(fwindow) >= lim) THEN
output(i-2,j-2) = 1
end if
end do
end do
end subroutine dilate
subroutine fill(lim,image,x,y,output)
!f2py intent(in) :: lim,image,x,y
!f2py intent(out) :: output
INTEGER :: x,y,lim
INTEGER :: image(1:x,1:y),pad(1:x+4,1:y+4)
INTEGER :: output(1:x,1:y)
INTEGER :: i,j,k,q
INTEGER :: window(1:5,1:5)
INTEGER :: fwindow(1:25)
pad = 1
output = image
do j = 3,y+2
do i = 3,x+2
pad(i,j) = image(i-2,j-2)
end do
end do
if (pad(i,j) == 1)THEN
do j = 3,y+2
do i = 3,x+2
q=-2
do k = 1,5
window(:,k) = pad(i-2:i+2,j+q)
q = q+1
end do
fwindow = pack(window, .TRUE.)
if (sum(fwindow) <= lim) THEN
output(i-2,j-2) = 0
end if
end do
end do
end if
end subroutine fill
subroutine binarize(thresh,image,x,y,output)
!f2py intent(in) :: thresh,image,x,y
!f2py intent(out) :: output
INTEGER :: x,y
REAL :: image(1:x,1:y)
REAL :: thresh
INTEGER :: output(1:x,1:y)
INTEGER :: i,j
do j = 1,y
do i = 1,x
if (image(i,j) >= thresh)THEN
output(i,j) = 1
else
output(i,j) = 0
end if
end do
end do
end subroutine binarize