Skip to content

Instantly share code, notes, and snippets.

@mcchae
Created August 24, 2017 00:58
Show Gist options
  • Save mcchae/0a30383dbddd90adf7a426a8e94c4c46 to your computer and use it in GitHub Desktop.
Save mcchae/0a30383dbddd90adf7a426a8e94c4c46 to your computer and use it in GitHub Desktop.
Python OpenCV Image diff
# USAGE
# python image_diff.py --first images/original_01.png --second images/modified_01.png
# import the necessary packages
from skimage.measure import compare_ssim
import argparse
import imutils
import cv2
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-f", "--first", required=True,
help="first input image")
ap.add_argument("-s", "--second", required=True,
help="second")
args = vars(ap.parse_args())
# load the two input images
imageA = cv2.imread(args["first"])
imageB = cv2.imread(args["second"])
# convert the images to grayscale
# next line make error
# OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
# compute the Structural Similarity Index (SSIM) between the two
# images, ensuring that the difference image is returned
(score, diff) = compare_ssim(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
print("SSIM: {}".format(score))
# threshold the difference image, followed by finding contours to
# obtain the regions of the two input images that differ
thresh = cv2.threshold(diff, 0, 255,
cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
# loop over the contours
for c in cnts:
# compute the bounding box of the contour and then draw the
# bounding box on both input images to represent where the two
# images differ
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(imageA, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.rectangle(imageB, (x, y), (x + w, y + h), (0, 0, 255), 2)
# show the output images
cv2.imshow("Original", imageA)
cv2.imshow("Modified", imageB)
cv2.imshow("Diff", diff)
cv2.imshow("Thresh", thresh)
cv2.waitKey(0)
@sowbhagya1
Copy link

sowbhagya1 commented Dec 16, 2019

if original image in one position and edited image in rotated position then it is not giving correct output then which method is used equalised both images

@sowbhagya1
Copy link

for example
i1
i2
in this two images are in different position one is at normal postion and other is at rotated position then output we get is not correct what method to be used to get correct output

@mightbeh
Copy link

for example
i1
i2
in this two images are in different position one is at normal postion and other is at rotated position then output we get is not correct what method to be used to get correct output

you could use edge detection algorithm and crop the image first, then implement the SSIM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment