Skip to content

Instantly share code, notes, and snippets.

@hirocarma
Created April 18, 2024 13:31
Show Gist options
  • Save hirocarma/d1ebc5401d4cf89db2d640d378f723f3 to your computer and use it in GitHub Desktop.
Save hirocarma/d1ebc5401d4cf89db2d640d378f723f3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import sys
import shutil
import numpy as np
import cv2
import pprint
def main():
img_file1 = sys.argv[1]
img_file2 = sys.argv[2]
OUT_DIR = sys.argv[3]
if os.path.exists(OUT_DIR):
shutil.rmtree(OUT_DIR)
os.mkdir(OUT_DIR)
fname1 = os.path.splitext(os.path.basename(img_file1))[0]
img1 = cv2.imread(img_file1)
img2 = cv2.imread(img_file2)
diff = cv2.absdiff(img1, img2)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
gray_diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
img1_rgb = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
img2_rgb = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
norm_diff = gray_diff / np.max(gray_diff)
diff_img = cv2.addWeighted(gray2, 0.1, gray_diff, 2, 100)
diff_colored = np.zeros_like(img2_rgb)
diff_colored[..., 0] = img2_rgb[..., 0] * norm_diff
diff_colored[..., 1] = img2_rgb[..., 1] * norm_diff
diff_colored[..., 2] = img2_rgb[..., 2] * norm_diff
cv2.imwrite(OUT_DIR + fname1 + '-diff_colored' + '.jpg',diff_colored)
cv2.imwrite(OUT_DIR + fname1 + '-diff_gray' + '.jpg',diff_img)
pprint.pprint (diff_colored)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment