这是我想做的:
我定期用网络摄像头拍照。就像时间流逝一样。然而,如果没有什么真正的改变,也就是说,图片看起来几乎相同,我不想存储最新的快照。
我想有某种方法可以量化这种差异,我必须根据经验确定一个阈值。
我追求的是简单而不是完美。 我用的是python。
这是我想做的:
我定期用网络摄像头拍照。就像时间流逝一样。然而,如果没有什么真正的改变,也就是说,图片看起来几乎相同,我不想存储最新的快照。
我想有某种方法可以量化这种差异,我必须根据经验确定一个阈值。
我追求的是简单而不是完美。 我用的是python。
当前回答
下面是我写的一个函数,它以2个图像(文件路径)作为参数,并返回两个图像“像素”组件之间的平均差值。这对我确定视觉上“相等”的图像(当它们不==相等时)非常有效。
(我发现8个是判断图像本质上是否相同的一个很好的限制。)
(如果不添加预处理,图像必须具有相同的尺寸。)
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))
其他回答
一个简单的解决方案:
将图像编码为jpeg格式,并寻找文件大小的实质性变化。
我曾经用视频缩略图实现过类似的东西,并且取得了很大的成功和可伸缩性。
两种流行且相对简单的方法是:(a)已经提出的欧几里得距离,或(b)标准化互相关。与简单的互相关相比,归一化互相关对光照变化的影响明显更强。维基百科给出了一个标准化互相关的公式。更复杂的方法也存在,但它们需要更多的工作。
使用numpy-like语法,
dist_euclidean = sqrt(sum((i1 - i2)^2)) / i1.size dist_manhattan = sum(abs(i1 - i2)) / i1.size dist_ncc = sum( (i1 - mean(i1)) * (i2 - mean(i2)) ) / ( (i1.size - 1) * stdev(i1) * stdev(i2) )
假设i1和i2为二维灰度图像阵列。
如果现在回复太晚,我很抱歉,但因为我一直在做类似的事情,我想我可以在某种程度上做出贡献。
也许在OpenCV中你可以使用模板匹配。假设你用的是摄像头
简化图像(可能是阈值?) 应用模板匹配和检查max_val与minMaxLoc
提示: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()
输出: 假 真正的 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