INT'对象没有属性' __ getitem __'

时间:2014-03-17 12:14:17

标签: python image python-2.7 image-processing computer-vision

我是python的初学者,我试图通过测试test_pic和包含图像的数据库来找到图像之间的相似性。我已经从目录中传递了图像,并使用SIFT功能对其进行了比较

from PIL import Image
from PIL import ImageStat
from PIL import ImageFilter
import os

def get_imlist(path):
    """    Returns a list of filenames for 
        all jpg images in a directory. """

    return [os.path.join(f) for f in os.listdir(path) if f.endswith('.png')]

def fe(file_name):
    im = Image.open(file_name)
    im = im.convert('L')
    w, h = 300, 300
    im = im.resize((w, h))
    imst = ImageStat.Stat(im)
    sr = imst.mean[0]
    def foo(t):
        if t < sr * 2 / 3: return 0
        if t <= sr: return 1
        if t < sr * 4 / 3: return 2
        return 3
    im = im.point(foo)
    res = [[0] * 4 for i in range(10)]
    for y in range(h):
        for x in range(w):
            k = im.getpixel((x, y))
            res[y / 60][k] += 1
            res[x / 60 + 5][k] += 1
    return res

def ff(file_name):
    im = Image.open(file_name)
    im = im.convert('L')
    w, h = 300, 300
    im = im.resize((w, h))
    im = im.filter(ImageFilter.FIND_EDGES)
    sr = ImageStat.Stat(im).mean[0]
    res = 0
    for i in range(h):
        for j in range(w):
            if im.getpixel((j, i)) > sr:
                res += 1
    #im.show()
    return res

test_pic = '128.png' #testing this pic against all pics in our db;
path='D:/ex'
imlist=[]
db=[]
imlist=get_imlist(path)
for file in imlist:
    x = [0, file]
    db.append(x)  
z = [fe(db[i][1]) for i in range(len(db))]
test_z = fe(test_pic)
for k in range(len(db)):
    for i in range(10):
        for j in range(4):
            db[k][0] += abs(z[k][i][j] - test_z[i][j])
db.sort()

print
print '1st-line sifting [by fe()]'
print '--------------------'
print test_pic
print '--------------------'
for k in range(len(db)):
    if db[k][0] < 20 * 3600:
        print '%12s %7.2f' % (db[k][1], db[k][0] / 3600.0,)
    else:
        break
print '--------------------'
print
print
print '2nd-line sifting [by ff()]'
print '--------------------'
print test_pic
print '--------------------'
test_t = ff(test_pic)
for k in range(len(db)):
    if db[k][0] < 5 * 3600:
        print '%12s' % (db[k][1],)
    elif db[k][0] < 20 * 3600:
        t = ff(db[k][1])
        if abs(t - test_t) * 200.0 / (t + test_t) < 20:
            print '%12s' % (db[k][1],)
    else:
        break
print '--------------------'

raw_input('\nPress...\n')

2 个答案:

答案 0 :(得分:2)

猜测:

db = [0,file]
i = 0
z = [fe(db[i][1]) for i in range(len(db))]
      # ^ here

i == 0db[i] == 0。然后你试图索引一个整数 - 0[1] - 在幕后有效地调用0.__getitem__(1)。整数不会实现__getitem__,所以这会因您看到的错误而失败。


您的代码修订似乎有点奇怪,为什么:

for file in imlist:
    db = [[0,file]]   
    z = [fe(db[i][1]) for i in range(len(db))]

而不仅仅是:

for file in imlist:
    z = [fe(file)]

但是,如果没有完整的追溯,很难准确诊断您的错误。

答案 1 :(得分:0)

def get_imlist(path):
    """    Returns a list of filenames for 
        all jpg images in a directory. """

    return [[0, 0 ,os.path.join(path, f)] for f in os.listdir(path) if f.endswith('.png')]

如果您只是像这样更改get_imlist功能。它返回的结构类似于您在其中一个注释中链接的代码。因此,您应该能够使用该代码而无需进一步编辑。