gpt4 book ai didi

python - 将 200 个大小为 (100*100) 的 2-d numpy 数组堆叠在 3-d numpy 数组 (200 * 100 * 100) 中

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:26 26 4
gpt4 key购买 nike

我想创建 200 个大小为 (100*100) 的图像帧,并将它们堆叠在最终大小为 (200 * 100 *100) 的 3-D 数组中,其中 x 轴代表每个帧,即 (1, 100, 100 )应该是第一帧。

我无法将它们堆叠在 3-D 数组中。第一个循环确实通过堆叠前两个帧创建了一个 (2,100,100) 数组,但此后不起作用并生成了一个 (2,) 数组

import numpy as np
import random




def createCircle(width,height , rad ):
w = random.randint(1, height)
h = random.randint(1, height)
center = [int(w), int(h)]
radius = rad
Y, X = np.ogrid[:height, :width]
dist_from_center = np.sqrt((X - center[0])**2 + (Y-center[1])**2)
mask = dist_from_center <= radius
return mask


def addCircle(test_image):
m = createCircle(width = 100, height = 100 , rad = 8 )
masked_img = test_image.copy()
masked_img[m] = 0
return masked_img

img = np.zeros([100,100],dtype=np.uint8)
img.fill(20)
img_test = img


def noise(image):
row,col= image.shape
mean = 0
var = 0.1
sigma = var**0.5
gauss = np.random.normal(mean,sigma,(row,col))
gauss = gauss.reshape(row,col)
noisy = image + gauss #adding gauss noise
s1 = np.sin(8) #adding sin fill
noisy += s1
return noisy


#creates 1st frame
for i in range(4):
im_first = addCircle(test_image=img_test)
im_first = noise(im_first)



for i in range(200):
for j in range(4):
img_test = addCircle(test_image=img_test)
im1 = noise(img_test)
img_test = img
im_first = np.array([im_first, im1])#stacks every new frame (im1)#error in this

我需要一个(200,100,100)

最佳答案

您可以初始化一个数组,然后用图像填充它。这往往比连续堆叠更有效。

ims = np.zeros((200, 100, 100)) # initialize your array
for i in range(200):
for j in range(4):
img_test = addCircle(test_image=img_test)
im1 = noise(img_test)
ims[i, ...] = im1 # add the image to our initialized array

关于python - 将 200 个大小为 (100*100) 的 2-d numpy 数组堆叠在 3-d numpy 数组 (200 * 100 * 100) 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54042094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com