比较两幅图像,忽略图像中的细微变化

2024-03-29 10:30:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我从ip摄像机上取了两张照片,这两张照片是完全稳定的,而且图像看起来完全相似,但当我比较时,它显示图像是不相等的。我不知道小的变化是什么,但我需要忽略小的变化,它应该显示图像是平等的。 我附上图像和我的方法。在

我的代码:

import Image

import cStringIO
import numpy
import ssl, time
import sys, math, operator
import urllib2


def main():
    print "In Main"

    ssl._create_default_https_context = ssl._create_unverified_context

    url = 'https://172.16.12.13/OpenHome/Streaming/channels/0/picture'
    # url = 'http://apod.nasa.gov/apod/image/1801/Tadpoles_Jimenez_3365.jpg'
    imgdata = urllib2.urlopen(url).read()
    img = Image.open(cStringIO.StringIO(imgdata))
    img.save("image1.png")
    time.sleep(2)
    imgdata = urllib2.urlopen(url).read()
    img = Image.open(cStringIO.StringIO(imgdata))
    img.save("image2.png")

    # IMAGE COMPARISON PART
    h1 = Image.open("image1.png").histogram()
    h2 = Image.open("image2.png").histogram()

    rms = math.sqrt(reduce(operator.add,
    map(lambda a, b: (a - b) ** 2, h1, h2)) / len(h1))

    print "RMS-->", rms

#     if img1.size != img2.size or img1.getbands() != img2.getbands():
#         return -1
#
#     s = 0
#     for band_index, band in enumerate(img1.getbands()):
#         m1 = numpy.array([p[band_index] for p in img1.getdata()]).reshape(*img1.size)
#         m2 = numpy.array([p[band_index] for p in img2.getdata()]).reshape(*img2.size)
#         s += numpy.sum(numpy.abs(m1 - m2))
#     print s


if __name__ == "__main__":
    sys.exit(main())

Image1

Image2

我得到了图像1和图像2的均方根值非零,如何得到均方根值0,因为我需要忽略小的变化,我只需要考虑主要的变化。请帮忙,并告知是否有可能从公开简历或任何其他途径。我需要这么做


Tags: 图像imageimportnumpyurlsslimgsize
1条回答
网友
1楼 · 发布于 2024-03-29 10:30:25

您可以使用scikit图像模块计算Structural Similarity Index (SSIM)

from skimage.measure import compare_ssim
import cv2
###convert you images to grayscale
gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)

###compute the similarity and difference
(score, diff) = compare_ssim(gray1, gray2, full=True)
diff = (diff * 255).astype("uint8")

print("Similarity: {}".format(score))

相关问题 更多 >