I have a question that I originally posted to Stackoverflow, but got no answers, and was encouraged to re-post here. I am developing a method for “straightening” binary-masks of pictures of carrot roots. I have traced midlines through the masks. For example, I start with a binary mask:
I then use Voronoi diagrams to estimate the midline, as shown in red:
I would now like to find the length of the “slices” that run perpendicular to the midline at each point, which would then constitute the appropriate width of the root at any given point along its length. I am using a method similar to that explained here to find the derivative of the fitted spline connecting points along the midline:
def get_spline_derivative(points): x_max = points[:, 0].max() # x_min = points[:, 0].min() new_length = x_max x = points[:, 0] y = points[:, 1] # new_x = np.linspace(x_min, x_max, new_length) # new_x = np.linspace(0, 1, 2000) # this is the one that works new_x = np.linspace(0, 1, new_length) # this is the one that works tck, u = itp.splprep([x, y]) dx, dy = itp.splev(new_x, tck, der=1) return list(zip(dx, dy))
This produces an enormous array, shortened here (full output here):
[(-559.8864500362733, -105.4032498463578), (-560.2848158832071, -109.774387868758), (-560.6711489905051, -113.82253391400316), (-561.0454493581669, -117.54768798209324), (-561.4077169861929, -120.94985007302827), ..., (-533.1106034956821, 74.87936144117594)]
I am unsure how to then use this output to find the vector perpendicular to this derivative, and then calculate the length of the “bin” of pixels that is within the polygon, running along this vector.
Does anyone have experience with performing this computations using Python?