-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDistance_Transform.py
215 lines (162 loc) · 6.82 KB
/
Distance_Transform.py
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
import numpy as np
from matplotlib import pyplot as plt
import math
from collections import OrderedDict
#build Distance Transfrom for generation of Medial Axis#
#Assignment 2#
filename = '/Users/shivani/Downloads/comb.img'
output = '/Users/shivani/Downloads/outputCV.img'
output1 = '/Users/shivani/Downloads/BTimg.img'
with open(filename , 'rb') as in_file:
with open(output, 'wb') as out_file:
out_file.write(in_file.read()[512:])
fo = open(output, 'rb')
Output512 = np.fromfile(output, dtype='uint8', sep="")
Output512 = Output512.reshape([512, 512])
with open(output , 'rb') as out_file1:
myArr = bytearray(out_file1.read())
# Threshold the image for the value T = 128 to generate the binary image Bt
i = 0
myArrNew = myArr
for value in myArr :
if(value <= 128 ) :
myArrNew[i] = 2
else:
myArrNew[i] = 0
i = i + 1
with open(output1, 'wb') as out_file:
out_file.write(myArrNew)
BTimage = np.fromfile(output1, dtype='uint8', sep="")
BTimage = BTimage.reshape([512, 512])
def buildDistanceTransfrom(BTimage):
#set all values of pixels to 1 which are not 0
for x in range(0,512,1):
for y in range(0,512,1):
if BTimage[x][y] != 0:
BTimage[x][y] = 1
counter = 0
while True:
try:
BTimageold = BTimage
tempImage1 = np.copy(BTimageold)
counter = counter + 1
for x in range(0,512,1):
for y in range(0,512,1):
if BTimage[x][y] != 0:
if x - 1 >= 0 and y - 1 >= 0 and x + 1 < 512 and y + 1 < 512:
fourNeighbor = [int(BTimage[x][y]), int(BTimage[x - 1][y]), int(BTimage[x][y + 1]),
int(BTimage[x + 1][y]), int(BTimage[x][y - 1])]
BTimage[x][y] = 1 + min(fourNeighbor)
if np.array_equal(tempImage1,BTimage): # tempImage1 is same as previous BTimage:
break
except:
if np.array_equal(tempImage1, BTimage):
break
# generate medial axis
def generateMedialAxis(BTimage):
tempImage = BTimage
tempImageNew = np.copy(tempImage)
medialAxisArray = np.copy(tempImage)
for x in range(0,512,1):
for y in range(0,512,1):
tempImageNew[x][y] = 0
medialAxisArray[x][y] = 0
for x in range(0,512,1):
for y in range(0,512,1):
if tempImage[x][y] != 0 :
if x - 1 >= 0 and y - 1 >= 0 and x + 1 < 512 and y + 1 < 512:
localmaxima = max(int(BTimage[x][y]),int(BTimage[x-1][y]),int(BTimage[x][y+1]),int(BTimage[x+1][y]),int(BTimage[x][y-1]))
if int(BTimage[x][y]) >= localmaxima:
tempImageNew[x][y] = 255
medialAxisArray[x][y] = BTimage[x][y]
for x in range(0,512,1):
for y in range(0,512,1):
if BTimage[x][y] != 0 :
BTimage[x][y] = 255
plt.imshow(tempImageNew,cmap='gray')
plt.show()
return medialAxisArray
buildDistanceTransfrom(BTimage)
medialAxisArr = generateMedialAxis(BTimage)
def reconstructImageNew(medialAxisArr):
# do post processing on the medial Axis Array
ravelMedialArr = set(medialAxisArr.ravel())
valueList = list(ravelMedialArr) # contains all the unique lables in medial arr
valueList.remove(0)
tempArr = np.copy(medialAxisArr) #copy the medial axis array to temperory array for processing
while True:
maximum = max(valueList) # select maximum from the pixel set
for x in range(0, 512, 1):
for y in range(0, 512, 1):
if tempArr[x][y] == maximum:
# extend left
i = x
j = y
leftdistance = int(tempArr[x][y])
tempArr[i][j] = tempArr[x][y]
while True:
j = j - 1
leftdistance = leftdistance - 1
if leftdistance <= 0 or j < 0:
break
if j >= 0:
if tempArr[i][j] < tempArr[i][j + 1] :
tempArr[i][j] = tempArr[i][j + 1] - 1
else:
break
# extend right
i = x
j = y
rightdistance = int(tempArr[x][y])
tempArr[i][j] = tempArr[x][y]
while True:
j = j + 1
rightdistance = rightdistance - 1
if rightdistance <= 0 or j > 511:
break
if j <= 511:
if tempArr[i][j] < tempArr[i][j - 1] :
tempArr[i][j] = tempArr[i][j - 1] - 1
else:
break
# extend up
i = x
j = y
updistance = int(tempArr[x][y])
tempArr[i][j] = tempArr[x][y]
while True:
i = i - 1
updistance = updistance - 1
if updistance <= 0 or i < 0:
break
if i >= 0:
if tempArr[i][j] < tempArr[i + 1][j] :
tempArr[i][j] = tempArr[i + 1][j] - 1
else:
break
#extend down
i = x
j = y
downdistance = int(tempArr[x][y])
tempArr[i][j] = tempArr[x][y]
while True:
i = i + 1
downdistance = downdistance - 1
if downdistance <= 0 or i > 511:
break
if i <= 511:
if tempArr[i][j] < tempArr[i - 1][j]:
tempArr[i][j] = tempArr[i - 1][j] - 1
else:
break
valueList.remove(maximum) # delete one the maximum pixel value data is processed for the iteration
if len(valueList) == 0:
break
# create binary image for display
for x in range(0,512,1):
for y in range(0,512,1):
if tempArr[x][y] != 0 :
tempArr[x][y] = 255
plt.imshow(tempArr, cmap='gray')
plt.show()
reconstructImageNew(medialAxisArr)