简体   繁体   中英

How to use cv2.resize (v4.2.0)

I'm confused, please help me understand:

I have this error:

    img_array = cv2.resize(img_array, dsize=(299, 299, 3), interpolation=cv2.INTER_CUBIC)                                                                                                              
TypeError: function takes exactly 2 arguments (3 given) 

When

print(img_array): <class 'numpy.ndarray'>
img_array.shape: (240, 320, 3)
print(cv2.__version__): 4.2.0 

Mouseover on resize in VSCode shows

def resize(src, dsize, dst=None, fx=None, fy=None, interpolation=None)
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 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). .
. @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 .
. @sa warpAffine, warpPerspective, remap

Which is inconsistent with what is in the docs https://docs.opencv.org/4.2.0/da/d54/group__imgproc__transform.html#ga47a974309e9102f5f08231edc7e7529d

Does anyone know what's up? How do I correctly use cv2.resize v4.2.0? (since all my googling yielded the code I attempted to run)

I think the error comes from the dimension that requires only the width and height.

Try this:

img_array = cv2.resize(img_array, dsize=(299, 299), interpolation=cv2.INTER_CUBIC)

You can find the detailed explanation here:

Explanation of cv2.resize

Hope it helps!

I would go about this by doing:

new_img_array = cv2.resize("img_array", dsize=(299,229))

I don't believe that interpolation is necessary for simple resizing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM