以下是我想做的事情:
我想定期使用网络摄像头拍照片。有点像时间流逝的东西。但是,如果没有真正发生改变,也就是说,图片基本上看起来相同,我不想存储最新的快照。
我想象有一种量化差异的方法,我将不得不凭经验确定阈值。
我寻求简单而非完美。 我正在使用Python。
以下是我想做的事情:
我想定期使用网络摄像头拍照片。有点像时间流逝的东西。但是,如果没有真正发生改变,也就是说,图片基本上看起来相同,我不想存储最新的快照。
我想象有一种量化差异的方法,我将不得不凭经验确定阈值。
我寻求简单而非完美。 我正在使用Python。
如果回复晚了,我很抱歉。由于我做过类似的事情,所以我想我可以做出一些贡献。
也许您可以使用OpenCV进行模板匹配。假设您正在使用网络摄像头:
提示:max_val(或min_val,具体取决于所使用的方法)将给您一些大数字。要获取百分比差异,请使用相同图像的模板匹配-结果将是100%。
伪代码如下:
previous_screenshot = ...
current_screenshot = ...
# simplify both images somehow
# get the 100% corresponding value
res = matchTemplate(previous_screenshot, previous_screenshot, TM_CCOEFF)
_, hundred_p_val, _, _ = minMaxLoc(res)
# hundred_p_val is now the 100%
res = matchTemplate(previous_screenshot, current_screenshot, TM_CCOEFF)
_, max_val, _, _ = minMaxLoc(res)
difference_percentage = max_val / hundred_p_val
# the tolerance is now up to you
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()
我一直在使用同一台相机上三脚架拍摄的jpg图像,运用以下方法取得了很好的效果:
在我的实现中(Mathematica 7),这种方法非常稳健和快速。
值得注意的是,应该尝试裁剪所有图像到感兴趣的图像部分并专注于该部分,否则可能会错过远离相机但重要的变化。
我不知道如何使用Python,但我确信它也可以进行相关性计算,不是吗?
你可以计算两张图片的直方图,然后计算Bhattacharyya系数,这是一个非常快速的算法,我曾经在使用openCV(用C语言)检测板球视频中的镜头变化时使用过它。
地球移动距离可能正是你所需要的。 不过实时实现可能会有些困难。
==
相等时),这对我非常有效。
< p> (我发现8是确定图像是否基本相同的好限制。)
< p> (如果您不添加任何预处理,则图像必须具有相同的尺寸。)
from PIL import Image
def imagesDifference( imageA, imageB ):
A = list(Image.open(imageA, r'r').convert(r'RGB').getdata())
B = list(Image.open(imageB, r'r').convert(r'RGB').getdata())
if (len(A) != len(B)): return -1
diff = []
for i in range(0, len(A)):
diff += [abs(A[i][0] - B[i][0]), abs(A[i][1] - B[i][1]), abs(A[i][2] - B[i][2])]
return (sum(diff) / len(diff))
看看isk-daemon如何实现Haar小波。您可以使用它的imgdb C++代码实时计算图像之间的差异:
isk-daemon是一个开源数据库服务器,能够为任何与图像相关的网站或软件添加基于内容(视觉)的图像搜索功能。
这项技术允许任何与图像相关的网站或软件的用户在小部件上草绘他们想要查找的图像,并让网站回复他们最相似的图像,或者仅在每个图像详细页面请求更多相似的照片。