我已经构建了两个包含图像直方图值的字典。每个字典都将图像文件的文件名作为键,并将三个一维向量的列表放在一起作为其值。
例子: {'someFileName.jpg' : ['forecolor=2,3,5,5,6','edge=2,4,5','texture=5,4,3']}
这是我的一本词典的实际表示:
词典1
{'/Users/images/Transcend-8GB-Class-10-SDHC-Flash-Memory-Card.jpg': ['fcolor=2,4,14,5,0,0,0,0,0,0,0,0,0,0,12,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,6,0,0,0,0,1,0,0,0,0,0,0,0,0,0,20,9,0,0,0,2,2,0,0,0,0,0,0,0,0,0,13,6,0,0,0,1,0,0,0,0,0,0,0,0,0,0,10,8,0,0,0,1,2,0,0,0,0,0,0,0,0,0,17,17,0,0,0,3,6,0,0,0,0,0,0,0,0,0,7,5,0,0,0,2,0,0,0,0,0,0,0,0,0,0,4,3,0,0,0,1,1,0,0,0,0,0,0,0,0,0,6,6,0,0,0,2,3', 'edge=1,252,1,32,124,194,63,252,67,15,240,1,7,244,66,47,0,192,63', 'texture=1,78,27,37,13,6,6,7,78']}
字典2
{'/Users/images/kodax-camera-M531.jpg': ['fcolor=2,74,6,20,30,1,2,0,1,0,0,0,1,3,2,0,0,0,0,0,1,1,1,0,0,2,0,0,0,2,2,0,0,0,0,0,2,2,1,0,0,5,0,0,0,1,4,0,0,0,0,0,2,2,1,0,0,1,0,0,0,3,1,0,0,0,0,0,1,1,0,0,0,3,0,0,0,1,2,0,0,0,0,0,2,2,1,0,0,4,0,0,0,0,5,0,0,0,0,0,2,1,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0', 'edge=1,4,1,88,128,22,8,39,25,142,230,226,31,60,64,255,252,12,76', 'texture=1,15,32,31,28,19,16,12,98']}
我的最终目标是将其中两个字典传递给一个方法并实际运行 cosign 值,
示例:所以每个字典都有列表作为它的值,所以对于每个字典键,我想在字典 1 的键 1、velu1 与字典 2 键 1、值 1 之间进行向量乘法,
我有向量乘法函数,所以我想弄清楚如何正确迭代,我一直在考虑使用 yield 函数,但是当我尝试时它并没有真正起作用。这是我到目前为止所拥有的:
def cosignSimilarity(image1VectorDict, image2VectorDict):
for image1Key, image2Value in image1VectorDict.iteritems():
print image1Key
for aValue in image1Value:
print aValue
for image2Key, image2Value in image2VectorDict.iteritems():
for eValue in image2Value:
print aValue
print "\n"
print eValue
仅供参考:我不是在寻求关于 cosign 计算的帮助。
这就是我当前的代码如何吐出数据的方式,如果我可以将一个键对键从一个字典隔离到另一个字典,那么我可以做剩下的事情,例如计算余弦值。
First Dictionary
{'/Users/test/Transcend-8GB-Class-10-SDHC-Flash-Memory-Card.jpg': ['fcolor=2,4,14,5,0,0,0,0,0,0,0,0,0,0,12,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,6,0,0,0,0,1,0,0,0,0,0,0,0,0,0,20,9,0,0,0,2,2,0,0,0,0,0,0,0,0,0,13,6,0,0,0,1,0,0,0,0,0,0,0,0,0,0,10,8,0,0,0,1,2,0,0,0,0,0,0,0,0,0,17,17,0,0,0,3,6,0,0,0,0,0,0,0,0,0,7,5,0,0,0,2,0,0,0,0,0,0,0,0,0,0,4,3,0,0,0,1,1,0,0,0,0,0,0,0,0,0,6,6,0,0,0,2,3', 'edge=1,252,1,32,124,194,63,252,67,15,240,1,7,244,66,47,0,192,63', 'texture=1,78,27,37,13,6,6,7,78']}
------------------
Second Dictionary
{'/Users/test/kodax-camera-M531.jpg': ['fcolor=2,74,6,20,30,1,2,0,1,0,0,0,1,3,2,0,0,0,0,0,1,1,1,0,0,2,0,0,0,2,2,0,0,0,0,0,2,2,1,0,0,5,0,0,0,1,4,0,0,0,0,0,2,2,1,0,0,1,0,0,0,3,1,0,0,0,0,0,1,1,0,0,0,3,0,0,0,1,2,0,0,0,0,0,2,2,1,0,0,4,0,0,0,0,5,0,0,0,0,0,2,1,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0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0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0
fcolor=2,4,14,5,0,0,0,0,0,0,0,0,0,0,12,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,6,0,0,0,0,1,0,0,0,0,0,0,0,0,0,20,9,0,0,0,2,2,0,0,0,0,0,0,0,0,0,13,6,0,0,0,1,0,0,0,0,0,0,0,0,0,0,10,8,0,0,0,1,2,0,0,0,0,0,0,0,0,0,17,17,0,0,0,3,6,0,0,0,0,0,0,0,0,0,7,5,0,0,0,2,0,0,0,0,0,0,0,0,0,0,4,3,0,0,0,1,1,0,0,0,0,0,0,0,0,0,6,6,0,0,0,2,3
edge=1,4,1,88,128,22,8,39,25,142,230,226,31,60,64,255,252,12,76
fcolor=2,4,14,5,0,0,0,0,0,0,0,0,0,0,12,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,6,0,0,0,0,1,0,0,0,0,0,0,0,0,0,20,9,0,0,0,2,2,0,0,0,0,0,0,0,0,0,13,6,0,0,0,1,0,0,0,0,0,0,0,0,0,0,10,8,0,0,0,1,2,0,0,0,0,0,0,0,0,0,17,17,0,0,0,3,6,0,0,0,0,0,0,0,0,0,7,5,0,0,0,2,0,0,0,0,0,0,0,0,0,0,4,3,0,0,0,1,1,0,0,0,0,0,0,0,0,0,6,6,0,0,0,2,3
texture=1,15,32,31,28,19,16,12,98
edge=1,252,1,32,124,194,63,252,67,15,240,1,7,244,66,47,0,192,63
edge=1,252,1,32,124,194,63,252,67,15,240,1,7,244,66,47,0,192,63
fcolor=2,74,6,20,30,1,2,0,1,0,0,0,1,3,2,0,0,0,0,0,1,1,1,0,0,2,0,0,0,2,2,0,0,0,0,0,2,2,1,0,0,5,0,0,0,1,4,0,0,0,0,0,2,2,1,0,0,1,0,0,0,3,1,0,0,0,0,0,1,1,0,0,0,3,0,0,0,1,2,0,0,0,0,0,2,2,1,0,0,4,0,0,0,0,5,0,0,0,0,0,2,1,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0
edge=1,252,1,32,124,194,63,252,67,15,240,1,7,244,66,47,0,192,63
edge=1,4,1,88,128,22,8,39,25,142,230,226,31,60,64,255,252,12,76
edge=1,252,1,32,124,194,63,252,67,15,240,1,7,244,66,47,0,192,63
texture=1,15,32,31,28,19,16,12,98
texture=1,78,27,37,13,6,6,7,78
texture=1,78,27,37,13,6,6,7,78
fcolor=2,74,6,20,30,1,2,0,1,0,0,0,1,3,2,0,0,0,0,0,1,1,1,0,0,2,0,0,0,2,2,0,0,0,0,0,2,2,1,0,0,5,0,0,0,1,4,0,0,0,0,0,2,2,1,0,0,1,0,0,0,3,1,0,0,0,0,0,1,1,0,0,0,3,0,0,0,1,2,0,0,0,0,0,2,2,1,0,0,4,0,0,0,0,5,0,0,0,0,0,2,1,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0
texture=1,78,27,37,13,6,6,7,78
edge=1,4,1,88,128,22,8,39,25,142,230,226,31,60,64,255,252,12,76
texture=1,78,27,37,13,6,6,7,78
texture=1,15,32,31,28,19,16,12,98
显然,正如您所看到的,我正在吐出许多重复相同值的方法
这些是我正在处理的实际字典:
词典 1:
{'/Users/test/Transcend-8GB-Class-10-SDHC-Flash-Memory-Card.jpg': ['fcolor=2,4,14,5,0,0,0,0,0,0,0,0,0,0,12,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,6,0,0,0,0,1,0,0,0,0,0,0,0,0,0,20,9,0,0,0,2,2,0,0,0,0,0,0,0,0,0,13,6,0,0,0,1,0,0,0,0,0,0,0,0,0,0,10,8,0,0,0,1,2,0,0,0,0,0,0,0,0,0,17,17,0,0,0,3,6,0,0,0,0,0,0,0,0,0,7,5,0,0,0,2,0,0,0,0,0,0,0,0,0,0,4,3,0,0,0,1,1,0,0,0,0,0,0,0,0,0,6,6,0,0,0,2,3', 'edge=1,252,1,32,124,194,63,252,67,15,240,1,7,244,66,47,0,192,63', 'texture=1,78,27,37,13,6,6,7,78']}
字典 2:
{'/Users/test/kodax-camera-M531.jpg': ['fcolor=2,74,6,20,30,1,2,0,1,0,0,0,1,3,2,0,0,0,0,0,1,1,1,0,0,2,0,0,0,2,2,0,0,0,0,0,2,2,1,0,0,5,0,0,0,1,4,0,0,0,0,0,2,2,1,0,0,1,0,0,0,3,1,0,0,0,0,0,1,1,0,0,0,3,0,0,0,1,2,0,0,0,0,0,2,2,1,0,0,4,0,0,0,0,5,0,0,0,0,0,2,1,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0', 'edge=1,4,1,88,128,22,8,39,25,142,230,226,31,60,64,255,252,12,76', 'texture=1,15,32,31,28,19,16,12,98']}
我有lamba函数
cosinLamba = lambda a, b : round(NP.inner(a, b)/(LA.norm(a)*LA.norm(b)), 3)
我想遍历字典 1 和字典 2 并获取字典 1 的 fcolor 值 'fcolor=2,4,14,5,0,0,0,0,0,0,0,0,0,0,12,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,6,0,0,0,0,1,0,0,0,0,0,0,0,0,0,20,9,0,0,0,2,2,0,0,0,0,0,0,0,0,0,13,6,0,0,0,1,0,0,0,0,0,0,0,0,0,0,10,8,0,0,0,1,2,0,0,0,0,0,0,0,0,0,17,17,0,0,0,3,6,0,0,0,0,0,0,0,0,0,7,5,0,0,0,2,0,0,0,0,0,0,0,0,0,0,4,3,0,0,0,1,1,0,0,0,0,0,0,0,0,0,6,6,0,0,0,2,3'
和dictionary2的fcolor值
'fcolor=2,74,6,20,30,1,2,0,1,0,0,0,1,3,2,0,0,0,0,0,1,1,1,0,0,2,0,0,0,2,2,0,0,0,0,0,2,2,1,0,0,5,0,0,0,1,4,0,0,0,0,0,2,2,1,0,0,1,0,0,0,3,1,0,0,0,0,0,1,1,0,0,0,3,0,0,0,1,2,0,0,0,0,0,2,2,1,0,0,4,0,0,0,0,5,0,0,0,0,0,2,1,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0'
将它们发送到我的 Lamba 函数cosinLamba(valu1, value2)
value1 和 value 2 是字符串,这就是为什么我将它们作为值保存在我的字典中。我想为每个字典中为给定图片存储的所有向量做 fcolor、texture、edge。