比较两个图像并忽略图像中的微小变化

时间:2018-01-25 07:12:22

标签: python opencv image-processing python-imaging-library

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

我的代码:

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

我为image_1和image_2获得rms值非零如何获得rms值为零因为我需要忽略微小的变化我只需要考虑重大变化。请帮助,并告诉我们是否可以通过open-cv或任何其他方法。我需要这样做

1 个答案:

答案 0 :(得分:0)

您可以使用scikit-image模块

计算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))