我一直在与一个程序斗争,我读过很多关于在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值后,有一些错误的卡得到了最好的RMS标点。所以识别卡不是钻石的八分之一。在
我该怎么做??如果你需要的话,我用别的话来解释,就说出来。在
非常感谢
通过查看正在比较的图像,您实际上不希望使用
RMSE
等度量。原因是因为所有的图像在“RMSE意义上”是相似的,或者甚至对于更精细的度量,这些度量并不关心图像中存在的基本关系。以下是你自己举的一些例子:在你的例子中,基本的关系是:颜色(也可以区分黑桃、红桃、钻石和梅花牌)和形状度量。所以,通过检测卡片的颜色,搜索空间就减少了,剩下的就是辨别卡片顶部的数字了。加上连接元件的数量和欧拉数,搜索进一步受到限制。现在剩下的是区分:“9”和“6”、“4”、大号或“A”;“3”和“J”、“2”、“5”或“7”;“8”和“10”被解决,前者是由于欧拉数,后者是由于其连接的组件的数量(这一切都假设卡是唯一的,否则你继续寻找最相似的卡)。这里要做的最简单的事情是计算每个剩余形状之间的Hausdorff距离,如果您在问题中添加更多的考虑,则可能会失败。在
这里是一个简单化的实现,它考虑了这些点并适用于所有给定的输入。它需要一个图像和一个目录来查找要比较的图像。每一步都可以改进。在
相关问题 更多 >
编程相关推荐