mirror of
https://github.com/opencv/opencv.git
synced 2026-11-17 06:47:47 -06:00
dnn: fix blobRectsToImageRects when the image is not resized - #30243 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work. No existing report covers this case; reproduction is below. Related: #26085, #30042. - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable. Accuracy test added; no test data needed. - [ ] The feature is well documented and sample code can be built with the project CMake. Not applicable to this bug fix. ### Problem `Image2BlobParams::blobRectToImageRect()` and `blobRectsToImageRects()` return `Rect(0, 0, 0, 0)` for every rect when `Image2BlobParams::size` equals the image size, in all three padding modes. With `size` left empty (the default, which `blobFromImageWithParams()` treats as "keep the image size"), the mapping divides by zero and returns `INT_MIN` coordinates. Running a 640x640 detector on 640x640 frames is enough to hit the first case. ```python import cv2 as cv p = cv.dnn.Image2BlobParams() p.size = (640, 640) p.paddingmode = cv.dnn.DNN_PMODE_LETTERBOX print(p.blobRectToImageRect((10, 20, 30, 40), (640, 640))) # (0, 0, 0, 0), expected (10, 20, 30, 40) p.size = (0, 0) print(p.blobRectToImageRect((10, 20, 30, 40), (640, 480))) # (-2147483648, -2147483648, -2147483648, -2147483648), expected (10, 20, 30, 40) ``` Observed with the `opencv-python` 5.0.0 wheel and with a 4.x build at `802cf95aa3`. The function is identical on 4.x and has been since it was added in #24539 (4.9.0). ### Cause `blobFromImagesWithParams()` replaces an empty `size` with the image size and applies no resize, crop or padding when the two sizes match. `blobRectsToImageRects()` only handles the `size != imgSize` branch: when the sizes match, the output keeps the zero rects created by `rImg.resize()`, and an empty `size` goes into the resize formulas as a zero divisor. ### Change `blobRectsToImageRects()` now follows the same two rules as `blobFromImagesWithParams()`: an empty `size` means the image size, and when the sizes match, blob and image coordinates are the same, so the input rects are returned. The resize, crop-center and letterbox mappings are unchanged. The new test covers both cases for all three padding modes. It also checks that the blob built with the same parameters is pixel-identical to the image, which is what makes returning the input rects correct here. #30042 (5.x feature PR) contains an equivalent pass-through for the equal-size case as part of a larger Model API change; this is the standalone fix for 4.x, including the empty-size case. ### Testing Built `opencv_test_dnn` and the Python bindings from this branch (head `d90164465c`, base 4.x `802cf95aa3`) on Ubuntu 26.04 (WSL2), GCC 15.2, Ninja, `BUILD_LIST=dnn,python3,ts`. - `opencv_test_dnn --gtest_filter=blobRectToImageRect*`: 4 passed. On base `802cf95aa3` the new `blobRectToImageRect.no_resize` fails in all six mode/size combinations (`[0 x 0 from (0, 0)]` for equal sizes, `INT_MIN` for empty size); the blob pixel-identity assertion passes there too. - `opencv_test_dnn --gtest_filter='blob*:*Image2Blob*:*imagesFromBlob*'`: 11 passed. - `modules/dnn/misc/python/test/test_dnn.py`: 10 passed, including all `blob*` tests; 6 model tests failed only because their opencv_extra files are not present locally (`dnn/googlenet_0.png`, `dnn/opencv_face_detector.prototxt`, ...). - The Python reproducer above prints `(10, 20, 30, 40)` for both cases with this branch. - No compiler warnings in the touched files; `git diff --check` clean. - Full `opencv_test_dnn` was not run (needs opencv_extra test data).