Skeletonizing an image

Introduction

External References

# -*- coding: utf-8 -*-
"""
Created on Wed Aug 18 14:24:17 2021

@author: danaukes
"""
'\nCreated on Wed Aug 18 14:24:17 2021\n\n@author: danaukes\n'
import skimage
import skimage.color
import skimage.filters
import skimage.io
import skimage.measure
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import shapely.geometry as sg
import foldable_robotics
from foldable_robotics.layer import Layer
foldable_robotics.resolution = 10
import ladybug_geometry_polyskel.polyskel as ps
filename = 'hi2.jpg'
sigma = 1
image = skimage.io.imread(fname=filename)
plt.figure()
plt.imshow(image)
<matplotlib.image.AxesImage at 0x7fe120048df0>

png

gray = skimage.color.rgb2gray(image)
plt.figure()
plt.imshow(gray,cmap=cm.gray)
<matplotlib.image.AxesImage at 0x7fe11ff62d60>

png

blur = skimage.filters.gaussian(gray, sigma=sigma)
plt.imshow(blur,cmap=cm.gray)
<matplotlib.image.AxesImage at 0x7fe11ff51670>

png

t = skimage.filters.threshold_otsu(blur)
mask = (blur > t)
plt.figure()
plt.imshow(mask,cmap=cm.gray)
<matplotlib.image.AxesImage at 0x7fe11feabac0>

png

result = skimage.measure.find_contours(mask)
result
[array([[ 66. , 147.5],
        [ 65. , 147.5],
        [ 64. , 147.5],
        [ 63. , 147.5],
        [ 62.5, 147. ],
        [ 62. , 146.5],
        [ 61.5, 146. ],
        [ 61. , 145.5],
        [ 60.5, 145. ],
        [ 60.5, 144. ],
        [ 61. , 143.5],
        [ 62. , 143.5],
        [ 63. , 143.5],
        [ 64. , 143.5],
        [ 64.5, 144. ],
        [ 65. , 144.5],
        [ 65.5, 145. ],
        [ 66. , 145.5],
        [ 66.5, 146. ],
        [ 66.5, 147. ],
        [ 66. , 147.5]]),
 array([[144. ,  88.5],
        [143. ,  88.5],
        [142. ,  88.5],
        [141. ,  88.5],
        [140.5,  88. ],
        [140. ,  87.5],
        [139. ,  87.5],
        [138.5,  87. ],
        [138. ,  86.5],
        [137. ,  86.5],
        [136. ,  86.5],
        [135.5,  86. ],
        [135. ,  85.5],
        [134. ,  85.5],
        [133. ,  85.5],
        [132. ,  85.5],
        [131. ,  85.5],
        [130. ,  85.5],
        [129.5,  85. ],
        [129. ,  84.5],
        [128. ,  84.5],
        [127. ,  84.5],
        [126. ,  84.5],
        [125. ,  84.5],
        [124. ,  84.5],
        [123. ,  84.5],
        [122. ,  84.5],
        [121. ,  84.5],
        [120.5,  84. ],
        [120. ,  83.5],
        [119. ,  83.5],
        [118. ,  83.5],
        [117. ,  83.5],
        [116. ,  83.5],
        [115. ,  83.5],
        [114.5,  83. ],
        [114. ,  82.5],
        [113. ,  82.5],
        [112. ,  82.5],
        [111. ,  82.5],
        [110. ,  82.5],
        [109. ,  82.5],
        [108. ,  82.5],
        [107. ,  82.5],
        [106.5,  83. ],
        [106. ,  83.5],
        [105.5,  84. ],
        [105.5,  85. ],
        [105.5,  86. ],
        [105.5,  87. ],
        [105.5,  88. ],
        [105.5,  89. ],
        [105.5,  90. ],
        [105.5,  91. ],
        [105. ,  91.5],
        [104.5,  92. ],
        [104.5,  93. ],
        [104.5,  94. ],
        [104.5,  95. ],
        [104. ,  95.5],
        [103.5,  96. ],
        [103.5,  97. ],
        [103.5,  98. ],
        [103.5,  99. ],
        [103. ,  99.5],
        [102.5, 100. ],
        [102.5, 101. ],
        [102.5, 102. ],
        [102. , 102.5],
        [101.5, 103. ],
        [101.5, 104. ],
        [101.5, 105. ],
        [101. , 105.5],
        [100.5, 106. ],
        [100.5, 107. ],
        [100.5, 108. ],
        [100.5, 109. ],
        [100. , 109.5],
        [ 99.5, 110. ],
        [ 99.5, 111. ],
        [ 99.5, 112. ],
        [ 99.5, 113. ],
        [ 99.5, 114. ],
        [ 99.5, 115. ],
        [100. , 115.5],
        [101. , 115.5],
        [102. , 115.5],
        [103. , 115.5],
        [103.5, 116. ],
        [104. , 116.5],
        [105. , 116.5],
        [106. , 116.5],
        [107. , 116.5],
        [108. , 116.5],
        [108.5, 117. ],
        [109. , 117.5],
        [110. , 117.5],
        [111. , 117.5],
        [112. , 117.5],
        [112.5, 118. ],
        [113. , 118.5],
        [114. , 118.5],
        [115. , 118.5],
        [116. , 118.5],
        [117. , 118.5],
        [118. , 118.5],
        [119. , 118.5],
        [120. , 118.5],
        [120.5, 119. ],
        [121. , 119.5],
        [122. , 119.5],
        [122.5, 120. ],
        [122.5, 121. ],
        [123. , 121.5],
        [123.5, 122. ],
        [123. , 122.5],
        [122.5, 123. ],
        [122. , 123.5],
        [121. , 123.5],
        [120. , 123.5],
        [119. , 123.5],
        [118. , 123.5],
        [117. , 123.5],
        [116.5, 123. ],
        [116. , 122.5],
        [115. , 122.5],
        [114. , 122.5],
        [113.5, 122. ],
        [113. , 121.5],
        [112. , 121.5],
        [111. , 121.5],
        [110. , 121.5],
        [109.5, 121. ],
        [109. , 120.5],
        [108. , 120.5],
        [107. , 120.5],
        [106. , 120.5],
        [105. , 120.5],
        [104.5, 120. ],
        [104. , 119.5],
        [103. , 119.5],
        [102. , 119.5],
        [101. , 119.5],
        [100.5, 119. ],
        [100. , 118.5],
        [ 99. , 118.5],
        [ 98. , 118.5],
        [ 97. , 118.5],
        [ 96. , 118.5],
        [ 95.5, 118. ],
        [ 95. , 117.5],
        [ 94. , 117.5],
        [ 93.5, 117. ],
        [ 93. , 116.5],
        [ 92. , 116.5],
        [ 91. , 116.5],
        [ 90.5, 116. ],
        [ 90. , 115.5],
        [ 89. , 115.5],
        [ 88. , 115.5],
        [ 87. , 115.5],
        [ 86.5, 115. ],
        [ 86. , 114.5],
        [ 85. , 114.5],
        [ 84. , 114.5],
        [ 83.5, 114. ],
        [ 83. , 113.5],
        [ 82. , 113.5],
        [ 81. , 113.5],
        [ 80. , 113.5],
        [ 79.5, 113. ],
        [ 79. , 112.5],
        [ 78. , 112.5],
        [ 77. , 112.5],
        [ 76.5, 112. ],
        [ 76. , 111.5],
        [ 75. , 111.5],
        [ 74. , 111.5],
        [ 73.5, 111. ],
        [ 73. , 110.5],
        [ 72. , 110.5],
        [ 71. , 110.5],
        [ 70. , 110.5],
        [ 69. , 110.5],
        [ 68.5, 110. ],
        [ 68. , 109.5],
        [ 67. , 109.5],
        [ 66.5, 109. ],
        [ 66. , 108.5],
        [ 65.5, 108. ],
        [ 65.5, 107. ],
        [ 66. , 106.5],
        [ 67. , 106.5],
        [ 68. , 106.5],
        [ 69. , 106.5],
        [ 70. , 106.5],
        [ 70.5, 107. ],
        [ 71. , 107.5],
        [ 72. , 107.5],
        [ 73. , 107.5],
        [ 73.5, 108. ],
        [ 74. , 108.5],
        [ 75. , 108.5],
        [ 76. , 108.5],
        [ 76.5, 109. ],
        [ 77. , 109.5],
        [ 78. , 109.5],
        [ 79. , 109.5],
        [ 80. , 109.5],
        [ 80.5, 110. ],
        [ 81. , 110.5],
        [ 82. , 110.5],
        [ 83. , 110.5],
        [ 84. , 110.5],
        [ 84.5, 111. ],
        [ 85. , 111.5],
        [ 86. , 111.5],
        [ 87. , 111.5],
        [ 87.5, 112. ],
        [ 88. , 112.5],
        [ 89. , 112.5],
        [ 90. , 112.5],
        [ 91. , 112.5],
        [ 91.5, 113. ],
        [ 92. , 113.5],
        [ 93. , 113.5],
        [ 94. , 113.5],
        [ 95. , 113.5],
        [ 95.5, 113. ],
        [ 95.5, 112. ],
        [ 96. , 111.5],
        [ 96.5, 111. ],
        [ 96.5, 110. ],
        [ 96.5, 109. ],
        [ 96.5, 108. ],
        [ 97. , 107.5],
        [ 97.5, 107. ],
        [ 97.5, 106. ],
        [ 97.5, 105. ],
        [ 98. , 104.5],
        [ 98.5, 104. ],
        [ 98.5, 103. ],
        [ 98.5, 102. ],
        [ 99. , 101.5],
        [ 99.5, 101. ],
        [ 99.5, 100. ],
        [ 99.5,  99. ],
        [ 99.5,  98. ],
        [100. ,  97.5],
        [100.5,  97. ],
        [100.5,  96. ],
        [100.5,  95. ],
        [100.5,  94. ],
        [101. ,  93.5],
        [101.5,  93. ],
        [101.5,  92. ],
        [101.5,  91. ],
        [101.5,  90. ],
        [101.5,  89. ],
        [101.5,  88. ],
        [101.5,  87. ],
        [101.5,  86. ],
        [102. ,  85.5],
        [102.5,  85. ],
        [102.5,  84. ],
        [102.5,  83. ],
        [102.5,  82. ],
        [102. ,  81.5],
        [101.5,  81. ],
        [101. ,  80.5],
        [100. ,  80.5],
        [ 99. ,  80.5],
        [ 98. ,  80.5],
        [ 97.5,  80. ],
        [ 97. ,  79.5],
        [ 96. ,  79.5],
        [ 95. ,  79.5],
        [ 94. ,  79.5],
        [ 93. ,  79.5],
        [ 92.5,  79. ],
        [ 92. ,  78.5],
        [ 91. ,  78.5],
        [ 90. ,  78.5],
        [ 89. ,  78.5],
        [ 88. ,  78.5],
        [ 87.5,  78. ],
        [ 87. ,  77.5],
        [ 86. ,  77.5],
        [ 85. ,  77.5],
        [ 84. ,  77.5],
        [ 83. ,  77.5],
        [ 82.5,  77. ],
        [ 82. ,  76.5],
        [ 81. ,  76.5],
        [ 80. ,  76.5],
        [ 79. ,  76.5],
        [ 78. ,  76.5],
        [ 77. ,  76.5],
        [ 76. ,  76.5],
        [ 75. ,  76.5],
        [ 74.5,  76. ],
        [ 74. ,  75.5],
        [ 73. ,  75.5],
        [ 72.5,  75. ],
        [ 72.5,  74. ],
        [ 73. ,  73.5],
        [ 74. ,  73.5],
        [ 74.5,  73. ],
        [ 75. ,  72.5],
        [ 76. ,  72.5],
        [ 77. ,  72.5],
        [ 77.5,  73. ],
        [ 78. ,  73.5],
        [ 79. ,  73.5],
        [ 80. ,  73.5],
        [ 81. ,  73.5],
        [ 82. ,  73.5],
        [ 83. ,  73.5],
        [ 84. ,  73.5],
        [ 84.5,  74. ],
        [ 85. ,  74.5],
        [ 86. ,  74.5],
        [ 87. ,  74.5],
        [ 88. ,  74.5],
        [ 89. ,  74.5],
        [ 89.5,  75. ],
        [ 90. ,  75.5],
        [ 91. ,  75.5],
        [ 92. ,  75.5],
        [ 93. ,  75.5],
        [ 94. ,  75.5],
        [ 94.5,  76. ],
        [ 95. ,  76.5],
        [ 96. ,  76.5],
        [ 97. ,  76.5],
        [ 98. ,  76.5],
        [ 99. ,  76.5],
        [ 99.5,  77. ],
        [100. ,  77.5],
        [101. ,  77.5],
        [102. ,  77.5],
        [103. ,  77.5],
        [103.5,  78. ],
        [104. ,  78.5],
        [105. ,  78.5],
        [106. ,  78.5],
        [107. ,  78.5],
        [108. ,  78.5],
        [109. ,  78.5],
        [110. ,  78.5],
        [110.5,  79. ],
        [111. ,  79.5],
        [112. ,  79.5],
        [113. ,  79.5],
        [114. ,  79.5],
        [115. ,  79.5],
        [116. ,  79.5],
        [116.5,  80. ],
        [117. ,  80.5],
        [118. ,  80.5],
        [119. ,  80.5],
        [120. ,  80.5],
        [121. ,  80.5],
        [122. ,  80.5],
        [123. ,  80.5],
        [124. ,  80.5],
        [124.5,  81. ],
        [125. ,  81.5],
        [126. ,  81.5],
        [127. ,  81.5],
        [128. ,  81.5],
        [129. ,  81.5],
        [130. ,  81.5],
        [131. ,  81.5],
        [132. ,  81.5],
        [133. ,  81.5],
        [134. ,  81.5],
        [134.5,  82. ],
        [135. ,  82.5],
        [136. ,  82.5],
        [137. ,  82.5],
        [138. ,  82.5],
        [138.5,  83. ],
        [139. ,  83.5],
        [140. ,  83.5],
        [141. ,  83.5],
        [142. ,  83.5],
        [143. ,  83.5],
        [143.5,  84. ],
        [144. ,  84.5],
        [144.5,  85. ],
        [144.5,  86. ],
        [144.5,  87. ],
        [144.5,  88. ],
        [144. ,  88.5]]),
 array([[112. , 153.5],
        [111.5, 154. ],
        [111. , 154.5],
        [110. , 154.5],
        [109. , 154.5],
        [108. , 154.5],
        [107. , 154.5],
        [106.5, 154. ],
        [106. , 153.5],
        [105. , 153.5],
        [104. , 153.5],
        [103. , 153.5],
        [102. , 153.5],
        [101. , 153.5],
        [100. , 153.5],
        [ 99. , 153.5],
        [ 98.5, 153. ],
        [ 98. , 152.5],
        [ 97. , 152.5],
        [ 96. , 152.5],
        [ 95. , 152.5],
        [ 94.5, 152. ],
        [ 94. , 151.5],
        [ 93. , 151.5],
        [ 92. , 151.5],
        [ 91.5, 151. ],
        [ 91. , 150.5],
        [ 90. , 150.5],
        [ 89. , 150.5],
        [ 88. , 150.5],
        [ 87.5, 150. ],
        [ 87. , 149.5],
        [ 86.5, 149. ],
        [ 86.5, 148. ],
        [ 87. , 147.5],
        [ 88. , 147.5],
        [ 89. , 147.5],
        [ 90. , 147.5],
        [ 91. , 147.5],
        [ 92. , 147.5],
        [ 93. , 147.5],
        [ 93.5, 148. ],
        [ 94. , 148.5],
        [ 95. , 148.5],
        [ 96. , 148.5],
        [ 97. , 148.5],
        [ 97.5, 149. ],
        [ 98. , 149.5],
        [ 99. , 149.5],
        [100. , 149.5],
        [101. , 149.5],
        [102. , 149.5],
        [102.5, 150. ],
        [103. , 150.5],
        [104. , 150.5],
        [105. , 150.5],
        [106. , 150.5],
        [107. , 150.5],
        [108. , 150.5],
        [109. , 150.5],
        [110. , 150.5],
        [110.5, 151. ],
        [111. , 151.5],
        [112. , 151.5],
        [112.5, 152. ],
        [112.5, 153. ],
        [112. , 153.5]])]
polys = [Layer(sg.Polygon(item)) for item in result]
l = Layer()
for item in polys:
    l^=item
l= l.rotate(-90)
l

svg

l<<=5
l

svg

l = l.simplify(3)
l

svg

l.plot(new=True)
ls = [Layer(item) for item in l.geoms]

for item in ls:
    skeleton = ps.skeleton_as_subtree_list(item.exteriors()[0][::-1], item.interiors())
    all_sources = [item.source for item in skeleton]
    all_sinks = [item2 for item in skeleton for item2 in item.sinks]
    non_terminal_sinks = [item for item in all_sinks if item in all_sources]
    
    for arc in skeleton:
        for sink in arc.sinks:
            if sink in non_terminal_sinks:
                plt.plot((arc.source.x, sink.x),(arc.source.y, sink.y))

png