Coverage for /usr/local/lib/python3.8/site-packages/spam/label/ITKwatershed.py: 92%

37 statements  

« prev     ^ index     » next       coverage.py v7.2.3, created at 2023-11-22 13:26 +0000

1""" 

2Library of wrapper functions for Simple ITK 

3Copyright (C) 2020 SPAM Contributors 

4 

5This program is free software: you can redistribute it and/or modify it 

6under the terms of the GNU General Public License as published by the Free 

7Software Foundation, either version 3 of the License, or (at your option) 

8any later version. 

9 

10This program is distributed in the hope that it will be useful, but WITHOUT 

11ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 

12FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 

13more details. 

14 

15You should have received a copy of the GNU General Public License along with 

16this program. If not, see <http://www.gnu.org/licenses/>. 

17""" 

18 

19import SimpleITK 

20import spam.label 

21import numpy 

22 

23def watershed(binary, markers=None, watershedLevel=1): 

24 """ 

25 This function runs an ITK watershed on a binary image and returns a labelled image. 

26 This function uses an interpixel watershed. 

27 

28 Parameters 

29 ----------- 

30 binary : 3D numpy array 

31 This image which is non-zero in the areas which should be split by the watershed algorithm 

32 

33 markers : 3D numpy array (optional, default = None) 

34 Not implemented yet, but try! 

35 

36 watershedLevel : int, optional 

37 Watershed level for merging maxima 

38 

39 Returns 

40 -------- 

41 labelled : 3D numpy array of ints 

42 3D array where each object is numbered 

43 """ 

44 import matplotlib.pyplot as plt 

45 def plotme(im): 

46 im = SimpleITK.GetArrayFromImage(im) 

47 plt.imshow(im[im.shape[0]//2]) 

48 plt.show() 

49 

50 binary = binary > 0 

51 

52 # Let's convert it 8-bit 

53 binary = binary.astype(numpy.uint8) * 255 

54 

55 bdata = SimpleITK.GetImageFromArray(binary) 

56 

57 if markers is not None: 

58 markers = markers.astype('<u4') if markers.max() > 65535 else markers.astype('<u2') 

59 markers = SimpleITK.GetImageFromArray(markers) 

60 

61 watershedLevel=1 

62 watershedLineOn=False 

63 fullyConnected=True 

64 

65 #thresholdFilt = SimpleITK.OtsuThresholdImageFilter() 

66 #bdata = thresholdFilt.Execute(data) 

67 

68 #rescaleFilt = SimpleITK.RescaleIntensityImageFilter() 

69 #rescaleFilt.SetOutputMinimum(0) 

70 #rescaleFilt.SetOutputMaximum(65535) 

71 #bdata = rescaleFilt.Execute(data) 

72 

73 #threshold = thresholdFilt.GetThreshold() 

74 

75 #fillFilt = SimpleITK.BinaryFillholeImageFilter() 

76 #bdata = fillFilt.Execute(bdata) 

77 

78 invertFilt = SimpleITK.InvertIntensityImageFilter() 

79 bdata = invertFilt.Execute(bdata) 

80 

81 distanceMapFilt = SimpleITK.DanielssonDistanceMapImageFilter() 

82 distance = distanceMapFilt.Execute(bdata) 

83 

84 distance = invertFilt.Execute(distance) 

85 

86 

87 if markers is None: 

88 watershedFilt = SimpleITK.MorphologicalWatershedImageFilter() 

89 else: 

90 watershedFilt = SimpleITK.MorphologicalWatershedFromMarkersImageFilter() 

91 watershedFilt.SetFullyConnected(fullyConnected) 

92 watershedFilt.SetMarkWatershedLine(watershedLineOn) 

93 

94 if markers is None: 

95 watershedFilt.SetLevel(watershedLevel) 

96 labelImage = watershedFilt.Execute(distance) 

97 else: 

98 labelImage = watershedFilt.Execute(distance, markers) 

99 

100 bdata = invertFilt.Execute(bdata) 

101 

102 maskFilt = SimpleITK.MaskImageFilter() 

103 mask = maskFilt.Execute(labelImage, bdata) 

104 

105 #overlayFilt = SimpleITK.LabelOverlayImageFilter() 

106 #overlay = overlayFilt.Execute(bdata, mask) 

107 

108 lab = SimpleITK.GetArrayFromImage(mask).astype(spam.label.labelType) 

109 

110 return lab