gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:13:25 24 4
gpt4 key购买 nike

我从 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

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

最佳答案

您可以使用 scikit-image 模块计算结构相似性指数 (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))

关于python - 比较两个图像并忽略图像中的微小变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48437443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com