Clearly, my first version is ugly.
Here is what I got. You need to do the following steps:
- Import your binary crop in Fiji
-
Skeletonize
it. Note: use the search bar of Fiji
- Invert this output to get something like this:
crop_bg_skeleton1.tif (105.2 KB)
this is the central line of your background. Save it to tiff.
- Go back to your binary crop image.
Distance map
it. Use the search bar. You will get this :
dis_map1.tif (105.2 KB)
- Once you have these two files saved. You can run this python script:
import tifffile as tf
import numpy as np
import matplotlib.pyplot as plt
def calculate_average_bg_to_object_dis(mask, dis_map):
real_mask = np.clip(mask, 0, 1)
filtered_dis_map = real_mask * dis_map
central_line = filtered_dis_map[filtered_dis_map != 0]
_, counts = np.unique(central_line, return_counts=True)
print(np.mean(central_line),
np.min(central_line),
np.max(central_line))
plt.imshow(filtered_dis_map)
plt.show()
mask1 = tf.imread("crop_bg_skeleton1.tif")
dis_map1 = tf.imread("dis_map1.tif")
mask2 = tf.imread("crop_bg_skeleton2.tif")
dis_map2 = tf.imread("dis_map2.tif")
calculate_average_bg_to_object_dis(mask1, dis_map1)
calculate_average_bg_to_object_dis(mask2, dis_map2)
It will show you the image with only the central line of background and its distance to the nearest object in intensity.


and the distances in pixel(mean, min, max) that are :
3.6217366628830874 1 8 - image on the left
4.515667311411992 1 9 - image on the right
You may want to * 2 of these distances if you want the distance between two branches
Sorry the last part is in Python. I simply feel its easier with matrix calculation.
Tell me if you got any issue.