From 909de1709d9461e993e9c82ea26cb56ee733987b Mon Sep 17 00:00:00 2001 From: Joe Knapper Date: Thu, 9 Oct 2025 15:36:01 +0100 Subject: [PATCH 1/3] set ce_enable in tuning_utils --- .../things/camera/picamera.py | 12 +++++++++++ .../camera/picamera_tuning_file_utils.py | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/openflexure_microscope_server/things/camera/picamera.py b/src/openflexure_microscope_server/things/camera/picamera.py index 40f28f8b..aa8ca5dd 100644 --- a/src/openflexure_microscope_server/things/camera/picamera.py +++ b/src/openflexure_microscope_server/things/camera/picamera.py @@ -826,6 +826,17 @@ class StreamingPiCamera2(BaseCamera): tf_utils.set_static_geq(self.tuning, offset) self._initialise_picamera() + @lt.thing_action + def set_ce_enable_to_off(self) -> None: + """Set the contrast enhancement to disabled. + + Adaptive contrast enhancement modifies settings to adapt to each field + of view, causing inconsistent settings when capturing. + """ + with self._streaming_picamera(pause_stream=True): + tf_utils.set_ce_to_disabled(self.tuning) + self._initialise_picamera() + @lt.thing_action def full_auto_calibrate(self, portal: lt.deps.BlockingPortal) -> None: """Perform a full auto-calibration. @@ -843,6 +854,7 @@ class StreamingPiCamera2(BaseCamera): self.flat_lens_shading() self.auto_expose_from_minimum() self.set_static_green_equalisation() + self.set_ce_enable_to_off() self.calibrate_lens_shading() self.reset_ccm() self.calibrate_white_balance() diff --git a/src/openflexure_microscope_server/things/camera/picamera_tuning_file_utils.py b/src/openflexure_microscope_server/things/camera/picamera_tuning_file_utils.py index f86753c7..d9c6076b 100644 --- a/src/openflexure_microscope_server/things/camera/picamera_tuning_file_utils.py +++ b/src/openflexure_microscope_server/things/camera/picamera_tuning_file_utils.py @@ -104,6 +104,26 @@ def geq_is_static(tuning: dict) -> bool: return geq["offset"] == 65535 +def set_ce_to_disabled( + tuning: dict, +) -> None: + """Update the ``rpi.ce_enable`` section of a camera tuning dict. + + :param tuning: the raspberry pi tuning file. This will be updated in-place to + set ce_enable to 0. + """ + contrast = Picamera2.find_tuning_algo(tuning, "rpi.contrast") + contrast["ce_enable"] = ( + 0 # disable ce_enable to prevent adaptive contrast enhancement + ) + + +def ce_enable_is_static(tuning: dict) -> bool: + """Whether the ce_enable flag is disabled.""" + contrast = Picamera2.find_tuning_algo(tuning, "rpi.contrast") + return contrast["ce_enable"] == 0 + + def copy_alsc_section(from_tuning: dict, to_tuning: dict) -> None: """Copy the ``rpi.alsc`` algorithm from one tuning to another. From 4135532476cdcace21a753da42e47fcf0874adef Mon Sep 17 00:00:00 2001 From: Julian Stirling Date: Thu, 9 Oct 2025 16:40:03 +0000 Subject: [PATCH 2/3] Apply suggestions from code review of branch ce-disable --- .../things/camera/picamera_tuning_file_utils.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/openflexure_microscope_server/things/camera/picamera_tuning_file_utils.py b/src/openflexure_microscope_server/things/camera/picamera_tuning_file_utils.py index d9c6076b..b202616a 100644 --- a/src/openflexure_microscope_server/things/camera/picamera_tuning_file_utils.py +++ b/src/openflexure_microscope_server/things/camera/picamera_tuning_file_utils.py @@ -107,15 +107,13 @@ def geq_is_static(tuning: dict) -> bool: def set_ce_to_disabled( tuning: dict, ) -> None: - """Update the ``rpi.ce_enable`` section of a camera tuning dict. + """Set ``ce_enable`` in ``rpi.contrast`` to zero to disable adaptive contrast enhancement. :param tuning: the raspberry pi tuning file. This will be updated in-place to set ce_enable to 0. """ contrast = Picamera2.find_tuning_algo(tuning, "rpi.contrast") - contrast["ce_enable"] = ( - 0 # disable ce_enable to prevent adaptive contrast enhancement - ) + contrast["ce_enable"] = 0 def ce_enable_is_static(tuning: dict) -> bool: From b58ddbf7d93ad7ea0cb13b3b234e2bfbd89569c8 Mon Sep 17 00:00:00 2001 From: jaknapper Date: Thu, 9 Oct 2025 16:51:10 +0100 Subject: [PATCH 3/3] Update testing --- picamera_coverage.zip | Bin 54038 -> 54038 bytes tests/test_cameras.py | 1 + 2 files changed, 1 insertion(+) diff --git a/picamera_coverage.zip b/picamera_coverage.zip index d1d27c2d3a5a54b3a3986031313001f9d9ca3036..d692e25b6ed62c5bc00be6b391307d61859f658d 100644 GIT binary patch delta 342 zcmbQXjCtBJW}yIYW)=|!5Qy#bjJCFsyu49Jr9mi*f&VA}NB(L2&HT}Pr}Q!Fdx{@ zSi;P}12WuzfuVuX$EBK0fq{X+fI;Fw!{+Y(^$r{$OEvyBo}KJ;;i5)jqD69|X-bNP ziJ_5EVwyo}qJg2YskxDnMQUPFTB?PyiIJs&v8CDM3m0rAmtUM#pJZfUWM*iVXlZPj zl4OvWVvw3*l44|Rnqq96YHDDRYH65~Xr7X0p`@drq?epumRgjUo~l=nnVguLT9l|{ zrJ$6Snqq90Xl`z1V3cZUY?f%5VrT&7q*x>wB_}5*8k<>In3$R>)dqMoGKnyw1Qp}u O$-S2>5Pmp&$rAw6x@0{7 delta 339 zcmbQXjCtBJW}yIYW)=|!5O4{1k1k*1yAk89AFICNuYE$}#+qb}(l+ z05X_?nSnv#fXkM21||ju2IdAv9*)i3{p%e#KsL_*+q!GA(}jx~CI)Fn7HJkqsm2D$ zMu}!7mWk%67M6)AX~u@;29~AIMwY2*lP_GbnOuHxT78;@nYpQPvbl+|ahh40 zrCDN%sZnBDYD$_}s<}aup;3~dkwv0`rKOUNf|6cxepzZ!VtT4xL1uDdZfa4Yl9hr| zs&T5NrK!1@VN$Y5vPoKsnSp_cp@nf;l8L#2xoL{2sgaq5v6-czQf+`YBa;X-N;olY OnB05G0^x_VmplQAykrOf diff --git a/tests/test_cameras.py b/tests/test_cameras.py index 27de7bd4..ffb65df1 100644 --- a/tests/test_cameras.py +++ b/tests/test_cameras.py @@ -114,6 +114,7 @@ def test_thing_description_equivalence(mock_picam_thing): picamera_extra_actions = { "flat_lens_shading_chrominance", "set_static_green_equalisation", + "set_ce_enable_to_off", "stop_streaming", "reset_ccm", }