2

我已经构建了两个包含图像直方图值的字典。每个字典都将图像文件的文件名作为键,并将三个一维向量的列表放在一起作为其值。

例子: {'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,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']}
    ++++++++++++++++++
    /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
    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


    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
    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。

4

2 回答 2

2

您可以首先将您的表示更改为:

{'someFileName.jpg' : {'forecolor': [2,3,5,5,6],'edge': [2,4,5],'texture':[5,4,3]}}

或者

{('someFileName.jpg', 'forecolor'): [2,3,5,5,6],
 ('someFileName.jpg', 'edge'): [2,4,5],
 ('someFileName.jpg', 'texture'):[5,4,3]}

例如,要获取第一种情况的相应列表:

from itertools import product

# pair info for each image with info of every image from another dictionary
for (fn1, d1), (fn2,d2) in product(dict1.iteritems(), dict2.iteritems()):
    for property_, list_value in d1.iteritems():
        compute_cosine_similarity(list_value, d2[property_])

将您的表示与字符串列表一起使用,它看起来像:

from itertools import product

# pair info for each image with info of every image from another dictionary
for (fn1,lst1), (fn2,lst2) in product(dict1.iteritems(), dict2.iteritems()):
    # assume all lists has the same order of elements
    for string_value1, string_value2 in zip(lst1, lst2):
        compute(string_value1, string_value2)

您不应该将数字存储为 ascii 字符串列表。如果需要节省内存,可以使用 numpy 数组。cosinLamba已经接受了他们。

from collections import namedtuple
import numpy as np

Info = namedtuple('Info', 'forecolor edge texture')

dict1 = {'someFileName.jpg': Info(np.array([...], dtype=np.uint8),
                                  np.array([...], dtype=np.uint8),
                                  np.array([...], dtype=np.uint8))}

调用 cosine_similarity() 的代码与您的表示完全相同。

于 2012-07-31T00:30:55.257 回答
0

我不熟悉您要执行的 cosign 计算,但除此之外,如果我对您的问题的理解是正确的,那么以下代码应该可以工作:

for key1, vals1 in dict1.iteritems():
    vals2 = dict2[key1]
    for val1, val2 in zip(vals1, vals2):
        # you now have the corresponding values for each image file
        compute_cosign(val1, val2)
于 2012-07-31T00:28:23.593 回答