From a5477631a60b626294ba0cd57c68a7bd3bc151b5 Mon Sep 17 00:00:00 2001 From: Russ Garrett Date: Sun, 5 Jan 2025 20:50:04 +0000 Subject: [PATCH 1/2] Dark frame subtraction --- marimapper/detector.py | 12 ++++++++---- marimapper/detector_process.py | 6 +++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/marimapper/detector.py b/marimapper/detector.py index 3acdffd..c27cdce 100644 --- a/marimapper/detector.py +++ b/marimapper/detector.py @@ -115,10 +115,12 @@ def set_cam_dark(cam: Camera, exposure: int) -> None: def find_led( - cam: Camera, threshold: int = 128, display: bool = True + cam: Camera, threshold: int = 128, display: bool = True, dark_frame=None ) -> Optional[Point2D]: image = cam.read() + if dark_frame is not None: + image = cv2.absdiff(image, dark_frame) results = find_led_in_image(image, threshold) if display: @@ -138,8 +140,10 @@ def enable_and_find_led( display: bool = False, ) -> Optional[LED2D]: + dark_frame = cam.read() + # First wait for no leds to be visible - while find_led(cam, threshold, display) is not None: + while find_led(cam, threshold, display, dark_frame=dark_frame) is not None: pass # Set the led to on and start the clock @@ -152,7 +156,7 @@ def enable_and_find_led( while ( point is None and time.time() < response_time_start + timeout_controller.timeout ): - point = find_led(cam, threshold, display) + point = find_led(cam, threshold, display, dark_frame) led_backend.set_led(led_id, False) @@ -161,7 +165,7 @@ def enable_and_find_led( timeout_controller.add_response_time(time.time() - response_time_start) - while find_led(cam, threshold, display) is not None: + while find_led(cam, threshold, display, dark_frame=dark_frame) is not None: pass return LED2D(led_id, view_id, point) diff --git a/marimapper/detector_process.py b/marimapper/detector_process.py index eba79f2..5b70c8e 100644 --- a/marimapper/detector_process.py +++ b/marimapper/detector_process.py @@ -86,9 +86,13 @@ def run(self): # scan start here set_cam_dark(cam, self._dark_exposure) + dark_frame = cam.read() # Firstly, if there are leds visible, break out - if find_led(cam, self._threshold, self._display) is not None: + if ( + find_led(cam, self._threshold, self._display, dark_frame=dark_frame) + is not None + ): logger.error( "Detector process can detect an LED when no LEDs should be visible" ) From 33adb99d26078a08fe3dabaade8d20a50970aeef Mon Sep 17 00:00:00 2001 From: Samuel Date: Tue, 7 Jan 2025 08:28:45 +0000 Subject: [PATCH 2/2] Replaced cv2.absdiff with cv2.subtract --- marimapper/detector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/marimapper/detector.py b/marimapper/detector.py index c27cdce..728530b 100644 --- a/marimapper/detector.py +++ b/marimapper/detector.py @@ -120,7 +120,7 @@ def find_led( image = cam.read() if dark_frame is not None: - image = cv2.absdiff(image, dark_frame) + cv2.subtract(image, dark_frame, image) results = find_led_in_image(image, threshold) if display: