这是我想做的:
我定期用网络摄像头拍照。就像时间流逝一样。然而,如果没有什么真正的改变,也就是说,图片看起来几乎相同,我不想存储最新的快照。
我想有某种方法可以量化这种差异,我必须根据经验确定一个阈值。
我追求的是简单而不是完美。 我用的是python。
这是我想做的:
我定期用网络摄像头拍照。就像时间流逝一样。然而,如果没有什么真正的改变,也就是说,图片看起来几乎相同,我不想存储最新的快照。
我想有某种方法可以量化这种差异,我必须根据经验确定一个阈值。
我追求的是简单而不是完美。 我用的是python。
当前回答
如何计算这两幅图像的曼哈顿距离呢?得到n*n个值。然后你可以做一些事情,比如行平均,把值减少到n个,然后再用一个函数得到一个值。
其他回答
import os
from PIL import Image
from PIL import ImageFile
import imagehash
#just use to the size diferent picture
def compare_image(img_file1, img_file2):
if img_file1 == img_file2:
return True
fp1 = open(img_file1, 'rb')
fp2 = open(img_file2, 'rb')
img1 = Image.open(fp1)
img2 = Image.open(fp2)
ImageFile.LOAD_TRUNCATED_IMAGES = True
b = img1 == img2
fp1.close()
fp2.close()
return b
#through picturu hash to compare
def get_hash_dict(dir):
hash_dict = {}
image_quantity = 0
for _, _, files in os.walk(dir):
for i, fileName in enumerate(files):
with open(dir + fileName, 'rb') as fp:
hash_dict[dir + fileName] = imagehash.average_hash(Image.open(fp))
image_quantity += 1
return hash_dict, image_quantity
def compare_image_with_hash(image_file_name_1, image_file_name_2, max_dif=0):
"""
max_dif: The maximum hash difference is allowed, the smaller and more accurate, the minimum is 0.
recommend to use
"""
ImageFile.LOAD_TRUNCATED_IMAGES = True
hash_1 = None
hash_2 = None
with open(image_file_name_1, 'rb') as fp:
hash_1 = imagehash.average_hash(Image.open(fp))
with open(image_file_name_2, 'rb') as fp:
hash_2 = imagehash.average_hash(Image.open(fp))
dif = hash_1 - hash_2
if dif < 0:
dif = -dif
if dif <= max_dif:
return True
else:
return False
def compare_image_dir_with_hash(dir_1, dir_2, max_dif=0):
"""
max_dif: The maximum hash difference is allowed, the smaller and more accurate, the minimum is 0.
"""
ImageFile.LOAD_TRUNCATED_IMAGES = True
hash_dict_1, image_quantity_1 = get_hash_dict(dir_1)
hash_dict_2, image_quantity_2 = get_hash_dict(dir_2)
if image_quantity_1 > image_quantity_2:
tmp = image_quantity_1
image_quantity_1 = image_quantity_2
image_quantity_2 = tmp
tmp = hash_dict_1
hash_dict_1 = hash_dict_2
hash_dict_2 = tmp
result_dict = {}
for k in hash_dict_1.keys():
result_dict[k] = None
for dif_i in range(0, max_dif + 1):
have_none = False
for k_1 in result_dict.keys():
if result_dict.get(k_1) is None:
have_none = True
if not have_none:
return result_dict
for k_1, v_1 in hash_dict_1.items():
for k_2, v_2 in hash_dict_2.items():
sub = (v_1 - v_2)
if sub < 0:
sub = -sub
if sub == dif_i and result_dict.get(k_1) is None:
result_dict[k_1] = k_2
break
return result_dict
def main():
print(compare_image('image1\\815.jpg', 'image2\\5.jpg'))
print(compare_image_with_hash('image1\\815.jpg', 'image2\\5.jpg', 7))
r = compare_image_dir_with_hash('image1\\', 'image2\\', 10)
for k in r.keys():
print(k, r.get(k))
if __name__ == '__main__':
main()
输出: 假 真正的 image2 jpg image1 5. \ \ 815. jpg image2 jpg image1 6. \ \ 819. jpg image2 jpg image1 7. \ \ 900. jpg image2 jpg image1 8. \ \ 998. jpg image2 jpg image1 9. \ \ 1012. jpg 示例图片: 815. jpg 5. jpg
您可以使用PIL中的函数来比较两个图像。
import Image
import ImageChops
im1 = Image.open("splash.png")
im2 = Image.open("splash2.png")
diff = ImageChops.difference(im2, im1)
diff对象是一幅图像,其中每个像素都是第二幅图像中该像素的颜色值与第一张图像相减的结果。使用差异图像你可以做几件事。最简单的是diff.getbbox()函数。它会告诉你包含两幅图像之间所有变化的最小矩形。
您也可以使用来自PIL的函数实现这里提到的其他东西的近似。
你可以计算这两个图像的直方图,然后计算Bhattacharyya系数,这是一个非常快速的算法,我已经用它来检测板球视频中的镜头变化(在C中使用openCV)
看看Haar小波是如何由isk-daemon实现的。你可以使用它的imgdb c++代码来实时计算图像之间的差异:
disk -daemon是一个开源的数据库服务器,能够将基于内容的(可视的)图像搜索添加到任何与图像相关的网站或软件。 这项技术允许任何与图像相关的网站或软件的用户在小部件上绘制他们想要查找的图像,并让网站回复他们最相似的图像或简单地在每个图像详细页面请求更多相似的照片。
我在工作中遇到了类似的问题,我正在重写我们的图像转换端点,我想检查新版本是否与旧版本产生相同或几乎相同的输出。所以我写了这个:
https://github.com/nicolashahn/diffimg
它对相同大小的图像进行操作,并在每个像素级别上测量每个通道的值的差异:R, G, B(, a),取这些通道的平均差值,然后对所有像素的差值进行平均,并返回一个比率。
例如,有一张10x10的白色像素的图像,而同一张图像只有一个像素变成了红色,该像素处的差异是1/3或0.33……(RGB 0,0,0 vs 255,0,0)并且在所有其他像素为0。总共100像素,0.33…/100 =一个~0.33%的图像差异。
我相信这将非常适合OP的项目(我意识到这是一个非常老的帖子,但张贴为未来的StackOverflowers谁也想用python比较图像)。