Python比较两个图片相似度的方法


Posted in Python onMarch 13, 2015

本文实例讲述了Python比较两个图片相似度的方法。分享给大家供大家参考。具体分析如下:

这段代码实用pil模块比较两个图片的相似度,根据实际实用,代码虽短但效果不错,还是非常靠谱的,前提是图片要大一些,太小的图片不好比较。附件提供完整测试代码和对比用的图片。

#!/usr/bin/python

# Filename: histsimilar.py

# -*- coding: utf-8 -*-

import Image

def make_regalur_image(img, size = (256, 256)):

    return img.resize(size).convert('RGB')

def split_image(img, part_size = (64, 64)):

    w, h = img.size

    pw, ph = part_size

    assert w % pw == h % ph == 0

    return [img.crop((i, j, i+pw, j+ph)).copy() \

                for i in xrange(0, w, pw) \

                for j in xrange(0, h, ph)]

def hist_similar(lh, rh):

    assert len(lh) == len(rh)

    return sum(1 - (0 if l == r else float(abs(l - r))/max(l, r)) for l, r in zip(lh, rh))/len(lh)

def calc_similar(li, ri):

#   return hist_similar(li.histogram(), ri.histogram())

    return sum(hist_similar(l.histogram(), r.histogram()) for l, r in zip(split_image(li), split_image(ri))) / 16.0

def calc_similar_by_path(lf, rf):

    li, ri = make_regalur_image(Image.open(lf)), make_regalur_image(Image.open(rf))

    return calc_similar(li, ri)

def make_doc_data(lf, rf):

    li, ri = make_regalur_image(Image.open(lf)), make_regalur_image(Image.open(rf))

    li.save(lf + '_regalur.png')

    ri.save(rf + '_regalur.png')

    fd = open('stat.csv', 'w')

    fd.write('\n'.join(l + ',' + r for l, r in zip(map(str, li.histogram()), map(str, ri.histogram()))))

#   print >>fd, '\n'

#   fd.write(','.join(map(str, ri.histogram())))

    fd.close()

    import ImageDraw

    li = li.convert('RGB')

    draw = ImageDraw.Draw(li)

    for i in xrange(0, 256, 64):

        draw.line((0, i, 256, i), fill = '#ff0000')

        draw.line((i, 0, i, 256), fill = '#ff0000')

    li.save(lf + '_lines.png')

if __name__ == '__main__':

    path = r'testpic/TEST%d/%d.JPG'

    for i in xrange(1, 7):

        print 'test_case_%d: %.3f%%'%(i, \

            calc_similar_by_path('testpic/TEST%d/%d.JPG'%(i, 1), 'testpic/TEST%d/%d.JPG'%(i, 2))*100)

#   make_doc_data('test/TEST4/1.JPG', 'test/TEST4/2.JPG')

完整实例代码点击此处本站下载。

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python中除法使用的注意事项
Aug 21 Python
Python with用法实例
Apr 14 Python
Python类属性与实例属性用法分析
May 09 Python
批量获取及验证HTTP代理的Python脚本
Apr 23 Python
多版本Python共存的配置方法
May 22 Python
Python学习思维导图(必看篇)
Jun 26 Python
Python使用type关键字创建类步骤详解
Jul 23 Python
Python 分发包中添加额外文件的方法
Aug 16 Python
python数据处理之如何选取csv文件中某几行的数据
Sep 02 Python
python中删除某个元素的方法解析
Nov 05 Python
pytorch实现用CNN和LSTM对文本进行分类方式
Jan 08 Python
python 管理系统实现mysql交互的示例代码
Dec 06 Python
python通过urllib2获取带有中文参数url内容的方法
Mar 13 #Python
python将MongoDB里的ObjectId转换为时间戳的方法
Mar 13 #Python
python通过正则查找微博@(at)用户的方法
Mar 13 #Python
python使用chardet判断字符串编码的方法
Mar 13 #Python
python根据时间生成mongodb的ObjectId的方法
Mar 13 #Python
python实现根据窗口标题调用窗口的方法
Mar 13 #Python
Python实现遍历windows所有窗口并输出窗口标题的方法
Mar 13 #Python
You might like
一篇入门的php Class 文章
2007/04/04 PHP
php 正则匹配函数体
2009/08/25 PHP
php实现的仿阿里巴巴实现同类产品翻页
2009/12/11 PHP
PHP设计模式之调解者模式的深入解析
2013/06/13 PHP
CentOS 安装 PHP5.5+Redis+XDebug+Nginx+MySQL全纪录
2015/03/25 PHP
PHP获取表单数据与HTML嵌入PHP脚本的实现
2017/02/09 PHP
Thinkphp5.0自动生成模块及目录的方法详解
2017/04/17 PHP
php处理多图上传压缩代码功能
2018/06/13 PHP
Yii框架 session 数据库存储操作方法示例
2019/11/18 PHP
使用console进行性能测试
2015/04/27 Javascript
JavaScript实现给按钮加上双重动作的方法
2015/08/14 Javascript
JavaScript制作简单的日历效果
2016/03/10 Javascript
Vue 2.0入门基础知识之内部指令详解
2017/10/15 Javascript
使用canvas实现一个vue弹幕组件功能
2018/11/30 Javascript
antd Upload 文件上传的示例代码
2018/12/14 Javascript
pm2启动ssr失败的解决方法
2019/06/29 Javascript
openlayers4.6.5实现距离量测和面积量测
2020/09/25 Javascript
Python使用稀疏矩阵节省内存实例
2014/06/27 Python
关于numpy中eye和identity的区别详解
2019/11/29 Python
python selenium实现发送带附件的邮件代码实例
2019/12/10 Python
TensorFlow实现打印每一层的输出
2020/01/21 Python
Python异常处理机制结构实例解析
2020/07/23 Python
详解Python中的编码问题(encoding与decode、str与bytes)
2020/09/30 Python
python 爬虫请求模块requests详解
2020/12/04 Python
python给list排序的简单方法
2020/12/10 Python
中东地区最大的奢侈品市场:The Luxury Closet
2019/04/09 全球购物
中国汽车租赁行业头部企业:一嗨租车
2019/05/16 全球购物
高中毕业生自我鉴定范文
2013/09/26 职场文书
遗体告别仪式答谢词
2014/01/23 职场文书
搞笑获奖感言
2014/01/30 职场文书
葛优非诚勿扰搞笑征婚台词
2014/03/17 职场文书
事业单位竞聘上岗实施方案
2014/03/28 职场文书
人事经理岗位职责
2014/04/28 职场文书
会员卡清退活动总结
2014/08/27 职场文书
优秀校长事迹材料
2014/12/24 职场文书
BCL经典机 SONY ICF-5900W电路分析
2022/04/24 无线电