Skip to content

Free Motion Capture for Everyone πŸ’€βœ¨

TypeScript

push before meeting

Jonc7513967ad4a
diff --git a/src/api/services/board_detect_service.py b/src/api/services/board_detect_service.py
index 0f10e506..e87a7aad 100644
--- a/src/api/services/board_detect_service.py
+++ b/src/api/services/board_detect_service.py
@@ -1,7 +1,7 @@
from typing import NamedTuple
from src.cameras.multicam_manager import CVCameraManager
-from src.core_processor.camera_calibration.charuco_board_detection.board_detection import BoardDetector
+from src.core_processor.camera_calibration.charuco_board_detection.board_detector import BoardDetector
class ImageResponse(NamedTuple):
diff --git a/src/core_processor/camera_calibration/calibration_data_classes.py b/src/core_processor/camera_calibration/calibration_data_classes.py
new file mode 100644
index 00000000..901c8bca
--- /dev/null
+++ b/src/core_processor/camera_calibration/calibration_data_classes.py
@@ -0,0 +1,27 @@
+from dataclasses import dataclass
+
+import numpy as np
+
+
+@dataclass
+class LensDistortionCalibrationData:
+ image_width: int
+ image_height: int
+ reprojection_error: float = 1e9
+ camera_matrix: np.ndarray = None
+ number_of_lens_distortion_coefficients: int = 4
+ lens_distortion_coefficients: np.ndarray = None
+ rotation_vectors_of_the_board: np.ndarray = None
+ translation_vectors_of_the_board: np.ndarray = None
+ lens_distortion_std_dev: float = None
+ camera_location_std_dev: float = None
+
+ def __post_init__(self):
+ if self.camera_matrix is None:
+ self.camera_matrix = np.array([[float(self.image_width), 0., self.image_width / 2.],
+ [0., float(self.image_width), self.image_height / 2.],
+ [0., 0., 1.]])
+
+ if self.lens_distortion_coefficients is None:
+ self.lens_distortion_coefficients = np.zeros((self.number_of_lens_distortion_coefficients, 1))
+
diff --git a/src/core_processor/camera_calibration/calibration_diagnostics_visualizer.py b/src/core_processor/camera_calibration/calibration_diagnostics_visualizer.py
index 0ef6e8ee..79d113e8 100644
--- a/src/core_processor/camera_calibration/calibration_diagnostics_visualizer.py
+++ b/src/core_processor/camera_calibration/calibration_diagnostics_visualizer.py
@@ -1,59 +1,118 @@
import numpy as np
import pyqtgraph as pg
+import pyqtgraph.opengl as gl
+from pyqtgraph.dockarea.Dock import Dock
+from pyqtgraph.dockarea.DockArea import DockArea
+from pyqtgraph.Qt import QtWidgets
+
import cv2
+from src.core_processor.camera_calibration.calibration_data_classes import LensDistortionCalibrationData
+
class CalibrationDiagnosticsVisualizer:
def __init__(self):
- self.graphics_layout_window = pg.GraphicsLayoutWidget(show=True)
- self.graphics_layout_window.setWindowTitle('Reprojection Error')
-
# dummy data to initialize subplots
self.reprojection_error_all = np.empty(0)
self.reprojection_error_current_best = np.empty(0)
self.image_point_original = np.empty(0)
self.image_point_remapped = np.empty(0)
- # create image subplot (based on `GraphicsItems - ImageItem - video` example `python -m pyqtgraph.examples`)
- self.undistorted_image_subplot = self.graphics_layout_window.addViewBox()
- self.undistorted_image_subplot.setAspectLocked(True)
- self.image_item_holder = pg.ImageItem(border='w')
- self.undistorted_image_subplot.addItem(self.image_item_holder)
-
- # create reprojection error subplot
- self.reprojection_error_subplot = self.graphics_layout_window.addPlot()
- self.reprojection_error_subplot.setLabel('left', "Reprojection Error", units='pixels')
- self.reprojection_error_subplot.setLabel('bottom', "Time", units='Frame#')
- self.reprojection_error_subplot.setYRange(0, 2)
- self.reprojection_error_subplot.addLegend()
- self.reprojection_error_all_line = self.reprojection_error_subplot.plot(self.reprojection_error_all,
- pen=(255, 0, 128),
- name="reprojection error- all")
- self.reprojection_error_current_best_line = self.reprojection_error_subplot.plot(
+ # create main app and window
+ self.pyqtgraph_app = pg.mkQApp("DockArea Example")
+ self.main_window_widget = QtWidgets.QMainWindow()
+ self.dock_area = DockArea()
+ self.main_window_widget.setCentralWidget(self.dock_area)
+ self.main_window_widget.resize(1000, 500)
+ self.main_window_widget.setWindowTitle('Lens Distortion Calibration Diagnostics (pyqtgraph)')
+
+ # create widget/dock for image subplot (based on `GraphicsItems - ImageItem - video` example `python -m pyqtgraph.examples`)
+ self.image_view_widget = pg.ImageView()
+
+ self.undistorted_image_dock = Dock("Undistorted Image")
+ self.undistorted_image_dock.addWidget(self.image_view_widget)
+ self.dock_area.addDock(self.undistorted_image_dock)
+
+ # create widget/dock for reprojection error subplot
+ self.reprojection_error_plot_widget = pg.PlotWidget(title="Reprojection Error")
+ self.reprojection_error_plot_widget.setLabel('left', "Reprojection Error", units='pixels')
+ self.reprojection_error_plot_widget.setLabel('bottom', "Time", units='Frame#')
+ self.reprojection_error_plot_widget.setYRange(0, 2)
+ self.reprojection_error_plot_widget.addLegend()
+ self.reprojection_error_all_line = self.reprojection_error_plot_widget.plot(self.reprojection_error_all,
+ pen=(255, 0, 128),
+ name="reprojection error- all")
+ self.reprojection_error_current_best_line = self.reprojection_error_plot_widget.plot(
self.reprojection_error_current_best,
name="current best")
- # make next row
- self.graphics_layout_window.nextRow()
-
- # create lens distortion remapping subplot
- self.image_point_remapping_subplot = self.graphics_layout_window.addPlot()
- self.image_point_remapping_subplot.setLabel('left', "Y-position", units='pixels')
- self.image_point_remapping_subplot.setLabel('bottom', "X-position", units='pixels')
- self.image_point_remapping_subplot.addLegend()
-
- self.image_point_original_line = self.image_point_remapping_subplot.plot(self.image_point_original,
- pen=(255, 200, 200),
- name="image points - original")
- self.image_point_remapped_line = self.image_point_remapping_subplot.plot(self.image_point_remapped,
- pen=(0, 255, 128),
- name="image points - remapped")
-
-
-
+ self.reprojection_error_dock = Dock("Reprojection Error")
+ self.reprojection_error_dock.addWidget(self.reprojection_error_plot_widget)
+ self.dock_area.addDock(self.reprojection_error_dock, position='right')
+
+ # create widget/dock lens distortion remapping subplot
+ self.image_point_remapping_plot_widget = pg.PlotWidget(title="Image Point Remapping")
+ self.image_point_remapping_plot_widget.setLabel('left', "Y-position", units='pixels')
+ self.image_point_remapping_plot_widget.setLabel('bottom', "X-position", units='pixels')
+ self.image_point_remapping_plot_widget.addLegend()
+
+ self.image_point_original_dottos = self.image_point_remapping_plot_widget.plot(self.image_point_original,
+ self.image_point_original,
+ pen=None,
+ symbol='o',
+ symbolBrush=None,
+ symbolPen=(150, 150, 150),
+ name="image points - original")
+ self.image_point_remapped_dottos = self.image_point_remapping_plot_widget.plot(self.image_point_remapped,
+ self.image_point_remapped,
+ symbol='+',
+ pen=None,
+ symbolBrush=None,
+ symbolPen=(50, 150, 210),
+ name="image points - remapped")
+ self.image_point_remapping_plot_dock = Dock("Image Point Remapping")
+ self.image_point_remapping_plot_dock.addWidget(self.image_point_remapping_plot_widget)
+ self.dock_area.addDock(self.image_point_remapping_plot_dock)
+
+ ###########
+ # create widget/dock opengl 3d plot for charuco dottos
+ self.opengl_3d_plot_widget = gl.GLViewWidget()
+ self.opengl_grid_item = gl.GLGridItem()
+ self.opengl_3d_plot_widget.addItem(self.opengl_grid_item)
+
+ # create XYZ axes
+ x_axis_line_array = np.array([[0, 0, 0], [1, 0, 0]])
+ y_axis_line_array = np.array([[0, 0, 0], [0, 1, 0]])
+ z_axis_line_array = np.array([[0, 0, 0], [0, 0, 1]])
+
+ self.origin_x_axis_gl_lineplot_item = gl.GLLinePlotItem(pos=x_axis_line_array,
+ color=(1, 0, 0, 1),
+ width=1.,
+ antialias=True)
+ self.origin_y_axis_gl_lineplot_item = gl.GLLinePlotItem(pos=y_axis_line_array,
+ color=(0, 1, 0, 1),
+ width=1.,
+ antialias=True)
+ self.origin_z_axis_gl_lineplot_item = gl.GLLinePlotItem(pos=z_axis_line_array,
+ color=(0, 0, 1, 1),
+ width=1.,
+ antialias=True)
+ self.opengl_3d_plot_widget.addItem(self.origin_x_axis_gl_lineplot_item)
+ self.opengl_3d_plot_widget.addItem(self.origin_y_axis_gl_lineplot_item)
+ self.opengl_3d_plot_widget.addItem(self.origin_z_axis_gl_lineplot_item)
+
+ self.opengl_charuco_scatter_item = gl.GLScatterPlotItem(pos=(0, 0, 0), color=(1, 0, 1), size=1, pxMode=False)
+
+ self.opengl_3d_plot_dock = Dock("Image Point Remapping")
+ self.opengl_3d_plot_dock.addWidget(self.opengl_3d_plot_widget)
+ self.dock_area.addDock(self.opengl_3d_plot_dock, 'right', self.image_point_remapping_plot_dock)
+
+ # display window
+ self.main_window_widget.show()
def update_image_subplot(self, new_image):
- self.image_item_holder.setImage(cv2.cvtColor(cv2.rotate(new_image, cv2.ROTATE_90_CLOCKWISE), cv2.COLOR_BGR2RGB))
+ self.image_view_widget.setImage(
+ cv2.cvtColor(cv2.rotate(new_image, cv2.ROTATE_90_COUNTERCLOCKWISE), cv2.COLOR_BGR2RGB))
def update_reprojection_error_subplot(self, reprojection_error_new, reprojection_error_current_best):
self.reprojection_error_all = np.append(self.reprojection_error_all, reprojection_error_new)
@@ -63,13 +122,23 @@ class CalibrationDiagnosticsVisualizer:
self.reprojection_error_all_line.setData(self.reprojection_error_all)
self.reprojection_error_current_best_line.setData(self.reprojection_error_current_best)
+ def update_calibration_text_overlay(self, calibration_data: LensDistortionCalibrationData):
+ return
+ self.reprojection_error_text_item.setText(f'reprojection error:{calibration_data.reprojection_error:.3f}')
+ lens_distortion_as_str = [f"{d[0]:.3f}" for d in calibration_data.lens_distortion_coefficients]
+ self.lens_distortion_text_item.setText(str(lens_distortion_as_str))
+ self.camera_matrix_text_item.setText(str(calibration_data.camera_matrix))
+
def update_image_point_remapping_subplot(self, image_point_original_x, image_point_original_y,
image_point_remapped_x, image_point_remapped_y):
- self.image_point_original_line.setData(x=image_point_original_x,
- y=image_point_original_y)
+ self.image_point_original_dottos.setData(x=image_point_original_x,
+ y=image_point_original_y)
+
+ self.image_point_remapped_dottos.setData(x=image_point_remapped_x,
+ y=image_point_remapped_y)
- self.image_point_original_line.setData(x=image_point_remapped_x,
- y=image_point_remapped_y)
+ def update_3d_subplot(self, charuco_xyz: np.ndarray):
+ self.opengl_charuco_scatter_item.setData(pos=charuco_xyz)
def close(self):
- self.graphics_layout_window.close()
+ self.main_window_widget.close()
diff --git a/src/core_processor/camera_calibration/charuco_board_detection/board_detection.py b/src/core_processor/camera_calibration/charuco_board_detection/board_detector.py
similarity index 90%
rename from src/core_processor/camera_calibration/charuco_board_detection/board_detection.py
rename to src/core_processor/camera_calibration/charuco_board_detection/board_detector.py
index 68e5a569..046292f1 100644
--- a/src/core_processor/camera_calibration/charuco_board_detection/board_detection.py
+++ b/src/core_processor/camera_calibration/charuco_board_detection/board_detector.py
@@ -5,11 +5,12 @@ import cv2
import numpy as np
from src.cameras.capture.frame_payload import FramePayload
-from src.core_processor.camera_calibration.charuco_board_detection.charuco_constants import aruco_marker_dict, \
+from src.core_processor.camera_calibration.charuco_board_detection.charuco_board_definition import aruco_marker_dict, \
charuco_board_object, \
number_of_charuco_corners
-from src.core_processor.camera_calibration.charuco_board_detection.detect_charuco_board import detect_charuco_board, \
+from src.core_processor.camera_calibration.charuco_board_detection.charuco_dataclasses import CharucoFramePayload, \
CharucoViewData
+
from src.core_processor.camera_calibration.charuco_board_detection.charuco_image_annotator import (
annotate_image_with_charuco_data,
)
@@ -17,21 +18,10 @@ from src.core_processor.camera_calibration.charuco_board_detection.charuco_image
logger = logging.getLogger(__name__)
-@dataclass
-class CharucoFramePayload:
- raw_frame_payload: FramePayload = FramePayload()
- annotated_image: np.ndarray = None
- charuco_view_data: CharucoViewData = CharucoViewData()
-
-
class BoardDetector:
def detect_charuco_board(self, raw_frame_payload: FramePayload) -> CharucoFramePayload:
- if not raw_frame_payload.success:
- # logger.error("CV2 failed to grab a frame")
- return CharucoFramePayload()
-
if raw_frame_payload.image is None:
logger.error("Frame is empty")
return CharucoFramePayload()
@@ -42,7 +32,6 @@ class BoardDetector:
if charuco_view_data.some_charuco_corners_found:
annotate_image_with_charuco_data(
annotated_image, # this image will have stuff drawn on top of it inside this function
- raw_frame_payload.webcam_id,
charuco_view_data
)
@@ -115,6 +104,7 @@ class BoardDetector:
image_height=image_height)
+
if __name__ == "__main__":
pass
# ## Jon, Run me to easily start a charuco board detect run
diff --git a/src/core_processor/camera_calibration/charuco_board_detection/charuco_constants.py b/src/core_processor/camera_calibration/charuco_board_detection/charuco_board_definition.py
similarity index 61%
rename from src/core_processor/camera_calibration/charuco_board_detection/charuco_constants.py
rename to src/core_processor/camera_calibration/charuco_board_detection/charuco_board_definition.py
index 2eda9a8d..1c61c6bd 100644
--- a/src/core_processor/camera_calibration/charuco_board_detection/charuco_constants.py
+++ b/src/core_processor/camera_calibration/charuco_board_detection/charuco_board_definition.py
@@ -3,6 +3,8 @@ import cv2
aruco_marker_dict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_4X4_250)
number_of_squares_width = 7
number_of_squares_height = 5
+black_square_side_length = 1
+aruco_marker_length_proportional = .8
-charuco_board_object = cv2.aruco.CharucoBoard_create(number_of_squares_width, number_of_squares_height, 1, 0.8, aruco_marker_dict)
+charuco_board_object = cv2.aruco.CharucoBoard_create(number_of_squares_width, number_of_squares_height, black_square_side_length, aruco_marker_length_proportional, aruco_marker_dict)
number_of_charuco_corners = (number_of_squares_width - 1) * (number_of_squares_height - 1)
diff --git a/src/core_processor/camera_calibration/charuco_board_detection/charuco_dataclasses.py b/src/core_processor/camera_calibration/charuco_board_detection/charuco_dataclasses.py
new file mode 100644
index 00000000..9a4d09a3
--- /dev/null
+++ b/src/core_processor/camera_calibration/charuco_board_detection/charuco_dataclasses.py
@@ -0,0 +1,25 @@
+from dataclasses import dataclass
+
+import numpy as np
+
+from src.cameras.capture.frame_payload import FramePayload
+
+
+@dataclass
+class CharucoViewData:
+ charuco_corners: np.ndarray = None
+ charuco_ids: np.ndarray = None
+ aruco_square_corners: np.ndarray = None
+ aruco_square_ids: np.ndarray = None
+ full_board_found: bool = False
+ some_charuco_corners_found: bool = False
+ any_markers_found: bool = False
+ image_width: int = None
+ image_height: int = None
+
+
+@dataclass
+class CharucoFramePayload:
+ raw_frame_payload: FramePayload = FramePayload()
+ annotated_image: np.ndarray = None
+ charuco_view_data: CharucoViewData = CharucoViewData()
diff --git a/src/core_processor/camera_calibration/charuco_board_detection/charuco_image_annotator.py b/src/core_processor/camera_calibration/charuco_board_detection/charuco_image_annotator.py
index 1683520b..6121b3d1 100644
--- a/src/core_processor/camera_calibration/charuco_board_detection/charuco_image_annotator.py
+++ b/src/core_processor/camera_calibration/charuco_board_detection/charuco_image_annotator.py
@@ -3,12 +3,11 @@ from typing import List
import cv2
import numpy as np
-from src.core_processor.camera_calibration.charuco_board_detection.charuco_constants import number_of_charuco_corners
-from src.core_processor.camera_calibration.charuco_board_detection.detect_charuco_board import CharucoViewData
+from src.core_processor.camera_calibration.charuco_board_detection.charuco_board_definition import number_of_charuco_corners
+from src.core_processor.camera_calibration.charuco_board_detection.charuco_dataclasses import CharucoViewData
def _board_text(
- cam_name: str,
charuco_ids: List[int],
num_charuco_corners: int,
full_board_detected_bool: bool,
@@ -16,7 +15,7 @@ def _board_text(
num_ids_as_str = str(len(charuco_ids))
num_corners_as_str = str(num_charuco_corners)
text = (
- f"{cam_name}: {num_ids_as_str} of {num_corners_as_str} Charuco Corner Points Detected "
+ f"{num_ids_as_str} of {num_corners_as_str} Charuco Corner Points Detected "
f"| Full Board Detected: {full_board_detected_bool}"
)
return text
@@ -24,7 +23,6 @@ def _board_text(
def annotate_image_with_charuco_data(
image,
- webcam_id: str,
charuco_view_data: CharucoViewData,
) -> bool:
@@ -36,11 +34,8 @@ def annotate_image_with_charuco_data(
) # yellow? I
# think cv2 uses BGR instead of RGB?
- this_cam_name = f"Camera {webcam_id}"
- text_to_write_on_this_camera = ""
current_cam_corner_count_str = _board_text(
- this_cam_name,
charuco_view_data.charuco_ids,
number_of_charuco_corners,
charuco_view_data.full_board_found,
diff --git a/src/core_processor/camera_calibration/charuco_board_detection/detect_charuco_board.py b/src/core_processor/camera_calibration/charuco_board_detection/detect_charuco_board.py
deleted file mode 100644
index 2bc9be3c..00000000
--- a/src/core_processor/camera_calibration/charuco_board_detection/detect_charuco_board.py
+++ /dev/null
@@ -1,79 +0,0 @@
-from dataclasses import dataclass
-from typing import Optional
-
-import cv2
-import numpy as np
-
-from src.core_processor.camera_calibration.charuco_board_detection.charuco_constants import aruco_marker_dict, \
- charuco_board_object, \
- number_of_charuco_corners
-
-
-@dataclass
-class CharucoViewData:
- charuco_corners: Optional[np.ndarray] = None
- charuco_ids: Optional[np.ndarray] = None
- aruco_square_corners: Optional[np.ndarray] = None
- aruco_square_ids: Optional[np.ndarray] = None
- full_board_found: bool = False
- some_charuco_corners_found: bool = False
- any_markers_found: bool = False
- image_width: int = None
- image_height: int = None
-
-
-def detect_charuco_board(image) -> CharucoViewData:
- """
- detect charuco board in an image.
- more-or-less copied from - https://mecaruco2.readthedocs.io/en/latest/notebooks_rst/Aruco/sandbox/ludovic/aruco_calibration_rotation.html
-
- """
- charuco_corners = []
- charuco_ids = []
- image_height, image_width, color_channels = image.shape
-
- # SUB PIXEL CORNER DETECTION CRITERION
- termination_criteria_threshold = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.00001)
-
- grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
-
- aruco_square_corners, aruco_square_ids, rejected_image_points = cv2.aruco.detectMarkers(
- grayscale_image, aruco_marker_dict
- )
-
- full_board_found = False
- any_markers_found = False
- some_charuco_corners_found = False
-
- if len(aruco_square_corners) > 0:
- any_markers_found = True
- # refine detected corner locations to provide sub-pixel precision
- # https://docs.opencv.org/4.x/dd/d1a/group__imgproc__feature.html#ga354e0d7c86d0d9da75de9b9701a9a87e
- for this_corner in aruco_square_corners:
- cv2.cornerSubPix(
- grayscale_image, this_corner, winSize=(3, 3), zeroZone=(-1, -1), criteria=termination_criteria_threshold
- )
- res2 = cv2.aruco.interpolateCornersCharuco(aruco_square_corners,
- aruco_square_ids,
- grayscale_image,
- charuco_board_object
- )
-
- if res2[1] is not None and res2[2] is not None and len(res2[1]) > 3:
- some_charuco_corners_found = True
-
- if len(res2[2]) == number_of_charuco_corners:
- full_board_found = True
-
- charuco_corners = res2[1]
- charuco_ids = res2[2]
-
- return CharucoViewData(full_board_found=full_board_found,
- some_charuco_corners_found=some_charuco_corners_found,
- any_markers_found=any_markers_found,
- image_width=image_width,
- image_height=image_height,
- charuco_corners=charuco_corners,
- charuco_ids=charuco_ids,
- aruco_square_corners=aruco_square_corners,
- aruco_square_ids=aruco_square_ids)
diff --git a/src/core_processor/camera_calibration/lens_distortion_calibrator.py b/src/core_processor/camera_calibration/lens_distortion_calibrator.py
index 13e6c48a..b6aef117 100644
--- a/src/core_processor/camera_calibration/lens_distortion_calibrator.py
+++ b/src/core_processor/camera_calibration/lens_distortion_calibrator.py
@@ -5,33 +5,13 @@ import numpy as np
from rich import print
from src.cameras.capture.frame_payload import FramePayload
-from src.core_processor.camera_calibration.charuco_board_detection.board_detection import BoardDetector
-from src.core_processor.camera_calibration.charuco_board_detection.charuco_constants import charuco_board_object
-from src.core_processor.camera_calibration.charuco_board_detection.detect_charuco_board import CharucoViewData
+from src.core_processor.camera_calibration.calibration_data_classes import LensDistortionCalibrationData
+from src.core_processor.camera_calibration.charuco_board_detection.board_detector import BoardDetector
+from src.core_processor.camera_calibration.charuco_board_detection.charuco_board_definition import charuco_board_object
from src.core_processor.camera_calibration.calibration_diagnostics_visualizer import CalibrationDiagnosticsVisualizer
-
-
-@dataclass
-class LensDistortionCalibrationData:
- image_width: int
- image_height: int
- reprojection_error: float = None
- camera_matrix: np.ndarray = None
- number_of_lens_distortion_coefficients: int = 4
- lens_distortion_coefficients: np.ndarray = None
- rotation_vectors_of_the_board: np.ndarray = None
- translation_vectors_of_the_board: np.ndarray = None
- lens_distortion_std_dev: float = None
- camera_location_std_dev: float = None
-
- def __post_init__(self):
- if self.camera_matrix is None:
- self.camera_matrix = np.array([[float(self.image_width), 0., self.image_width / 2.],
- [0., float(self.image_width), self.image_height / 2.],
- [0., 0., 1.]])
-
- if self.lens_distortion_coefficients is None:
- self.lens_distortion_coefficients = np.zeros((self.number_of_lens_distortion_coefficients, 1))
+from src.core_processor.camera_calibration.charuco_board_detection.charuco_dataclasses import CharucoViewData
+from src.core_processor.camera_calibration.charuco_board_detection.charuco_image_annotator import \
+ annotate_image_with_charuco_data
class LensDistortionCalibrator:
@@ -67,13 +47,19 @@ class LensDistortionCalibrator:
self.estimate_lens_distortion(charuco_view_data) # this is where the magic happens
- if self._current_calibration.camera_matrix is None:
+ if self._current_calibration is None:
self._current_calibration = LensDistortionCalibrationData(charuco_view_data.image_width,
charuco_view_data.image_height)
return raw_frame_payload.image
undistorted_image_for_debug = self.undistort_image_with_invalid_pixels_as_black(raw_frame_payload.image,
self._current_calibration)
+ if charuco_view_data.some_charuco_corners_found:
+ annotate_image_with_charuco_data(
+ undistorted_image_for_debug, # this image will have stuff drawn on top of it inside this function
+ charuco_view_data
+ )
+
if self.show_calibration_diagnostics_visualizer:
self.calibration_diagnostics_visualizer.update_image_subplot(undistorted_image_for_debug)
@@ -81,6 +67,7 @@ class LensDistortionCalibrator:
self._current_calibration.camera_matrix,
self._current_calibration.lens_distortion_coefficients,
)
+
return undistorted_image
def estimate_lens_distortion(self, new_charuco_view: CharucoViewData):
@@ -150,7 +137,10 @@ class LensDistortionCalibrator:
if self.show_calibration_diagnostics_visualizer:
self.calibration_diagnostics_visualizer.update_reprojection_error_subplot(this_reprojection_error,
- current_best_reprojection_error)
+ current_best_reprojection_error,
+ )
+ if self._current_calibration is not None:
+ self.calibration_diagnostics_visualizer.update_calibration_text_overlay(self._current_calibration)
if this_reprojection_error < current_best_reprojection_error:
@@ -256,17 +246,23 @@ class LensDistortionCalibrator:
3] # ...which I might want to check on later? Currently fixed at Zero
k3 = this_calibration.lens_distortion_coefficients[4]
- number_of_points = 100
- original_points_x = np.linspace(-this_calibration.image_width, this_calibration.image_width, number_of_points)
- original_points_y = np.linspace(-this_calibration.image_height, this_calibration.image_height, number_of_points)
+ number_of_points = 40
+ original_x = np.linspace(this_calibration.image_width, number_of_points)
+ original_y = np.linspace(this_calibration.image_height, number_of_points)
+
+ original_points_xx, original_points_yy = np.meshgrid(original_x, original_y)
+
+ original_points_x = original_points_xx.flatten() # transform 2D grid with 1D array
+ original_points_y = original_points_yy.flatten() # transform 2D grid with 1D array
original_points_xy = np.vstack((original_points_x, original_points_y))
distorted_points_xy = cv2.undistortPoints(original_points_xy,
this_calibration.camera_matrix,
this_calibration.lens_distortion_coefficients)
- distorted_points_x = np.squeeze(distorted_points_xy[0])
- distorted_points_y = np.squeeze(distorted_points_xy[1])
+
+ distorted_points_x = np.squeeze(distorted_points_xy[:, :, 0])
+ distorted_points_y = np.squeeze(distorted_points_xy[:, :, 1])
if self.show_calibration_diagnostics_visualizer:
self.calibration_diagnostics_visualizer.update_image_point_remapping_subplot(original_points_x,