【问题标题】:Resize 3D stack of images in Python在 Python 中调整 3D 图像堆栈的大小
【发布时间】:2017-02-25 02:16:31
【问题描述】:

我有一个 3D numpy 数组,它是 100 个 300x300 图像的堆栈。我想将堆栈中的所有图像调整为 200x200。我尝试使用 numpy 调整大小功能:

import numpy as np

img_stack_sm = np.resize(img_stack, (100, 200, 200))

...但是这样做会打乱图像(如绘图所示)。如何一次性完成?谢谢。

【问题讨论】:

  • numpy resize 只是截断值。阅读它的文档。您需要调整图像大小以插入新值或重新采样。
  • 如何调整一张图片的大小?

标签: python image numpy


【解决方案1】:

最后我只是使用了一个for循环和cv2:

import cv2

width = 200
height = 200
img_stack_sm = np.zeros((len(img_stack), width, height))

for idx in range(len(img_stack)):
    img = img_stack[idx, :, :]
    img_sm = cv2.resize(img, (width, height), interpolation=cv2.INTER_CUBIC)
    img_stack_sm[idx, :, :] = img_sm

【讨论】:

  • 你应该使用np.zeros((len(img_stack), height, width)),因为 Numpy 按行工作,而 OpenCV 需要经典的宽度 x 高度。
猜你喜欢
  • 1970-01-01
  • 2012-09-13
  • 1970-01-01
  • 2021-10-11
  • 1970-01-01
  • 2016-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多