-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunExample.py
More file actions
253 lines (226 loc) · 14.1 KB
/
runExample.py
File metadata and controls
253 lines (226 loc) · 14.1 KB
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
# obtain pixel list, bitslist
import readpixel
image = 'G:\pythoncode\segmentationpitre\\4.bmp'
headers_messge, bits_list = readpixel.readpixelfunction(image)
# print(headersmessge)
# exchange to RGB matrix, rgbMatrix
import listtoMatrix
rgb_Matrix = listtoMatrix.listtoMatrixfunction(headers_messge['biWidth'], headers_messge['biHeight'], 3, bits_list)
# print(len(rgbMatrix), len(rgbMatrix[0]))
# obtain graylist, graylist
import graylist
gray_list_from_rgbMatrix = graylist.obtaingraylist(rgb_Matrix)
# obtain gray matrix, grayMatrix
gray_Matrix = listtoMatrix.listtoMatrixfunction(headers_messge['biWidth'], headers_messge['biHeight'], 1, gray_list_from_rgbMatrix)
# print(len(grayMatrix), len(grayMatrix[0]))
# obtain gray counts list and gray list
import grayandcount
signal_graylist, counts_list, counts_gray_list = grayandcount.obtaincountgray(gray_Matrix)
# test countslist
# sumcount = 0
# for x in countslist:
# sumcount += x[1]
# print(sumcount)
# obtain count peaks list, peaks_maxtab
import peakdet
peaks_maxtab, peaks_mintab = peakdet.peakdetfunction(counts_list, 100, signal_graylist)
# two marked matrix: visitedMatrix, regionMatrix. visitedMatrix describes whether this pixel have been visited or not, and regionMatrix describes what this pixel belongs to.
'''zero_list = []
i = 0
while i < len(gray_list_from_rgbMatrix):
zero_list.append(0)
i += 1
print("in-----------------------------------------------------------in")'''
# possessed_Matrix = [[0 for i in range(headers_messge['biWidth'])] for j in range(headers_messge['biHeight'])]
# region_Matrix = [[0 for i in range(headers_messge['biWidth'])] for j in range(headers_messge['biHeight'])]
# test
# print(len(visited_Matrix), len(visited_Matrix[0]))
# print(len(region_Matrix), len(region_Matrix[0]))
# obtain sorted seed's pixel
# ensure sorted function compare the first item
def ensurecompareitem(item):
return item[1]
sorted_peaks_maxtab = sorted(peaks_maxtab, key = ensurecompareitem, reverse= True)
# print(sorted_peaks_maxtab)
# seprate seed pixels from sorted_peaks_maxtab
seed_pixel_list = [row[0] for row in sorted_peaks_maxtab]
# print(seed_pixel)
# obtain average value of every column Standard deviation
import standarddev
wholemean = standarddev.Matrixstdfuntion(gray_Matrix)
print(wholemean)
# solve present segmentation average value
'''def grayMeanfromcoordMatrix(coordMatrix, grayMatrix):
graysum = 0
graynum = 0
mean = 0
for x in coordMatrix:
graysum += grayMatrix[x[0]][x[1]]
graynum += 1
mean = graysum * 1.0 /graynum
return mean'''
# generate coordinate Matrix
coordinate_Matrix = []
row_count = 0
while row_count < headers_messge['biHeight']:
col_count = 0
temp = []
while col_count < headers_messge['biWidth']:
temp.append([row_count, col_count])
col_count += 1
coordinate_Matrix.extend(temp)
row_count += 1
# region growing algorithm
# determin seed
import findseed
from collections import deque
ith = 0
allsegments_list = []
# segmentation_label = 'a'
while ith < len(seed_pixel_list):
print("____________________________________________________________________________", ith)
present_seed = findseed.findseedfunction(gray_Matrix, coordinate_Matrix, seed_pixel_list[ith])
if present_seed[0] == -1 or present_seed[1] == -1:
ith += 1
continue
else:
# possessed_Matrix[present_seed[0]][present_seed[1]] = 1
# region_Matrix[present_seed[0]][present_seed[1]] = 'a'
temp_segmentation_ele = [present_seed]
# remember deque([seed_pixel_list[ith]])
queue_seed = deque([present_seed])
coordinate_Matrix.remove(present_seed)
#
last_count = 1
last_sum = gray_Matrix[present_seed[0]][present_seed[1]] * 1.0
while queue_seed:
# print(queue_seed)
present_seed_centre = queue_seed.popleft()
print("present_seed_centre", present_seed_centre)
if present_seed_centre[0] + 1 < len(gray_Matrix):
temp_mean = last_sum / last_count
if [present_seed_centre[0] + 1, present_seed_centre[1]] in coordinate_Matrix and abs(temp_mean - gray_Matrix[present_seed_centre[0] + 1][present_seed_centre[1]]) <= wholemean: # and possessed_Matrix[present_seed_centre[0] + 1][present_seed_centre[1]] == 0
queue_seed.append([present_seed_centre[0] + 1, present_seed_centre[1]])
# region_Matrix[present_seed_centre[0] + 1][present_seed_centre[1]] = segmentation_label
# possessed_Matrix[present_seed_centre[0] + 1][present_seed_centre[1]] = 1
temp_segmentation_ele.append([present_seed_centre[0] + 1, present_seed_centre[1]])
coordinate_Matrix.remove([present_seed_centre[0] + 1, present_seed_centre[1]])
last_sum = temp_mean * last_count + gray_Matrix[present_seed_centre[0] + 1][present_seed_centre[1]]
last_count += 1
if present_seed_centre[0] + 1 < len(gray_Matrix)and present_seed_centre[1] + 1 < len(gray_Matrix[present_seed_centre[0] + 1]):
temp_mean = last_sum / last_count
if [present_seed_centre[0] + 1, present_seed_centre[1] + 1] in coordinate_Matrix and abs(temp_mean - gray_Matrix[present_seed_centre[0] + 1][present_seed_centre[1] + 1]) <= wholemean: # and possessed_Matrix[present_seed_centre[0] + 1][present_seed_centre[1] + 1] == 0
queue_seed.append([present_seed_centre[0] + 1, present_seed_centre[1] + 1])
# region_Matrix[present_seed_centre[0] + 1][present_seed_centre[1] + 1] = segmentation_label
# possessed_Matrix[present_seed_centre[0] + 1][present_seed_centre[1] + 1] = 1
temp_segmentation_ele.append([present_seed_centre[0] + 1, present_seed_centre[1] + 1])
coordinate_Matrix.remove([present_seed_centre[0] + 1, present_seed_centre[1] + 1])
last_sum = temp_mean * last_count + gray_Matrix[present_seed_centre[0] + 1][present_seed_centre[1] + 1]
last_count += 1
if present_seed_centre[1] + 1 < len(gray_Matrix[present_seed_centre[0]]):
temp_mean = last_sum / last_count
if [present_seed_centre[0], present_seed_centre[1] + 1] in coordinate_Matrix and abs(temp_mean - gray_Matrix[present_seed_centre[0]][present_seed_centre[1] + 1]) <= wholemean: # and possessed_Matrix[present_seed_centre[0]][present_seed_centre[1] + 1] == 0
queue_seed.append([present_seed_centre[0], present_seed_centre[1] + 1])
# region_Matrix[present_seed_centre[0]][present_seed_centre[1] + 1] = segmentation_label
# possessed_Matrix[present_seed_centre[0]][present_seed_centre[1] + 1] = 1
temp_segmentation_ele.append([present_seed_centre[0], present_seed_centre[1] + 1])
coordinate_Matrix.remove([present_seed_centre[0], present_seed_centre[1] + 1])
last_sum = temp_mean * last_count + gray_Matrix[present_seed_centre[0]][present_seed_centre[1] + 1]
last_count += 1
if present_seed_centre[0] - 1 > 0 and present_seed_centre[1] + 1 < len(gray_Matrix[present_seed_centre[0] - 1]):
temp_mean = last_sum / last_count
if [present_seed_centre[0] - 1, present_seed_centre[1] + 1] in coordinate_Matrix and abs(temp_mean - gray_Matrix[present_seed_centre[0] - 1][present_seed_centre[1] + 1]) <= wholemean: # and possessed_Matrix[present_seed_centre[0] - 1][present_seed_centre[1] + 1] == 0
queue_seed.append([present_seed_centre[0] - 1, present_seed_centre[1] + 1])
# region_Matrix[present_seed_centre[0] - 1][present_seed_centre[1] + 1] = segmentation_label
# possessed_Matrix[present_seed_centre[0] - 1][present_seed_centre[1] + 1] = 1
temp_segmentation_ele.append([present_seed_centre[0] - 1, present_seed_centre[1] + 1])
coordinate_Matrix.remove([present_seed_centre[0] - 1, present_seed_centre[1] + 1])
last_sum = temp_mean * last_count + gray_Matrix[present_seed_centre[0] - 1][present_seed_centre[1] + 1]
last_count += 1
if present_seed_centre[0] - 1 > 0:
temp_mean = last_sum / last_count
if [present_seed_centre[0] - 1, present_seed_centre[1]] in coordinate_Matrix and abs(temp_mean - gray_Matrix[present_seed_centre[0] - 1][present_seed_centre[1]]) <= wholemean: # and possessed_Matrix[present_seed_centre[0] - 1][present_seed_centre[1]] == 0
queue_seed.append([present_seed_centre[0] - 1, present_seed_centre[1]])
# region_Matrix[present_seed_centre[0] - 1][present_seed_centre[1]] = segmentation_label
# possessed_Matrix[present_seed_centre[0] - 1][present_seed_centre[1]] = 1
temp_segmentation_ele.append([present_seed_centre[0] - 1, present_seed_centre[1]])
coordinate_Matrix.remove([present_seed_centre[0] - 1, present_seed_centre[1]])
last_sum = temp_mean * last_count + gray_Matrix[present_seed_centre[0] - 1][present_seed_centre[1]]
last_count += 1
if present_seed_centre[0] - 1 > 0 and present_seed_centre[1] - 1 > 0:
temp_mean = last_sum / last_count
if [present_seed_centre[0] - 1, present_seed_centre[1] - 1] in coordinate_Matrix and abs(temp_mean - gray_Matrix[present_seed_centre[0] - 1][present_seed_centre[1] - 1]) <= wholemean: # possessed_Matrix[present_seed_centre[0] - 1][present_seed_centre[1] -1] == 0
queue_seed.append([present_seed_centre[0] - 1, present_seed_centre[1] - 1])
# region_Matrix[present_seed_centre[0] - 1][present_seed_centre[1] - 1] = segmentation_label
# possessed_Matrix[present_seed_centre[0] - 1][present_seed_centre[1] - 1] = 1
temp_segmentation_ele.append([present_seed_centre[0] - 1, present_seed_centre[1] - 1])
coordinate_Matrix.remove([present_seed_centre[0] - 1, present_seed_centre[1] - 1])
last_sum = temp_mean * last_count + gray_Matrix[present_seed_centre[0] - 1][present_seed_centre[1] - 1]
last_count += 1
if present_seed_centre[1] - 1 > 0:
temp_mean = last_sum / last_count
if [present_seed_centre[0], present_seed_centre[1] - 1] in coordinate_Matrix and abs(temp_mean - gray_Matrix[present_seed_centre[0]][present_seed_centre[1] - 1]) <= wholemean: # and possessed_Matrix[present_seed_centre[0]][present_seed_centre[1] -1] == 0
queue_seed.append([present_seed_centre[0], present_seed_centre[1] - 1])
# region_Matrix[present_seed_centre[0]][present_seed_centre[1] - 1] = segmentation_label
# possessed_Matrix[present_seed_centre[0]][present_seed_centre[1] - 1] = 1
temp_segmentation_ele.append([present_seed_centre[0], present_seed_centre[1] - 1])
coordinate_Matrix.remove([present_seed_centre[0], present_seed_centre[1] - 1])
last_sum = temp_mean * last_count + gray_Matrix[present_seed_centre[0]][present_seed_centre[1] - 1]
last_count += 1
if present_seed_centre[0] + 1 < len(gray_Matrix) and present_seed_centre[1] - 1 > 0:
temp_mean = last_sum / last_count
if [present_seed_centre[0] + 1, present_seed_centre[1] - 1] in coordinate_Matrix and abs(temp_mean - gray_Matrix[present_seed_centre[0] + 1][present_seed_centre[1] - 1]) <= wholemean: # and possessed_Matrix[present_seed_centre[0] + 1][present_seed_centre[1] -1] == 0
queue_seed.append([present_seed_centre[0] + 1, present_seed_centre[1] - 1])
# region_Matrix[present_seed_centre[0] + 1][present_seed_centre[1] - 1] = segmentation_label
# possessed_Matrix[present_seed_centre[0] + 1][present_seed_centre[1] - 1] = 1
temp_segmentation_ele.append([present_seed_centre[0] + 1, present_seed_centre[1] - 1])
coordinate_Matrix.remove([present_seed_centre[0] + 1, present_seed_centre[1] - 1])
last_sum = temp_mean * last_count + gray_Matrix[present_seed_centre[0] + 1][present_seed_centre[1] - 1]
last_count += 1
allsegments_list.append(temp_segmentation_ele)
#segmentation_label = chr(ord('a') + 1)
ith += 1
print(len(allsegments_list),allsegments_list)
# color different segmentations
for x in allsegments_list[0]:
rgb_Matrix[x[0]][x[1]][0] = 255
rgb_Matrix[x[0]][x[1]][1] = 0
rgb_Matrix[x[0]][x[1]][2] = 0
for x in allsegments_list[1]:
rgb_Matrix[x[0]][x[1]][0] = 0
rgb_Matrix[x[0]][x[1]][1] = 255
rgb_Matrix[x[0]][x[1]][2] = 0
for x in allsegments_list[2]:
rgb_Matrix[x[0]][x[1]][0] = 0
rgb_Matrix[x[0]][x[1]][1] = 0
rgb_Matrix[x[0]][x[1]][2] = 255
for x in allsegments_list[3]:
rgb_Matrix[x[0]][x[1]][0] = 255
rgb_Matrix[x[0]][x[1]][1] = 255
rgb_Matrix[x[0]][x[1]][2] = 255
for x in allsegments_list[4]:
rgb_Matrix[x[0]][x[1]][0] = 0
rgb_Matrix[x[0]][x[1]][1] = 0
rgb_Matrix[x[0]][x[1]][2] = 0
# sava picture
import generateBMP
generateBMP.generateBMPfunction('G:\pythoncode\\result\\4_seg.bmp', headers_messge, rgb_Matrix)
# exchange bmp to png
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mping
img = Image.open('G:\pythoncode\\result\\4_seg.bmp')
img.save("G:\pythoncode\\result\\4_seg.png")
img.close()
img1 = Image.open('G:\pythoncode\segmentationpitre\\4.bmp')
img1.save('G:\pythoncode\\result\\4.png')
img1.close()
# display png picture
imgmat = mping.imread('G:\pythoncode\\result\\4_seg.png')
imgmat1 = mping.imread('G:\pythoncode\\result\\4.png')
plt.subplot(1, 2, 1)
implot1 = plt.imshow(imgmat1)
plt.subplot(1, 2, 2)
implot1 = plt.imshow(imgmat)
plt.show()
plt.close()