-
-
Notifications
You must be signed in to change notification settings - Fork 56.5k
Error in resize method when using nearest neighbour interpolation #10146
Description
System information (version)
- OpenCV => master
- Operating System / Platform => all
- Compiler => all
Detailed description
When resizing image with nearest neighbour source image pixel coordinates are computed incorrectly. For example shrink a 5x5 image down to 1x1, you should get center pixel value in the result, instead top-left pixel value is present in the result.
Incorrect code is here:
opencv/modules/imgproc/src/resize.cpp
Line 235 in 981009a
| int sx = cvFloor(x*ifx); |
Should be instead:
int sx = cvFloor(x*ifx + (ifx-1)*0.5);and here:
opencv/modules/imgproc/src/resize.cpp
Line 144 in 981009a
| int sy = std::min(cvFloor(y*ify), ssize.height-1); |
Should be instead:
int sy = std::min(cvFloor(y*ify + (ify-1)*0.5), ssize.height-1);There are probably more optimized versions (maybe AVX version) that are also using wrong math, I didn't look for them.
I have a more detailed explanation with equations in here:
https://gist.github.com/Kirill888/6b187e65f088971a48d078df72d27d32
I'm aware of this issue #9096, it's the same problem, but I thought I'll create new issue anyway to discuss the solution for it.