将图像与列表Python进行比较

时间:2013-01-21 01:46:33

标签: python image

我一直在与一个程序进行斗争,我已经阅读了许多关于在python中使用图像的信息,但我没有得到我的程序。

我正在做一个识别卡片的程序。我有一个“数据库”,所有卡都在不同的jpg文件中。因此,我们尝试将我们想知道的卡与所有可能的卡进行比较。然后更相似的卡将是我正在寻找的卡。我尝试了几种不同的代码,但没有人正确地完成他的工作。

def get_card(image1path):
    from PIL import Image
    import math
    import os
    import operator
    __docstring__ = "compare two image files"
    h1 = Image.open(image1path).resize((40,55)).histogram()
    best=99999999
    for root,dirs,files in os.walk("cards"):
        for file in [f for f in files]:
            list=(os.path.join(root, file))
            h2 = Image.open(list).resize((40,55)).histogram()
            rms = math.sqrt(reduce(operator.add, map(lambda a,b: (a-b)**2, h1, h2))/len(h1))
            print "RMS = "+str(rms)+"  and the picture is:"+list
            if rms<best:
                best=rms
                card=(os.path.join(file)).split(".")[0]

    return card

image1path="C:\\8d.jpg" #eight of diamonds.
card=get_card(image1path)
print card

问题在于它不能正常工作,因为在每张卡的RMS与所有卡的比较之后,有一些错误的卡可以获得最佳的RMS标点符号。因此,识别卡不是必须的八颗钻石。

我该怎么办?如果你需要我用其他的话来解释它就说出来。

非常感谢

1 个答案:

答案 0 :(得分:3)

通过查看您要比较的图片,您实际上不希望使用RMSE等其他指标。原因是因为所有图像在“RMSE感”中是相似的,或者甚至是对于不关心图像中存在的基本关系的更精细的度量。以下是您自己提供的一些示例:

enter image description here enter image description here enter image description here enter image description here enter image description here

您的案例中的基本关系是:颜色(也可以区分黑桃,心形,钻石和俱乐部卡片)和形状测量。因此,通过检测卡的颜色,搜索空间减少,剩下的就是在卡顶部的数字之间辨别。连同组件和euler号码的数量一起进一步限制了搜索。现在留下的是区分:“9”,“6”,“4”,“女王”或“A”; “J”,“2”,“5”或“7”中的“3”; “8”和“10”被解决,前者由于欧拉数而后者由于其连接组件的数量(这一切都假设卡是唯一的,否则你继续找到最相似的卡)。在这里做的最简单的事情,如果你为你的问题添加更多考虑因素,可能会失败,就是计算每个剩余形状之间的Hausdorff距离。

这是一个简单的实现,它考虑了这些点并适用于所有给定的输入。它期望图像和目录查找要比较的图像。每个步骤都可以改进。

import sys
import numpy
from scipy.ndimage import morphology, label, find_objects
from PIL import Image

COLORS = range(4)
RED, GREEN, BLUE, BLACK = COLORS

def card_color(img):
    im = img.load()
    width, height = img.size
    black, blue, green, red = 0, 0, 0, 0
    for x in xrange(width):
        for y in xrange(height):
            r, g, b = im[x, y]
            if r > 200 and g > 200 and b > 200:
                # "white", ignore
                continue

            if r > 200 and g < 100 and b < 100:
                red += 1
            elif r < 100 and g < 100 and b > 200:
                blue += 1
            elif r < 50 and g < 50 and b < 50:
                black += 1
            elif r < 100 and g > 120 and b < 50: # dark green
                green += 1
    return max(zip((black, blue, green, red), COLORS))

def euler_number(img, conn=4):
    im = img.load()
    width, height = img.size

    c1, c2, c3 = 0, 0, 0
    for x in xrange(width - 1):
        for y in xrange(height - 1):
            s = (im[x,y] + im[x+1,y] + im[x,y+1] + im[x+1,y+1]) / 255
            if s == 1:
                c1 += 1
            elif s == 2:
                if (im[x+1,y] and im[x,y+1]) or (im[x,y] and im[x+1,y+1]):
                    c3 += 1
            elif s == 3:
                c2 += 1
    if conn == 4:
        return (c1 - c2 + 2 * c3) / 4
    else: # 8
        return (c1 - c2 - 2 * c3) / 4

def carefully_binarize(img, color):
    if color == BLACK:
        img = img.convert('L')
    else:
        img = img.split()[color]
    width, height = img.size
    im = numpy.empty((height + 2, width + 2), dtype=numpy.uint8) # Padding
    im.fill(255)
    im[1:-1, 1:-1] = numpy.array(img)
    threshold = im.mean() - im.std()
    im[im <= threshold] = 1
    im[im > threshold] = 0
    # Discard small components.
    lbl, ncc = label(im)
    for i in xrange(1, ncc + 1):
        py, px = numpy.nonzero(lbl == i)
        if len(py) < 30:
            im[lbl == i] = 0
    return Image.fromarray(im * 255)

def discard_bottom(img, k=0.5):
    width, height = img.size
    im = numpy.array(img)
    limit = height * k
    lbl, ncc = label(im)
    for i, oslice in enumerate(find_objects(lbl)):
        srow, scol = oslice
        if srow.stop > limit:
            ncc -= 1
            im[srow.start:srow.stop, scol.start:scol.stop] = 0
    return Image.fromarray(im), ncc

def signature(img):
    # Assumption: a single connected component is present now.
    im = numpy.array(img)
    im = morphology.binary_fill_holes(im)
    im = morphology.binary_dilation(im) - im

    py, px = numpy.nonzero(im)
    return Image.fromarray(im.astype(numpy.uint8)*255), zip(py, px)

def hausdorff(a, b):
    dist = 0
    for ai in a:
        mindist = float('inf')
        for bi in b:
            chess = max(abs(ai[0]-bi[0]), abs(ai[1]-bi[1]))
            if chess < mindist:
                mindist = chess
        if mindist > dist:
            dist = mindist
    return dist

img1 = Image.open(sys.argv[1]).convert('RGB')
dirpath = sys.argv[2]

img1_color = card_color(img1)[1]
img1 = carefully_binarize(img1, img1_color)
img1_top, img1_top_ncc = discard_bottom(img1)
img1_top_en = euler_number(img1_top)

feature = [img1_color, img1_top_ncc, img1_top_en]

match = []
for fname in os.listdir(dirpath):
    try:
        img2 = Image.open(os.path.join(dirpath, fname)).convert('RGB')
    except IOError:
        print "Ignoring", fname
        continue

    if card_color(img2)[1] != feature[0]:
        continue

    img2 = carefully_binarize(img2, feature[0])
    img2_top, ncc = discard_bottom(img2)
    if ncc != feature[1]:
        continue
    en = euler_number(img2_top)
    if en != feature[2]:
        continue

    match.append((img2_top, os.path.join(dirpath, fname)))

if len(match) == 1:
    print "Here is your best match:", match[0][1]
else:
    img1_sig, sig1 = signature(img1_top)
    best_match = float('inf'), None
    for img2, fname in match:
        img2_sig, sig2 = signature(img2)
        dist = hausdorff(sig1, sig2)
        if dist < best_match[0]:
            best_match = dist, fname

    print "Best match:", best_match[1]