且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Numpy调整大小/重新缩放图像

更新时间:2022-12-25 19:13:12

是的,您可以安装 opencv (这是一个用于图像处理和计算机视觉的库),并使用 cv2.resize 功能。例如使用:

Yeah, you can install opencv (this is a library used for image processing, and computer vision), and use the cv2.resize function. And for instance use:

import cv2
import numpy as np

img = cv2.imread('your_image.jpg')
res = cv2.resize(img, dsize=(54, 140), interpolation=cv2.INTER_CUBIC)

这里 img 因此是包含原始图像的numpy数组,而 res 是一个包含已调整大小的图像的numpy数组。一个重要的方面是插值参数:有几种方法可以调整图像大小。特别是在缩小图像时,原始图像的大小是调整大小后图像大小的倍数。可能的插值模式是:

Here img is thus a numpy array containing the original image, whereas res is a numpy array containing the resized image. An important aspect is the interpolation parameter: there are several ways how to resize an image. Especially since you scale down the image, and the size of the original image is not a multiple of the size of the resized image. Possible interpolation schemas are:



  • INTER_NEAREST - a最近邻插值

  • INTER_LINEAR - 双线性插值(默认使用)

  • INTER_AREA - 使用像素区域关系重新采样。它可能是图像抽取的首选方法,因为它提供了莫尔条纹的
    结果。但是当图像被缩放时,它类似于
    INTER_NEAREST 方法。

  • INTER_CUBIC - 4x4像素邻域的双三次插值

  • INTER_LANCZOS4 - 8x8像素邻域的Lanczos插值

  • INTER_NEAREST - a nearest-neighbor interpolation
  • INTER_LINEAR - a bilinear interpolation (used by default)
  • INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.
  • INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood
  • INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood

与大多数选项一样,对于每个调整大小架构,没有***选项,有些情况下,一种策略可以优先于另一种策略。

Like with most options, there is no "best" option in the sense that for every resize schema, there are scenarios where one strategy can be preferred over another.