【发布时间】: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