2017-08-02 48 views
0

我有一个数据透视表数组,其中包含因子和X和Y坐标(如下面的坐标),并且我有一个带有64个颜色的查找表,它们具有RGB值。我已经使用元组字典为每个因子组合分配了一种颜色,但我很难弄清楚如何将我的dictonary(这些因子的不同组合)的键与我的数组进行比较,以便每行该因素组合可以赋予词典中给出的颜色。从字典中指定元组到数组Python

这是透视表的一个示例:

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) 

有没有办法在这个字典中的元组比较数据透视表阵列中的行,或者也许有这样做的更好的办法?任何帮助将不胜感激!

回答

1

如果你的目标是,正如我在评论上述假设,追溯颜色的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]) 
0

请问您可以为LUT64.csv添加一个简短的示例,用于PivotTable_2017-07-13_001.txt?也许对于这个,你应该使用不同于\ t的分隔符来确保你的例子的可移植性。

问候

+0

谢谢您的建议。作为编辑,我的问题已添加了LUT的一个示例。 – mdicrist

+0

如果我理解正确,您可以在“数据透视表”中指定一定数量的因素组合,然后按照其出现顺序提取并与查找表关联。这基本上将不同的颜色与不同的因素组合联系起来。它起作用,在我做了一些小的更正之后,那么你的问题是什么?你想追溯到你的数据透视表的颜色? – Lorenzo

+0

总的来说,是的,我想追溯到数据透视表的颜色,然后使用它们来对图像上的点进行着色 – mdicrist