diff --git a/caiman/base/movies.py b/caiman/base/movies.py index 5dec250cf..044be63c2 100644 --- a/caiman/base/movies.py +++ b/caiman/base/movies.py @@ -1948,13 +1948,17 @@ def process_frame(iddxx, frame, bord_px, magnification, interpolation, minmov, m fontFace=5, fontScale=0.8, thickness=1)[0] - cv2.putText(frame, + # Since OpenCV 5, cv2.putText only draws on 8-bit images and frame is floating point + # here, so render the text into an 8-bit mask and copy it onto the frame + text_mask = np.zeros(frame.shape[:2], dtype=np.uint8) + cv2.putText(text_mask, 'Frame = ' + str(iddxx), ((frame.shape[1] - text_width) // 2, frame.shape[0] - (text_height + 5)), fontFace=5, fontScale=0.8, - color=(255, 255, 255), + color=255, thickness=1) + frame[text_mask > 0] = 255 return frame if backend == 'pyplot': diff --git a/caiman/tests/test_movies.py b/caiman/tests/test_movies.py index 35e8c6f9f..87f26b65e 100644 --- a/caiman/tests/test_movies.py +++ b/caiman/tests/test_movies.py @@ -36,3 +36,23 @@ def reference_to2D(mov, order: str): npt.assert_array_equal(movie_flat, reference_to2D(movie, order)) movie_from_flat = movie_flat.to3DFromPixelxTime(shape, order) npt.assert_array_equal(movie_from_flat, movie) + + +def test_play_plot_text(monkeypatch): + # cv2.putText only draws on 8-bit images in OpenCV 5, while play() works on float frames. + # Record the frames instead of opening a window so this runs headless. + shown = [] + monkeypatch.setattr(movies.cv2, 'imshow', lambda name, frame: shown.append(frame.copy())) + monkeypatch.setattr(movies.cv2, 'waitKey', lambda *args: -1) + monkeypatch.setattr(movies.cv2, 'destroyAllWindows', lambda: None) + + rng = np.random.default_rng(0) + mov = movies.movie(rng.random((3, 40, 60)).astype(np.float32), fr=30) + mov.play(backend='opencv', plot_text=True, fr=1000, q_max=100, q_min=0) + + assert len(shown) == 3 + for frame in shown: + assert np.issubdtype(frame.dtype, np.floating) + # the frame number is burned in at full brightness, the rest stays in [0, 1] + assert np.any(frame == 255) + npt.assert_array_less(frame[frame != 255], 1 + 1e-6)