1、網上手冊查詢
A. 打開Welcome to opencv documentation
B. 在左側輸入要查詢的函數, 如圖1.1,假設我們要查詢resize函數
C. 點擊Search按鈕后可以看到結果,如圖1.2
D. 點開第一條函數說明,進去查找到resize函數,如圖1.3
從圖1.3中可以看到在python中的resize函數說明為
cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst
-
[, dst[, fx[, fy[, interpolation]]]])
意味着這些參數是可選參數 -
→ dst
意味着函數返回dst -
參數說明
Parameters
可以看到參數的具體含義
D.A:src – input image
, src為輸入圖片
D.B:dst
, dst為輸出圖片
D.C:dsize
, dsize為輸出圖片大小,當它為0時,要計算dsize = Size(round(fx*src.cols), round(fy&src.rows))
,由這句話可以得出當dsize=0時,要同時輸入src、fx、fy
D.D:fx
, 水平縮放倍率,當它為0時則有fx = (double)dsize.width/src.clos
D.E:fy
, 垂直縮放倍率,當它為0時則有fy = (double)dsize.height/src.rows
D.F:interpolation
插值,主要有以下幾個選項
INTER_NEAREST
INTER_LINEAR
INTER_AREA
INTER_CUBIC
INTER_LANCZOS4
- 根據D.C、D.D、D.E可知道定有dsize和fx、fy兩種方式可以指定輸出圖片大小。
dsize方式
dsize=(180, 180)
fx、fy方式
fx=2, fy=2
如果dsize和fx、fy都存在,先計算dsize,如果dsize=None, 則輸出圖片大小為fx、fy,如果dsize!=None, 則為dsize。
- 閱讀了上面的說明,我們知道resize可以像下面這樣用
# 只指定src和dsize
res = cv2.resize(src=img, dsize=(2*width, 2*height))
# 只指定src、dsize和interpolation
res = cv2.resize(src=img, dsize=(2*width, 2*height), interpolation = cv2.INTER_LINEAR)
# 只指定src、dsize、fx、fy
res = cv2.resize(src=img, dsize=None, fx=0.5, fy = 0.5)
# 只指定src、dsize、fx、fy和interpolation
res = cv2.resize(src=img, dsize=None, fx=0.5, fy = 0.5, interpolation = cv2.INTER_LINEAR)
2、使用help命令查看
A. 在CMD中運行python進入python模式中
F:\QAQ\python\opencv>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
B. 導入cv2模塊
>>> import cv2
C. 使用help查看cv2.resize函數
>>> help(cv2.resize)
Help on built-in function resize:
resize(...)
resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) -> dst
. @brief Resizes an image.
.
. The function resize resizes the image src down to or up to the specified
size. Note that the
. initial dst type or size are not taken into account. Instead, the size a
nd type are derived from
. the `src`,`dsize`,`fx`, and `fy`. If you want to resize src so that it f
its the pre-created dst,
. you may call the function as follows:
. @code
. // explicitly specify dsize=dst.size(); fx and fy will be computed from
that.
. resize(src, dst, dst.size(), 0, 0, interpolation);
. @endcode
. If you want to decimate the image by factor of 2 in each direction, you
can call the function this
. way:
. @code
. // specify fx and fy and let the function compute the destination image
size.
. resize(src, dst, Size(), 0.5, 0.5, interpolation);
. @endcode
. To shrink an image, it will generally look best with cv::INTER_AREA inte
rpolation, whereas to
. enlarge an image, it will generally look best with cv::INTER_CUBIC (slow
) or cv::INTER_LINEAR
. (faster but still looks OK).
.
. @param src input image.
. @param 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.
. @param dsize output image size; if it equals zero, it is computed as:
. \f[\texttt{dsize = Size(round(fx*src.cols), round(fy*src.rows))}\f]
. Either dsize or both fx and fy must be non-zero.
. @param fx scale factor along the horizontal axis; when it equals 0, it i
s computed as
. \f[\texttt{(double)dsize.width/src.cols}\f]
. @param fy scale factor along the vertical axis; when it equals 0, it is
computed as
. \f[\texttt{(double)dsize.height/src.rows}\f]
. @param interpolation interpolation method, see cv::InterpolationFlags
.
. @sa warpAffine, warpPerspective, remap