Sachhya
Sachhya

Reputation: 1442

compare two images and ignore minor changes in image

I am taking two image from ip camera where camera is fully stable and also image look totally similar but when i compare it shows images are not equal. I don't know what the minor changes are but i need to ignore minor change it should be showed images are equal. I am attaching both images and my approach.

my code:

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

I am getting rms value non zero for image_1 and image_2 how to get rms value zero because i need to ignore minor changes i only need to consider major changes. please help and also tell if it is possible from open-cv or any other approach. I need to do this

Upvotes: 4

Views: 2193

Answers (1)

Prakash Palnati
Prakash Palnati

Reputation: 3409

You can calculate Structural Similarity Index (SSIM) using scikit-image module,

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

Upvotes: 1

Related Questions