如何在numpy数组中合并维? [英] How to combine dimensions in numpy array?

查看:446
本文介绍了如何在numpy数组中合并维?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenCV将图像读取到numpy.array中,并且它们具有以下形状.

I'm using OpenCV to read images into numpy.array, and they have the following shape.

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张图片.

Each of the image has 718x686 pixels/dimension. There are 100 images.

我不想在718x686上工作,我想将像素合并为一个尺寸.也就是说,形状应如下所示:(100,492548,3).无论如何,OpenCV(或任何其他库)或Numpy中是否都允许我这样做?

I don't want to work on 718x686, I'd like to combine the pixels into a single dimension. That is, the shape should look like: (100,492548,3). Is there anyway either in OpenCV (or any other library) or Numpy that allows me to do that?

推荐答案

无需修改您的阅读功能:

Without modifying your reading function:

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)

这篇关于如何在numpy数组中合并维?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆