Skip to content

Instantly share code, notes, and snippets.

@cjhanks
Created March 6, 2014 22:38
Show Gist options
  • Save cjhanks/9401327 to your computer and use it in GitHub Desktop.
Save cjhanks/9401327 to your computer and use it in GitHub Desktop.
Image difference
#!/usr/bin/env python
import cv2
import math
import numpy
from argparse import ArgumentParser
def main():
ap = ArgumentParser('image difference tool')
ap.add_argument('--img1', required = True)
ap.add_argument('--img2', required = True)
ap.add_argument('--out' , required = True)
args = ap.parse_args()
try:
image_1 = cv2.imread(args.img1)
except Exception:
print('Failed to open img1')
return 1
try:
image_2 = cv2.imread(args.img2)
except Exception:
print('Failed to open img2')
return 1
try:
diff_img = image_2 - image_1
except Exception:
print('Failed to diff images')
return 1
# split channels
r_chan, g_chan, b_chan = cv2.split(diff_img)
cv2.imwrite(args.out, diff_img)
cv2.imwrite('r.%s' % args.out, r_chan)
cv2.imwrite('g.%s' % args.out, g_chan)
cv2.imwrite('b.%s' % args.out, b_chan)
print('''
Max Difference:
r -> {max_r}
g -> {max_g}
b -> {max_b}
'''.format(max_r = numpy.max(numpy.fabs(r_chan))
, max_g = numpy.max(numpy.fabs(g_chan))
, max_b = numpy.max(numpy.fabs(b_chan))))
return 0
if __name__ == '__main__':
from sys import exit
exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment