我想调整图像大小,但我的图像有很多零和楠。我想通过排除零和Nan来重新调整大小。
到目前为止我的代码是。
# Import Libraries
import numpy as np
import cv2
# Generate sample data with zero and NaNs
a = np.zeros(shape=(8,8))
a[0] = [1,1,3,3,5,5,7,0]
a[1] = [1,0,3,0,5,0,0,7]
a[2] = [1,2,3,4,5,6,7,8]
a[3] = [1,2,3,0,5,6,7,8]
a[4] = [1,2,3,3,5,6,7,8]
a[5] = [1,2,3,4,5,6,7,8]
a[6] = [2,2,3,4,5,0,7,8]
a[7] = [0,2,3,4,5,6,7,8]
a[1:3,5] = np.nan
# Replace nan with zero (or zero with nan)
# a[np.isnan(a)] = 0
a[a==0] = np.nan
# Resize (8x8) array into (4x4)
c = cv2.resize(a,(4,4),cv2.INTER_CUBIC)
问题在于它在平均时包括零和nan。我还用NaN替换了零,反之亦然。我也试过INTER_NEAREST,INTER_LINEAR等但结果仍然相同。
由于