如何在列表中的列表的情况下忽略外部列表(不使用 Numpy)
How to ignore external list in case of lists in list(without using Numpy)
我写了一个函数,它的输入是- image_one, image_two, mask)。
该函数通过根据公式-
计算每个新像素来输出新图像
new_image[i][j] = round(image_one[i][j] x mask[i][j] + image_two[i][j] x (1 - mask[i][j]))
写到现在-
def image_plus_mask(image_one, image_two, mask):
hight = len(image_one)
width = len(image_one[0])
new_image = [[0 for _ in range(width)] for _ in range(hight)]
for i in range(hight):
for j in range(width):
new_image[i][j] = round((image_one[i][j] * mask[i][j]) + (image_two[i][j] * (1-mask[i][j])))
return new_image
函数 dose 适用于双重列表。
例如:
输入-
image_plus_mask([[50, 50, 50]], [[200, 200, 200]], [[0, 0.5, 1]])
输出(如预期)-
[[200, 125, 50]]
问题是它不能处理 3 个列表,我需要它同时处理 - 2 和 3 个列表。(2 我的意思是 -[[] ], 3 我的意思是- [[[]]])
例如-
输入-
image_plus_mask([[[1,2,3], [4,5,6]],[[7,8,9],[10,11,12]]],
[[[250,250,250], [0,0,0]],[[250,250,100],[1,11,13]]],[[0,0.5]]*2))
预期输出-
[[[250, 250, 250,], [2,2,3]], [[250, 250, 100], [6, 11, 12]]]
我能做什么?
您的蒙版必须与输入的形状相同。
如果不同,则 image_one[i][j] * mask[i][j]
将不起作用。
我强烈建议您像这样使用 numpy 数组:
image1 = np.array([[50, 50, 50]])
image2 = np.array([[200, 200, 200]])
mask = np.array([[0, 0.5, 1]])
output = np.round(image1 * mask + image2 * (1 - mask))
print(output)
输出:
[[200. 125. 50.]]
如果您输入的形状(图像和蒙版)一致,这将提供您想要的任何尺寸。
如评论所说:
def image_plus_mask(image_one, image_two, mask):
hight = len(image_one)
width = len(image_one[0])
#case: 3 lists
if isinstance(image_one[0][0], (list,tuple)):
depth = len(image_one[0][0])
new_image = [[[0 for _ in range(depth)] for _ in range(width)] for _ in range(hight)]
print(new_image)
for i in range(hight):
for j in range(width):
for k in range(depth):
new_image[i][j][k] = round((image_one[i][j][k] * mask[i][j]) + (image_two[i][j][k] * (1-mask[i][j])))
#case: 2 lists
else:
new_image = [[0 for _ in range(width)] for _ in range(hight)]
for i in range(hight):
for j in range(width):
new_image[i][j] = round((image_one[i][j] * mask[i][j]) + (image_two[i][j] * (1-mask[i][j])))
return new_image
您需要调整 new_image
的形状。查看 3 个列表的预期输出并查看函数内部为 new_image
定义的内容。无法工作。
现在输出:
res1 = image_plus_mask([[[1,2,3], [4,5,6]],[[7,8,9],[10,11,12]]],
[[[250,250,250], [0,0,0]],[[250,250,100],[1,11,13]]],[[0,0.5]]*2)
res2 = image_plus_mask([[50, 50, 50]], [[200, 200, 200]], [[0, 0.5, 1]])
print(res1)
[[[250, 250, 250], [2, 2, 3]], [[250, 250, 100], [6, 11, 12]]]
print(res2)
[[200, 125, 50]]
我写了一个函数,它的输入是- image_one, image_two, mask)。 该函数通过根据公式-
计算每个新像素来输出新图像new_image[i][j] = round(image_one[i][j] x mask[i][j] + image_two[i][j] x (1 - mask[i][j]))
写到现在-
def image_plus_mask(image_one, image_two, mask):
hight = len(image_one)
width = len(image_one[0])
new_image = [[0 for _ in range(width)] for _ in range(hight)]
for i in range(hight):
for j in range(width):
new_image[i][j] = round((image_one[i][j] * mask[i][j]) + (image_two[i][j] * (1-mask[i][j])))
return new_image
函数 dose 适用于双重列表。 例如:
输入-
image_plus_mask([[50, 50, 50]], [[200, 200, 200]], [[0, 0.5, 1]])
输出(如预期)-
[[200, 125, 50]]
问题是它不能处理 3 个列表,我需要它同时处理 - 2 和 3 个列表。(2 我的意思是 -[[] ], 3 我的意思是- [[[]]])
例如-
输入-
image_plus_mask([[[1,2,3], [4,5,6]],[[7,8,9],[10,11,12]]],
[[[250,250,250], [0,0,0]],[[250,250,100],[1,11,13]]],[[0,0.5]]*2))
预期输出-
[[[250, 250, 250,], [2,2,3]], [[250, 250, 100], [6, 11, 12]]]
我能做什么?
您的蒙版必须与输入的形状相同。
如果不同,则 image_one[i][j] * mask[i][j]
将不起作用。
我强烈建议您像这样使用 numpy 数组:
image1 = np.array([[50, 50, 50]])
image2 = np.array([[200, 200, 200]])
mask = np.array([[0, 0.5, 1]])
output = np.round(image1 * mask + image2 * (1 - mask))
print(output)
输出:
[[200. 125. 50.]]
如果您输入的形状(图像和蒙版)一致,这将提供您想要的任何尺寸。
如评论所说:
def image_plus_mask(image_one, image_two, mask):
hight = len(image_one)
width = len(image_one[0])
#case: 3 lists
if isinstance(image_one[0][0], (list,tuple)):
depth = len(image_one[0][0])
new_image = [[[0 for _ in range(depth)] for _ in range(width)] for _ in range(hight)]
print(new_image)
for i in range(hight):
for j in range(width):
for k in range(depth):
new_image[i][j][k] = round((image_one[i][j][k] * mask[i][j]) + (image_two[i][j][k] * (1-mask[i][j])))
#case: 2 lists
else:
new_image = [[0 for _ in range(width)] for _ in range(hight)]
for i in range(hight):
for j in range(width):
new_image[i][j] = round((image_one[i][j] * mask[i][j]) + (image_two[i][j] * (1-mask[i][j])))
return new_image
您需要调整 new_image
的形状。查看 3 个列表的预期输出并查看函数内部为 new_image
定义的内容。无法工作。
现在输出:
res1 = image_plus_mask([[[1,2,3], [4,5,6]],[[7,8,9],[10,11,12]]],
[[[250,250,250], [0,0,0]],[[250,250,100],[1,11,13]]],[[0,0.5]]*2)
res2 = image_plus_mask([[50, 50, 50]], [[200, 200, 200]], [[0, 0.5, 1]])
print(res1)
[[[250, 250, 250], [2, 2, 3]], [[250, 250, 100], [6, 11, 12]]]
print(res2)
[[200, 125, 50]]