我要评分
获取效率
正确性
完整性
易理解

resize

Usage

Scales up or down an image. You can adjust the image width and height by specifying the target size or scaling factor.

Multiple interpolation modes, such as cv2.INTER_LINEAR, cv2.INTER_CUBIC, and cv2.INTER_NEAREST_EXACT, are available to maintain the image quality during scaling.

Interface

1
cv2.resize(src, dsize, fx=0, fy=0, interpolation=cv2.INTER_LINEAR)

Parameters

Parameter

Description

Value Range

Input/Output

src

Input image.

Not null

Input

dsize

Size (width and height) of the target image. If this parameter is specified, the values of fx and fy are invalid. If this parameter is set to (0, 0) or not specified, the values of fx and fy are used.

Tuple (x, y), where x and y are positive integers

Input

fx

Horizontal scaling factor. The default value is 0.

(0, inf)

Input

fy

Vertical scaling factor. The default value is 0.

(0, inf)

Input

interpolation

Interpolation mode. The value can be cv2.INTER_LINEAR (default), cv2.INTER_NEAREST_EXACT, or cv2.INTER_CUBIC.

cv2.INTER_LINEAR (default), cv2.INTER_NEAREST_EXACT, cv2.INTER_CUBIC

Input

Return Values

  • Success: KP_CV_SUCCESS
  • Failure: an error code

Error Codes

Error Code

Description

INVALID_PARAM_MSG

The src parameter is null; the dsize value is incorrect; or the value of fx or fy is incorrect.

NOT_SUPPORT_MSG

The type of src is incorrect; the interpolation mode is not supported by interpolation; or the value of fx or fy is incorrect.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import numpy as np
import cv2

# Create a 5x5 image.
src = np.array([[0, 0, 0, 0, 0],
               [0, 1, 1, 1, 0],
               [0, 1, 0, 1, 0],
               [0, 1, 1, 1, 0],
               [0, 0, 0, 0, 0]], dtype=np.uint8)  

dst = cv2.resize(src, dsize=(10, 10), interpolation=cv2.INTER_LINEAR)  

print(dst)

Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[[0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 1 1 1 1 1 1 0 0]
 [0 0 1 1 1 1 1 1 0 0]
 [0 0 1 1 0 0 1 1 0 0]
 [0 0 1 1 0 0 1 1 0 0]
 [0 0 1 1 1 1 1 1 0 0]
 [0 0 1 1 1 1 1 1 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]]