Opencv Python resize with one constant

Opencv Python resize with one constant

我想使用 cv2.resize() 方法根据给定一条边的新长度调整图像大小。

例如,我的图像尺寸为 300x400,我想将高度从 400 增加到 500,但我应该只写一条边的新尺寸(仅 500),另一条边应该自动增加。什么是顺序?

您可以使用 resize 函数的 fxfy 参数调整大小。

img = cv2.resize(img, (0,0), fx=500/400, fy=500/400)

我知道您只要求 cv2.resize,但您可以使用 imutils 库,它会为您执行一些比率代码。

import imutils
resized = imutils.resize(img, width=newwidth)

imutils 内部:

dim = None
(h, w) = image.shape[:2]

# check to see if the width is None
if width is None:
    # calculate the ratio of the height and construct the
    # dimensions
    r = height / float(h)
    dim = (int(w * r), height)

# otherwise, the height is None
else:
    # calculate the ratio of the width and construct the
    # dimensions
    r = width / float(w)
    dim = (width, int(h * r))

# resize the image
resized = cv2.resize(image, dim, interpolation=inter)