这是我想做的:
我定期用网络摄像头拍照。就像时间流逝一样。然而,如果没有什么真正的改变,也就是说,图片看起来几乎相同,我不想存储最新的快照。
我想有某种方法可以量化这种差异,我必须根据经验确定一个阈值。
我追求的是简单而不是完美。 我用的是python。
这是我想做的:
我定期用网络摄像头拍照。就像时间流逝一样。然而,如果没有什么真正的改变,也就是说,图片看起来几乎相同,我不想存储最新的快照。
我想有某种方法可以量化这种差异,我必须根据经验确定一个阈值。
我追求的是简单而不是完美。 我用的是python。
当前回答
如果现在回复太晚,我很抱歉,但因为我一直在做类似的事情,我想我可以在某种程度上做出贡献。
也许在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
希望能有所帮助。
其他回答
两种流行且相对简单的方法是:(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为二维灰度图像阵列。
I have been having a lot of luck with jpg images taken with the same camera on a tripod by (1) simplifying greatly (like going from 3000 pixels wide to 100 pixels wide or even fewer) (2) flattening each jpg array into a single vector (3) pairwise correlating sequential images with a simple correlate algorithm to get correlation coefficient (4) squaring correlation coefficient to get r-square (i.e fraction of variability in one image explained by variation in the next) (5) generally in my application if r-square < 0.9, I say the two images are different and something happened in between.
这是强大的和快速的在我的实现(Mathematica 7)
这是值得玩转的部分,你感兴趣的图像,并通过裁剪所有的图像到那个小区域,否则一个远离相机但重要的变化将被错过。
我不知道如何使用Python,但我确信它也有相关性,不是吗?
如果现在回复太晚,我很抱歉,但因为我一直在做类似的事情,我想我可以在某种程度上做出贡献。
也许在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
希望能有所帮助。
一种更有原则的方法是使用全局描述符来比较图像,比如GIST或CENTRIST。这里描述的哈希函数也提供了类似的解决方案。
有很多指标可以用来评估两张图片是否像/有多像。
这里我就不讲代码了,因为我认为这应该是一个科学问题,而不是技术问题。
一般来说,问题与人类对图像的感知有关,因此每种算法都有其对人类视觉系统特征的支持。
经典方法有:
可见差异预测器:一种评估图像保真度的算法(https://www.spiedigitallibrary.org/conference-proceedings-of-spie/1666/0000/Visible-differences-predictor--an-algorithm-for-the-assessment-of/10.1117/12.135952.short?SSO=1)
图像质量评估:从错误可见性到结构相似性(http://www.cns.nyu.edu/pub/lcv/wang03-reprint.pdf)
FSIM:一种用于图像质量评估的特征相似度指数(https://www4.comp.polyu.edu.hk/~cslzhang/IQA/TIP_IQA_FSIM.pdf)
其中,SSIM (Image Quality Assessment: From Error Visibility to Structural Similarity)是最容易计算的,其开销也较小,另一篇论文《基于梯度相似度的图像质量评估》(https://www.semanticscholar.org/paper/Image-Quality-Assessment-Based-on-Gradient-Liu-Lin/2b819bef80c02d5d4cb56f27b202535e119df988)也有报道。
还有很多其他的方法。如果你对艺术感兴趣或真正关心,可以在谷歌Scholar上搜索“视觉差异”、“图像质量评估”等。