Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions caiman/base/movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
20 changes: 20 additions & 0 deletions caiman/tests/test_movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading