我正在拍摄图像并将其变成阵列。我想将数组追加到一个空列表中,但是当我这样做时,我会在每个图像开始之前将array(arr)
添加到列表中。 arr
是实际的图像数组。我只想附加values
,不附加string
或()
代码:
DIR = 'predictions\\12288_cropped\\'
pred = "predictions"
files = os.listdir(DIR)
norm_images = []
def prepfiles():
for file in files:
def prepare(filepath):
img_size = 256
img_array = pydicom.read_file(filepath)
new_array = cv2.resize(img_array.pixel_array, (img_size, img_size))
arr = new_array.copy().astype(np.float)
M = np.float(np.max(new_array))
if M != 0:
arr *= 1./M
norm_images.append(arr)
return arr.reshape(-1, img_size, img_size, 1)
prepare(DIR+file)
prepfiles()
print('normalized images', norm_images)
打印:
normalized images [array([[-0.39198218, -0.37936154, -0.36228656, ..., -0.7423905 ,
-0.7423905 , -0.7423905 ],
[-0.38678545, -0.36154417, -0.32665182, ..., -0.7423905 ,
-0.7423905 , -0.7423905 ],
[-0.36377134, -0.33927246, -0.29101707, ..., -0.7423905 ,
-0.7423905 , -0.7423905 ],
...,
[ 0.03266518, 0.0311804 , 0.04231626, ..., 0.16332591,
0.16332591, 0.14105419],
[ 0.02152932, -0.00445434, 0.04008909, ..., 0.16778025,
0.16926503, 0.16703786],
[ 0.02004454, 0.00890869, 0.05642168, ..., 0.1484781 ,
0.16555308, 0.16184113]]), array([[-0.32047686, -0.31486676, -0.32328191, ..., -0.70126227,
-0.70126227, -0.70126227],
[-0.32398317, -0.30645161, -0.2973352 , ..., -0.70126227,
-0.70126227, -0.70126227],
[-0.30715288, -0.28681627, -0.27840112, ..., -0.70126227,
-0.70126227, -0.70126227],
...,
[ 0.02173913, 0.01192146, 0.00981767, ..., 0.09396914,
0.07713885, 0.07293128],
[ 0.00701262, -0.00350631, 0.03856942, ..., 0.11220196,
0.09467041, 0.10869565],
[ 0.00981767, 0.02173913, 0.07573633, ..., 0.11290323,
0.1227209 , 0.11991585]])]
期望的输出:
normalized images [[[-0.39198218, -0.37936154, -0.36228656, ..., -0.7423905 ,
-0.7423905 , -0.7423905 ],
[-0.38678545, -0.36154417, -0.32665182, ..., -0.7423905 ,
-0.7423905 , -0.7423905 ],
[-0.36377134, -0.33927246, -0.29101707, ..., -0.7423905 ,
-0.7423905 , -0.7423905 ],
...,
[ 0.03266518, 0.0311804 , 0.04231626, ..., 0.16332591,
0.16332591, 0.14105419],
[ 0.02152932, -0.00445434, 0.04008909, ..., 0.16778025,
0.16926503, 0.16703786],
[ 0.02004454, 0.00890869, 0.05642168, ..., 0.1484781 ,
0.16555308, 0.16184113]], [[-0.32047686, -0.31486676, -0.32328191, ..., -0.70126227,
-0.70126227, -0.70126227],
[-0.32398317, -0.30645161, -0.2973352 , ..., -0.70126227,
-0.70126227, -0.70126227],
[-0.30715288, -0.28681627, -0.27840112, ..., -0.70126227,
-0.70126227, -0.70126227],
...,
[ 0.02173913, 0.01192146, 0.00981767, ..., 0.09396914,
0.07713885, 0.07293128],
[ 0.00701262, -0.00350631, 0.03856942, ..., 0.11220196,
0.09467041, 0.10869565],
[ 0.00981767, 0.02173913, 0.07573633, ..., 0.11290323,
0.1227209 , 0.11991585]]]
没有“ string
”,它只是输出格式。如果要将其更改为列表,则只需在添加数组之前将np的tolist()
函数应用于数组即可:
norm_images.append(arr.tolist())
这应该会为您提供所需的输出。