opencv 的resize 和numpy 的reshape 中 ,高和宽位置的区别

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/loovelj/article/details/81097614

今天在处理图片的时候,遇到了两行代码,就是先resize图片,在加一个维度。执行发现出了问题,代码如下:

img = cv2.resize(img, (image_height,image_width ),interpolation=cv2.INTER_CUBIC)
img = np.reshape(img, [image_height, image_width, image_channel])

隐约估计是宽高的顺序问题,经过查文档,其中,resize的文档如下:

resize
Resizes an image.
C++: void resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR )¶
Python: cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst
C: void cvResize(const CvArr* src, CvArr* dst, int interpolation=CV_INTER_LINEAR )
Python: cv.Resize(src, dst, interpolation=CV_INTER_LINEAR) → None
Parameters:
src – input image.
dst – output image; it has the size dsize (when it is non-zero) or the size computed from src.size(), fx, and fy; the type of dst is the same as of src.
dsize –
output image size; if it equals zero, it is computed as:
\texttt{dsize = Size(round(fx*src.cols), round(fy*src.rows))}
Either dsize or both fx and fy must be non-zero.

也就是说resize 是默认的先fx轴,后fy轴,也就是先宽后高

但是numpy中的reshape,是指矩阵中的(行,列),对应图片就是(高,宽)

#img = cv2.resize(img, (image_height,image_width ),interpolation=cv2.INTER_CUBIC)
img = cv2.resize(img, (image_width ,image_height),interpolation=cv2.INTER_CUBIC)
img = np.reshape(img, [image_height, image_width, image_channel])

图片中关于size或shape的操作经常变,需要多多注意

猜你喜欢

转载自blog.csdn.net/loovelj/article/details/81097614
今日推荐