【发布时间】:2021-10-14 22:15:30
【问题描述】:
回溯错误:
Traceback (most recent call last):
File "C:\trial2\trial.py", line 56, in <module>
image_stack(image)
File "C:\trial2\trial.py", line 44, in image_stack
reshaped = transposed_axes.reshape(new_arr_shape)
TypeError: 'generator' object cannot be interpreted as an integer
我在代码中的问题是我无法用新的数组形状重塑转置的数组。该代码采用 cv2 读取的图像路径,然后将其转换为数组。使用数组的长度计算转置轴的值。然后转置轴值用于转置数组。我将numpy.prod() 用于if 语句生成的轴上的数组值的乘积。我想用new_arr_shape 的值重塑转置数组,但我一直收到错误消息,说'generator' object cannot be interpreted as an integer。
import numpy as np
def image_stack(image):
imgs = np.array(cv2.imread(image).shape)
print(type(imgs))
n = len(imgs)
img = np.ones(imgs)
val_1 = list(range(1, n - 1, 2))
val_2 = list(range(0, n - 1, 2))
print(img)
if n % 2 == 0:
y_ax = val_1
x_ax = val_2
axes = (y_ax, x_ax, [n-1])
else:
y_ax = val_2
x_ax = val_1
axes = (y_ax, x_ax, [n - 1])
print(type(axes))
'''The axes need to be in form of a tuple in order to be
transposed'''
if type(axes) == tuple:
transposed_axes = np.transpose(img, axes=np.concatenate(axes))
print(transposed_axes)
new_arr_shape = [np.prod(img[x] for x in axes)]
print(type(new_arr_shape))
print(new_arr_shape)
reshaped = transposed_axes.reshape(new_arr_shape)
#print(type(reshaped))
#print(reshaped)
image = 'C:\\trial_images\\9.jpg'
image_stack(image)
【问题讨论】:
标签: arrays numpy reshape transpose cv2