Skip to content

Error in resize method when using nearest neighbour interpolation #10146

@Kirill888

Description

@Kirill888
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:

int sx = cvFloor(x*ifx);

Should be instead:

   int sx = cvFloor(x*ifx + (ifx-1)*0.5);

and here:

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions