Skip to content

Instantly share code, notes, and snippets.

@jrkerns
Last active April 10, 2023 18:07
Show Gist options
  • Save jrkerns/244dc2ec073cb98dd2564e638dfbe16f to your computer and use it in GitHub Desktop.
Save jrkerns/244dc2ec073cb98dd2564e638dfbe16f to your computer and use it in GitHub Desktop.
PF picket distances by leaf pair
import numpy as np
import pylinac
from pylinac.core.profile import MultiProfile
pf = pylinac.PicketFence.from_demo_image()
pf.analyze()
# the secret sauce. This gives the y-value for each leaf kiss.
# We convert to a set because we have N kisses for each pair where N is the # of pickets.
y_values = list(set([meas.leaf_center_px for meas in pf.mlc_meas]))
# sample the image and take a profile; note this is only for up/down pickets. need to invert for left/right pickets.
line = pf.image[int(round(y_values[0])), :] # grab the first leaf pair. Wrap this in a for loop or similar to iterate over all y_values.
profile = MultiProfile(line)
peak_idxs, _ = profile.find_peaks(min_distance=0.01, threshold=0.6) # might need to tweak these parameters for your image(s)
profile.plot() # optional, plot to verify peaks were correctly found
plt.show()
dists = np.diff(peak_idxs) / pf.image.dpmm # get distances and convert to mm
print(f"Distances between pickets in mm: {dists}")
# with the demo image I get:
# Distances between pickets in mm: [14.78342839 15.45540241 14.78342839 15.1194154 15.1194154 14.78342839
# 15.1194154 15.1194154 14.78342839]
# note this isn't subpixel!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment