如何将图像附加到 numpy 数组中?

发布于 2025-01-12 15:23:17 字数 390 浏览 2 评论 0原文

我试图用这个形状(number_of_images,32,32)制作一个numpy,所有图像都具有相同的尺寸:32,32 这是我的代码:

data=np.array([])
for filename in glob.glob(path+'*.tif'): 
    im = np.array([np.array(cv2.imread(filename, cv2.IMREAD_GRAYSCALE))])
    #im = preprocess(im)
    im = NormalizeData(im)
    data=np.append(data,im)

我的形状是(1,32,32)。然而数据形状不是我想要的(number_of_images,32,32)。我目前拥有的是(113664,)

I'm trying to make a numpy with this shape(number_of_images,32,32) all the images have the same dimensions: 32,32
here is my code:

data=np.array([])
for filename in glob.glob(path+'*.tif'): 
    im = np.array([np.array(cv2.imread(filename, cv2.IMREAD_GRAYSCALE))])
    #im = preprocess(im)
    im = NormalizeData(im)
    data=np.append(data,im)

im shape is (1,32,32). however data shape is not the one I wanted (number_of_images,32,32). the one I currently have is (113664,)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So尛奶瓶 2025-01-19 15:23:17

尝试使用列表并在最后转换为 numpy - 减少混乱

def main():
    cv_img_fake = np.zeros(shape=(1, 32, 32), dtype=np.uint8)
    print('image shape {}'.format(cv_img_fake.shape))

    images = []
    for filename_i in range(10):  # imagine 10 images
        print('filename {}:'.format(filename_i))
        im = cv_img_fake.copy()  # shape 1,32,32
        print('\timage shape {}'.format(im.shape))
        im = im.reshape(-1, im.shape[-1])  # shape 32,32
        print('\timage shape {}'.format(im.shape))

        # do to im whatever you want except changing the dims
        # check after every function by printing dims didn't change - e.g. still 32,32
        # im = NormalizeData(im)
        # print('\timage shape {}'.format(im.shape))
        # im = preprocess(im)
        # print('\timage shape {}'.format(im.shape))
        images.append(im)
    images = np.uint8(images)  # 10 images of 32, 32
    print('images shape {}'.format(images.shape))  # 10,32,32
    return

输出:

image shape (1, 32, 32)
filename 0:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 1:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 2:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 3:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 4:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 5:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 6:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 7:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 8:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 9:
    image shape (1, 32, 32)
    image shape (32, 32)
images shape (10, 32, 32)

try using lists and at the end cast to numpy - less confusing

def main():
    cv_img_fake = np.zeros(shape=(1, 32, 32), dtype=np.uint8)
    print('image shape {}'.format(cv_img_fake.shape))

    images = []
    for filename_i in range(10):  # imagine 10 images
        print('filename {}:'.format(filename_i))
        im = cv_img_fake.copy()  # shape 1,32,32
        print('\timage shape {}'.format(im.shape))
        im = im.reshape(-1, im.shape[-1])  # shape 32,32
        print('\timage shape {}'.format(im.shape))

        # do to im whatever you want except changing the dims
        # check after every function by printing dims didn't change - e.g. still 32,32
        # im = NormalizeData(im)
        # print('\timage shape {}'.format(im.shape))
        # im = preprocess(im)
        # print('\timage shape {}'.format(im.shape))
        images.append(im)
    images = np.uint8(images)  # 10 images of 32, 32
    print('images shape {}'.format(images.shape))  # 10,32,32
    return

Output:

image shape (1, 32, 32)
filename 0:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 1:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 2:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 3:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 4:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 5:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 6:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 7:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 8:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 9:
    image shape (1, 32, 32)
    image shape (32, 32)
images shape (10, 32, 32)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文