1. 程式人生 > >opencv 的resize 和numpy 的reshape 中 ,高和寬位置的區別

opencv 的resize 和numpy 的reshape 中 ,高和寬位置的區別

今天在處理圖片的時候,遇到了兩行程式碼,就是先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的操作經常變,需要多多注意