【问题标题】:Rearrange image in Python在 Python 中重新排列图像
【发布时间】:2020-02-19 13:15:16
【问题描述】:

我正在尝试加载 tif 格式的图像文件并将其重新排列成一个长条,但保留水平顺序。当我尝试使用 numpy reshape 时,它​​只会将不同的行相互叠加或破坏图像。 假设我有一个这样的数组

imagearray = [1,2,3,4,5,6,7,8,9,10],
             [0,0,0,0,0,0,0,0,0,0],
             [1,1,1,1,1,1,1,1,1,1],
             [2,2,2,2,2,2,2,2,2,2] 

我想重新排列它,使它看起来像这样

[1,2],
[0,0],
[1,1],
[2,2],
[3,4],
[0,0],
[1,1],
[2,2]

等等。我的问题是保持该顺序,因为我重新排列它而不使用更多内存,因为这些是使用重塑或重新排列的 3GB 图像是我可以使用更少内存的方法。我认为这也更困难,因为图像在技术上具有我不完全理解如何操作的形状 (n,m,3)。 this is what the output looks like 来自 this image,它应该看起来更类似于 this

这是我正在使用的文件的基础知识

import cv2
import matplotlib.pyplot as plt
from PIL import Image, ImageTk
Image.MAX_IMAGE_PIXELS = None
width =  4000
height = 4000
rollwidth = 1000
I = cv2.imread("image path")
print("read image")
resizedImage = cv2.resize(I,(height,width), interpolation= cv2.INTER_LANCZOS4)
print("combined done")
testimage = np.reshape(resizedImage,((np.ceil(width/rollwidth).astype(np.int64)*height),rollwidth,3), order ='C')
cv2.imwrite("save path", testimage)
print("done")

我认为这与重复标志略有不同,因为使用建议的步骤我仍然无法获得所需的结果

【问题讨论】:

  • 从链接的 Q&A 接受的答案中,将 .ravel() 替换为 .reshape(-1,2)
  • 看起来它不适用于我的图像。如果您尝试使用图像,您可能会看到我的问题,但说我希望图像为 16000 高和 1000 宽,我是否必须像这样使用它? testimage = np.reshape(image,(16000,1000,3),rollwidth).reshape(-1,1000)
  • 你为什么跳过swapaxes?认为它会是 - a.reshape(a.shape[0],-1,ncols).swapaxes(0,1).reshape(-1,ncols) a 是输入数组。另外,您的输入不是二维数组吗?
  • 因为它是具有 (n,m,3) 形状的图像
  • 请为您的 3D 阵列案例和预期输出添加一个最小代表。

标签: python python-3.x image numpy matplotlib


【解决方案1】:

基于this post,我们将简单地将其扩展到 3D 案例,记住我们需要拆分第二个轴,同时保持第三个轴(颜色通道一)不变是-

# a is input 3D array
# ncols would be the width of the desired output image
m,n,r = a.shape
b = a.reshape(m,-1,ncols,r).swapaxes(0,1).reshape(-1,ncols,r)

背后的动机在this post中有详细讨论。

【讨论】:

  • @Dominic.New.Work 你能详细说明一下吗?
  • 我是个白痴。我想这可能是我所需要的,现在要用我的一张全尺寸图像对其进行测试,看看它是否有效,然后我会将其标记为答案
猜你喜欢
  • 2019-04-04
  • 2015-10-16
  • 2012-09-28
  • 2020-12-06
  • 2020-07-26
  • 2019-04-23
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
相关资源
最近更新 更多