我正在使用 OpenCV
将图像读入 numpy.array
,它们具有以下形状。
import cv2
def readImages(path):
imgs = []
for file in os.listdir(path):
if file.endswith('.png'):
img = cv2.imread(file)
imgs.append(img)
imgs = numpy.array(imgs)
return (imgs)
imgs = readImages(...)
print imgs.shape # (100, 718, 686, 3)
每张图片的尺寸为 718x686 像素。有 100 张图像。
我不想在 718x686 上工作,我想将像素组合成一个维度。也就是说,形状应如下所示:(100,492548,3)
。无论如何,在 OpenCV(或任何其他库)或 Numpy 中是否有允许我这样做的?
最佳答案
不修改你的阅读功能:
imgs = readImages(...)
print imgs.shape # (100, 718, 686, 3)
# flatten axes -2 and -3, using -1 to autocalculate the size
pixel_lists = imgs.reshape(imgs.shape[:-3] + (-1, 3))
print pixel_lists.shape # (100, 492548, 3)
关于python - 如何在 numpy 数组中组合维度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36370956/