【发布时间】: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