Skip to content

Python - cv2.resize with INTER_NEAREST return corrupted slice for channels > 4 #15075

Description

@mrunibe
System information (version)

OpenCV => 4.1.0
Operating System / Platform => Ubuntu 16.04.1, Linux 4.13.0
Python => 3.6.8
numpy => 1.16.4

Detailed description

cv2.resize with an array having a 3rd-dimension > 4 (must be an odd number) and nearest-neighbor interpolation returns corrupted data in the last channel. It is not deterministic and looks like memory corruption.

Steps to reproduce
import numpy as np
import cv2

# up to DIM=4 is OK
DIM = 5
T = np.zeros((12, 12,DIM)).astype(np.uint8)
T[2, 2, DIM-1] = 1
print("Sum: ", T.sum())
print(T[:, :, DIM-1])
R = cv2.resize(T, None, fx=0.9, fy=1,interpolation=cv2.INTER_NEAREST)
# it does not help to supply a pre-allocated array either
#R = np.zeros((12, 11, DIM)).astype(np.uint8)
#R = cv2.resize(T, dsize=R.shape[:2], dst=R, interpolation=cv2.INTER_NEAREST)

print("Sum: ", R.sum())
print(R[:, :, DIM-1])

Output:

Sum:  1
[[0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 1 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0]]
Sum:  836
[[248 113   0 214 176  85   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0]]

Activity

  1. keeper121 commented on Feb 3, 2020

    @keeper121
    Contributor

    Seems problem in resizeNNInvoker class with loop over image.

    https://github.com/keeper121/opencv/blob/57c99914133bea42b1fd6e176ebf35e210d7163d/modules/imgproc/src/resize.cpp#L1044-L1050

    for( x = 0; x < dsize.width; x++, D += pix_size )
    {
        const int* _tS = (const int*)(S + x_ofs[x]);
        int* _tD = (int*)D;
        for( int k = 0; k < pix_size4; k++ )              // <-------------
            _tD[k] = _tS[k];
    }
    

    The last array's dimension not going through the loop if it has odd size.

    Potential fix is replace loop defenition with:

    for( int k = 0; k <= pix_size4; k++ )
            _tD[k] = _tS[k];
    

    After that provided code works as expected.

    Output:

    ('Sum: ', 1)
    [[0 0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0 0]
     [0 0 1 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0 0]]
    ('Sum: ', 1)
    [[0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0]
     [0 0 1 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0]]
    

    But I don't know how this change affect on other modules witch uses NN resize.
    I create pull request to future investigate what it breaks or not.
    #16497

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions