Opencv Python 用一个常数调整大小

use*_*r70 3 python opencv resize dimension

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

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

Ben*_*iko 6

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

import imutils
resized = imutils.resize(img, width=newwidth)
Run Code Online (Sandbox Code Playgroud)

内部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)
Run Code Online (Sandbox Code Playgroud)