使用 OpenCV Python 调整图像大小的最佳方法

Jam*_*mes 5 python opencv image image-resizing python-imaging-library

我想根据百分比调整图像大小,并使其尽可能接近原始图像,同时将噪音和失真降至最低。调整大小可以向上或向下,就像我可以缩放到原始图像大小的 5% 或 500%(或作为示例给出的任何其他值)

这就是我尝试过的,我需要对调整大小的图像进行绝对最小的更改,因为我将使用它与其他图像进行比较

def resizing(main,percentage):
    main = cv2.imread(main)
    height = main.shape[ 0] * percentage
    width = crop.shape[ 1] * percentage
    dim = (width,height)
    final_im = cv2.resize(main, dim, interpolation = cv2.INTER_AREA)
    cv2.imwrite("C:\\Users\\me\\nature.jpg", final_im)
Run Code Online (Sandbox Code Playgroud)

小智 3

您可以使用以下语法cv2.resize

  cv2.resize(image,None,fx=int or float,fy=int or float)
Run Code Online (Sandbox Code Playgroud)

fx取决于宽度

fy取决于身高

您可以输入第二个参数None(0,0)

例子:

  img = cv2.resize(oriimg,None,fx=0.5,fy=0.5)
Run Code Online (Sandbox Code Playgroud)

笔记:

0.5 表示图像缩放 50%