如何使用 cv2.resize (v4.2.0)

Wur*_*rmD 1 python opencv

我\xe2\x80\x99m很困惑,请帮我理解:

\n\n

我有这个错误:

\n\n
    img_array = cv2.resize(img_array, dsize=(299, 299, 3), interpolation=cv2.INTER_CUBIC)                                                                                                              \nTypeError: function takes exactly 2 arguments (3 given) \n
Run Code Online (Sandbox Code Playgroud)\n\n

什么时候

\n\n
print(img_array): <class \'numpy.ndarray\'>\nimg_array.shape: (240, 320, 3)\nprint(cv2.__version__): 4.2.0 \n
Run Code Online (Sandbox Code Playgroud)\n\n

VSCode 中将鼠标悬停在调整大小上会显示

\n\n
def resize(src, dsize, dst=None, fx=None, fy=None, interpolation=None)\nresize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) -> dst . @brief Resizes an image. .\n. 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 and type are derived from . the src,dsize,fx, and fy. If you want to resize src so that it fits 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 #INTER_AREA interpolation, whereas to . enlarge an image, it will generally look best with c#INTER_CUBIC (slow) or #INTER_LINEAR . (faster but still looks OK). .\n. @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 is 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 #InterpolationFlags .\n. @sa warpAffine, warpPerspective, remap\n
Run Code Online (Sandbox Code Playgroud)\n\n

这与文档中的内容不一致https://docs.opencv.org/4.2.0/da/d54/group__imgproc__transform.html#ga47a974309e9102f5f08231edc7e7529d

\n\n

有谁知道\xe2\x80\x99s 是什么?如何正确使用cv2.resize v4.2.0?(因为我所有的谷歌搜索都产生了我尝试运行的代码)

\n

小智 6

我认为错误来自于只需要宽度和高度的尺寸。

尝试这个:

img_array = cv2.resize(img_array, dsize=(299, 299), interpolation=cv2.INTER_CUBIC)
Run Code Online (Sandbox Code Playgroud)

您可以在这里找到详细的解释:

cv2.resize的解释

希望能帮助到你!