-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataManipulation.py
More file actions
938 lines (744 loc) · 28.4 KB
/
Copy pathdataManipulation.py
File metadata and controls
938 lines (744 loc) · 28.4 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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
import ast
import json
import h5py
import open3d as o3d
from skimage import measure
from PIL import Image, ImageSequence, ImageColor
import numpy as np
import matplotlib as mpl
defaultMatplotlibBackend = mpl.get_backend()
defaultMatplotlibBackend = 'TkAgg'
from matplotlib import pyplot as plt
from utils import *
def getImageFromDataset(inputDataset, zindex):
"""When given a dataset filename of either a .tif, .json, or .txt dataset and a zindex, returns the image at that index of the dataset
Parameters
----------
inputDataset : str
The filepath of the dataset you want to get an image from
zindex : int
The z index at which you want to get a 2D image slice from
Returns
-------
PIL.Image
A PIL Image that is a 2D slice of `inputDataset` perpendicular to the z plan at location `zindex`
Raises
------
Exception
If the filename passed does not end in '.tif', '.tiff', '.json', or '.txt'
"""
if inputDataset[-4:] == '.tif' or inputDataset[-5:] == '.tiff':
img = Image.open(inputDataset)
img.seek(zindex)
return img
elif inputDataset[-5:] == '.json':
with open(inputDataset, 'r') as inFile:
d = json.load(inFile)
img = Image.open(d['image'][zindex])
return img
elif inputDataset[-4:] == '.txt':
filteredImages = []
with open(inputDataset, 'r') as inFile:
images = inFile.readlines()
for image in images:
if len(image.strip() > 3):
filteredImages.append(image)
img = Image.open(filteredImages[zindex].strip())
return img
else:
raise Exception('Unknown Dataset filetype ' + inputDataset[inputDataset.rindex('.'):] + ' Passed to getImageFromDataset')
def create2DLabelCheckSemanticImage(inputDataset, modelOutputDataset, z, colors = ['#ffe119', '#4363d8', '#f58231', '#dcbeff', '#800000', '#000075', '#a9a9a9', '#ffffff', '#000000']):
"""Creates an image with the raw image from `inputDataset` in the background, and the model's predictions (semantic models only) highlighted in different colors on top of it
Note: this is very similar to create2DLabelCheckSemanticImageForIndex. The difference is as follows:
create2DLabelCheckSemanticImage - Creates an image that has all semantic planes highlighted using the list of colors
create2DLabelCheckSemanticImageForIndex - Creates an image that only highlights one specified plane in red
Parameters
----------
inputDataset : str
The filepath of the dataset you want to use for the raw images
modelOutputDataset : H5 Dataset
The H5 Dataset from the a model's prediction h5 file
z : int
The z index to grab the 2D Images from
colors : list of strings of colors encoded in hex, ex: white is '#ffffff', optional
These colors will be used for the highlighting in order.
The first is used for the second semantic prediction plane (the first is presumably null)
The second is used for the third semantic prediction plane, etc.
The default list was one I found online that was supposed to be very accessible including different types of color blindness
Returns
-------
numpy.ndarray
A 5 dimensional array (x, y, r, g, b) that represents the image with model predictions highlighted different colors
"""
rawImage = getImageFromDataset(inputDataset, z)
background = np.array(rawImage.convert('RGB'))
maskList = []
modelPrediction = modelOutputDataset[:,z,:,:]
numPlanes = modelOutputDataset.shape[0]
for plane in range(1, numPlanes):
indexesToCheck = []
for i in range(numPlanes):
if i == plane:
continue
indexesToCheck.append(i)
mask = modelPrediction[indexesToCheck[0]] < modelPrediction[plane]
for i in indexesToCheck[1:]:
mask = mask & modelPrediction[i] < modelPrediction[plane]
maskList.append(mask)
colorToUse = ImageColor.getcolor(colors[plane-1], "RGB")
background[mask] = colorToUse
return background
def create2DLabelCheckSemanticImageForIndex(inputDataset, modelOutputDataset, z, plane):
"""Creates an image with the raw image from `inputDataset` in the background, and the model's predictions for the specified `plane` in red
Note: this is very similar to create2DLabelCheckSemanticImage. The difference is as follows:
create2DLabelCheckSemanticImage - Creates an image that has all semantic planes highlighted using the list of colors
create2DLabelCheckSemanticImageForIndex - Creates an image that only highlights one specified plane in red
Parameters
----------
inputDataset : str
The filepath of the dataset you want to use for the raw images
modelOutputDataset : H5 Dataset
The H5 Dataset from the a model's prediction h5 file
z : int
The z index to grab the 2D Images from
plane : int
The index of the plane to highlight on the image
Returns
-------
numpy.ndarray
A 5 dimensional array (x, y, r, g, b) that represents the image at index `z` with the semantic plane `plane` highlighted in red
"""
rawImage = getImageFromDataset(inputDataset, z)
background = np.array(rawImage.convert('RGB'))
maskList = []
modelPrediction = modelOutputDataset[:,z,:,:]
numPlanes = modelOutputDataset.shape[0]
indexesToCheck = []
for planeIter in range(0, numPlanes):
if planeIter == plane:
continue
indexesToCheck.append(planeIter)
mask = modelPrediction[indexesToCheck[0]] < modelPrediction[plane]
for i in indexesToCheck[1:]:
mask = mask & modelPrediction[i] < modelPrediction[plane]
maskList.append(mask)
colorToUse = ImageColor.getcolor("#ff0000", "RGB")
background[mask] = colorToUse
return background
def create2DLabelCheckInstanceImage(inputDataset, modelOutputDataset, z):
"""Creates an image with the raw image from `inputDataset` in the background, and the model's predictions (instance models only) are highlighted in various colors
Parameters
----------
inputDataset : str
The filepath of the dataset you want to use for the raw images
modelOutputDataset : H5 Dataset
The H5 Dataset from the a model's prediction h5 file (must be an instance model)
z : int
The z index to grab the 2D Images from
Returns
-------
numpy.ndarray
A 5 dimensional array (x, y, r, g, b) that represents the image at index `z` with the model's predictions highlighted in random colors
"""
rawImage = getImageFromDataset(inputDataset, z)
background = np.array(rawImage.convert('RGB'))
modelPrediction = modelOutputDataset[z,:,:]
for unique in np.unique(modelPrediction):
if unique == 0:
continue
mask = modelPrediction == unique
colorToUse = np.random.rand(3,) * 255
background[mask] = colorToUse
return background
def create2DLabelCheckSemantic(inputDataset, modelOutput, numberOfImages, colors = ['#ffe119', '#4363d8', '#f58231', '#dcbeff', '#800000', '#000075', '#a9a9a9', '#ffffff', '#000000']):
"""Similar to create2DLabelCheckSemanticImage, however it opens an interactive matplotlib window that shows several examples
Parameters
----------
inputDataset : str
The filepath of the dataset you want to use for the raw images
modelOutputDataset : H5 Dataset
The H5 Dataset from the a model's prediction h5 file
numberOfImages : int
How many interactive windows to show
colors : list of hexadecimal strings. Ex. white = '#ffffff', optional
Returns
-------
None
Opens an interactive window that shows several images from create2DLabelCheckSemanticImage evenly spaced throughout the dataset. Shows `numberOfImages` images
Raises
------
Assert
If the shape of `inputDataset` and `modelOutput` are not the same
"""
mpl.use(defaultMatplotlibBackend)
h5File = h5py.File(modelOutput, 'r')
dataset = h5File['vol0']
datasetShape = dataset.shape
imageShape = getShapeOfDataset(inputDataset)
numPlanes = dataset.shape[0]
stride = int(datasetShape[1] / numberOfImages)
assert datasetShape[1:] == imageShape, "in create2DLabelCheckSemantic, inputDataset and model output do not have the same shape"
for z in range(0, datasetShape[1], stride):
rawImage = getImageFromDataset(inputDataset, z)
background = np.array(rawImage.convert('RGB'))
maskList = []
modelPrediction = dataset[:,z,:,:]
for plane in range(1, numPlanes):
background = create2DLabelCheckSemanticImage(inputDataset, dataset, z)
rawImage = np.array(rawImage)
plt.figure(figsize=(20,10))
plt.suptitle('Index: ' + str(z))
plt.subplot(121)
plt.imshow(255-rawImage, cmap='binary')
plt.title('Raw Image')
plt.subplot(122)
plt.imshow(background)
plt.title('Labelled')
plt.show()
plt.close()
h5File.close()
mpl.use('Agg')
def create2DLabelCheckInstance(inputDataset, modelOutput, numberOfImages):
"""Similar to create2DLabelCheckInstanceImage, however it opens an interactive matplotlib window that shows several examples
Parameters
----------
inputDataset : str
The filepath of the dataset you want to use for the raw images
modelOutputDataset : H5 Dataset
The H5 Dataset from the a model's prediction h5 file (must be from an instance model)
numberOfImages : int
How many interactive windows to show
Returns
-------
None
Opens an interactive window that shows several images from create2DLabelCheckInstanceImage evenly spaced throughout the dataset. Shows `numberOfImages` images
Raises
------
Assert
If the shape of `inputDataset` and `modelOutput` are not the same
"""
mpl.use(defaultMatplotlibBackend)
h5File = h5py.File(modelOutput, 'r')
dataset = h5File['processed']
datasetShape = dataset.shape
imageShape = getShapeOfDataset(inputDataset)
stride = int(datasetShape[0] / numberOfImages)
assert datasetShape == imageShape, "in create2DLabelCheckInstance, inputDataset and model output do not have the same shape"
for z in range(0, datasetShape[1], stride):
rawImage = getImageFromDataset(inputDataset, z)
background = np.array(rawImage.convert('RGB'))
maskList = []
background = create2DLabelCheckInstanceImage(inputDataset, dataset, z)
rawImage = np.array(rawImage)
plt.figure(figsize=(20,10))
plt.suptitle('Index: ' + str(z))
plt.subplot(121)
plt.imshow(255-rawImage, cmap='binary')
plt.title('Raw Image')
plt.subplot(122)
plt.imshow(background)
plt.title('Labelled')
plt.show()
plt.close()
mpl.use('Agg')
def getMetadataForH5(h5filename):
"""Gets the metadata in a models H5 File
Parameters
----------
h5filename : str
The filepath of the dataset you want to get the metadata for
Returns
-------
dictionary
Returns a dictionary of the metadata of the dataset in `h5filename`
"""
f = h5py.File(h5filename, 'r')
metadata = f['vol0'].attrs['metadata']
return ast.literal_eval(metadata)
def create3DLabelAnimationSemantic(inputDataset, modelOutput, outputDir, scaleFactor=10, colors = ['#ffe119', '#4363d8', '#f58231', '#dcbeff', '#800000', '#000075', '#a9a9a9', '#ffffff', '#000000']):
# TODO add docstring
mpl.use(defaultMatplotlibBackend)
h5File = h5py.File(modelOutput, 'r')
dataset = h5File['vol0']
datasetShape = dataset.shape
imageShape = getShapeOfDataset(inputDataset)
assert datasetShape[1:] == imageShape, "in create3DLabelAnimationSemantic, inputDataset and model output do not have the same shape"
for z in range(0, datasetShape[1]):
tc = TimeCounter(dataset.shape[1], 'minutes')
twoDImage = create2DLabelCheckSemanticImage(inputDataset, dataset, z)
maskList = []
modelPrediction = dataset[:,:z+1,:,:]
numPlanes = modelPrediction.shape[0]
for plane in range(1, numPlanes):
indexesToCheck = []
for i in range(numPlanes):
if i == plane:
continue
indexesToCheck.append(i)
mask = modelPrediction[indexesToCheck[0]] < modelPrediction[plane]
for i in indexesToCheck[1:]:
mask = mask & modelPrediction[i] < modelPrediction[plane]
maskList.append(mask)
colorToUse = ImageColor.getcolor(colors[plane-1], "RGB")
maskToPlot = maskList[0]
maskToPlot = maskToPlot[::1,::scaleFactor,::scaleFactor]
fig = plt.figure()
plt.title('Frame ' + str(z))
plt.axis('off')
ax1 = fig.add_subplot(121)
ax1.imshow(twoDImage)
ax2 = fig.add_subplot(122, projection='3d')
ax2.voxels(maskToPlot)
ax2.axes.set_zlim3d(bottom=0, top=int(dataset.shape[3] / scaleFactor + 1))
ax2.set_zlabel('Z')
ax2.axes.set_ylim3d(bottom=0, top=int(dataset.shape[2] / scaleFactor + 1))
ax2.set_ylabel('Y')
ax2.axes.set_xlim3d(left=0, right=int(dataset.shape[1] / 1 + 1))
ax2.set_xlabel('X')
plt.savefig(os.path.join(outputDir, 'frame' + str(z).zfill(4) + '.png'))
plt.close()
tc.tick()
tc.print()
h5File.close()
mpl.use('Agg')
def createTxtFileFromImageList(imageList, outputFile):
"""Given a list of images and an output filepath, creates a .txt dataset
Parameters
----------
imageList : list of strings
list of string filepaths of images to be put in the .txt dataset. Ex ['test1.tif', 'test2.tif']
outputFile : str
filepath to save the .txt dataset to
Returns
-------
None
saves a .txt file
"""
with open(outputFile, 'w') as out:
for image in imageList:
print('Writing', image)
out.write(image.strip() + '\n')
print('Completely Done, output at:', outputFile)
def createTifFromImageList(imageList, outputFile):
"""Combines a list of images into one page .tiff file
Parameters
----------
imageList : list of strings
list of string filepaths of .tif images to combine
outputFile : str
output path to save the combined .tif file to
Returns
-------
None
Saves a paged .tif file
"""
images = []
for image in imageList:
print("Reading image:", image)
if not image == '_combined.tif':
im = Image.open(image)
images.append(im)
print("Writing Combined image:", outputFile)
images[0].save(outputFile, save_all=True, append_images=images[1:])
print("Finished Combining Images")
def writeJsonForImages(imageList, outputJsonPath):
"""Creates a .json dataset from a list of images
Parameters
----------
imageList : list of strings
list of string filepaths of .tif images to combine
outputJsonPath : str
output path to save the .json file to
Returns
-------
None
Saves a .json file
"""
jsonD = {}
im1 = Image.open(imageList[0])
width, height = im1.size
jsonD["dtype"] = "uint8"
jsonD['ndim'] = 1
jsonD['tile_ratio'] = 1
jsonD['tile_st'] = [0, 0]
jsonD["image"] = imageList
jsonD["height"] = height
jsonD["width"] = width
jsonD["tile_size"] = width
jsonD["depth"] = len(imageList)
if not outputJsonPath[-5:] == '.json':
outputJsonPath = outputJsonPath + '.json'
prettyJson = json.dumps(jsonD, indent=4)
with open(outputJsonPath, 'w') as outJson:
outJson.write(prettyJson)
print('Done, writing json file output at:', outputJsonPath)
def getWeightsFromLabels(labelStack):
"""Computes the weights to use in a weighted semantic model
Parameters
----------
labelStack : str
filepath of the dataset that are labels to be used for training
Returns
-------
list
a list of normalized weights to use for training
"""
listOfImages = []
countDic = {}
if labelStack[-3:] == '.h5':
h5File = h5py.File(labelStack)
data = np.array(h5File['dataset_1'])
h5File.close()
for subImage in data:
listOfImages.append(subImage)
elif labelStack[-4:] == '.txt':
with open(labelStack, 'r') as f:
labelStack = f.readlines()
for label in labelStack:
if len(label.rstrip()) > 3:
im = Image.open(label.rstrip())
listOfImages.append(im)
elif labelStack[-5:] == '.json':
pass
elif labelStack[-4:] == '.tif' or labelStack[-5:] == '.tiff':
labelStackFile = Image.open(labelStack)
for i, page in enumerate(ImageSequence.Iterator(labelStackFile)):
listOfImages.append(page)
for im in listOfImages:
data = np.array(im)
unique, nSamples = np.unique(data, return_counts=True)
for i in range(len(unique)):
if not unique[i] in countDic:
countDic[unique[i]] = 0
countDic[unique[i]] += nSamples[i]
sortedKeys = list(sorted(list(countDic.keys())))
nSamples = []
for key in sortedKeys:
nSamples.append(countDic[key])
m = max(nSamples)
normedWeights = [1 - (x / sum(nSamples)) for x in nSamples]
return normedWeights
def instanceArrayToMesh(d, uniqueList=None):
"""Turns an instance array into a 3D triangle mesh.
Instance Array - 3D array representing space, 0s encode background and other numbers represent a unique id of an instance
Transformation of array to mesh is based on the marching cubes algorithm, and then smoothed
Each instance is painted a random color
Parameters
----------
d : numpy.ndarray
an instance array to turn into a 3D mesh
uniqueList : list, optional
not neccisary, if defined may make this process slightly faster, but is probably negligable. Wouldn't worry about using
Returns
-------
open3d.geometry.TriangleMesh
Triangle Mesh that represents the passed in instance array
"""
if not uniqueList:
uniqueList, countList = np.unique(d, return_counts=True)
maxIndex = list(countList).index(max(countList))
maxValueProb0 = uniqueList[maxIndex]
fullMesh = o3d.geometry.TriangleMesh()
deltaTracker = TimeCounter(len(uniqueList), timeUnits='minutes', prefix='Creating Mesh: ')
for subUnique in uniqueList:
if subUnique == maxValueProb0:
continue
trueMask = d == subUnique
verts, faces, normals, values = measure.marching_cubes(trueMask, 0)
verts = o3d.utility.Vector3dVector(verts)
faces = o3d.utility.Vector3iVector(faces)
subMesh = o3d.geometry.TriangleMesh(verts, faces)
subMesh.compute_vertex_normals()
subMesh.paint_uniform_color(np.random.rand(3))
fullMesh = fullMesh + subMesh
deltaTracker.tick()
deltaTracker.print()
fullMesh = fullMesh.simplify_vertex_clustering(3)
fullMesh = fullMesh.filter_smooth_taubin(number_of_iterations=100)
fullMesh.compute_vertex_normals()
return fullMesh
def instanceArrayToPointCloud(d, uniqueList=None):
"""Turns an instance array into a 3D point cloud
Instance Array - 3D array representing space, 0s encode background and other numbers represent a unique id of an instance
Each instance is painted a random color
Parameters
----------
d : numpy.ndarray
an instance array to turn into a 3D mesh
uniqueList : list, optional
not neccisary, if defined may make this process slightly faster, but is probably negligable. Wouldn't worry about using
Returns
-------
open3d.geometry.PointCloud
Point Cloud that represents the passed in instance array
"""
if not uniqueList:
uniqueList, countList = np.unique(d, return_counts=True)
maxIndex = list(countList).index(max(countList))
maxValueProb0 = uniqueList[maxIndex]
fullCloud = o3d.geometry.PointCloud()
deltaTracker = TimeCounter(len(uniqueList), timeUnits='minutes', prefix='Creating Point Cloud: ')
for subUnique in uniqueList:
if subUnique == maxValueProb0:
continue
pointListWhere = np.where(d == subUnique)
pointListWhere = np.array(pointListWhere)
pointListWhere = pointListWhere.transpose()
subCloud = o3d.geometry.PointCloud()
subCloud.points = o3d.utility.Vector3dVector(pointListWhere)
subCloud.paint_uniform_color(np.random.rand(3))
fullCloud = fullCloud + subCloud
deltaTracker.tick()
deltaTracker.print()
return fullCloud
def getMultiClassImage(imageFilepath, uniquePixels=[]):
"""Turns an image into an array where each unique color gets a value
Parameters
----------
imageFilepath : str
filepath of the image to analyze
uniquePixels : list, optional
not neccisary, if defined may make this process slightly faster, but is probably negligable. Wouldn't worry about using. Mostly exists just for getMultiClassImageStack
Returns
-------
numpy.ndarray
An array that represents the input image in `imageFilepath` where each unique color gets an integer value in this array
"""
if type(imageFilepath) == type('Test'):
im = Image.open(imageFilepath)
else:
im = imageFilepath
im = im.convert("RGBA")
data = np.array(im)
info = np.iinfo(data.dtype)
data = data.astype(np.float64) / info.max
data = 255 * data
a = data.astype(np.uint8)
b = np.zeros((a.shape[0],a.shape[1]))
c = list(b)
for i in range(a.shape[0]):
for j in range(a.shape[1]):
r,g,b,alpha = a[i,j]
value = (r,g,b)
if value in uniquePixels:
c[i][j] = uniquePixels.index(value)
else:
uniquePixels.append(value)
c[i][j] = uniquePixels.index(value)
d = np.array(c)
return d, uniquePixels
def getMultiClassImageStack(imageFilepath,uniquePixels=[]):
"""Turns a paged image (from filename, probably .tif), into a 3D Class Image
Essentially, this function calls getMultiClassImage on every page in a paged .tif file
Returns a 3D array representing all pages of a paged image
Parameters
----------
imageFilepath : str
filepath of the paged .tif image that you want to analyze
uniquePixels : list, optional
not neccisary, if defined may make this process slightly faster, but is probably negligable. Wouldn't worry about using
Returns
-------
open3d.geometry.TriangleMesh
Triangle Mesh that represents the passed in instance array
"""
labelStack = []
unique = []
im = Image.open(imageFilepath)
for i, imageSlice in enumerate(ImageSequence.Iterator(im)):
labels, unique = getMultiClassImage(imageSlice, uniquePixels=unique)
labelStack.append(labels)
return np.array(labelStack)
def createH5FromNumpy(npArray, filename):
"""Saves a numpy array as an H5 File
Parameters
----------
npArray : numpy.ndarray
the array you want to save
filename : str
the filename that you are saving the H5 file at
Returns
-------
None
Saves an H5 file at `filename`
"""
h5f = h5py.File(filename, 'w')
h5f.create_dataset('dataset_1', data=npArray)
h5f.close()
def getImagesForLabels(d, index):
indexesToCheck = []
for i in range(d.shape[0]):
if i == index:
continue
indexesToCheck.append(i)
print('first index')
mask = d[indexesToCheck[0]] < d[index]
for i in indexesToCheck[1:]:
print(i,indexesToCheck)
mask = mask & d[i] < d[index]
#TODO output as images
def getPointCloudForIndex(d, index):
"""Turns a semantic numpy array into a point cloud
Semantic Array - Numpy array in the shape of (number of planes, z, x, y)
Essentially, wherever the plane selected by `index` is greater in value than all other planes, a point is placed in the point cloud.
Parameters
----------
d : numpy.ndarray
a semantic array to turn into a 3D mesh
index : int
which plane to turn into a point cloud
Returns
-------
open3d.geometry.PointCloud
Point Cloud that represents the passed in semantic array
"""
indexesToCheck = []
for i in range(d.shape[0]):
if i == index:
continue
indexesToCheck.append(i)
print('first index')
mask = d[indexesToCheck[0]] < d[index]
for i in indexesToCheck[1:]:
print(i,indexesToCheck)
mask = mask & d[i] < d[index]
print('Creating Point List')
pointListWhere = np.where(mask == True)
pointListWhere = np.array(pointListWhere)
pointListWhere = pointListWhere.transpose()
print('Creating Cloud')
cloud = o3d.geometry.PointCloud()
cloud.points = o3d.utility.Vector3dVector(pointListWhere)
del(pointListWhere)
return cloud
def arrayToMesh(d, index):
"""Turns a semantic array into a 3D triangle mesh.
Semantic Array - Np array in the shape of (number of planes, z, x, y)
A 'mask' array is created that is a 3D array of True/False. True where that plane has the greatest value, false otherwise
Transformation of the mask array to mesh is based on the marching cubes algorithm, and then smoothed
Parameters
----------
d : numpy.ndarray
a semantic array to turn into a 3D mesh
index : int
which plane of the semantic array to create a mesh for
Returns
-------
open3d.geometry.TriangleMesh
Triangle Mesh that represents the passed in instance array
"""
indexesToCheck = []
for i in range(d.shape[0]):
if i == index:
continue
indexesToCheck.append(i)
print('first index')
mask = d[indexesToCheck[0]] < d[index]
for i in indexesToCheck[1:]:
print(i,indexesToCheck)
mask = mask & d[i] < d[index]
array = mask
print('marching_cubes')
verts, faces, normals, values = measure.marching_cubes(array, 0)
print('verts and faces')
verts = o3d.utility.Vector3dVector(verts)
faces = o3d.utility.Vector3iVector(faces)
print('creating triangles')
mesh = o3d.geometry.TriangleMesh(verts, faces)
print('calculating normals')
mesh.compute_vertex_normals()
print('Simplification Calculations')
mesh = mesh.simplify_vertex_clustering(3)
mesh = mesh.filter_smooth_taubin(number_of_iterations=100)
print('Calculating final Normals')
mesh.compute_vertex_normals()
return mesh
def subSampled3DH5(dataset, sampleFactor, cubeSize = 1000):
"""Reads an H5 Dataset, and returns a numpy array of it that is downsampled
Cube Size should probably be divisible by sampleFactor, or there may be some skipping of points on the boundaries of cubes
Essentially, a cube of `cubeSize` scans through the entire dataset, and adds a downsampled version to the array to be returned
Parameters
----------
dataset : h5py dataset
dataset to read
sampleFactor : int
how much to downsample. Ex, 1 would be no downsampling, 2 would have half the points on each axis (1/8 total size, 2^3=8)
cubeSize : int
Size of the cube that scans through the larger dataset. This is adjustable, ex computers with larger amounts of ram can use a larger cube for scanning, and smaller computers can use a smaller size
Returns
-------
numpy.ndarray
numpy array that is a downsampled version of the H5 dataset, `dataset`
"""
newX = float(dataset.shape[0]) / float(sampleFactor)
newY = float(dataset.shape[1]) / float(sampleFactor)
newZ = float(dataset.shape[2]) / float(sampleFactor)
if newX - int(newX) > 0:
newX = int(newX + 1)
if newY - int(newY) > 0:
newY = int(newY + 1)
if newZ - int(newZ) > 0:
newZ = int(newZ + 1)
newShape = (int(newX), int(newY), int(newZ))
toReturn = np.empty(shape=newShape, dtype=dataset.dtype)
for xiteration in range(0,dataset.shape[0], int(cubeSize)):
for yiteration in range(0, dataset.shape[1], int(cubeSize)):
for ziteration in range(0, dataset.shape[2], int(cubeSize)):
xmin = xiteration
xmax = min(xiteration + cubeSize, dataset.shape[0])
ymin = yiteration
ymax = min(yiteration + cubeSize, dataset.shape[1])
zmin = ziteration
zmax = min(ziteration + cubeSize, dataset.shape[2])
startSlice = dataset[xmin:xmax:sampleFactor, ymin:ymax:sampleFactor, zmin:zmax:sampleFactor]
xpad, ypad, zpad = 0, 0, 0
if xmax == dataset.shape[0]:
xpad = 1
if ymax == dataset.shape[1]:
ypad = 1
if zmax == dataset.shape[2]:
zpad = 1
toReturn[int(xmin/sampleFactor):int(xmax/sampleFactor + xpad), int(ymin/sampleFactor):int(ymax/sampleFactor + ypad), int(zmin/sampleFactor):int(zmax/sampleFactor + zpad)] = startSlice
return toReturn
def makeLabels(filename):
"""Calls getMultiClassImageStack, and returns 'labels', discarding 'unique' which is also returned
"""
labels, unique = getMultiClassImageStack(filename)
return labels
def getImageForLabelNaming(images, labelArray, index, filename):
"""Shows an interactive matplotlib window with two images. One is a raw image, and the other is the image with a particular set of labels for a semantic model highlighted in red. This could help identify the different planes of a semantic model
Parameters
----------
images : str or PIL Image
these are the raw microscope images
labelArray : str or numpy.ndarray
this is a numpy array containing labels corresponding to `images`
index : int
selects which plane of the semantic labels to highlight
filename : str
saves the resulting matplotlib window as a file at this filename
Returns
-------
None
Opens an interactive matplotlib window and saves it to a file
"""
if type(images) == type('str'):
images = Image.open(images)
if type(labelArray) == type('str'):
h5f = h5py.File(labelArray, 'r')
labelArray = np.array(h5f['dataset_1'])
h5f.close()
if labelArray.ndim == 3:
labelArray = labelArray[0]
images = images.convert('L').convert('RGB')
images = np.array(images)
mask = labelArray == index
unlabelledImage = np.copy(images)
images[mask] = (255,0,0)
plt.subplot(1,2,1)
plt.imshow(unlabelledImage)
plt.title('Image')
plt.subplot(1,2,2)
plt.title('Label')
plt.imshow(images)
plt.savefig(filename)