Arrays 将字典中的元组分配给数组Python
我有一个包含因子和X、Y坐标的透视表数组,比如下面的一个,还有一个包含64种颜色的查找表,这些颜色都有RGB值。我已经使用元组字典为每个因子组合分配了颜色,但我现在很难弄清楚如何将我的听写键(这是不同的因子组合)与数组进行比较,以便可以为具有该因子组合的每一行分配字典中给定的颜色 这是透视表的一个示例:Arrays 将字典中的元组分配给数组Python,arrays,python-2.7,image-processing,tuples,Arrays,Python 2.7,Image Processing,Tuples,我有一个包含因子和X、Y坐标的透视表数组,比如下面的一个,还有一个包含64种颜色的查找表,这些颜色都有RGB值。我已经使用元组字典为每个因子组合分配了颜色,但我现在很难弄清楚如何将我的听写键(这是不同的因子组合)与数组进行比较,以便可以为具有该因子组合的每一行分配字典中给定的颜色 这是透视表的一个示例: A B C D Xpoint Ypoint 0 1 0 0 20 20 0 1 1 0 30 30 0 1 0 0 40 40 1
A B C D Xpoint Ypoint
0 1 0 0 20 20
0 1 1 0 30 30
0 1 0 0 40 40
1 0 1 0 50 50
1 0 1 0 60 60
编辑:这是LUT的一个示例:
R G B
0 0 0
1 0 103
0 21 68
95 173 58
这是一个制作词典的例子:
{
(0, 1, 0, 0): (1, 0, 103),
(0, 1, 1, 0): (12, 76, 161),
(1, 0, 1, 0): (0, 0, 0)
}
这是我使用的代码:
import numpy as np
from PIL import Image, ImageDraw
## load in LUT of 64 colours ##
LUT = np.loadtxt('LUT64.csv', skiprows=1, delimiter=',')
print LUT
## load in XY COordinates ##
PivotTable = np.loadtxt('PivotTable_2017-07-13_001.txt', skiprows=1, delimiter='\t')
print PivotTable
## Bring in image ##
IM = Image.open("mothTest.tif")
#bring in number of factors
numFactors = 4
#assign colour vectors to factor combos
iterColours = iter(LUT)
colour_dict = dict() # size will tell you how many colours will be used
for entry in PivotTable:
key = tuple(entry[0:numBiomarkers])
if key not in colour_dict:
colour_dict[key] = next(iterColours)
print(colour_dict)
是否有方法将此字典中的元组与透视表数组中的行进行比较,或者有更好的方法?任何帮助都将不胜感激 能否为LUT64.csv添加一个简短的示例,用于数据透视表_2017-07-13_001.txt?也许对于这个例子,您还应该使用不同于\t的分隔符来确保示例的可移植性
关于如果你的目标是,正如我在上面的评论中所假设的那样,将颜色追溯到n倍,那么你已经做了一切。但是我不知道tif文件扮演什么角色。。。请注意,我更正了对不存在的NumBiomarkers变量的引用
import numpy as np
from PIL import Image, ImageDraw
## load in LUT of 64 colours ##
LUT = np.loadtxt('LUT64.csv', skiprows=1, delimiter=',')
print LUT
## load in XY COordinates ##
PivotTable = np.loadtxt('PivotTable_2017-07-13_001.txt', skiprows=1, delimiter=',')
print PivotTable
## Bring in image ##
IM = Image.open("Lenna.tif")
#bring in number of factors
numFactors = 4
#assign colour vectors to factor combos
iterColours = iter(LUT)
colour_dict = dict() # size will tell you how many colours will be used
for entry in PivotTable:
key = tuple(entry[0:numFactors])
if key not in colour_dict:
colour_dict[key] = next(iterColours)
print(colour_dict)
print '===='
for entry in PivotTable:
key = tuple(entry[0:numFactors])
print str(entry) + ' ' + str(colour_dict[key])
谢谢你的建议。我的问题中添加了一个LUT示例作为编辑。如果我理解正确,请在“透视表”中指定一定数量的因子组合,然后按其出现顺序提取这些因子,并与查找表关联。这基本上将不同的颜色与不同的因素组合相关联。在我做了一些小的修正之后,它就起作用了,那么你的问题是什么?是否要将颜色追溯到透视表?总体而言,是的,我希望将颜色追溯到透视表,然后使用它们为图像上的点上色