fix: split large channel masks to handle cv2.resize 512 limitations#21947
Conversation
- Add batch processing for masks with >512 channels to avoid cv2.resize limitation - Process masks in batches of 512 channels and concatenate results - Fixes Error when max_det>512 causes wong result returned
|
👋 Hello @ShuaiLYU, thank you for submitting an
Since this PR addresses a bug (OpenCV resize limitation with masks having >512 channels in
Example MRE template (feel free to adapt): import numpy as np
import cv2
from ultralytics.utils.ops import scale_image
# Synthetic mask with >512 channels to reproduce the issue
h, w, c = 8, 8, 600
masks = np.random.rand(h, w, c).astype(np.float32)
im0_shape = (64, 64) # target size
# Call the function under test
out = scale_image(masks, im0_shape, ratio_pad=(1.0, (0, 0)))
print(out.shape)For more guidance, please refer to our Contributing Guide. Don’t hesitate to leave a comment if you have any questions. Thank you for contributing to Ultralytics! 🚀 |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
result |
cv2.resize 512 limitations
|
this issue is found by @ShuaiLYU that cv2.resize can not handle arrays with the 3rd dimension greater than 512, reproduce example: from ultralytics import YOLO
model = YOLO("yolo11n-seg.pt")
model.val(data="coco128-seg.yaml", max_det=513, save_json=True) # it works with max_det=512 |
|
@ShuaiLYU I checked the code again, no worries about the case that there's no 3rd dimension as input masks, the input masks would always have shape (H, W, N). And this two lines of code of for the case when N=1, cv2.resize would eliminate the 3rd dimension and output resized image with shape ultralytics/ultralytics/utils/ops.py Lines 248 to 249 in 56e6153 |
|
🎉 Fantastic work on this merge, @ShuaiLYU — and big kudos to @Laughing-q for the collaboration! “Details make perfection, and perfection is not a detail.” — Leonardo da Vinci. Your fix for OpenCV’s 512-channel limit in mask resizing is exactly that kind of detail: a precise improvement that makes our segmentation workflows more robust, reliable, and future-proof for large multi-channel outputs. Thank you for strengthening |
…ultralytics#21947) Co-authored-by: Jing Qiu <61612323+Laughing-q@users.noreply.github.com> Co-authored-by: Laughing-q <1185102784@qq.com>
🛠️ PR Summary
Made with ❤️ by Ultralytics Actions
🌟 Summary
Fixes a bug in mask resizing by handling OpenCV’s 512-channel limit, ensuring large multi-channel masks resize reliably without errors. ✅
📊 Key Changes
scale_imageinultralytics/utils/ops.pyto:cv2.resizeand concatenate the results.🎯 Purpose & Impact