这是我想做的事情:
我定期用网络摄像头拍照。有点像时间流逝的事情。但是,如果没有真正改变,即图片看起来几乎相同,我不想存储最新的快照。
我想有某种量化差异的方法,我必须凭经验确定一个阈值。
我在寻找简单而不是完美。
我正在使用 python 。
最佳答案
大概的概念
选项 1:将两个图像作为数组加载 ( scipy.misc.imread
) 并计算逐元素(逐像素)差异。计算差异的范数。
选项 2:加载两个图像。为它们中的每一个计算一些特征向量(如直方图)。计算特征向量而不是图像之间的距离。
但是,首先要做出一些决定。
问题
您应该首先回答以下问题:
import sys
from scipy.misc import imread
from scipy.linalg import norm
from scipy import sum, average
def main():
file1, file2 = sys.argv[1:1+2]
# read images as 2D arrays (convert to grayscale for simplicity)
img1 = to_grayscale(imread(file1).astype(float))
img2 = to_grayscale(imread(file2).astype(float))
# compare
n_m, n_0 = compare_images(img1, img2)
print "Manhattan norm:", n_m, "/ per pixel:", n_m/img1.size
print "Zero norm:", n_0, "/ per pixel:", n_0*1.0/img1.size
img1
和 img2
这里是 2D SciPy 数组:def compare_images(img1, img2):
# normalize to compensate for exposure difference, this may be unnecessary
# consider disabling it
img1 = normalize(img1)
img2 = normalize(img2)
# calculate the difference and its norms
diff = img1 - img2 # elementwise for scipy arrays
m_norm = sum(abs(diff)) # Manhattan norm
z_norm = norm(diff.ravel(), 0) # Zero norm
return (m_norm, z_norm)
imread
返回一个 3D 数组,平均 RGB channel (最后一个数组轴)以获得强度。无需为灰度图像执行此操作(例如 .pgm
):def to_grayscale(arr):
"If arr is a color image (3D array), convert it to grayscale (2D array)."
if len(arr.shape) == 3:
return average(arr, -1) # average over the last axis (color channels)
else:
return arr
arr
这里是一个 SciPy 数组,所以所有操作都是按元素进行的:def normalize(arr):
rng = arr.max()-arr.min()
amin = arr.min()
return (arr-amin)*255/rng
main
功能:if __name__ == "__main__":
main()
$ python compare.py one.jpg one.jpg
Manhattan norm: 0.0 / per pixel: 0.0
Zero norm: 0 / per pixel: 0.0
$ python compare.py one.jpg one-blurred.jpg
Manhattan norm: 92605183.67 / per pixel: 13.4210411116
Zero norm: 6900000 / per pixel: 1.0
cv2
Python 模块。Similarity of consecutive frames. The distance between two consecutive frames is measured. If it is too high, it means that the second frame is corrupted and thus the image is eliminated. The Kullback–Leibler distance, or mutual entropy, on the histograms of the two frames:
where p and q are the histograms of the frames is used. The threshold is fixed on 0.2.
https://stackoverflow.com/questions/189943/