且构网

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

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

更新时间:2022-12-25 19:09:34

是的,你可以安装 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 是一个包含 resized 图像的 numpy 数组.一个重要的方面是 interpolation 参数:有几种方法可以调整图像大小.特别是因为您缩小了图像,并且原始图像的大小不是调整后图像大小的倍数.可能的插值模式有:

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 - 最近邻插值
  • 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.