I am very new to coding and this is the first real code I am working on. I am trying to write code, which finds dead ends in a skeletonized 3d structure and sets a marker and the position of the dead end. So I only have pixels with the value 0 and 1. My problem right now is when a canal splits I can only track 1 branch and the second branch is not iterated through so that I miss a lot of dead ends. This is the code I wrote with self constructed canals:
c = np.zeros([40,40,30], dtype=np.unit8)
c[5,5,0:4] = 1
c[5,4,4] = 1
c[5,3,4:10] = 1
c[4,4,10:25] = 1
c[5,5,25:30] = 1
c[5,6,4] = 1
c[5,7,4:6] = 1
c[6,7,6:15] = 1
c[6,6,15:24] = 1
c[5,5,24:25] = 1
c[15,15,0:15] = 1
c[16,15,15:16] = 1
c[17,16,16:18] = 1
c[16,16,18:20] = 1
c[15,17,20:25] = 1
c[18,17,18:20] = 1
c[19,17,20:30] = 1
c[14,14,15:16] = 1
c[13,13,16:20] = 1
c[13,12,20:21] = 1
c[14,12,21:23] = 1
c[15,11,23:25] = 1
c[12,13,20:21] = 1
c[11,13,21:25] = 1
c[30,30,20:30] = 1
c[29,29,18:20] = 1
c[28,28,15:18] = 1
c[27,27,14:15] = 1
c[26,26,13:14] =1
c[29,29,14:15] = 1
c[30,30,10:14] = 1
c[29,29,9:10] = 1
c[28,29,8:9] = 1
c[27,28,3:8] = 1
c[31,31,9:10] = 1
c[32,32,6:9] = 1
k = np.pad(c,pad_width=2)
b = np.array(k,dtype=np.uint8)
d = np.size(b,axis=0)
h = np.size(b,axis=1)
w = np.size(b,axis=2)
img = np.zeros_like(b,dtype=np.uint8)
occupied_pixels = np.where(b[:,:,3] == 1)
pixels_x = occupied_pixels[0]
pixels_y = occupied_pixels[1]
def is_deadend(x, y, z):
pixel_found_in_cone = False
for dz in range(1,3):
for dx in [2, 1, 0, -1, -2]: #for dx i2,n [-1, 0, 1]:
for dy in [2, 1, 0, -1, -2]: #for dy in [-1, 0, 1]
if b[x+dx,y+dy,z+dz] == 1:
pixel_found_in_cone = True
position, bool_deadend = is_deadend(x+dx,y+dy,z+dz)
if bool_deadend:
return position, bool_deadend
return [x,y,z], not pixel_found_in_cone
markers_found = []
for i in range(len(pixels_x)):
occupied_pixel = [pixels_x[i],pixels_y[i],3]
position, bool_deadend = is_deadend(occupied_pixel[0],occupied_pixel[1],occupied_pixel[2])
print(is_deadend(occupied_pixel[0],occupied_pixel[1],occupied_pixel[2]))
if bool_deadend:
markers_found.append(position)
for marker in markers_found:
img[marker[0],marker[1],marker[2]] = 1
#for visualizing:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
data = b
x, y, z = data.nonzero()
ax.scatter(x,y,z, c=z, alpha =1)
plt.show()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
data = img
x, y, z = data.nonzero()
ax.scatter(x,y,z, c=z, alpha =1)
plt.show()
I don´t really know how I can catch every possible canal after a splitup and find every dead in that way. My code right now only takes the first canal it finds and then iterates through that one. I am open for any suggestion regarding the specific problem or the code in general. Thanks in advance!
