From beffa1ba463ebff3fc6bdb84ec22d96edc3307a2 Mon Sep 17 00:00:00 2001 From: Francesco Mattioli Date: Tue, 7 Oct 2025 13:08:13 +0200 Subject: [PATCH] Fix 2 channel TIFF support for `plot_images` (#22282) Signed-off-by: Glenn Jocher Co-authored-by: UltralyticsAssistant Co-authored-by: Glenn Jocher --- ultralytics/utils/plotting.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/ultralytics/utils/plotting.py b/ultralytics/utils/plotting.py index 563a5288..733a7642 100644 --- a/ultralytics/utils/plotting.py +++ b/ultralytics/utils/plotting.py @@ -712,6 +712,12 @@ def plot_images( Note: This function supports both tensor and numpy array inputs. It will automatically convert tensor inputs to numpy arrays for processing. + + Channel Support: + - 1 channel: Greyscale + - 2 channels: Third channel added as zeros + - 3 channels: Used as-is (standard RGB) + - 4+ channels: Cropped to first 3 channels """ for k in {"cls", "bboxes", "conf", "masks", "keypoints", "batch_idx", "images"}: if k not in labels: @@ -731,7 +737,13 @@ def plot_images( if len(images) and isinstance(images, torch.Tensor): images = images.cpu().float().numpy() - if images.shape[1] > 3: + + # Handle 2-ch and n-ch images + c = images.shape[1] + if c == 2: + zero = np.zeros_like(images[:, :1]) + images = np.concatenate((images, zero), axis=1) # pad 2-ch with a black channel + elif c > 3: images = images[:, :3] # crop multispectral images to first 3 channels bs, _, h, w = images.shape # batch size, _, height, width