python - 如何在 numpy 数组中组合维度?

标签 python opencv numpy

我正在使用 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/

相关文章:

opencv - 使用 canny/hough 对非常小的线进行边缘检测

python - 计算向量中大于常数的连续值的总和?

python - 比较矩阵中相邻单元格的更好方法

python - OpenPyXl:将行标记为标题

Python 和 Jinja2 - 导入另一个环境的模板

python - 在存储的数据上重播 Scrapy 蜘蛛

python - 如何同时监控loss和val_loss以避免神经网络对训练集或测试集过度拟合?

c++ - 应该在 opencv 中进行白平衡的这段代码有什么问题

c++ - 使用 OpenCV 在 C++ 中缩放图像,但不使用 pyrDown() 或 pyrUp() 函数

python - 优化Python : Large arrays,内存问题