【问题标题】:Assigning a tuple from a dictionary to an array Python将字典中的元组分配给数组 Python
【发布时间】:2018-01-10 06:09:35
【问题描述】:

我有一个包含因子和 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  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)

有没有办法将此字典中的元组与数据透视表数组中的行进行比较,或者是否有更好的方法?任何帮助将不胜感激!

【问题讨论】:

    标签: arrays python-2.7 image-processing tuples


    【解决方案1】:

    如果您的目标是,正如我在上面的评论中所想的那样,将颜色追溯到 ntuple,那么您已经完成了所有工作。但我不知道 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])
    

    【讨论】:

      【解决方案2】:

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

      问候

      【讨论】:

      • 感谢您的建议。 LUT 的一个示例已作为编辑添加到我的问题中。
      • 如果我理解正确,您在“数据透视表”中指定了一定数量的因素组合,然后按出现顺序提取这些组合并与查找表相关联。这基本上将不同的颜色与每种不同的因素组合相关联。它有效,在我做了一些小的更正之后,你的问题是什么?您想将颜色追溯到您的数据透视表吗?
      • 总的来说,是的,我想将颜色追溯到数据透视表,然后用它们为图像上的点着色
      猜你喜欢
      • 2017-08-20
      • 2016-10-09
      • 2019-08-28
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 2014-03-31
      • 2019-12-20
      相关资源
      最近更新 更多