Calibrate from Image
Note
This notebook is modified from how it appears in the ‘notebooks’ directory so it can render properly in the documentation. Primarily, this includes changed paths and mocked user interactions.
Imports
from misalign.calibration.calibrate import CalibrationManual
%matplotlib widget
Basic Calibration
Load image with reference dimension.
Specify known length measurement.
Select matching pairs of points.
Resolve distance between points.
Save calibration to
.miscal.jsonfile.
cm=CalibrationManual(calibration_image_path="../../../example/project_a/scale_5x_1mm.bmp")
cm.calibrate_measurement(length=1,units="mm")
cm.calibrate_setup()
### Code used to simulate user-input for documentation generation. ###
# Replaces a user selecting feature points on the calibration image.
class mock_event:
def __init__(self, button, xdata, ydata):
self.button = button
self.xdata = xdata
self.ydata = ydata
cm._calibrate_callback(mock_event(button=1, xdata=500, ydata=480))
cm._calibrate_callback(mock_event(button=1, xdata=1099, ydata=480))
cm._calibrate_callback(mock_event(button=1, xdata=498, ydata=690))
cm._calibrate_callback(mock_event(button=1, xdata=1095, ydata=695))
cm._calibrate_callback(mock_event(button=1, xdata=1095, ydata=695))
cm.calibrate_resolve()
from IPython.display import clear_output
from matplotlib import pyplot as plt
clear_output()
plt.show()
### End ###
cm.calibrate_resolve()
Pixel Distance:599.00
Pixel Distance:597.02
Average Pixel Distance: 598.01
cm.print_pixels_per_length()
cm.print_length_per_pixel()
Pixels: 598.01
Length: 1 mm
Pixels/Length = 598.01 pixels/mm
Pixels: 598.01
Length: 1 mm
length/pixel = 0.001672 mm/pixels
cm.save_calibration("../../../example/project_a/demo-scale_5x_1mm.miscal.json")
Confirm Calibration
Optional
Load calibration from file to check saved values.
cm2=CalibrationManual()
cm2.load_calibration("../../../example/project_a/scale_5x_1mm.miscal.json")
cm2.print_pixels_per_length()
cm2.print_length_per_pixel()
print(cm2.distances)
Pixels: 600.00
Length: 1 mm
Pixels/Length = 600.00 pixels/mm
Pixels: 600.00
Length: 1 mm
length/pixel = 0.001667 mm/pixels
{'pixel': 600, 'length': 1, 'length_unit': 'mm'}
cm3=CalibrationManual()
cm3.load_calibration("../../../example/project_a/project_a-no_relations-calibrated.mis.json")
cm3.print_pixels_per_length()
cm3.print_length_per_pixel()
print(cm3.distances)
Pixels: 600.00
Length: 1 mm
Pixels/Length = 600.00 pixels/mm
Pixels: 600.00
Length: 1 mm
length/pixel = 0.001667 mm/pixels
{'pixel': 600, 'length': 1, 'length_unit': 'mm'}