Skip to content

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

@mrunibe

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]]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions