1. 程式人生 > >老衛帶你學---opencv中shape與resize的區別

老衛帶你學---opencv中shape與resize的區別




  我們 習慣的座標表示 是 先 x 橫座標,再 y 縱座標。在影象處理中,這種慣性思維尤其需要擔心。

  因為在計算機中,影象是以矩陣的形式儲存的,先行後列。所以,一張 寬×高×顏色通道=480×256×3 的圖片會儲存在一個 256×480×3 的三維張量中。影象處理時也是按照這種思想進行計算的(其中就包括 OpenCV 下的影象處理),即 高×寬×顏色通道


  但是問題來了,cv2.resize這個api卻是個小例外。因為它的引數輸入卻是 寬×高×顏色通道

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.

  由以下語段可知, cv2.resizedsize 的引數輸入是 x軸×y軸,即 寬×高

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.


  自己寫了一個程式碼例項來驗證它:

import cv2
import numpy as np
import random


seq = [random.randint(0, 255) for _ in range(256*480*3)]
mat = np.resize(seq, new_shape=[256, 480, 3])
print ('mat.shape = {}'.format(mat.shape))
cv2.imwrite('origin_pic.jpg', mat)
origin_pic = cv2.imread('./origin_pic.jpg')
print ('origin_pic.shape = {}'.format(origin_pic.shape))
resize_pic = cv2.resize(src=origin_pic,
                        dsize=(int(origin_pic.shape[1] * 2),
                               int(origin_pic.shape[0] * 1))
                        )
print ('resize_pic.shape = {}'.format(resize_pic.shape))
cv2.imshow('resize_pic', resize_pic)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

  Output:

mat.shape = (256, 480, 3)
origin_pic.shape = (256, 480, 3)
resize_pic.shape = (256, 960, 3)
  • 1
  • 2
  • 3

  成功應驗了文件裡的引數說明。

也就是說,opencv中的image的shape屬性是=高*寬*通道,而cv.resize操作卻是輸入為(寬*高),這裡尤為要注意!!!