这是我想做的:
我定期用网络摄像头拍照。就像时间流逝一样。然而,如果没有什么真正的改变,也就是说,图片看起来几乎相同,我不想存储最新的快照。
我想有某种方法可以量化这种差异,我必须根据经验确定一个阈值。
我追求的是简单而不是完美。 我用的是python。
这是我想做的:
我定期用网络摄像头拍照。就像时间流逝一样。然而,如果没有什么真正的改变,也就是说,图片看起来几乎相同,我不想存储最新的快照。
我想有某种方法可以量化这种差异,我必须根据经验确定一个阈值。
我追求的是简单而不是完美。 我用的是python。
当前回答
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,但我确信它也有相关性,不是吗?
其他回答
另一个衡量两张图片相似度的好方法是:
import sys
from skimage.measure import compare_ssim
from skimage.transform import resize
from scipy.ndimage import imread
# get two images - resize both to 1024 x 1024
img_a = resize(imread(sys.argv[1]), (2**10, 2**10))
img_b = resize(imread(sys.argv[2]), (2**10, 2**10))
# score: {-1:1} measure of the structural similarity between the images
score, diff = compare_ssim(img_a, img_b, full=True)
print(score)
如果其他人对更强大的比较图像相似性的方法感兴趣,我将使用Tensorflow测量和可视化相似图像的教程和web应用程序放在一起。
有很多指标可以用来评估两张图片是否像/有多像。
这里我就不讲代码了,因为我认为这应该是一个科学问题,而不是技术问题。
一般来说,问题与人类对图像的感知有关,因此每种算法都有其对人类视觉系统特征的支持。
经典方法有:
可见差异预测器:一种评估图像保真度的算法(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上搜索“视觉差异”、“图像质量评估”等。
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,但我确信它也有相关性,不是吗?
通过计算均方误差,numpy有一个简单而快速的解决方案:
before = np.array(get_picture())
while True:
now = np.array(get_picture())
MSE = np.mean((now - before)**2)
if MSE > threshold:
break
before = now
您可以使用PIL中的函数来比较两个图像。
import Image
import ImageChops
im1 = Image.open("splash.png")
im2 = Image.open("splash2.png")
diff = ImageChops.difference(im2, im1)
diff对象是一幅图像,其中每个像素都是第二幅图像中该像素的颜色值与第一张图像相减的结果。使用差异图像你可以做几件事。最简单的是diff.getbbox()函数。它会告诉你包含两幅图像之间所有变化的最小矩形。
您也可以使用来自PIL的函数实现这里提到的其他东西的近似。